cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
alloc.h
Go to the documentation of this file.
1
6#ifndef __CWIST_CORE_MEM_ALLOC_H__
7#define __CWIST_CORE_MEM_ALLOC_H__
8
9#include <stddef.h>
10#include <ttak/mem/owner.h>
12
16ttak_owner_t *cwist_create_owner(void);
17
21void *cwist_malloc(size_t size);
22
26void *cwist_alloc(size_t size);
27
31void *cwist_alloc_array(size_t count, size_t elem_size);
32
36void *cwist_realloc(void *ptr, size_t new_size);
37
41char *cwist_strdup(const char *src);
42
46char *cwist_strndup(const char *src, size_t n);
47
51void cwist_free(void *ptr);
52
61#if defined(__GNUC__) || defined(__clang__)
62#define CWIST_ATTRIBUTE_CLEANUP(func) __attribute__((cleanup(func)))
63#else
64#define CWIST_ATTRIBUTE_CLEANUP(func)
65#endif
66
73static inline void cwist_defer_free_cb(void *pp) {
74 void **p = (void **)pp;
75 if (*p) {
76 cwist_free(*p);
77 }
78}
79
101#define CWIST_DEFER_FREE CWIST_ATTRIBUTE_CLEANUP(cwist_defer_free_cb)
102
116#define cwist_alloc_scoped(var, cast, size) \
117 void *_cwist_scoped_##var CWIST_DEFER_FREE = cwist_alloc(size); \
118 cast var = (cast)_cwist_scoped_##var
119
131typedef struct {
133 void *ptr;
134 int heap; /* 1 if ptr came from cwist_alloc() instead of the arena */
136
145static inline void *cwist_scratch_alloc(cwist_scratch_t *s, size_t size) {
147 if (s->arena) {
148 s->ptr = cwist_arena_alloc(s->arena, size);
149 if (s->ptr) {
150 s->heap = 0;
151 return s->ptr;
152 }
153 }
154 s->ptr = cwist_alloc(size);
155 s->heap = 1;
156 return s->ptr;
157}
158
163static inline void cwist_scratch_cleanup(void *sp) {
165 if (s->heap && s->ptr) {
166 cwist_free(s->ptr);
167 }
168 if (s->arena) {
170 }
171}
172
190#define CWIST_SCRATCH_DEFER CWIST_ATTRIBUTE_CLEANUP(cwist_scratch_cleanup)
191
192#endif
void cwist_free(void *ptr)
Free memory obtained via cwist_alloc/cwist_strdup/etc.
Definition alloc.c:52
void * cwist_realloc(void *ptr, size_t new_size)
Resize an existing allocation (libttak tracked).
Definition alloc.c:27
char * cwist_strdup(const char *src)
Duplicate a string using libttak-backed storage.
Definition alloc.c:32
void * cwist_malloc(size_t size)
Allocate zeroed memory via the CWIST owner context.
Definition alloc.c:13
void * cwist_alloc_array(size_t count, size_t elem_size)
Allocate zeroed memory for count elements.
Definition alloc.c:21
ttak_owner_t * cwist_create_owner(void)
Lazily create (or return) the shared CWIST owner context.
char * cwist_strndup(const char *src, size_t n)
Duplicate up to n bytes of a string (libttak-backed).
Definition alloc.c:41
void * cwist_alloc(size_t size)
Allocate zeroed memory tracked by libttak.
Definition alloc.c:17
Request-scoped arena allocator backed by libttak arena generations.
struct cwist_arena cwist_arena_t
Definition arena.h:22
void * cwist_arena_alloc(cwist_arena_t *arena, size_t size)
Bump-allocate size bytes from the active generation.
Definition arena.c:44
void cwist_arena_destroy(cwist_arena_t *arena)
Retire the generation and destroy the arena, releasing every allocation made from it at once.
Definition arena.c:60
cwist_arena_t * cwist_arena_create(size_t generation_bytes)
Create an arena backed by a recycled thread-local generation buffer.
Definition arena.c:32
size_t count
Definition otel.c:58
Backing state for cwist_scratch_alloc()/CWIST_SCRATCH_DEFER: a single block-scoped allocation that pr...
Definition alloc.h:131
void * ptr
Definition alloc.h:133
cwist_arena_t * arena
Definition alloc.h:132