Derive-C
Loading...
Searching...
No Matches
template.h File Reference
Include dependency graph for template.h:

Go to the source code of this file.

Classes

struct  TRACKED_ENTRY
struct  SELF
 An allocator that prints to stdout when it allocates or frees memory. More...

Macros

#define ENTRIES_VECTOR   NS(NAME, entries)
 For unit tests expected to throw, as C has no unwind, we cannot free allocated memory. This macro wraps the allocator in debug, to allow clearing leaks after an exception.
#define TRACKED_ENTRY   NS(EXPAND(ENTRIES), entry)
#define ITEM   TRACKED_ENTRY
#define ALLOC   stdalloc
#define INTERNAL_NAME   ENTRIES_VECTOR

Functions

static SELF new (ALLOC *alloc)
static ENTRIES_VECTOR const * get_entries (SELF const *self)
static void unleak_and_delete (SELF *self)
static void * calloc (SELF *self, size_t count, size_t size)
static void * malloc (SELF *self, size_t size)
static void * realloc (SELF *self, void *ptr, size_t size)
static void free (SELF *self, void *ptr)

Macro Definition Documentation

◆ ALLOC

#define ALLOC   stdalloc

Definition at line 56 of file template.h.

◆ ENTRIES_VECTOR

#define ENTRIES_VECTOR   NS(NAME, entries)

For unit tests expected to throw, as C has no unwind, we cannot free allocated memory. This macro wraps the allocator in debug, to allow clearing leaks after an exception.

In release, it is a no-op / pass through.

As this is entirely C, we do not get the niceties of a C++ RAII allocator guard shebang.

Definition at line 40 of file template.h.

◆ INTERNAL_NAME

#define INTERNAL_NAME   ENTRIES_VECTOR

Definition at line 57 of file template.h.

◆ ITEM

#define ITEM   TRACKED_ENTRY

Definition at line 55 of file template.h.

◆ TRACKED_ENTRY

#define TRACKED_ENTRY   NS(EXPAND(ENTRIES), entry)

Definition at line 41 of file template.h.

Function Documentation

◆ calloc()

void * calloc ( SELF * self,
size_t count,
size_t size )
static

Definition at line 89 of file template.h.

89 {
90 DEBUG_ASSERT(self);
91 void* ptr = NS(ALLOC, calloc)(self->alloc, count, size);
92 if (ptr) {
94 .ptr = ptr,
95 .freed = false,
96 });
97 }
98 return ptr;
99}
static void * calloc(SELF *self, size_t count, size_t size)
Definition template.h:34
#define ALLOC
Definition template.h:56
#define TRACKED_ENTRY
Definition template.h:41
#define ENTRIES_VECTOR
For unit tests expected to throw, as C has no unwind, we cannot free allocated memory....
Definition template.h:40
#define NS(pre, post)
Definition helpers.h:6
#define DEBUG_ASSERT(expr)
Definition panic.h:34
ENTRIES_VECTOR entries
Definition template.h:64
ALLOC * alloc
Definition template.h:63
static INDEX_TYPE size(SELF const *self)
Definition template.h:220
static ITEM * push(SELF *self, ITEM item)
Definition template.h:140
Here is the call graph for this function:

◆ free()

void free ( SELF * self,
void * ptr )
static

Definition at line 119 of file template.h.

119 {
120 DEBUG_ASSERT(ptr);
121 DEBUG_ASSERT(self);
122
123 NS(ENTRIES_VECTOR, iter) iter = NS(ENTRIES_VECTOR, get_iter)(&self->entries);
124 TRACKED_ENTRY* entry;
125
126 while ((entry = NS(ENTRIES_VECTOR, iter_next)(&iter))) {
127 if (entry->ptr == ptr) {
128 DEBUG_ASSERT(!entry->freed);
129 entry->freed = true;
130 break;
131 }
132 }
133
134 NS(ALLOC, free)(self->alloc, ptr);
135}
static void free(SELF *self, void *ptr)
Definition template.h:56
bool freed
Definition template.h:45
void * ptr
Definition template.h:44
static ITER get_iter(SELF *self)
Definition template.h:318
Here is the call graph for this function:

◆ get_entries()

ENTRIES_VECTOR const * get_entries ( SELF const * self)
static

Definition at line 71 of file template.h.

71 {
72 DEBUG_ASSERT(self);
73 return &self->entries;
74}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ malloc()

void * malloc ( SELF * self,
size_t size )
static

Definition at line 101 of file template.h.

101 {
102 DEBUG_ASSERT(self);
103 void* ptr = NS(ALLOC, malloc)(self->alloc, size);
104 if (ptr) {
106 .ptr = ptr,
107 .freed = false,
108 });
109 }
110 return ptr;
111}
static void * malloc(SELF *self, size_t size)
Definition template.h:23
Here is the call graph for this function:

◆ new()

SELF new ( ALLOC * alloc)
static

Definition at line 67 of file template.h.

67 {
68 return (SELF){.alloc = alloc, .entries = NS(ENTRIES_VECTOR, new)(stdalloc_get())};
69}
#define SELF
Definition def.h:51

◆ realloc()

void * realloc ( SELF * self,
void * ptr,
size_t size )
static

Definition at line 113 of file template.h.

113 {
114 DEBUG_ASSERT(self);
115 DEBUG_ASSERT(ptr);
116 return NS(ALLOC, realloc)(self->alloc, ptr, size);
117}
static void * realloc(SELF *self, void *ptr, size_t size)
Definition template.h:45
Here is the call graph for this function:

◆ unleak_and_delete()

void unleak_and_delete ( SELF * self)
static

Definition at line 76 of file template.h.

76 {
77 NS(ENTRIES_VECTOR, iter) iter = NS(ENTRIES_VECTOR, get_iter)(&self->entries);
78 TRACKED_ENTRY* entry;
79
80 while ((entry = NS(ENTRIES_VECTOR, iter_next)(&iter))) {
81 if (!entry->freed) {
82 NS(ALLOC, free)(self->alloc, entry->ptr);
83 }
84 }
85
86 NS(ENTRIES_VECTOR, delete)(&self->entries);
87}
Here is the call graph for this function:
Here is the caller graph for this function: