Branch
Hash :
516d1a0f
Author :
Thomas de Grivel
Date :
2024-12-25T16:07:10
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
/* kc3
* Copyright 2022,2023,2024 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 (callable->reference_count <= 0)
goto clean;
if (--callable->reference_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
alloc_unmap(callable, sizeof(s_callable));
return;
clean:
#if HAVE_PTHREAD
mutex_unlock(&callable->mutex);
#endif
return;
}
s_callable * callable_new (void)
{
s_callable *callable;
if (! (callable = alloc_map(sizeof(s_callable))))
return NULL;
#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 (src->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;
}
tmp->reference_count = 1;
return tmp;
ko:
alloc_unmap(tmp, sizeof(s_callable));
return NULL;
}
s_callable * callable_new_ref (s_callable *callable)
{
assert(callable);
#if HAVE_PTHREAD
mutex_lock(&callable->mutex);
#endif
if (callable->reference_count <= 0) {
err_puts("callable_new_ref: reference count <= 0");
assert(! "callable_new_ref: reference count <= 0");
abort();
}
callable->reference_count++;
#if HAVE_PTHREAD
mutex_unlock(&callable->mutex);
#endif
return callable;
}
void p_callable_clean (p_callable *callable)
{
callable_delete(*callable);
}
p_callable * p_callable_init (p_callable *callable)
{
p_callable tmp;
if (! (tmp = callable_new()))
return NULL;
*callable = tmp;
return callable;
}
p_callable * p_callable_init_cast (p_callable *callable,
const s_sym * const *type,
const s_tag *tag)
{
const s_sym *tag_type_;
(void) callable;
switch (tag->type) {
case TAG_CALLABLE:
err_puts("p_callable_init_cast: not implemented.");
assert(! "p_callable_init_cast: not implemented.");
return NULL;
default:
break;
}
err_write_1("p_callable_init_cast: cannot cast from ");
tag_type(tag, &tag_type_);
err_inspect_sym(&tag_type_);
err_write_1(" to ");
err_inspect_sym(type);
err_write_1(" aka Callable.\n");
assert(! "p_callable_init_cast: cannot cast to Callable.");
return NULL;
}
p_callable * p_callable_init_copy (p_callable *callable,
p_callable *src)
{
p_callable tmp;
if (! (tmp = callable_new_ref(*src)))
return NULL;
*callable = tmp;
return callable;
}