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...

Macros

#define INVARIANT_CHECK(self)

Functions

static ssize_t PRIV read (void *capture, char *buf, size_t size)
static ssize_t PRIV write (void *capture, const char *data, size_t size)
static int PRIV seek (void *capture, off_t *offset, int whence)
static int PRIV close (void *capture)
static DC_PUBLIC SELF new (ref alloc_ref)
static DC_PUBLIC FILE * stream (SELF *self)
static DC_PUBLIC void reset (SELF *self)
 Resets the string, but keps the same stream pointer alive.
static DC_PUBLIC char const * string (SELF const *self)
 Gets access to the null terminated string.
static DC_PUBLIC char * release_string (SELF *self)
static DC_PUBLIC size_t string_size (SELF *self)
static DC_PUBLIC void delete (SELF *self)

Variables

static size_t const additional_alloc_size = 32

Macro Definition Documentation

◆ INVARIANT_CHECK

#define INVARIANT_CHECK ( self)
Value:
DC_ASSUME(self); \
DC_ASSUME(DC_WHEN((self)->buf, (self)->stream && (self)->capacity > 0)); \
DC_ASSUME(DC_WHEN((self)->capacity == 0, !(self)->buf)); \
DC_ASSUME(DC_WHEN((self)->buf, (self)->size_without_null + 1 <= (self)->capacity));
#define DC_WHEN(cond, expr)
Definition panic.h:52
#define DC_ASSUME(expr,...)
Definition panic.h:57
static DC_PUBLIC FILE * stream(SELF *self)
Definition template.h:108

Definition at line 26 of file template.h.

26#define INVARIANT_CHECK(self) \
27 DC_ASSUME(self); \
28 DC_ASSUME(DC_WHEN((self)->buf, (self)->stream && (self)->capacity > 0)); \
29 DC_ASSUME(DC_WHEN((self)->capacity == 0, !(self)->buf)); \
30 DC_ASSUME(DC_WHEN((self)->buf, (self)->size_without_null + 1 <= (self)->capacity));

Function Documentation

◆ close()

int PRIV close ( void * capture)
static

Definition at line 92 of file template.h.

92 {
93 SELF* self = (SELF*)capture;
94 INVARIANT_CHECK(self);
95 return 0;
96}
#define INVARIANT_CHECK(self)
Definition template.h:90
#define SELF
Definition def.h:52

◆ delete()

DC_PUBLIC void delete ( SELF * self)
static

Definition at line 165 of file template.h.

165 {
166 INVARIANT_CHECK(self);
167 if (self->stream) {
168 fclose(self->stream);
169 }
170 if (self->buf) {
171 NS(ALLOC, deallocate)(self->alloc_ref, self->buf, self->capacity);
172 }
173}
static DC_PUBLIC void deallocate(SELF *self, void *ptr, size_t size)
Definition template.h:127
#define ALLOC
Definition template.h:31
#define NS(pre, post)
Definition namespace.h:14
size_t capacity
Definition template.h:72
FILE * stream
Definition template.h:16
char * buf
Definition template.h:18
ref alloc_ref
Definition template.h:49

◆ new()

DC_PUBLIC SELF new ( ref alloc_ref)
static

Definition at line 98 of file template.h.

98 {
99 return (SELF){
100 .stream = NULL,
101 .buf = NULL,
102 .size_without_null = 0,
103 .capacity = 0,
104 .alloc_ref = alloc_ref,
105 };
106}

◆ read()

ssize_t PRIV read ( void * capture,
char * buf,
size_t size )
static

Definition at line 32 of file template.h.

34 {
35 SELF* self = (SELF*)capture;
36 INVARIANT_CHECK(self);
37
38 (void)buf;
39 (void)size;
40
41 DC_PANIC("cookie set in write-only mode, but read was called.");
42}
static DC_PUBLIC size_t size(SELF const *self)
Definition template.h:252
#define DC_PANIC(str,...)
Definition panic.h:29

◆ release_string()

DC_PUBLIC char * release_string ( SELF * self)
static

Disowns the current string, free/management with chosen allocator determined by user. DANGER: The user needs to be careful to cleanup the string using the same allocator as the string builder.

Definition at line 150 of file template.h.

150 {
151 INVARIANT_CHECK(self);
152 char* buf = self->buf;
153 self->size_without_null = 0;
154 self->buf = NULL;
155 self->capacity = 0;
156
157 return buf;
158}
size_t size_without_null
Definition template.h:19

◆ reset()

DC_PUBLIC void reset ( SELF * self)
static

Resets the string, but keps the same stream pointer alive.

Definition at line 133 of file template.h.

