Derive-C
Loading...
Searching...
No Matches
template.h
Go to the documentation of this file.
1
5
7#if !defined(SKIP_INCLUDES)
8 #include "includes.h"
9#endif
10
13
14typedef struct {
15 char const* name;
17} SELF;
18
19static SELF NS(SELF, new)(char const* name, ALLOC* alloc) {
20 return (SELF){.name = name, .base = alloc};
21}
22
23static void* NS(SELF, malloc)(SELF* self, size_t size) {
24 DC_ASSUME(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}
33
34static void* NS(SELF, calloc)(SELF* self, size_t count, size_t size) {
35 DC_ASSUME(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}
44
45static void* NS(SELF, realloc)(SELF* self, void* ptr, size_t size) {
46 DC_ASSUME(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}
55
56static void NS(SELF, free)(SELF* self, void* ptr) {
57 DC_ASSUME(self);
58 printf("%s freeing memory at %p\n", self->name, ptr);
59 NS(ALLOC, free)(self->base, ptr);
60}
61
62static void NS(SELF, debug)(SELF const* self, dc_debug_fmt fmt, FILE* stream) {
63 fprintf(stream, STRINGIFY(SELF) "@%p {\n", self);
64 fmt = dc_debug_fmt_scope_begin(fmt);
65 dc_debug_fmt_print(fmt, stream, "name: %s,\n", self->name);
66 dc_debug_fmt_print(fmt, stream, "base: " STRINGIFY(ALLOC) "@%p,\n", self->base);
67 fmt = dc_debug_fmt_scope_end(fmt);
68 dc_debug_fmt_print(fmt, stream, "}");
69}
70
72
static void debug(SELF const *self, dc_debug_fmt fmt, FILE *stream)
Definition template.h:62
static void free(SELF *self, void *ptr)
Definition template.h:56
static void * realloc(SELF *self, void *ptr, size_t size)
Definition template.h:45
static void * malloc(SELF *self, size_t size)
Definition template.h:23
static void * calloc(SELF *self, size_t count, size_t size)
Definition template.h:34
#define ALLOC
Definition template.h:64
#define DC_TRAIT_ALLOC(SELF)
Definition trait.h:5
static INDEX_TYPE size(SELF const *self)
Definition template.h:252
dc_debug_fmt dc_debug_fmt_scope_end(dc_debug_fmt fmt)
Definition fmt.h:39
dc_debug_fmt dc_debug_fmt_scope_begin(dc_debug_fmt fmt)
Definition fmt.h:33
static void dc_debug_fmt_print(dc_debug_fmt fmt, FILE *stream, const char *format,...)
Definition fmt.h:22
#define STRINGIFY(MACRO)
Definition namespace.h:7
#define NS(pre, post)
Definition namespace.h:4
#define DC_ASSUME(expr,...)
Definition panic.h:56
#define SELF
Definition def.h:52
ALLOC * base
Definition template.h:16
char const * name
Definition template.h:15
Debug format helpers for debug printin data structures.
Definition fmt.h:10
static FILE * stream(SELF *self)
Opens a file for.
Definition template.h:107