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  SELF
 An allocator that prints to stdout when it allocates or frees memory. More...

Functions

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

Function Documentation

◆ calloc()

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

Definition at line 34 of file template.h.

34 {
35 DEBUG_ASSERT(self);
36 void* ptr = NS(ALLOC, calloc)(self->base, count, size);
37 if (ptr) {
38 printf("%s allocated %zu bytes at %p\n", self->name, count * size, ptr);
39 } else {
40 printf("%s failed to allocate %zu bytes\n", self->name, count * size);
41 }
42 return ptr;
43}
static void * calloc(SELF *self, size_t count, size_t size)
Definition template.h:34
#define ALLOC
Definition template.h:56
#define NS(pre, post)
Definition helpers.h:6
#define DEBUG_ASSERT(expr)
Definition panic.h:34
ALLOC * base
Definition template.h:16
char const * name
Definition template.h:15
static INDEX_TYPE size(SELF const *self)
Definition template.h:220
Here is the call graph for this function:
Here is the caller graph for this function:

◆ free()

void free ( SELF * self,
void * ptr )
static
Examples
structures/arena.c, structures/hashmap.c, structures/option.c, and structures/vector.c.

Definition at line 56 of file template.h.

56 {
57 DEBUG_ASSERT(self);
58 printf("%s freeing memory at %p\n", self->name, ptr);
59 NS(ALLOC, free)(self->base, ptr);
60}
static void free(SELF *self, void *ptr)
Definition template.h:56
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
Examples
structures/arena.c.

Definition at line 23 of file template.h.

23 {
24 DEBUG_ASSERT(self);
25 void* ptr = NS(ALLOC, malloc)(self->base, size);
26 if (ptr) {
27 printf("%s allocated %zu bytes at %p\n", self->name, size, ptr);
28 } else {
29 printf("%s failed to allocate %zu bytes\n", self->name, size);
30 }
31 return ptr;
32}
static void * malloc(SELF *self, size_t size)
Definition template.h:23
Here is the call graph for this function:
Here is the caller graph for this function:

◆ new()

SELF new ( char const * name,
ALLOC * alloc )
static

Definition at line 19 of file template.h.

19 {
20 return (SELF){.name = name, .base = alloc};
21}
#define SELF
Definition def.h:51

◆ realloc()

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

Definition at line 45 of file template.h.

45 {
46 DEBUG_ASSERT(self);
47 void* new_ptr = NS(ALLOC, realloc)(self->base, ptr, size);
48 if (new_ptr) {
49 printf("%s reallocated memory at %p to %zu bytes\n", self->name, new_ptr, size);
50 } else {
51 printf("%s failed to reallocate memory at %p to %zu bytes\n", self->name, ptr, size);
52 }
53 return new_ptr;
54}
static void * realloc(SELF *self, void *ptr, size_t size)
Definition template.h:45
Here is the call graph for this function:
Here is the caller graph for this function: