|
Csilk 0.2.1
A lightweight, high-performance C HTTP web framework
|
Arena (bump) allocator for request-scoped memory management. More...
#include <stdint.h>#include <stdlib.h>#include <string.h>#include "context_internal.h"#include "csilk/core/internal.h"
Data Structures | |
| struct | csilk_arena_chunk_t |
| A single chunk in the arena linked list. More... | |
| struct | csilk_arena_t |
| Arena allocator for request-scoped memory. More... | |
Macros | |
| #define | CSILK_CACHE_LINE_SIZE 64 |
| Cache line size (typically 64 bytes on modern CPUs). Used for padding structures to prevent false sharing and improve memory alignment. | |
Functions | |
| static void * | arena_aligned_alloc (size_t size) |
| Helper for cache-line aligned allocations. Ensures the returned pointer starts at a 64-byte boundary. Respects TEST_OOM for unit testing. | |
| csilk_arena_t * | csilk_arena_new (size_t default_chunk_size) |
| Create a new arena allocator. | |
| void * | csilk_arena_alloc (csilk_arena_t *arena, size_t size) |
| Allocate memory from the arena with 8-byte alignment. | |
| char * | csilk_arena_strdup (csilk_arena_t *arena, const char *s) |
| Duplicate a null-terminated string using the arena allocator. | |
| char * | csilk_arena_strndup (csilk_arena_t *arena, const char *s, size_t n) |
Duplicate n bytes of a string using the arena allocator. | |
| void | csilk_arena_free (csilk_arena_t *arena) |
| Free all arena chunks and the arena structure itself. | |
| void | csilk_arena_reset (csilk_arena_t *arena) |
| Reset arena for reuse without freeing underlying chunks. | |
| void | csilk_arena_get_stats (csilk_arena_t *arena, size_t *total_size, size_t *total_used) |
| Get total allocated size and used bytes in the arena. | |
Arena (bump) allocator for request-scoped memory management.
The arena allocator is the cornerstone of csilk's zero-freedown model. Instead of freeing individual allocations (which causes fragmentation and overhead), the arena allocates from large contiguous chunks and resets all memory at once when the request completes.
Benefits over malloc/free per allocation:
Chunk structure: Each chunk is a linked-list node with a flexible array member (data[]) containing the usable memory. When the current chunk runs out of space, a new chunk (at least default_chunk_size bytes) is prepended to the list. This means allocation always happens in the head chunk (most recently added), which typically has good cache residency.
| struct csilk_arena_chunk_t |
A single chunk in the arena linked list.
Arena allocator manages memory in chunks. When a chunk is full, a new chunk is allocated. All memory is freed at once when the arena is freed, making it ideal for request-scoped allocations.
| Data Fields | ||
|---|---|---|
| uint8_t | _padding[CSILK_CACHE_LINE_SIZE -(3 *sizeof(size_t))] | |
| uint8_t | data[] |
Flexible array for chunk data. |
| struct csilk_arena_chunk_s * | next |
Pointer to next chunk. |
| size_t | size |
Total size of this chunk. |
| size_t | used |
Bytes used in this chunk. |
| struct csilk_arena_t |
Arena allocator for request-scoped memory.
Arena allocators allocate memory in large chunks and never free individual allocations until the entire arena is freed. This eliminates fragmentation and is ideal for per-request memory management where all allocations are discarded together after processing.

| Data Fields | ||
|---|---|---|
| uint8_t | _padding[CSILK_CACHE_LINE_SIZE -(2 *sizeof(size_t))] | |
| size_t | default_chunk_size |
Default size for new chunks. |
| csilk_arena_chunk_t * | head |
Head of chunk linked list. |
| #define CSILK_CACHE_LINE_SIZE 64 |
Cache line size (typically 64 bytes on modern CPUs). Used for padding structures to prevent false sharing and improve memory alignment.
|
static |
Helper for cache-line aligned allocations. Ensures the returned pointer starts at a 64-byte boundary. Respects TEST_OOM for unit testing.
| void * csilk_arena_alloc | ( | csilk_arena_t * | arena, |
| size_t | size | ||
| ) |
Allocate memory from the arena with 8-byte alignment.
Allocate zero-initialised memory from an arena.
Returns memory from the current chunk if there is room; otherwise allocates a new chunk large enough to satisfy the request. The returned memory is zero-initialized only by virtue of being freshly allocated from the OS.
| arena | The arena allocator (must not be NULL). |
| size | Number of bytes to allocate. The actual allocation is rounded up to the nearest multiple of 8 for alignment. |
| void csilk_arena_free | ( | csilk_arena_t * | arena | ) |
Free all arena chunks and the arena structure itself.
Free all memory chunks owned by the arena.
Walks the linked list of chunks, frees each one, then frees the arena header. After this call the arena pointer is invalid and must not be used.
| arena | The arena to destroy (may be NULL). |
| void csilk_arena_get_stats | ( | csilk_arena_t * | arena, |
| size_t * | total_size, | ||
| size_t * | total_used | ||
| ) |
Get total allocated size and used bytes in the arena.
Walks the chunk list and sums the total allocated size and total used bytes.
| arena | The arena to query (must not be NULL). | |
| [out] | total_size | Pointer to receive the total allocated size in bytes. |
| [out] | total_used | Pointer to receive the total used bytes in the arena. |
| csilk_arena_t * csilk_arena_new | ( | size_t | default_chunk_size | ) |
Create a new arena allocator.
Allocates and initializes an arena memory manager. The arena allocates memory in chunks of at least default_chunk_size bytes. Individual allocations within the arena are never freed separately; instead, all memory is reclaimed at once by calling csilk_arena_free() or csilk_arena_reset().
| default_chunk_size | Minimum size in bytes for each new chunk. Pass 0 to let the implementation choose a default. |
| void csilk_arena_reset | ( | csilk_arena_t * | arena | ) |
Reset arena for reuse without freeing underlying chunks.
Reset the arena without freeing its chunks.
Sets the used counter to zero on every chunk in the chain, making all arena memory available for new allocations. No system calls (malloc/free) are performed, making this much cheaper than csilk_arena_free() + _new().
| arena | The arena to reset (may be NULL). |
| char * csilk_arena_strdup | ( | csilk_arena_t * | arena, |
| const char * | s | ||
| ) |
Duplicate a null-terminated string using the arena allocator.
Duplicate a NUL-terminated string using the arena allocator.
Allocates enough arena memory for a copy of s, including the null terminator, and copies the string contents.
| arena | The arena allocator. |
| s | Source string to duplicate. |
s is NULL or on allocation failure. | char * csilk_arena_strndup | ( | csilk_arena_t * | arena, |
| const char * | s, | ||
| size_t | n | ||
| ) |
Duplicate n bytes of a string using the arena allocator.
Allocates n + 1 bytes of arena memory, copies n bytes from s, and adds a null terminator.
| arena | The arena allocator. |
| s | Source string to duplicate. |
| n | Number of bytes to copy. |
s is NULL or on allocation failure.