Derive-C
Loading...
Searching...
No Matches
template.h
Go to the documentation of this file.
1
5
6// JUSTIFY: No custom memory tracking
7// - already done by the wrapper allocator
8
9#include <stdio.h>
10
15
16typedef struct {
17 char const* name;
19} SELF;
20
21static SELF NS(SELF, new)(char const* name, ALLOC* alloc) {
22 return (SELF){.name = name, .base = alloc};
23}
24
25static void* NS(SELF, malloc)(SELF* self, size_t size) {
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}
35
36static void* NS(SELF, calloc)(SELF* self, size_t count, size_t size) {
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}
46
47static void* NS(SELF, realloc)(SELF* self, void* ptr, size_t size) {
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}
57
58static void NS(SELF, free)(SELF* self, void* ptr) {
59 ASSUME(self);
60 printf("%s freeing memory at %p\n", self->name, ptr);
61 NS(ALLOC, free)(self->base, ptr);
62}
63
65
static void free(SELF *self, void *ptr)
Definition template.h:58
static void * realloc(SELF *self, void *ptr, size_t size)
Definition template.h:47
static void * malloc(SELF *self, size_t size)
Definition template.h:25
static void * calloc(SELF *self, size_t count, size_t size)
Definition template.h:36
#define ALLOC
Definition template.h:59
#define TRAIT_ALLOC(SELF)
Definition trait.h:4
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
#define SELF
Definition def.h:52
ALLOC * base
Definition template.h:18
char const * name
Definition template.h:17