Derive-C
Loading...
Searching...
No Matches
template.h
Go to the documentation of this file.
1
9
11#if !defined(SKIP_INCLUDES)
12 #include "includes.h"
13#endif
14
18
19typedef struct {
20 NS(ALLOC, ref) alloc_ref;
21} SELF;
22
23DC_PUBLIC static SELF NS(SELF, new)(NS(ALLOC, ref) alloc_ref) {
24 return (SELF){.alloc_ref = alloc_ref};
25}
26
27DC_MOCKABLE(void*, NS(SELF, allocate_uninit), (SELF * self, size_t size)) {
28 return NS(ALLOC, allocate_uninit)(self->alloc_ref, size);
29}
30
31DC_MOCKABLE(void*, NS(SELF, allocate_zeroed), (SELF * self, size_t size)) {
32 return NS(ALLOC, allocate_zeroed)(self->alloc_ref, size);
33}
34
36 (SELF * self, void* ptr, size_t old_size, size_t new_size)) {
37 return NS(ALLOC, reallocate)(self->alloc_ref, ptr, old_size, new_size);
38}
39
40DC_MOCKABLE(void, NS(SELF, deallocate), (SELF * self, void* ptr, size_t size)) {
41 NS(ALLOC, deallocate)(self->alloc_ref, ptr, size);
42}
43
44DC_PUBLIC static void NS(SELF, debug)(SELF const* self, dc_debug_fmt fmt, FILE* stream) {
45 fprintf(stream, DC_EXPAND_STRING(SELF) "@%p {\n", (void*)self);
46 fmt = dc_debug_fmt_scope_begin(fmt);
47 dc_debug_fmt_print(fmt, stream, "alloc: ");
48 NS(ALLOC, debug)(NS(NS(ALLOC, ref), deref)(self->alloc_ref), fmt, stream);
49 fprintf(stream, ",\n");
50
51 dc_debug_fmt_print(fmt, stream, "mocking: {\n");
52 fmt = dc_debug_fmt_scope_begin(fmt);
53
54#define DEBUG_MOCK_TOGGLE(name) \
55 dc_debug_fmt_print(fmt, stream, DC_EXPAND_STRING(NS(SELF, name)) ": %s,\n", \
56 DC_MOCKABLE_ENABLED(NS(SELF, name)) ? "enabled" : "disabled")
57
62
63#undef DEBUG_MOCK_TOGGLE
64
65 fmt = dc_debug_fmt_scope_end(fmt);
66 dc_debug_fmt_print(fmt, stream, "}\n");
67
68 fmt = dc_debug_fmt_scope_end(fmt);
69 dc_debug_fmt_print(fmt, stream, "}");
70}
71
72DC_PUBLIC static void NS(SELF, delete)(SELF* self) { DC_ASSUME(self); }
73
75
77
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
#define DEBUG_MOCK_TOGGLE(name)
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_MOCKABLE(ret, name, args)
Defines a function as mockable, combining declaration & definition.
Definition mock.h:44
#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
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