Cthulhu  0.2.10
Cthulhu compiler collection
arena.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-3.0-only
2 
3 #pragma once
4 
5 #include <ctu_arena_api.h>
6 #include <ctu_config.h>
7 
8 #include "core/analyze.h"
9 #include "core/compiler.h"
10 
11 #include <stdbool.h>
12 #include <stddef.h>
13 #include <stdint.h>
14 
16 
24 
28 
29 #ifdef WITH_DOXYGEN
30 # define CTU_TRACE_MEMORY 0
31 #endif
32 
37 #define CT_ALLOC_SIZE_UNKNOWN SIZE_MAX
38 
40 typedef struct arena_t arena_t;
41 
50 typedef void *(*mem_alloc_t)(size_t size, void *user);
51 
61 typedef void *(*mem_resize_t)(void *ptr, size_t new_size, size_t old_size, void *user);
62 
68 typedef void (*mem_release_t)(void *ptr, size_t size, void *user);
69 
75 typedef void (*mem_rename_t)(const void *ptr, const char *name, void *user);
76 
82 typedef void (*mem_reparent_t)(const void *ptr, const void *parent, void *user);
83 
85 typedef struct arena_t
86 {
88  const char *name;
89 
92 
95 
98 
102 
106 
108  void *user;
109 } arena_t;
110 
119 CT_ARENA_API void arena_free(
120  STA_RELEASE void *ptr,
121  IN_DOMAIN(>, 0) size_t size,
122  IN_NOTNULL arena_t *arena);
123 
134 CT_ARENA_API void *arena_malloc(
135  IN_DOMAIN(>, 0) size_t size,
136  IN_NOTNULL arena_t *arena);
137 
151 CT_ARENA_API void *arena_malloc_info(
152  IN_DOMAIN(>, 0) size_t size,
153  const char *name,
154  const void *parent,
155  IN_NOTNULL arena_t *arena);
156 
171 CT_ARENA_API void *arena_realloc(
172  STA_RELEASE void *ptr,
173  IN_DOMAIN(>, 0) size_t new_size,
174  IN_DOMAIN(>, 0) size_t old_size,
175  IN_NOTNULL arena_t *arena);
176 
187 CT_ARENA_API char *arena_strdup(
188  IN_STRING const char *str,
189  IN_NOTNULL arena_t *arena);
190 
202 CT_ARENA_API char *arena_strndup(
203  STA_READS(len) const char *str,
204  size_t len,
205  IN_NOTNULL arena_t *arena);
206 
220 CT_ARENA_API void *arena_memdup(
221  STA_READS(size) const void *ptr,
222  IN_DOMAIN(>, 0) size_t size,
223  IN_NOTNULL arena_t *arena);
224 
231 
238 CT_ARENA_API void arena_opt_free(
239  STA_RELEASE void *ptr,
240  IN_DOMAIN(>, 0) size_t size,
241  IN_NOTNULL arena_t *arena);
242 
251 CT_ARENA_API void *arena_opt_malloc(
252  IN_DOMAIN(>, 0) size_t size,
253  IN_NOTNULL arena_t *arena);
254 
266 CT_ARENA_API void *arena_opt_malloc_info(
267  IN_DOMAIN(>, 0) size_t size,
268  const char *name,
269  const void *parent,
270  IN_NOTNULL arena_t *arena);
271 
282 CT_ARENA_API void *arena_opt_realloc(
283  STA_RELEASE void *ptr,
284  IN_DOMAIN(>, 0) size_t new_size,
285  IN_DOMAIN(>, 0) size_t old_size,
286  IN_NOTNULL arena_t *arena);
287 
297 CT_ARENA_API char *arena_opt_strdup(
298  IN_STRING const char *str,
299  IN_NOTNULL arena_t *arena);
300 
311 CT_ARENA_API char *arena_opt_strndup(
312  STA_READS(len) const char *str,
313  IN_DOMAIN(>, 0) size_t len,
314  IN_NOTNULL arena_t *arena);
315 
327 CT_ARENA_API void *arena_opt_memdup(
328  STA_READS(size) const void *ptr,
329  IN_DOMAIN(>, 0) size_t size,
330  IN_NOTNULL arena_t *arena);
331 
333 
340 CT_ARENA_API void *arena_data(IN_NOTNULL arena_t *arena);
341 
347 CT_ARENA_API void arena_rename(IN_NOTNULL const void *ptr, IN_STRING const char *name, IN_NOTNULL arena_t *arena);
348 
354 CT_ARENA_API void arena_reparent(IN_NOTNULL const void *ptr, const void *parent, IN_NOTNULL arena_t *arena);
355 
363 
371 
382 
383 // cast to (const void*) to support using these in C++
384 #if CTU_TRACE_MEMORY
385 # define ARENA_RENAME(ptr, name, arena) arena_rename((const void*)(ptr), name, arena)
386 # define ARENA_REPARENT(ptr, parent, arena) arena_reparent((const void*)(ptr), (const void*)(parent), arena)
387 # define ARENA_MALLOC(size, name, parent, arena) arena_malloc_info(size, name, (const void*)(parent), arena)
388 # define ARENA_OPT_MALLOC(size, name, parent, arena) arena_opt_malloc_info(size, name, (const void*)(parent), arena)
389 #else
390 # define ARENA_RENAME(arena, ptr, name)
391 # define ARENA_REPARENT(arena, ptr, parent)
392 # define ARENA_MALLOC(size, name, parent, arena) arena_malloc(size, arena)
393 # define ARENA_OPT_MALLOC(size, name, parent, arena) arena_opt_malloc(size, arena)
394 #endif
395 
408 
409 #define ARENA_IDENTIFY(ptr, name, parent, arena) \
410  do \
411  { \
412  ARENA_RENAME(ptr, name, arena); \
413  ARENA_REPARENT(ptr, parent, arena); \
414  } while (0)
415 
417 
CT_NODISCARD size_t size
Definition: scan.h:128
#define RET_NOTNULL
annotate the return value as not being null
#define CT_ALLOC(...)
mark a function as allocating memory
Definition: analyze.h:244
#define STA_RELEASE
annotate a pointer as invalid after the function returns
#define CT_NODISCARD
mark a function as returning a value that must be used
#define IN_NOTNULL
annotate a parameter as not being null
#define CT_ALLOC_SIZE(...)
mark a function as allocating memory with a specific size
Definition: analyze.h:247
#define STA_READS(size)
annotate a parameter as reading expr elements
#define IN_DOMAIN(cmp, it)
annotate a parameter as being bounded by the expression of cmp and it
#define IN_STRING
annotate a parameter as being a null terminated string
#define CT_BEGIN_API
Definition: compiler.h:129
#define CT_END_API
Definition: compiler.h:130
CT_NODISCARD CT_ARENA_API void * arena_opt_malloc_info(size_t size, const char *name, const void *parent, arena_t *arena)
allocate memory from a custom allocator
Definition: arena.c:56
CT_NODISCARD CT_ARENA_API void * arena_opt_malloc(size_t size, arena_t *arena)
allocate memory from a custom allocator
Definition: arena.c:47
CT_NODISCARD CT_ARENA_API void * arena_opt_realloc(STA_RELEASE void *ptr, size_t new_size, size_t old_size, arena_t *arena)
resize a memory allocation from a custom allocator
CT_NODISCARD CT_ARENA_API char * arena_opt_strdup(const char *str, arena_t *arena)
allocate a copy of a string from a custom allocator
Definition: arena.c:11
CT_NODISCARD CT_ARENA_API void * arena_opt_memdup(STA_READS(size) const void *ptr, size_t size, arena_t *arena)
duplicate a memory region from a custom allocator duplicate a region of memory and return a pointer t...
CT_NODISCARD CT_ARENA_API char * arena_opt_strndup(STA_READS(len) const char *str, size_t len, arena_t *arena)
allocate a copy of a string with a maximum length from a custom allocator
CT_ARENA_API void arena_opt_free(STA_RELEASE void *ptr, size_t size, arena_t *arena)
release memory from a custom allocator
void(* mem_reparent_t)(const void *ptr, const void *parent, void *user)
arena reparent callback
Definition: arena.h:82
CT_NODISCARD CT_ARENA_API void * arena_malloc_info(size_t size, const char *name, const void *parent, arena_t *arena)
allocate memory from a custom allocator
Definition: arena.c:129
CT_NODISCARD CT_ARENA_API void * arena_memdup(STA_READS(size) const void *ptr, size_t size, arena_t *arena)
duplicate a memory region from a custom allocator duplicate a region of memory and return a pointer t...
CT_NODISCARD CT_ARENA_API void * arena_data(arena_t *arena)
get the user data pointer from an arena
Definition: arena.c:186
CT_NODISCARD CT_ARENA_API void * arena_malloc(size_t size, arena_t *arena)
allocate memory from a custom allocator
Definition: arena.c:119
CT_ARENA_API void arena_reparent(const void *ptr, const void *parent, arena_t *arena)
reparent a pointer in a custom allocator
Definition: arena.c:173
CT_NODISCARD CT_ARENA_API char * arena_strdup(const char *str, arena_t *arena)
allocate a copy of a string from a custom allocator
Definition: arena.c:95
void *(* mem_alloc_t)(size_t size, void *user)
arena malloc callback
Definition: arena.h:50
CT_ARENA_API void arena_rename(const void *ptr, const char *name, arena_t *arena)
rename a pointer in a custom allocator
Definition: arena.c:160
CT_NODISCARD CT_ARENA_API char * arena_strndup(STA_READS(len) const char *str, size_t len, arena_t *arena)
allocate a copy of a string with a maximum length from a custom allocator
void(* mem_rename_t)(const void *ptr, const char *name, void *user)
arena rename callback
Definition: arena.h:75
CT_NODISCARD CT_ARENA_API void * arena_realloc(STA_RELEASE void *ptr, size_t new_size, size_t old_size, arena_t *arena)
resize a memory allocation from a custom allocator
typedefCT_BEGIN_API struct arena_t arena_t
a memory allocator
Definition: config.h:16
CT_ARENA_API void arena_free(STA_RELEASE void *ptr, size_t size, arena_t *arena)
release memory from a custom allocator
void(* mem_release_t)(void *ptr, size_t size, void *user)
arena free callback
Definition: arena.h:68
void *(* mem_resize_t)(void *ptr, size_t new_size, size_t old_size, void *user)
arena realloc callback
Definition: arena.h:61
an allocator object
Definition: arena.h:86
mem_resize_t fn_realloc
the realloc function
Definition: arena.h:94
const char * name
the name of the allocator
Definition: arena.h:88
mem_release_t fn_free
the free function
Definition: arena.h:97
void * user
user data pointer
Definition: arena.h:108
mem_reparent_t fn_reparent
the reparent function
Definition: arena.h:105
mem_rename_t fn_rename
the rename function
Definition: arena.h:101
mem_alloc_t fn_malloc
the malloc function
Definition: arena.h:91