GCC Code Coverage Report


Directory: src/
File: src/standalone/memory_sa.c
Date: 2026-11-18 16:12:57
Exec Total Coverage
Lines: 160 163 98.2%
Functions: 20 20 100.0%
Branches: 46 56 82.1%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2026 Tiger Data, Inc.
3 * Licensed under the PostgreSQL License. See LICENSE for details.
4 *
5 * memory_standalone.c - Arena allocator implementation
6 *
7 * Implements a memory context system compatible with PostgreSQL's semantics
8 * but without PostgreSQL dependencies. Used for standalone testing.
9 */
10
11 #include <assert.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 #include "standalone/memory_sa.h"
16
17 /* Thread-local current context */
18 _Thread_local VsMemCtx vs_current_memctx = NULL;
19
20 /* Align size up to alignment boundary */
21 static inline size_t
22 1118563 align_up(size_t size, size_t alignment)
23 {
24 1118563 return (size + alignment - 1) & ~(alignment - 1);
25 }
26
27 /* Allocate a new block */
28 static VsArenaBlock *
29 11588 arena_block_create(size_t min_size)
30 {
31 11588 size_t block_size = min_size > VS_ARENA_BLOCK_SIZE ? min_size
32 : VS_ARENA_BLOCK_SIZE;
33 /* Include space for header, aligned */
34 11588 size_t header_size = align_up(sizeof(VsArenaBlock), VS_ARENA_ALIGNMENT);
35 11588 size_t total_size = align_up(header_size + block_size, VS_ARENA_ALIGNMENT);
36
37 11588 VsArenaBlock *block = aligned_alloc(VS_ARENA_ALIGNMENT, total_size);
38
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11588 times.
11588 if (!block)
39 ✗ return NULL;
40
41 11588 block->next = NULL;
42 11588 block->size = block_size;
43 11588 block->used = 0;
44 11588 return block;
45 }
46
47 /* Free a block */
48 static void
49 11588 arena_block_free(VsArenaBlock *block)
50 {
51 11588 free(block);
52 11588 }
53
54 /* Get data pointer for block */
55 static inline void *
56 183575 arena_block_data(VsArenaBlock *block)
57 {
58 183575 size_t header_size = align_up(sizeof(VsArenaBlock), VS_ARENA_ALIGNMENT);
59 183575 return (char *)block + header_size;
60 }
61
62 /*
63 * Each allocation is prefixed with a size_t header storing the
64 * requested size. This lets vs_realloc copy min(old, new) bytes
65 * without reading past the old allocation.
66 */
67 #define ALLOC_HDR_SIZE align_up(sizeof(size_t), VS_ARENA_ALIGNMENT)
68
69 /* Allocate from arena with specified alignment */
70 static void *
71 178087 arena_alloc(VsArena *arena, size_t size, size_t alignment)
72 {
73
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 178085 times.
178087 if (size == 0)
74 2 return NULL;
75
76 178085 size_t aligned_size = align_up(size, alignment);
77 178085 size_t need = ALLOC_HDR_SIZE + aligned_size;
78
79 /* Try current block first */
80
2/2
✓ Branch 0 taken 171987 times.
✓ Branch 1 taken 6098 times.
178085 if (arena->current)
81 {
82 171987 void *data = arena_block_data(arena->current);
83 171987 uintptr_t base = (uintptr_t)data + arena->current->used;
84
85 /* Align the user pointer; header sits just before it */
86 171987 uintptr_t user_ptr = align_up(base + ALLOC_HDR_SIZE, alignment);
87 171987 uintptr_t hdr_ptr = user_ptr - ALLOC_HDR_SIZE;
88 171987 size_t end = (hdr_ptr - (uintptr_t)data) + need;
89
90
2/2
✓ Branch 0 taken 166497 times.
✓ Branch 1 taken 5490 times.
171987 if (end <= arena->current->size)
91 {
92 166497 *(size_t *)hdr_ptr = size;
93 166497 arena->current->used = end;
94 166497 arena->total_allocated += need;
95 166497 return (void *)user_ptr;
96 }
97 }
98
99 /*
100 * Need a new block. Request extra space for alignment padding.
101 * Worst case: we need (alignment - 1) extra bytes to align.
102 */
103 11588 size_t extra_for_alignment = alignment > VS_ARENA_ALIGNMENT
104 ? alignment - VS_ARENA_ALIGNMENT
105 11588 : 0;
106 11588 VsArenaBlock *block = arena_block_create(need + extra_for_alignment);
107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11588 times.
11588 if (!block)
108 ✗ return NULL;
109
110 /* Link new block */
111 11588 block->next = arena->blocks;
112 11588 arena->blocks = block;
113 11588 arena->current = block;
114
115 /* Allocate from new block — align the user pointer */
116 11588 void *data = arena_block_data(block);
117 11588 uintptr_t base = (uintptr_t)data;
118 11588 uintptr_t user_ptr = align_up(base + ALLOC_HDR_SIZE, alignment);
119 11588 uintptr_t hdr_ptr = user_ptr - ALLOC_HDR_SIZE;
120 11588 size_t end = (hdr_ptr - base) + need;
121
122 11588 *(size_t *)hdr_ptr = size;
123 11588 block->used = end;
124 11588 arena->total_allocated += need;
125 11588 return (void *)user_ptr;
126 }
127
128 /* Create a new arena context */
129 VsMemCtx
130 6834 vs_memctx_create(VsMemCtx parent, const char *name)
131 {
132 /* Allocate arena struct from parent or malloc */
133 VsArena *arena;
134
2/2
✓ Branch 0 taken 2206 times.
✓ Branch 1 taken 4628 times.
6834 if (parent)
135 2206 arena = arena_alloc(parent, sizeof(VsArena), VS_ARENA_ALIGNMENT);
136 else
137 4628 arena = aligned_alloc(
138 VS_ARENA_ALIGNMENT,
139 align_up(sizeof(VsArena), VS_ARENA_ALIGNMENT));
140
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6834 times.
6834 if (!arena)
142 ✗ return NULL;
143
144 6834 memset(arena, 0, sizeof(VsArena));
145 6834 arena->name = name;
146 6834 arena->parent = parent;
147 6834 arena->block_size = VS_ARENA_BLOCK_SIZE;
148
149 /* Link into parent's child list */
150
2/2
✓ Branch 0 taken 2206 times.
✓ Branch 1 taken 4628 times.
6834 if (parent)
151 {
152 2206 arena->next_sibling = parent->first_child;
153 2206 parent->first_child = arena;
154 }
155
156 6834 return arena;
157 }
158
159 /* Invoke all reset callbacks for an arena */
160 static void
161 140982 invoke_reset_callbacks(VsArena *arena)
162 {
163 140982 VsMemCtxCallback *cb = arena->reset_callbacks;
164
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 140982 times.
140988 while (cb)
165 {
166 6 cb->func(cb->arg);
167 6 cb = cb->next;
168 }
169 140982 }
170
171 /* Delete arena and all children */
172 void
173 6836 vs_memctx_delete(VsMemCtx ctx)
174 {
175
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6834 times.
6836 if (!ctx)
176 2 return;
177
178 6834 VsArena *arena = ctx;
179
180 /* Invoke callbacks before destroying anything */
181 6834 invoke_reset_callbacks(arena);
182
183 /* Recursively delete children first */
184 6834 VsArena *child = arena->first_child;
185
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 6834 times.
6892 while (child)
186 {
187 58 VsArena *next = child->next_sibling;
188 58 vs_memctx_delete(child);
189 58 child = next;
190 }
191
192 /* Unlink from parent */
193
2/2
✓ Branch 0 taken 2206 times.
✓ Branch 1 taken 4628 times.
6834 if (arena->parent)
194 {
195 2206 VsArena **pp = &arena->parent->first_child;
196
3/4
✓ Branch 0 taken 2216 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 2206 times.
2216 while (*pp && *pp != arena)
197 10 pp = &(*pp)->next_sibling;
198
1/2
✓ Branch 0 taken 2206 times.
✗ Branch 1 not taken.
2206 if (*pp)
199 2206 *pp = arena->next_sibling;
200 }
201
202 /* Free all blocks */
203 6834 VsArenaBlock *block = arena->blocks;
204
2/2
✓ Branch 0 taken 11510 times.
✓ Branch 1 taken 6834 times.
18344 while (block)
205 {
206 11510 VsArenaBlock *next = block->next;
207 11510 arena_block_free(block);
208 11510 block = next;
209 }
210
211 /* Free arena struct if it was top-level (no parent) */
212
2/2
✓ Branch 0 taken 4628 times.
✓ Branch 1 taken 2206 times.
6834 if (!arena->parent)
213 4628 free(arena);
214 }
215
216 /* Reset arena - free all allocations but keep arena */
217 void
218 134150 vs_memctx_reset(VsMemCtx ctx)
219 {
220
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 134148 times.
134150 if (!ctx)
221 2 return;
222
223 134148 VsArena *arena = ctx;
224
225 /* Invoke callbacks before destroying anything */
226 134148 invoke_reset_callbacks(arena);
227
228 /* Recursively delete children */
229 134148 VsArena *child = arena->first_child;
230
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 134148 times.
134152 while (child)
231 {
232 4 VsArena *next = child->next_sibling;
233 4 vs_memctx_delete(child);
234 4 child = next;
235 }
236 134148 arena->first_child = NULL;
237 134148 arena->reset_callbacks = NULL; /* Callbacks were in arena memory */
238
239 /* Free all blocks except first (if any) */
240
2/2
✓ Branch 0 taken 1670 times.
✓ Branch 1 taken 132478 times.
134148 if (arena->blocks)
241 {
242 1670 VsArenaBlock *keep = arena->blocks;
243 1670 VsArenaBlock *block = keep->next;
244
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 1670 times.
1748 while (block)
245 {
246 78 VsArenaBlock *next = block->next;
247 78 arena_block_free(block);
248 78 block = next;
249 }
250 1670 keep->next = NULL;
251 1670 keep->used = 0;
252 1670 arena->blocks = keep;
253 1670 arena->current = keep;
254 }
255 else
256 {
257 132478 arena->current = NULL;
258 }
259 134148 arena->total_allocated = 0;
260 }
261
262 /* Allocate from current context */
263 void *
264 91749 vs_alloc(size_t size)
265 {
266
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 91749 times.
91749 assert(vs_current_memctx != NULL);
267 91749 return arena_alloc(vs_current_memctx, size, VS_ARENA_ALIGNMENT);
268 }
269
270 void *
271 14382 vs_alloc0(size_t size)
272 {
273 14382 void *ptr = vs_alloc(size);
274
1/2
✓ Branch 0 taken 14382 times.
✗ Branch 1 not taken.
14382 if (ptr)
275 14382 memset(ptr, 0, size);
276 14382 return ptr;
277 }
278
279 void *
280 5334 vs_memctx_alloc(VsMemCtx ctx, size_t size)
281 {
282 5334 return arena_alloc(ctx, size, VS_ARENA_ALIGNMENT);
283 }
284
285 void *
286 2 vs_memctx_alloc0(VsMemCtx ctx, size_t size)
287 {
288 2 void *ptr = vs_memctx_alloc(ctx, size);
289
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (ptr)
290 2 memset(ptr, 0, size);
291 2 return ptr;
292 }
293
294 void *
295 903 vs_realloc(void *ptr, size_t size)
296 {
297
2/2
✓ Branch 0 taken 612 times.
✓ Branch 1 taken 291 times.
903 if (!ptr)
298 612 return vs_alloc(size);
299
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 289 times.
291 if (size == 0)
300 2 return NULL;
301
302 289 size_t old_size = *(size_t *)((char *)ptr - ALLOC_HDR_SIZE);
303 289 void *new_ptr = vs_alloc(size);
304 289 memcpy(new_ptr, ptr, old_size < size ? old_size : size);
305 289 return new_ptr;
306 }
307
308 /* Free is a no-op in arena mode */
309 void
310 39182 vs_free(void *ptr)
311 {
312 (void)ptr; /* Memory freed on context reset/delete */
313 39182 }
314
315 /* Aligned allocation from current context */
316 void *
317 78798 vs_alloc_aligned(size_t size, size_t alignment)
318 {
319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78798 times.
78798 assert(vs_current_memctx != NULL);
320 78798 return arena_alloc(vs_current_memctx, size, alignment);
321 }
322
323 void
324 76984 vs_free_aligned(void *ptr)
325 {
326 (void)ptr; /* No-op in arena mode */
327 76984 }
328
329 /* Switch context, return old context */
330 VsMemCtx
331 32188 vs_memctx_switch(VsMemCtx ctx)
332 {
333 32188 VsMemCtx old = vs_current_memctx;
334 32188 vs_current_memctx = ctx;
335 32188 return old;
336 }
337
338 /* Get total bytes allocated in context */
339 size_t
340 20 vs_memctx_total_allocated(VsMemCtx ctx)
341 {
342
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 return ctx ? ctx->total_allocated : 0;
343 }
344
345 /* Register a callback to be called on context reset or delete */
346 void
347 6 vs_memctx_register_reset_callback(
348 VsMemCtx ctx, VsMemCtxCallback *cb, void (*func)(void *), void *arg)
349 {
350 6 cb->func = func;
351 6 cb->arg = arg;
352 6 cb->next = ctx->reset_callbacks;
353 6 ctx->reset_callbacks = cb;
354 6 }
355