libpulsar
A modular compiler for the pulsar programming language
Loading...
Searching...
No Matches
Data Structures | Macros | Enumerations | Functions
arena.h File Reference

Defines an arena allocator for the compiler. More...

#include "def.h"

Go to the source code of this file.

Data Structures

struct  ps_arena_allocator
 

Macros

#define PS_INITIAL_ARENA_MMAP   0x2000000ULL
 An allocation of 16MB per arena.
 
#define arena_alloc(arena, n)   ps_arena_allocate(arena, n)
 
#define astalloc(n)   arena_alloc(PS_ARENA_AST, n)
 
#define iralloc(n)   arena_alloc(PS_ARENA_IR, n)
 
#define ast_strndup(str, len)   arena_strndup(PS_ARENA_AST, str, len)
 
#define arena_sprintf(arena, fmt, ...)
 
#define ast_sprintf(fmt, ...)   arena_sprintf(PS_ARENA_AST, fmt, __VA_ARGS__)
 

Enumerations

enum  ps_arena_domain { PS_ARENA_AST , PS_ARENA_IR , __PS_ARENA_COUNT }
 

Functions

void ps_arena_open (enum ps_arena_domain domain)
 Initializes the given arena for allocation.
 
void ps_arena_close (enum ps_arena_domain domain)
 Destroys any allocated objects in the given arena.
 
void * ps_arena_allocate (enum ps_arena_domain domain, usize n)
 Allocates a new object in the given arena.
 
void * ps_arena_allocate_debug (enum ps_arena_domain domain, usize n, const char *file, const char *func, usize line)
 
char * arena_strndup (enum ps_arena_domain arena, STR str, usize length)
 Duplicates the specified string and produces a null-terminated arena-allocated copy.
 

Detailed Description

Defines an arena allocator for the compiler.

Author
Ethan Uppal

Definition in file arena.h.

Macro Definition Documentation

◆ arena_alloc

#define arena_alloc (   arena,
 
)    ps_arena_allocate(arena, n)

Definition at line 50 of file arena.h.

◆ arena_sprintf

#define arena_sprintf (   arena,
  fmt,
  ... 
)
Value:
({ \
char* str = NULL; \
int length = snprintf(NULL, 0, fmt, __VA_ARGS__); \
if (length >= 0) { \
str = arena_alloc(arena, length + 1); \
if (str) { \
snprintf(str, length + 1, fmt, __VA_ARGS__); \
} \
} \
str; \
})
#define arena_alloc(arena, n)
Definition arena.h:50
static void usize length
Definition lexer.h:71

Definition at line 65 of file arena.h.

◆ ast_sprintf

#define ast_sprintf (   fmt,
  ... 
)    arena_sprintf(PS_ARENA_AST, fmt, __VA_ARGS__)

Definition at line 77 of file arena.h.

◆ ast_strndup

#define ast_strndup (   str,
  len 
)    arena_strndup(PS_ARENA_AST, str, len)

Definition at line 63 of file arena.h.

◆ astalloc

#define astalloc (   n)    arena_alloc(PS_ARENA_AST, n)

Definition at line 51 of file arena.h.

◆ iralloc

#define iralloc (   n)    arena_alloc(PS_ARENA_IR, n)

Definition at line 52 of file arena.h.

◆ PS_INITIAL_ARENA_MMAP

#define PS_INITIAL_ARENA_MMAP   0x2000000ULL

An allocation of 16MB per arena.

Definition at line 13 of file arena.h.

Enumeration Type Documentation

◆ ps_arena_domain

Enumerator
PS_ARENA_AST 
PS_ARENA_IR 
__PS_ARENA_COUNT 

Definition at line 25 of file arena.h.

Function Documentation

◆ arena_strndup()

char * arena_strndup ( enum ps_arena_domain  arena,
STR  str,
usize  length 
)

Duplicates the specified string and produces a null-terminated arena-allocated copy.

Parameters
arenaThe arena to allocate in.
strThe string to duplicate.
lengthThe length of the string to duplicate.
Returns
A pointer to the allocated string.

◆ ps_arena_allocate()

void * ps_arena_allocate ( enum ps_arena_domain  domain,
usize  n 
)

Allocates a new object in the given arena.

Parameters
domainThe arena to allocate in.
nThe size of the object to allocate.
Returns
A pointer to the allocated object.

◆ ps_arena_allocate_debug()

void * ps_arena_allocate_debug ( enum ps_arena_domain  domain,
usize  n,
const char *  file,
const char *  func,
usize  line 
)

◆ ps_arena_close()

void ps_arena_close ( enum ps_arena_domain  domain)

Destroys any allocated objects in the given arena.

If the arena is already closed, this function does nothing.

Parameters
domainThe arena to close.

Definition at line 45 of file arena.c.

◆ ps_arena_open()

void ps_arena_open ( enum ps_arena_domain  domain)

Initializes the given arena for allocation.

Parameters
domainThe arena to initialize.

Definition at line 24 of file arena.c.