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 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 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);
29 #define SLOT_VALUE_DEBUG slot_value_debug
30static void SLOT_VALUE_DEBUG(SLOT_VALUE const*, dc_debug_fmt, FILE* stream);
31#endif
32
33#if !defined SLOT_VALUE_DELETE
34 #define SLOT_VALUE_DELETE DC_NO_DELETE
35#endif
36
37#if !defined SLOT_VALUE_CLONE
38 #define SLOT_VALUE_CLONE DC_COPY_CLONE
39#endif
40
41#if !defined SLOT_VALUE_DEBUG
42 #define SLOT_VALUE_DEBUG DC_DEFAULT_DEBUG
43#endif
44
45typedef struct {
46 union {
49 };
50 bool present;
51} SELF;
52
61
62static void NS(SELF, set_empty)(SELF* slot, SLOT_INDEX_TYPE next_free) {
64 slot->present = false;
65 slot->next_free = next_free;
66}
67
76
77static void NS(SELF, fill)(SELF* slot, SLOT_VALUE value) {
79 slot->present = true;
80 slot->value = value;
81}
82
83static void NS(SELF, clone_from)(SELF const* from_slot, SELF* to_slot) {
84 to_slot->present = from_slot->present;
85 if (from_slot->present) {
86 to_slot->value = SLOT_VALUE_CLONE(&from_slot->value);
88 } else {
89 to_slot->next_free = from_slot->next_free;
90 NS(SELF, memory_tracker_empty)(to_slot);
91 }
92}
93
94static void NS(SELF, debug)(SELF const* self, dc_debug_fmt fmt, FILE* stream) {
95 if (self->present) {
96 fprintf(stream, "[empty] { next_free: %lu}", (size_t)self->next_free);
97 } else {
98 fprintf(stream, "[full] ");
99 SLOT_VALUE_DEBUG(&self->value, fmt, stream);
100 }
101}
102
103static void NS(SELF, delete)(SELF* self) {
104 if (self->present) {
105 SLOT_VALUE_DELETE(&self->value);
106 }
107}
108
109#undef SLOT_VALUE_DEBUG
110#undef SLOT_VALUE_CLONE
111#undef SLOT_VALUE_DELETE
112#undef SLOT_VALUE
113#undef SLOT_INDEX_TYPE
114
static void debug(SELF const *self, dc_debug_fmt fmt, FILE *stream)
Definition template.h:62
#define SLOT_VALUE_DELETE
Definition template.h:69
#define SLOT_INDEX_TYPE
Definition template.h:65
#define SLOT_VALUE_CLONE
Definition template.h:67
#define SLOT_VALUE
Definition template.h:66
static 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:4
#define TEMPLATE_ERROR(...)
With the user provided name, even in nested templates.
Definition def.h:56
#define SELF
Definition def.h:52
SLOT_INDEX_TYPE next_free
Definition template.h:48
SLOT_VALUE value
Definition template.h:47
Debug format helpers for debug printin data structures.
Definition fmt.h:10
#define SLOT_VALUE_DEBUG
A tagged union containing an integer index type.
Definition template.h:42
static void memory_tracker_present(SELF const *slot)
Definition template.h:68
static void clone_from(SELF const *from_slot, SELF *to_slot)
Definition template.h:83
static void memory_tracker_empty(SELF const *slot)
Definition template.h:53
static void set_empty(SELF *slot, SLOT_INDEX_TYPE next_free)
Definition template.h:62
static void fill(SELF *slot, SLOT_VALUE value)
Definition template.h:77
static FILE * stream(SELF *self)
Opens a file for.
Definition template.h:107