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

Go to the source code of this file.

Classes

struct  SELF

Macros

#define ALLOC   nullalloc
 An allocator that prints to stdout when it allocates or frees memory.

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)

Macro Definition Documentation

◆ ALLOC

#define ALLOC   nullalloc

An allocator that prints to stdout when it allocates or frees memory.

  • Takes a specific instance, so we can define different printers for

Definition at line 17 of file template.h.

Function Documentation

◆ calloc()

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

Definition at line 40 of file template.h.

40 {
41 DEBUG_ASSERT(self);
42 void* ptr = NAME(ALLOC, calloc)(self->base, count, size);
43 if (ptr) {
44 printf("%s allocated %zu bytes at %p\n", self->name, count * size, ptr);
45 } else {
46 printf("%s failed to allocate %zu bytes\n", self->name, count * size);
47 }
48 return ptr;
49}
#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 NAME(pre, post)
Definition core.h:5
#define DEBUG_ASSERT(expr)
Definition panic.h:23
ALLOC * base
Definition template.h:22
char const * name
Definition template.h:21
static INDEX_TYPE size(SELF const *self)
Definition template.h:207
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 62 of file template.h.

62 {
63 DEBUG_ASSERT(self);
64 printf("%s freeing memory at %p\n", self->name, ptr);
65 NAME(ALLOC, free)(self->base, ptr);
66}
static void free(SELF *self, void *ptr)
Definition template.h:62
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 29 of file template.h.

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

25 {
26 return (SELF){.name = name, .base = alloc};
27}
#define SELF
Definition template.h:70

◆ realloc()

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

Definition at line 51 of file template.h.

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