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

Function Documentation

◆ calloc()

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

Definition at line 36 of file template.h.

36 {
37 ASSUME(self);
38 void* ptr = NS(ALLOC, calloc)(self->base, count, size);
39 if (ptr) {
40 printf("%s allocated %zu bytes at %p\n", self->name, count * size, ptr);
41 } else {
42 printf("%s failed to allocate %zu bytes\n", self->name, count * size);
43 }
44 return ptr;
45}
static void * calloc(SELF *self, size_t count, size_t size)
Definition template.h:36
#define ALLOC
Definition template.h:59
static INDEX_TYPE size(SELF const *self)
Definition template.h:275
#define ASSUME(expr,...)
Definition macros.h:62
#define NS(pre, post)
Definition namespace.h:4
ALLOC * base
Definition template.h:18
char const * name
Definition template.h:17
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
container/arena/basic.c, container/map/decomposed.c, container/vector/dynamic.c, and utils/option.c.

Definition at line 58 of file template.h.

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

Definition at line 25 of file template.h.

25 {
26 ASSUME(self);
27 void* ptr = NS(ALLOC, malloc)(self->base, size);
28 if (ptr) {
29 printf("%s allocated %zu bytes at %p\n", self->name, size, ptr);
30 } else {
31 printf("%s failed to allocate %zu bytes\n", self->name, size);
32 }
33 return ptr;
34}
static void * malloc(SELF *self, size_t size)
Definition template.h:25
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 21 of file template.h.

21 {
22 return (SELF){.name = name, .base = alloc};
23}
#define SELF
Definition def.h:52

◆ realloc()

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

Definition at line 47 of file template.h.

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

◆ TRAIT_ALLOC()

TRAIT_ALLOC ( SELF )