Derive-C
Loading...
Searching...
No Matches
template.h File Reference

Go to the source code of this file.

Data Structures

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

Functions

static DC_PUBLIC SELF new (char const *name, FILE *stream, ref alloc_ref)
static DC_PUBLIC void * allocate_uninit (SELF *self, size_t size)
static DC_PUBLIC void * allocate_zeroed (SELF *self, size_t size)
static DC_PUBLIC void * reallocate (SELF *self, void *ptr, size_t old_size, size_t new_size)
static DC_PUBLIC void deallocate (SELF *self, void *ptr, size_t size)
static DC_PUBLIC void debug (SELF const *self, dc_debug_fmt fmt, FILE *stream)
static DC_PUBLIC void delete (SELF *self)
 DC_TRAIT_REFERENCABLE_BY_PTR (SELF)
 DC_TRAIT_ALLOC (SELF)

Function Documentation

◆ allocate_uninit()

DC_PUBLIC void * allocate_uninit ( SELF * self,
size_t size )
static

Definition at line 30 of file template.h.

30 {
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}
static DC_PUBLIC void * allocate_uninit(SELF *self, size_t size)
Definition template.h:92
#define ALLOC
Definition template.h:31
static DC_PUBLIC size_t size(SELF const *self)
Definition template.h:252
#define NS(pre, post)
Definition namespace.h:14
#define DC_ASSUME(expr,...)
Definition panic.h:57
FILE * stream
Definition template.h:16
char const * name
Definition template.h:15
ref alloc_ref
Definition template.h:49

◆ allocate_zeroed()

DC_PUBLIC void * allocate_zeroed ( SELF * self,
size_t size )
static

Definition at line 37 of file template.h.

37 {
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}
static DC_PUBLIC void * allocate_zeroed(SELF *self, size_t size)
Definition template.h:117

◆ DC_TRAIT_ALLOC()

DC_TRAIT_ALLOC ( SELF )

◆ DC_TRAIT_REFERENCABLE_BY_PTR()

DC_TRAIT_REFERENCABLE_BY_PTR ( SELF )

◆ deallocate()

DC_PUBLIC void deallocate ( SELF * self,
void * ptr,
size_t size )
static

Definition at line 53 of file template.h.

53 {
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}
static DC_PUBLIC void deallocate(SELF *self, void *ptr, size_t size)
Definition template.h:127

◆ debug()

DC_PUBLIC void debug ( SELF const * self,
dc_debug_fmt fmt,
FILE * stream )
static

Definition at line 59 of file template.h.

59 {
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}
static DC_PUBLIC void debug(SELF const *self, dc_debug_fmt fmt, FILE *stream)
Definition template.h:212
#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_EXPAND_STRING(NAME)
Definition namespace.h:6
static DC_PUBLIC FILE * stream(SELF *self)
Definition template.h:108

◆ delete()

DC_PUBLIC void delete ( SELF * self)
static

Definition at line 70 of file template.h.

70 {
71 DC_ASSUME(self);
72 fprintf(self->stream, "[%s]: %s\n", self->name, __func__);
73}

◆ new()

DC_PUBLIC SELF new ( char const * name,
FILE * stream,
ref alloc_ref )
static

Definition at line 20 of file template.h.

20 {
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}

◆ reallocate()

DC_PUBLIC void * reallocate ( SELF * self,
void * ptr,
size_t old_size,
size_t new_size )
static

Definition at line 44 of file template.h.

45 {
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}
static DC_PUBLIC void * reallocate(SELF *self, void *ptr, size_t old_size, size_t new_size)
Definition template.h:137