Derive-C
Loading...
Searching...
No Matches
template.h File Reference
#include <stddef.h>
#include <derive-c/allocs/std.h>
#include <derive-c/core.h>
#include <derive-c/panic.h>
#include <derive-c/self.h>
#include <derive-c/allocs/null.h>
#include <derive-c/structures/vector/template.h>
Include dependency graph for template.h:

Go to the source code of this file.

Classes

struct  TRACKED_ENTRY
struct  SELF

Macros

#define ALLOC   nullalloc
 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 ENTRIES   derive_c_entries_placeholder_name
#define TRACKED_ENTRY   NAME(ENTRIES, entry)
#define T   TRACKED_ENTRY
#define ALLOC   stdalloc
 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 SELF   ENTRIES

Functions

static SELF new (ALLOC *alloc)
static ENTRIES 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 [1/2]

#define ALLOC   nullalloc

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 21 of file template.h.

◆ ALLOC [2/2]

#define ALLOC   stdalloc

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 21 of file template.h.

◆ ENTRIES

#define ENTRIES   derive_c_entries_placeholder_name

Definition at line 28 of file template.h.

◆ SELF

#define SELF   ENTRIES

Definition at line 70 of file template.h.

◆ T

#define T   TRACKED_ENTRY

Definition at line 68 of file template.h.

◆ TRACKED_ENTRY

#define TRACKED_ENTRY   NAME(ENTRIES, entry)

Definition at line 55 of file template.h.

Function Documentation

◆ calloc()

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

Definition at line 102 of file template.h.

102 {
103 DEBUG_ASSERT(self);
104 void* ptr = NAME(ALLOC, calloc)(self->alloc, count, size);
105 if (ptr) {
107 .ptr = ptr,
108 .freed = false,
109 });
110 }
111 return ptr;
112}
#define ALLOC
An allocator that prints to stdout when it allocates or frees memory.
Definition template.h:17
static void * calloc(SELF *self, size_t count, size_t size)
Definition template.h:40
#define TRACKED_ENTRY
Definition template.h:55
#define ENTRIES
Definition template.h:28
#define NAME(pre, post)
Definition core.h:5
#define DEBUG_ASSERT(expr)
Definition panic.h:23
ENTRIES entries
Definition template.h:77
ALLOC * alloc
Definition template.h:76
static INDEX_TYPE size(SELF const *self)
Definition template.h:207
static T * push(SELF *self, T value)
Definition template.h:102
Here is the call graph for this function:

◆ free()

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

Definition at line 132 of file template.h.

132 {
133 DEBUG_ASSERT(ptr);
134 DEBUG_ASSERT(self);
135
136 NAME(ENTRIES, iter) iter = NAME(ENTRIES, get_iter)(&self->entries);
137 TRACKED_ENTRY* entry;
138
139 while ((entry = NAME(ENTRIES, iter_next)(&iter))) {
140 if (entry->ptr == ptr) {
141 DEBUG_ASSERT(!entry->freed);
142 entry->freed = true;
143 break;
144 }
145 }
146
147 NAME(ALLOC, free)(self->alloc, ptr);
148}
static void free(SELF *self, void *ptr)
Definition template.h:62
bool freed
Definition template.h:59
void * ptr
Definition template.h:58
static ITER get_iter(SELF *self)
Definition template.h:312
Here is the call graph for this function:

◆ get_entries()

ENTRIES const * get_entries ( SELF const * self)
static

Definition at line 84 of file template.h.

84 {
85 DEBUG_ASSERT(self);
86 return &self->entries;
87}
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 114 of file template.h.

114 {
115 DEBUG_ASSERT(self);
116 void* ptr = NAME(ALLOC, malloc)(self->alloc, size);
117 if (ptr) {
119 .ptr = ptr,
120 .freed = false,
121 });
122 }
123 return ptr;
124}
static void * malloc(SELF *self, size_t size)
Definition template.h:29
Here is the call graph for this function:

◆ new()

SELF new ( ALLOC * alloc)
static

Definition at line 80 of file template.h.

80 {
81 return (SELF){.alloc = alloc, .entries = NAME(ENTRIES, new)(stdalloc_get())};
82}
#define SELF
Definition template.h:70

◆ realloc()

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

Definition at line 126 of file template.h.

126 {
127 DEBUG_ASSERT(self);
128 DEBUG_ASSERT(ptr);
129 return NAME(ALLOC, realloc)(self->alloc, ptr, size);
130}
static void * realloc(SELF *self, void *ptr, size_t size)
Definition template.h:51
Here is the call graph for this function:

◆ unleak_and_delete()

void unleak_and_delete ( SELF * self)
static

Definition at line 89 of file template.h.

89 {
90 NAME(ENTRIES, iter) iter = NAME(ENTRIES, get_iter)(&self->entries);
91 TRACKED_ENTRY* entry;
92
93 while ((entry = NAME(ENTRIES, iter_next)(&iter))) {
94 if (!entry->freed) {
95 NAME(ALLOC, free)(self->alloc, entry->ptr);
96 }
97 }
98
99 NAME(ENTRIES, delete)(&self->entries);
100}
Here is the call graph for this function:
Here is the caller graph for this function: