Hash :
747d0ec2
Author :
Thomas de Grivel
Date :
2025-06-12T18:58:09
wip memleak call callable
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
/* 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 "fn.h"
#include "mutex.h"
#include "tag.h"
void callable_delete (s_callable *callable)
{
assert(callable);
#if HAVE_PTHREAD
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
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_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;
}