7#define PM_ARENA_BLOCK_SIZE(data_size) (offsetof(pm_arena_block_t, data) + (data_size))
10#define PM_ARENA_INITIAL_SIZE 8192
13#define PM_ARENA_GROWTH_INTERVAL 8
16#define PM_ARENA_MAX_SIZE (1024 * 1024)
22pm_arena_next_block_size(
const pm_arena_t *arena,
size_t min_size) {
23 size_t size = PM_ARENA_INITIAL_SIZE;
25 for (
size_t i = PM_ARENA_GROWTH_INTERVAL; i <= arena->
block_count; i += PM_ARENA_GROWTH_INTERVAL) {
26 if (size < PM_ARENA_MAX_SIZE) size *= 2;
29 return size > min_size ? size : min_size;
37pm_arena_alloc(
pm_arena_t *arena,
size_t size,
size_t alignment) {
40 size_t used_aligned = (arena->
current->
used + alignment - 1) & ~(alignment - 1);
41 size_t needed = used_aligned + size;
44 if (used_aligned >= arena->
current->
used && needed >= used_aligned && needed <= arena->current->capacity) {
53 size_t block_data_size = pm_arena_next_block_size(arena, size);
57 fprintf(stderr,
"prism: out of memory; aborting\n");
75pm_arena_zalloc(
pm_arena_t *arena,
size_t size,
size_t alignment) {
76 void *ptr = pm_arena_alloc(arena, size, alignment);
85pm_arena_memdup(
pm_arena_t *arena,
const void *src,
size_t size,
size_t alignment) {
86 void *dst = pm_arena_alloc(arena, size, alignment);
87 memcpy(dst, src, size);
98 while (block != NULL) {
100 xfree_sized(block, PM_ARENA_BLOCK_SIZE(block->
capacity));
#define xmalloc
Old name of ruby_xmalloc.
A bump allocator for the prism parser.
A single block of memory in the arena.
struct pm_arena_block * prev
The previous block in the chain (for freeing).
char data[PM_FLEX_ARY_LEN]
The block's data.
size_t used
The number of bytes consumed so far.
size_t capacity
The total usable bytes in data[].
size_t block_count
The number of blocks allocated.
pm_arena_block_t * current
The active block (allocate from here).