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)
 TRAIT_ALLOC (SELF)

Macro Definition Documentation

◆ ALLOC

#define ALLOC   stdalloc

Definition at line 59 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. However, this is usable inside unit tests written in C.

Definition at line 43 of file template.h.

◆ INTERNAL_NAME

#define INTERNAL_NAME   ENTRIES_VECTOR

Definition at line 60 of file template.h.

◆ ITEM

#define ITEM   TRACKED_ENTRY

Definition at line 58 of file template.h.

◆ TRACKED_ENTRY

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

Definition at line 44 of file template.h.

Function Documentation

◆ calloc()

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

Definition at line 92 of file template.h.

92 {
93 ASSUME(self);
94 void* ptr = NS(ALLOC, calloc)(self->alloc, count, size);
95 if (ptr) {
97 .ptr = ptr,
98 .freed = false,
99 });
100 }
101 return ptr;
102}
static void * calloc(SELF *self, size_t count, size_t size)
Definition template.h:36
#define ALLOC
Definition template.h:59
#define TRACKED_ENTRY
Definition template.h:44
#define ENTRIES_VECTOR
For unit tests expected to throw, as C has no unwind, we cannot free allocated memory....
Definition template.h:43
static INDEX_TYPE size(SELF const *self)
Definition template.h:275
static ITEM * push(SELF *self, ITEM item)
Definition template.h:214
#define ASSUME(expr,...)
Definition macros.h:62
#define NS(pre, post)
Definition namespace.h:4
ENTRIES_VECTOR entries
Definition template.h:67
ALLOC * alloc
Definition template.h:66
Here is the call graph for this function:

◆ free()

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

Definition at line 122 of file template.h.

122 {
123 ASSUME(ptr);
124 ASSUME(self);
125
126 NS(ENTRIES_VECTOR, iter) iter = NS(ENTRIES_VECTOR, get_iter)(&self->entries);
127 TRACKED_ENTRY* entry;
128
129 while ((entry = NS(ENTRIES_VECTOR, iter_next)(&iter))) {
130 if (entry->ptr == ptr) {
131 ASSUME(!entry->freed);
132 entry->freed = true;
133 break;
134 }
135 }
136
137 NS(ALLOC, free)(self->alloc, ptr);
138}
static void free(SELF *self, void *ptr)
Definition template.h:58
static ITER get_iter(SELF *self)
Definition template.h:388
bool freed
Definition template.h:48
void * ptr
Definition template.h:47
Here is the call graph for this function:

◆ get_entries()

ENTRIES_VECTOR const * get_entries ( SELF const * self)
static

Definition at line 74 of file template.h.

74 {
75 ASSUME(self);
76 return &self->entries;
77}
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 104 of file template.h.

104 {
105 ASSUME(self);
106 void* ptr = NS(ALLOC, malloc)(self->alloc, size);
107 if (ptr) {
109 .ptr = ptr,
110 .freed = false,
111 });
112 }
113 return ptr;
114}
static void * malloc(SELF *self, size_t size)
Definition template.h:25
Here is the call graph for this function:

◆ new()

SELF new ( ALLOC * alloc)
static

Definition at line 70 of file template.h.

70 {
71 return (SELF){.alloc = alloc, .entries = NS(ENTRIES_VECTOR, new)(stdalloc_get())};
72}
#define SELF
Definition def.h:52

◆ realloc()

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

Definition at line 116 of file template.h.

116 {
117 ASSUME(self);
118 ASSUME(ptr);
119 return NS(ALLOC, realloc)(self->alloc, ptr, size);
120}
static void * realloc(SELF *self, void *ptr, size_t size)
Definition template.h:47
Here is the call graph for this function:

◆ TRAIT_ALLOC()

TRAIT_ALLOC ( SELF )

◆ unleak_and_delete()

void unleak_and_delete ( SELF * self)
static

Definition at line 79 of file template.h.

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