Derive-C
Loading...
Searching...
No Matches
template.h
Go to the documentation of this file.
1
2// - Used the the arena data structures
3
5#if !defined(SKIP_INCLUDES)
6 #include "includes.h"
7#endif
9
10#if !defined SLOT_INDEX_TYPE
11 #if !defined DC_PLACEHOLDERS
12TEMPLATE_ERROR("A SLOT_INDEX_TYPE needs to be specified for a slot")
13 #endif
14 #define SLOT_INDEX_TYPE int
15#endif
16
17#if !defined SLOT_VALUE
18 #if !defined DC_PLACEHOLDERS
19TEMPLATE_ERROR("A SLOT_VALUE must be defined for a slot")
20 #endif
21 #define SLOT_VALUE slot_value_t
22typedef struct {
23 int x;
25 #define SLOT_VALUE_DELETE slot_value_delete
26static void SLOT_VALUE_DELETE(slot_value_t* /* self */) {}
27 #define SLOT_VALUE_CLONE slot_value_clone
28static slot_value_t SLOT_VALUE_CLONE(slot_value_t const* self) { return *self; }
29 #define SLOT_VALUE_DEBUG slot_value_debug
30static void SLOT_VALUE_DEBUG(SLOT_VALUE const* /* self */, dc_debug_fmt /*fmt */,
31 FILE* /* stream */) {}
32#endif
33
34#if !defined SLOT_VALUE_DELETE
35 #define SLOT_VALUE_DELETE DC_NO_DELETE
36#endif
37
38#if !defined SLOT_VALUE_CLONE
39 #define SLOT_VALUE_CLONE DC_COPY_CLONE
40#endif
41
42#if !defined SLOT_VALUE_DEBUG
43 #define SLOT_VALUE_DEBUG DC_DEFAULT_DEBUG
44#endif
45
46typedef struct {
47// JUSTIFY: Union for release, struct for debug
48// In order to allow for easy msan / uninitialised checks with debug
49// - To allow derive-c's own tests to just check the value* goes to uninitialised
50// - To allow for a throw on any access through user's pointers accessing the value
51// In release, we save memory by using a union.
52#if defined NDEBUG
53 union {
54 SLOT_VALUE value;
55 SLOT_INDEX_TYPE next_free;
56 };
57#else
60#endif
61 bool present;
62} SELF;
63
72
73DC_INTERNAL static void NS(SELF, set_empty)(SELF* slot, SLOT_INDEX_TYPE next_free) {
75 slot->present = false;
76 slot->next_free = next_free;
77}
78
87
88DC_INTERNAL static void NS(SELF, fill)(SELF* slot, SLOT_VALUE value) {
90 slot->present = true;
91 slot->value = value;
92}
93
94DC_INTERNAL static void NS(SELF, clone_from)(SELF const* from_slot, SELF* to_slot) {
95 to_slot->present = from_slot->present;
96 if (from_slot->present) {
97 NS(SELF, fill)(to_slot, SLOT_VALUE_CLONE(&from_slot->value));
98 } else {
99 NS(SELF, set_empty)(to_slot, from_slot->next_free);
100 }
101}
102
103DC_INTERNAL static void NS(SELF, debug)(SELF const* self, dc_debug_fmt fmt, FILE* stream) {
104 if (self->present) {
105 fprintf(stream, "[empty] { next_free: %lu}", (size_t)self->next_free);
106 } else {
107 fprintf(stream, "[full] ");
108 SLOT_VALUE_DEBUG(&self->value, fmt, stream);
109 }
110}
111
112DC_INTERNAL static void NS(SELF, delete)(SELF* self) {
113 if (self->present) {
114 SLOT_VALUE_DELETE(&self->value);
115 }
116}
117
118#undef SLOT_VALUE_DEBUG
119#undef SLOT_VALUE_CLONE
120#undef SLOT_VALUE_DELETE
121#undef SLOT_VALUE
122#undef SLOT_INDEX_TYPE
123
static DC_PUBLIC void debug(SELF const *self, dc_debug_fmt fmt, FILE *stream)
Definition template.h:212
#define SLOT_VALUE_DELETE
Definition template.h:71
#define SLOT_INDEX_TYPE
Definition template.h:67
#define SLOT_VALUE_CLONE
Definition template.h:69
#define SLOT_VALUE
Definition template.h:68
#define TEMPLATE_ERROR(...)
With the user provided name, even in nested templates.
Definition def.h:56
#define SELF
Definition def.h:52
static DC_PUBLIC void dc_memory_tracker_set(dc_memory_tracker_level level, dc_memory_tracker_capability cap, const volatile void *addr, size_t size)
@ DC_MEMORY_TRACKER_LVL_CONTAINER
@ DC_MEMORY_TRACKER_CAP_WRITE
@ DC_MEMORY_TRACKER_CAP_NONE
@ DC_MEMORY_TRACKER_CAP_READ_WRITE
#define NS(pre, post)
Definition namespace.h:14
#define DC_INTERNAL
Definition namespace.h:30
bool present
Definition template.h:45
SLOT_INDEX_TYPE next_free
Definition template.h:59
SLOT_VALUE value
Definition template.h:58
Debug format helpers for debug printin data structures.
Definition fmt.h:11
static DC_INTERNAL void clone_from(SELF const *from_slot, SELF *to_slot)
Definition template.h:94
#define SLOT_VALUE_DEBUG
A tagged union containing an integer index type.
Definition template.h:43
static DC_INTERNAL void set_empty(SELF *slot, SLOT_INDEX_TYPE next_free)
Definition template.h:73
static DC_INTERNAL void memory_tracker_present(SELF const *slot)
Definition template.h:79
static DC_INTERNAL void fill(SELF *slot, SLOT_VALUE value)
Definition template.h:88
static DC_INTERNAL void memory_tracker_empty(SELF const *slot)
Definition template.h:64
static DC_PUBLIC FILE * stream(SELF *self)
Definition template.h:108