Hash :
36d32503
Author :
Thomas de Grivel
Date :
2023-12-10T23:48:01
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
/* c3
* Copyright 2022,2023 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 <assert.h>
#include <err.h>
#include <stdlib.h>
#include <string.h>
#include "env.h"
#include "map.h"
#include "struct.h"
#include "struct_type.h"
#include "sym.h"
#include "tag.h"
#include "tag_type.h"
void struct_clean (s_struct *s)
{
f_clean clean;
s8 *data;
uw i = 0;
const s_sym *sym;
assert(s);
data = s->data;
while (i < s->type.map.count) {
if (tag_type(s->type.map.value + i, &sym)) {
clean = sym_to_clean(sym);
if (clean)
clean(data + s->type.offset[i]);
i++;
}
}
free(data);
struct_type_clean(&s->type);
}
s_struct * struct_init_copy (s_struct *s, const s_struct *src)
{
f_clean clean;
f_init_copy init_copy;
uw i = 0;
uw size;
const s_sym *sym;
s_struct tmp;
assert(s);
assert(src);
if (! struct_type_init_copy(&tmp.type, &src->type))
return NULL;
tmp.free = true;
tmp.data = calloc(1, tmp.type.size);
while (i < tmp.type.map.count) {
if (tag_type(tmp.type.map.value + i, &sym)) {
init_copy = sym_to_init_copy(sym);
if (init_copy) {
if (! init_copy((s8 *) tmp.data + tmp.type.offset[i],
(s8 *) src->data + tmp.type.offset[i]))
goto ko;
}
else {
size = tag_size(tmp.type.map.value + i);
memcpy((s8 *) tmp.data + tmp.type.offset[i],
(s8 *) src->data + tmp.type.offset[i],
size);
}
}
i++;
}
*s = tmp;
return s;
ko:
while (i > 0) {
i--;
tag_type(tmp.type.map.value + i, &sym);
clean = sym_to_clean(sym);
if (clean)
clean((s8 *) tmp.data + tmp.type.offset[i]);
}
free(tmp.data);
struct_type_clean(&tmp.type);
return NULL;
}
void struct_delete (s_struct *s)
{
assert(s);
struct_clean(s);
free(s);
}
s_struct * struct_init (s_struct *s, const s_sym *module)
{
assert(s);
assert(module);
if (! struct_type_init_from_env(&s->type, module, &g_c3_env))
return NULL;
s->data = calloc(s->type.size, 1);
return s;
}
s_struct * struct_init_1 (s_struct *s, const s8 *p)
{
assert(s);
assert(p);
(void) s;
(void) p;
return s;
}
s_struct * struct_new (const s_sym *module)
{
s_struct *s;
assert(module);
s = calloc(1, sizeof(s_struct));
if (! s) {
warn("struct_new: %s: calloc", module->str.ptr.ps8);
return NULL;
}
if (! struct_init(s, module)) {
free(s);
return NULL;
}
return s;
}
s_struct * struct_new_1 (const s8 *p)
{
s_struct *s;
assert(p);
s = calloc(1, sizeof(s_struct));
if (! s) {
warn("struct_new_1: %s: calloc", p);
return NULL;
}
if (! struct_init_1(s, p)) {
free(s);
return NULL;
}
return s;
}