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 SELF new (ALLOC *alloc)
static FILE * stream (SELF *self)
 Opens a file for.
static void reset (SELF *self)
 Resets the string, but keps the same stream pointer alive.
static char const * string (SELF const *self)
 Gets access to the null terminated string.
static char * release_string (SELF *self)
 Disowns the current string, free/management with chosen allocator determined by user.
static size_t string_size (SELF *self)
static 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((self)->alloc); \
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));
static size_t capacity()
Definition template.h:66
#define DC_WHEN(cond, expr)
Definition panic.h:51
#define DC_ASSUME(expr,...)
Definition panic.h:56
static FILE * stream(SELF *self)
Opens a file for.
Definition template.h:107

Definition at line 26 of file template.h.

26#define INVARIANT_CHECK(self) \
27 DC_ASSUME(self); \
28 DC_ASSUME((self)->alloc); \
29 DC_ASSUME(DC_WHEN((self)->buf, (self)->stream && (self)->capacity > 0)); \
30 DC_ASSUME(DC_WHEN((self)->capacity == 0, !(self)->buf)); \
31 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 90 of file template.h.

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

◆ delete()

void delete ( SELF * self)
static

Definition at line 162 of file template.h.

162 {
163 INVARIANT_CHECK(self);
164 if (self->stream) {
165 fclose(self->stream);
166 }
167 if (self->buf) {
168 NS(ALLOC, free)(self->alloc, self->buf);
169 }
170}
static void free(SELF *self, void *ptr)
Definition template.h:56
#define ALLOC
Definition template.h:64
#define NS(pre, post)
Definition namespace.h:4
char * buf
Definition template.h:18
FILE * stream
Definition template.h:17
ALLOC * alloc
Definition template.h:71

◆ new()

SELF new ( ALLOC * alloc)
static

Definition at line 96 of file template.h.

96 {
97 return (SELF){
98 .stream = NULL,
99 .buf = NULL,
100 .size_without_null = 0,
101 .capacity = 0,
102 .alloc = alloc,
103 };
104}

◆ read()

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

Definition at line 33 of file template.h.

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

◆ release_string()

char * release_string ( SELF * self)
static

Disowns the current string, free/management with chosen allocator determined by user.

Definition at line 147 of file template.h.

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

◆ reset()

void reset ( SELF * self)
static

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

Definition at line 132 of file template.h.

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

◆ seek()

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

Definition at line 78 of file template.h.

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

◆ stream()

FILE * stream ( SELF * self)
static

Opens a file for.

Examples
complex/employees.c, container/arena/basic.c, container/map/decomposed.c, container/vector/dynamic.c, and utils/option.c.

Definition at line 107 of file template.h.

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

◆ string()

char const * string ( SELF const * self)
static

Gets access to the null terminated string.

Definition at line 138 of file template.h.

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

◆ string_size()

size_t string_size ( SELF * self)
static

Definition at line 157 of file template.h.

157 {
158 INVARIANT_CHECK(self);
159 return self->size_without_null;
160}

◆ write()

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

Definition at line 45 of file template.h.

45 {
46 SELF* self = (SELF*)capture;
47 INVARIANT_CHECK(self);
48
49 if (self->size_without_null + size + 1 > self->capacity) {
50 size_t new_capacity;
51 char* new_buf;
52 if (self->buf != NULL) {
53 size_t const growth_factor = 2;
54 new_capacity =
55 (self->capacity * growth_factor) + size + NS(SELF, additional_alloc_size);
56 new_buf = (char*)NS(ALLOC, realloc)(self->alloc, self->buf, new_capacity);
57 } else {
58 DC_ASSUME(self->capacity == 0);
59 new_capacity = size + 1 + NS(SELF, additional_alloc_size);
60 new_buf = (char*)NS(ALLOC, malloc)(self->alloc, new_capacity);
61 }
62
63 if (!new_buf) {
64 errno = ENOMEM; // NOLINT(misc-include-cleaner)
65 return -1;
66 }
67
68 self->capacity = new_capacity;
69 self->buf = new_buf;
70 }
71
72 memcpy(self->buf + self->size_without_null, data, size);
73 self->size_without_null += size;
74 self->buf[self->size_without_null] = '\0';
75 return (ssize_t)size;
76}
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 ITEM * data(SELF *self)
Definition template.h:261
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.