-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsingleton.c
More file actions
71 lines (63 loc) · 1.67 KB
/
singleton.c
File metadata and controls
71 lines (63 loc) · 1.67 KB
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
#ifdef CEE_AMALGAMATION
#undef S
#define S(f) _cee_singleton_##f
#else
#define S(f) _##f
#include <string.h>
#include "cee.h"
#endif
#include "cee-header.h"
struct S(header) {
struct cee_sect cs;
uintptr_t _; /* tag */
uintptr_t val;
};
/*
* the parameter of this function has to be a global/static
* uintptr_t array of two elements
*/
#if 1 /* original */
/*
* singleton should never be deleted, hence we pass a noop
*/
static void S(noop)(void *p, enum cee_trace_action ta) {}
struct cee_singleton * cee_singleton_init(void *s, uintptr_t tag, uintptr_t val) {
struct S(header) * b = (struct S(header)* )s;
ZERO_CEE_SECT(&b->cs);
b->cs.trace = S(noop);
b->cs.resize_method = CEE_RESIZE_WITH_IDENTITY;
b->cs.mem_block_size = 0;
b->cs.n_product = 0;
b->_ = tag;
b->val = val;
return (struct cee_singleton *)&(b->_);
}
#else /* update attempt */
#include "cee-resize.h"
static void S(trace) (void * v, enum cee_trace_action ta) {
struct S(header) * m = FIND_HEADER(v);
switch(ta) {
case CEE_TRACE_DEL_FOLLOW:
case CEE_TRACE_DEL_NO_FOLLOW:
S(de_chain)(m);
free(m);
break;
case CEE_TRACE_MARK:
m->cs.gc_mark = ta - CEE_TRACE_MARK;
break;
}
}
struct cee_singleton * cee_singleton_mk(struct cee_state * st, cee_tag_t tag, uintptr_t val) {
size_t mem_block_size = sizeof(struct S(header));
struct S(header) * b = malloc(mem_block_size);
ZERO_CEE_SECT(&b->cs);
S(chain)(b, st);
b->cs.trace = S(trace);
b->cs.resize_method = CEE_RESIZE_WITH_IDENTITY;
b->cs.mem_block_size = mem_block_size;
b->_ = tag;
b->val = val;
cee_state_add_singleton(st, tag, val);
return (struct cee_singleton *)&(b->_);
}
#endif