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;
16 FILE* stream;
17 NS(ALLOC, ref) alloc_ref;
18} SELF;
19
20DC_PUBLIC static SELF NS(SELF, new)(char const* name, FILE* stream, NS(ALLOC, ref) alloc_ref) {
21 fprintf(stream, "[%s] %s(alloc=" DC_EXPAND_STRING(ALLOC) "@%p)\n", name, __func__,
22 (void*)NS(NS(ALLOC, ref), deref)(alloc_ref));
23 return (SELF){
24 .name = name,
25 .stream = stream,
26 .alloc_ref = alloc_ref,
27 };
28}
29
30DC_PUBLIC static void* NS(SELF, allocate_uninit)(SELF* self, size_t size) {
31 DC_ASSUME(self);
32 void* ptr = NS(ALLOC, allocate_uninit)(self->alloc_ref, size);
33 fprintf(self->stream, "[%s] %s(size=%zu) -> %p\n", self->name, __func__, size, ptr);
34 return ptr;
35}
36
37DC_PUBLIC static void* NS(SELF, allocate_zeroed)(SELF* self, size_t size) {
38 DC_ASSUME(self);
39 void* ptr = NS(ALLOC, allocate_zeroed)(self->alloc_ref, size);
40 fprintf(self->stream, "[%s] %s(size=%zu) -> %p\n", self->name, __func__, size, ptr);
41 return ptr;
42}
43
44DC_PUBLIC static void* NS(SELF, reallocate)(SELF* self, void* ptr, size_t old_size,
45 size_t new_size) {
46 DC_ASSUME(self);
47 void* new_ptr = NS(ALLOC, reallocate)(self->alloc_ref, ptr, old_size, new_size);
48 fprintf(self->stream, "[%s] %s(ptr=%p, old_size=%zu, new_size=%zu) -> %p\n", self->name,
49 __func__, ptr, old_size, new_size, new_ptr);
50 return new_ptr;
51}
52
53DC_PUBLIC static void NS(SELF, deallocate)(SELF* self, void* ptr, size_t size) {
54 DC_ASSUME(self);
55 fprintf(self->stream, "[%s] %s(ptr=%p, size=%zu)\n", self->name, __func__, ptr, size);
56 NS(ALLOC, deallocate)(self->alloc_ref, ptr, size);
57}
58
59DC_PUBLIC static void NS(SELF, debug)(SELF const* self, dc_debug_fmt fmt, FILE* stream) {
60 fprintf(stream, DC_EXPAND_STRING(SELF) "@%p {\n", (void*)self);
61 fmt = dc_debug_fmt_scope_begin(fmt);
62 dc_debug_fmt_print(fmt, stream, "name: %s,\n", self->name);
63 dc_debug_fmt_print(fmt, stream, "alloc: ");
64 NS(ALLOC, debug)(NS(NS(ALLOC, ref), deref)(self->alloc_ref), fmt, stream);
65 fprintf(stream, "\n");
66 fmt = dc_debug_fmt_scope_end(fmt);
67 dc_debug_fmt_print(fmt, stream, "}");
68}
69
70DC_PUBLIC static void NS(SELF, delete)(SELF* self) {
71 DC_ASSUME(self);
72 fprintf(self->stream, "[%s]: %s\n", self->name, __func__);
73}
74
76
78
static DC_PUBLIC void deallocate(SELF *self, void *ptr, size_t size)
Definition template.h:127
static DC_PUBLIC void debug(SELF const *self, dc_debug_fmt fmt, FILE *stream)
Definition template.h:212
static DC_PUBLIC void * allocate_zeroed(SELF *self, size_t size)
Definition template.h:117
static DC_PUBLIC void * allocate_uninit(SELF *self, size_t size)
Definition template.h:92
static DC_PUBLIC void * reallocate(SELF *self, void *ptr, size_t old_size, size_t new_size)
Definition template.h:137
#define ALLOC
Definition template.h:31
#define DC_TRAIT_ALLOC(SELF)
Definition trait.h:18
static DC_PUBLIC size_t size(SELF const *self)
Definition template.h:252
#define SELF
Definition def.h:52
static DC_PUBLIC void dc_debug_fmt_print(dc_debug_fmt fmt, FILE *stream, const char *format,...)
Definition fmt.h:32
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_scope_end(dc_debug_fmt fmt)
Definition fmt.h:57
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_scope_begin(dc_debug_fmt fmt)
Definition fmt.h:50
#define DC_PUBLIC
Definition namespace.h:25
#define NS(pre, post)
Definition namespace.h:14
#define DC_EXPAND_STRING(NAME)
Definition namespace.h:6
#define DC_ASSUME(expr,...)
Definition panic.h:57
#define DC_TRAIT_REFERENCABLE_BY_PTR(SELF)
Definition ref.h:19
FILE * stream
Definition template.h:16
char const * name
Definition template.h:15
ref alloc_ref
Definition template.h:49
Debug format helpers for debug printin data structures.
Definition fmt.h:11
static DC_PUBLIC FILE * stream(SELF *self)
Definition template.h:108