Ruby 4.1.0dev (2026-03-20 revision eb04ab9117336f2b3613244cfe8a528c52faf6d6)
Data Structures | Typedefs | Functions
pm_arena.h File Reference

(eb04ab9117336f2b3613244cfe8a528c52faf6d6)

A bump allocator for the prism parser. More...

#include "prism/defines.h"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for pm_arena.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  pm_arena_block
 A single block of memory in the arena. More...
 
struct  pm_arena_t
 A bump allocator. More...
 

Typedefs

typedef struct pm_arena_block pm_arena_block_t
 A single block of memory in the arena.
 

Functions

void pm_arena_reserve (pm_arena_t *arena, size_t capacity)
 Ensure the arena has at least capacity bytes available in its current block, allocating a new block if necessary.
 
void * pm_arena_alloc_slow (pm_arena_t *arena, size_t size)
 Slow path for pm_arena_alloc: allocate a new block and return a pointer to the first size bytes.
 
static PRISM_FORCE_INLINE void * pm_arena_alloc (pm_arena_t *arena, size_t size, size_t alignment)
 Allocate memory from the arena.
 
static void * pm_arena_zalloc (pm_arena_t *arena, size_t size, size_t alignment)
 Allocate zero-initialized memory from the arena.
 
static void * pm_arena_memdup (pm_arena_t *arena, const void *src, size_t size, size_t alignment)
 Allocate memory from the arena and copy the given data into it.
 
PRISM_EXPORTED_FUNCTION void pm_arena_free (pm_arena_t *arena)
 Free all blocks in the arena.
 

Detailed Description

A bump allocator for the prism parser.

Definition in file pm_arena.h.

Typedef Documentation

◆ pm_arena_block_t

A single block of memory in the arena.

Blocks are linked via prev pointers so they can be freed by walking the chain.

Function Documentation

◆ pm_arena_alloc()

static PRISM_FORCE_INLINE void * pm_arena_alloc ( pm_arena_t *  arena,
size_t  size,
size_t  alignment 
)
static

Allocate memory from the arena.

The returned memory is NOT zeroed. This function is infallible — it aborts on allocation failure.

The fast path (bump pointer within the current block) is inlined at each call site. The slow path (new block allocation) is out-of-line.

Parameters
arenaThe arena to allocate from.
sizeThe number of bytes to allocate.
alignmentThe required alignment (must be a power of 2).
Returns
A pointer to the allocated memory.

Definition at line 80 of file pm_arena.h.

Referenced by pm_arena_memdup(), pm_arena_zalloc(), pm_constant_id_list_append(), pm_constant_pool_init(), pm_diagnostic_list_append_format(), pm_line_offset_list_append_slow(), pm_line_offset_list_init(), and pm_parser::pm_parser_init().

◆ pm_arena_alloc_slow()

void * pm_arena_alloc_slow ( pm_arena_t *  arena,
size_t  size 
)

Slow path for pm_arena_alloc: allocate a new block and return a pointer to the first size bytes.

Do not call directly — use pm_arena_alloc instead.

Parameters
arenaThe arena to allocate from.
sizeThe number of bytes to allocate.
Returns
A pointer to the allocated memory.

Called when the current block has insufficient space.

Definition at line 74 of file pm_arena.c.

Referenced by pm_arena_alloc().

◆ pm_arena_free()

PRISM_EXPORTED_FUNCTION void pm_arena_free ( pm_arena_t *  arena)

Free all blocks in the arena.

After this call, all pointers returned by pm_arena_alloc and pm_arena_zalloc are invalid.

Parameters
arenaThe arena to free.

Definition at line 84 of file pm_arena.c.

◆ pm_arena_memdup()

static void * pm_arena_memdup ( pm_arena_t *  arena,
const void *  src,
size_t  size,
size_t  alignment 
)
inlinestatic

Allocate memory from the arena and copy the given data into it.

This is a convenience wrapper around pm_arena_alloc + memcpy.

Parameters
arenaThe arena to allocate from.
srcThe source data to copy.
sizeThe number of bytes to allocate and copy.
alignmentThe required alignment (must be a power of 2).
Returns
A pointer to the allocated copy.

Definition at line 121 of file pm_arena.h.

◆ pm_arena_reserve()

void pm_arena_reserve ( pm_arena_t *  arena,
size_t  capacity 
)

Ensure the arena has at least capacity bytes available in its current block, allocating a new block if necessary.

This allows callers to pre-size the arena to avoid repeated small block allocations.

Parameters
arenaThe arena to pre-size.
capacityThe minimum number of bytes to ensure are available.

This allows callers to pre-size the arena to avoid repeated small block allocations.

Definition at line 63 of file pm_arena.c.

◆ pm_arena_zalloc()

static void * pm_arena_zalloc ( pm_arena_t *  arena,
size_t  size,
size_t  alignment 
)
inlinestatic

Allocate zero-initialized memory from the arena.

This function is infallible — it aborts on allocation failure.

Parameters
arenaThe arena to allocate from.
sizeThe number of bytes to allocate.
alignmentThe required alignment (must be a power of 2).
Returns
A pointer to the allocated, zero-initialized memory.

Definition at line 104 of file pm_arena.h.

Referenced by pm_constant_id_list_init_capacity(), pm_constant_pool_init(), pm_diagnostic_list_append(), and pm_diagnostic_list_append_format().