133 {
134 INVARIANT_CHECK(self);
135 self->size_without_null = 0;
136}

◆ seek()

int PRIV seek ( void * capture,
off_t * offset,
int whence )
static

Definition at line 80 of file template.h.

82 {
83 SELF* self = (SELF*)capture;
84 INVARIANT_CHECK(self);
85 (void)offset;
86 (void)whence;
87
88 errno = EPERM; // NOLINT(misc-include-cleaner)
89 return -1;
90}

◆ stream()

DC_PUBLIC FILE * stream ( SELF * self)
static
Examples
complex/employees.c, container/arena.c, container/map.c, container/queue.c, container/set.c, utils/option.c, and utils/result.c.

Definition at line 108 of file template.h.

108 {
109 INVARIANT_CHECK(self);
110
111 if (self->stream == NULL) {
112 cookie_io_functions_t /* NOLINT(misc-include-cleaner) */ const io = {
113 .read = PRIV(NS(SELF, read)),
114 .write = PRIV(NS(SELF, write)),
115 .seek = PRIV(NS(SELF, seek)),
116 .close = PRIV(NS(SELF, close)),
117 };
118 self->stream = fopencookie(self, "w", io);
119 DC_ASSERT(self->stream != NULL, "Failed to open stream for string builder, with error: %s",
120 strerror(errno));
121
122 // JUSTIFY: No buffering
123 // - No performance advantage to buffering writes - this already writes to an in memory
124 // buffer
125 // - No need to fflush
126 setvbuf(self->stream, NULL, _IONBF, 0);
127 }
128
129 return self->stream;
130}
static DC_PUBLIC VALUE const * read(SELF const *self, INDEX index)
Definition template.h:199
static DC_PUBLIC VALUE * write(SELF *self, INDEX index)
Definition template.h:209
#define PRIV(name)
Definition namespace.h:20
#define DC_ASSERT(expr,...)
Definition panic.h:37
static int PRIV close(void *capture)
Definition template.h:92
static int PRIV seek(void *capture, off_t *offset, int whence)
Definition template.h:80

◆ string()

DC_PUBLIC char const * string ( SELF const * self)
static

Gets access to the null terminated string.

Definition at line 139 of file template.h.

139 {
140 INVARIANT_CHECK(self);
141 if (self->size_without_null == 0) {
142 return "";
143 }
144 return self->buf;
145}

◆ string_size()

DC_PUBLIC size_t string_size ( SELF * self)
static

Definition at line 160 of file template.h.

160 {
161 INVARIANT_CHECK(self);
162 return self->size_without_null;
163}

◆ write()

ssize_t PRIV write ( void * capture,
const char * data,
size_t size )
static

Definition at line 44 of file template.h.

44 {
45 SELF* self = (SELF*)capture;
46 INVARIANT_CHECK(self);
47
48 // JUSTIFY: Unpoisoning passed data to write
49 // - glibc is not instrumented for msan, so sometimes the entire buffer can be deivered as
50 // uninitialised.
51 // - Hence we consider fopenccokie's write as a 'tainted source', and enforce correct memory
52 // capabilities
54
55 if (self->size_without_null + size + 1 > self->capacity) {
56 size_t new_capacity;
57 char* new_buf;
58 if (self->buf != NULL) {
59 size_t const growth_factor = 2;
60 new_capacity =
61 (self->capacity * growth_factor) + size + NS(SELF, additional_alloc_size);
62 new_buf = (char*)NS(ALLOC, reallocate)(self->alloc_ref, self->buf, self->capacity,
63 new_capacity);
64 } else {
65 DC_ASSUME(self->capacity == 0);
66 new_capacity = size + 1 + NS(SELF, additional_alloc_size);
67 new_buf = (char*)NS(ALLOC, allocate_uninit)(self->alloc_ref, new_capacity);
68 }
69
70 self->capacity = new_capacity;
71 self->buf = new_buf;
72 }
73
74 memcpy(self->buf + self->size_without_null, data, size);
75 self->size_without_null += size;
76 self->buf[self->size_without_null] = '\0';
77 return (ssize_t)size;
78}
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
static DC_PUBLIC ITEM * data(SELF *self)
Definition template.h:284
static DC_PUBLIC void dc_memory_tracker_set(dc_memory_tracker_level level, dc_memory_tracker_capability cap, const volatile void *addr, size_t size)
@ DC_MEMORY_TRACKER_LVL_NONE
@ DC_MEMORY_TRACKER_CAP_READ_WRITE
static size_t const additional_alloc_size
Definition template.h:24

Variable Documentation

◆ additional_alloc_size

size_t const additional_alloc_size = 32
static

Definition at line 24 of file template.h.