Hash :
497817c3
Author :
Thomas de Grivel
Date :
2025-09-05T20:26:33
wip memleak
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
/* kc3
* Copyright from 2022 to 2025 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include <unistd.h>
#include "alloc.h"
#include "assert.h"
#include "callable.h"
#include "cfn.h"
#include "env.h"
#include "fn.h"
#include "mutex.h"
#include "tag.h"
s8 callable_arity (const s_callable *callable)
{
assert(callable);
switch (callable->type) {
case CALLABLE_CFN:
return callable->data.cfn.arity;
case CALLABLE_FN:
return fn_arity(&callable->data.fn);
case CALLABLE_VOID:
return -1;
}
err_puts("callable_arity: unknown callable type");
assert(! "callable_arity: unknown callable type");
return -1;
}
void callable_delete (s_callable *callable)
{
assert(callable);
#if HAVE_PTHREAD
if (callable->mutex.ready)
mutex_lock(&callable->mutex);
#endif
if (env_global()->pass_by_copy)
assert(callable->ref_count == 1);
else {
if (callable->ref_count <= 0) {
err_puts("callable_delete: invalid ref count");
assert(! "callable_delete: invalid ref count");
goto clean;
}
if (--callable->ref_count > 0)
goto clean;
}
switch (callable->type) {
case CALLABLE_CFN: cfn_clean(&callable->data.cfn); break;
case CALLABLE_FN: fn_clean(&callable->data.fn); break;
case CALLABLE_VOID: break;
}
#if HAVE_PTHREAD
if (callable->mutex.ready) {
mutex_unlock(&callable->mutex);
mutex_clean(&callable->mutex);
}
#endif
free(callable);
return;
clean:
#if HAVE_PTHREAD
mutex_unlock(&callable->mutex);
#endif
return;
}
s_callable * callable_new (void)
{
s_callable *callable;
if (! (callable = alloc(sizeof(s_callable))))
return NULL;
callable->ref_count = 1;
#if HAVE_PTHREAD
mutex_init(&callable->mutex);
#endif
return callable;
}
s_callable * callable_new_copy (s_callable *src)
{
s_callable *tmp;
if (! (tmp = callable_new()))
return NULL;
tmp->type = src->type;
switch (tmp->type) {
case CALLABLE_CFN:
if (! cfn_init_copy(&tmp->data.cfn, &src->data.cfn))
goto ko;
break;
case CALLABLE_FN:
if (! fn_init_copy(&tmp->data.fn, &src->data.fn))
goto ko;
break;
case CALLABLE_VOID:
break;
}
return tmp;
ko:
callable_delete(tmp);
return NULL;
}
s_callable * callable_new_ref (s_callable *callable)
{
assert(callable);
#if HAVE_PTHREAD
mutex_lock(&callable->mutex);
#endif
if (callable->ref_count <= 0) {
err_puts("callable_new_ref: invalid reference count");
assert(! "callable_new_ref: invalid reference count");
abort();
}
callable->ref_count++;
#if HAVE_PTHREAD
mutex_unlock(&callable->mutex);
#endif
return callable;
}
s_callable * callable_set_name (s_callable *callable,
const s_ident *name)
{
assert(callable);
assert(name);
switch (callable->type) {
case CALLABLE_VOID:
err_puts("callable_set_name: uninitialized callable");
assert(! "callable_set_name: uninitialized callable");
return NULL;
case CALLABLE_CFN:
if (! cfn_set_name(&callable->data.cfn, name))
return NULL;
return callable;
case CALLABLE_FN:
if (! fn_set_name(&callable->data.fn, name))
return NULL;
return callable;
}
err_puts("callable_set_name: unknown callable type");
assert(! "callable_set_name: unknown callable type");
return NULL;
}
s_callable * callable_set_name_if_null (s_callable *callable,
const s_ident *name)
{
assert(callable);
assert(name);
switch (callable->type) {
case CALLABLE_VOID:
err_puts("callable_set_name_if_null: uninitialized callable");
assert(! "callable_set_name_if_null: uninitialized callable");
return NULL;
case CALLABLE_CFN:
if (! cfn_set_name_if_null(&callable->data.cfn, name))
return NULL;
return callable;
case CALLABLE_FN:
if (! fn_set_name_if_null(&callable->data.fn, name))
return NULL;
return callable;
}
err_puts("callable_set_name_if_null: unknown callable type");
assert(! "callable_set_name_if_null: unknown callable type");
return NULL;
}
s_callable * callable_set_special (s_callable *callable, bool special)
{
assert(callable);
switch (callable->type) {
case CALLABLE_CFN:
callable->data.cfn.special_operator = special;
return callable;
case CALLABLE_FN:
callable->data.fn.special_operator = special;
return callable;
case CALLABLE_VOID:
err_puts("callable_set_special: CALLABLE_TYPE_VOID");
assert(! "callable_set_special: CALLABLE_TYPE_VOID");
return NULL;
}
err_puts("callable_set_special: unknown callable type");
assert(! "callable_set_special: unknown callable type");
return NULL;
}