Hash :
08657763
Author :
Thomas de Grivel
Date :
2025-09-05T19:37:44
wip mem leaks
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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
/* 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 <string.h>
#include "alloc.h"
#include "assert.h"
#include "buf.h"
#include "buf_inspect.h"
#include "buf_parse.h"
#include "call.h"
#include "callable.h"
#include "facts.h"
#include "facts_cursor.h"
#include "facts_with.h"
#include "facts_with_cursor.h"
#include "fn.h"
#include "ident.h"
#include "list.h"
#include "mutex.h"
#include "pcallable.h"
#include "plist.h"
#include "tag.h"
sw call_arity (const s_call *call)
{
return list_length(call->arguments);
}
void call_clean (s_call *call)
{
assert(call);
list_delete_all(call->arguments);
if (call->pcallable)
pcallable_clean(&call->pcallable);
#if HAVE_PTHREAD
mutex_clean(&call->mutex);
#endif
}
void call_delete (s_call *call)
{
assert(call);
if (call->ref_count <= 0) {
err_puts("call_delete: invalid ref count");
assert(! "call_delete: invalid ref count");
return;
}
#if HAVE_PTHREAD
mutex_lock(&call->mutex);
#endif
if (false) {
err_write_1("call_delete: 0x");
err_inspect_uw_hexadecimal((uw) call);
err_write_1(" ref count = ");
err_inspect_uw_decimal(call->ref_count);
err_write_1("\n");
}
call->ref_count--;
if (call->ref_count) {
#if HAVE_PTHREAD
mutex_unlock(&call->mutex);
#endif
return;
}
#if HAVE_PTHREAD
mutex_unlock(&call->mutex);
#endif
call_clean(call);
free(call);
}
bool call_get (s_call *call)
{
return env_call_get(env_global(), call);
}
s_call * call_init (s_call *call)
{
s_call tmp = {0};
assert(call);
#if HAVE_PTHREAD
mutex_init(&tmp.mutex);
#endif
tmp.ref_count = 1;
*call = tmp;
return call;
}
s_call * call_init_call_cast (s_call *call, const s_sym *type)
{
s_list *next;
s_call tmp;
call_init(&tmp);
tmp.ident.module = type;
tmp.ident.sym = &g_sym_cast;
next = list_new(NULL);
if (! next) {
call_clean(&tmp);
return NULL;
}
tmp.arguments = list_new(next);
if (! tmp.arguments) {
call_clean(&tmp);
list_delete_all(next);
return NULL;
}
tag_init_psym(&tmp.arguments->tag, type);
*call = tmp;
return call;
}
s_call * call_init_copy (s_call *call, s_call *src)
{
s_call tmp = {0};
assert(src);
assert(call);
call_init(&tmp);
if (! ident_init_copy(&tmp.ident, &src->ident) ||
! plist_init_copy(&tmp.arguments, &src->arguments)) {
call_clean(&tmp);
return NULL;
}
if (src->pcallable &&
! pcallable_init_copy(&tmp.pcallable, &src->pcallable)) {
call_clean(&tmp);
return NULL;
}
*call = tmp;
return call;
}
s_call * call_init_op (s_call *call)
{
s_list *arg;
s_call tmp = {0};
assert(call);
call_init(&tmp);
arg = list_new(NULL);
if (! arg) {
call_clean(&tmp);
return NULL;
}
tmp.arguments = list_new(arg);
if (! tmp.arguments) {
list_delete(arg);
call_clean(&tmp);
return NULL;
}
*call = tmp;
return call;
}
s_call * call_init_op_unary (s_call *call)
{
s_call tmp = {0};
assert(call);
call_init(&tmp);
tmp.arguments = list_new(NULL);
*call = tmp;
return call;
}
s_call * call_new (void)
{
s_call *call;
if (! (call = alloc(sizeof(s_call))))
return NULL;
if (! call_init(call)) {
free(call);
return NULL;
}
return call;
}
s_call * call_new_copy (s_call *src)
{
s_call *call;
if (! (call = alloc(sizeof(s_call))))
return NULL;
if (! call_init_copy(call, src)) {
free(call);
return NULL;
}
return call;
}
s_call * call_new_ref (s_call *src)
{
assert(src);
#if HAVE_PTHREAD
mutex_lock(&src->mutex);
#endif
if (src->ref_count <= 0) {
err_puts("call_new_ref: invalid ref count");
assert(! "call_new_ref: invalid ref count");
#if HAVE_PTHREAD
mutex_unlock(&src->mutex);
#endif
return NULL;
}
src->ref_count++;
#if HAVE_PTHREAD
mutex_unlock(&src->mutex);
#endif
return src;
}