Skip to content

Arena Allocator #3231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions include/prism/allocator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# /**
* @file allocator.h
*
* An arena allocator.
*/
#ifndef PRISM_ALLOCATOR_H
#define PRISM_ALLOCATOR_H

#include "prism/defines.h"
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>

typedef struct pm_allocator_page {
struct pm_allocator_page *next;
char *start;
char *current;
char *end;
} pm_allocator_page_t;

typedef struct pm_allocator {
pm_allocator_page_t head;
pm_allocator_page_t *tail;
} pm_allocator_t;

/**
* Initialize an allocator with the given capacity.
*/
void pm_allocator_init(pm_allocator_t *allocator, size_t capacity);

/**
* Allocate memory from the given allocator.
*/
void * pm_allocator_arena_alloc(pm_allocator_t *allocator, size_t size, size_t alignment);

/**
* Allocate memory from the given allocator and zero out the memory.
*/
void * pm_allocator_arena_calloc(pm_allocator_t *allocator, size_t count, size_t size, size_t alignment);

/**
* Frees the internal memory associated with the allocator.
*/
void pm_allocator_free(pm_allocator_t *allocator);

#endif
1 change: 1 addition & 0 deletions include/prism/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
* #endif
* ```
*/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

#ifdef PRISM_XALLOCATOR
#include "prism_xallocator.h"
#else
Expand Down
14 changes: 4 additions & 10 deletions include/prism/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define PRISM_NODE_H

#include "prism/defines.h"
#include "prism/allocator.h"
#include "prism/parser.h"
#include "prism/util/pm_buffer.h"

Expand All @@ -23,30 +24,23 @@
* @param list The list to append to.
* @param node The node to append.
*/
void pm_node_list_append(pm_node_list_t *list, pm_node_t *node);
void pm_node_list_append(pm_allocator_t *allocator, pm_node_list_t *list, pm_node_t *node);

/**
* Prepend a new node onto the beginning of the node list.
*
* @param list The list to prepend to.
* @param node The node to prepend.
*/
void pm_node_list_prepend(pm_node_list_t *list, pm_node_t *node);
void pm_node_list_prepend(pm_allocator_t *allocator, pm_node_list_t *list, pm_node_t *node);

/**
* Concatenate the given node list onto the end of the other node list.
*
* @param list The list to concatenate onto.
* @param other The list to concatenate.
*/
void pm_node_list_concat(pm_node_list_t *list, pm_node_list_t *other);

/**
* Free the internal memory associated with the given node list.
*
* @param list The list to free.
*/
void pm_node_list_free(pm_node_list_t *list);
void pm_node_list_concat(pm_allocator_t *allocator, pm_node_list_t *list, pm_node_list_t *other);

/**
* Deallocate a node and all of its children.
Expand Down
13 changes: 13 additions & 0 deletions include/prism/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define PRISM_PARSER_H

#include "prism/defines.h"
#include "prism/allocator.h"
#include "prism/ast.h"
#include "prism/encoding.h"
#include "prism/options.h"
Expand Down Expand Up @@ -581,6 +582,12 @@ typedef struct pm_scope {
/** A pointer to the previous scope in the linked list. */
struct pm_scope *previous;

/**
* This allocator is responsible for allocating the space for the list of
* the locals and the list of implicit parameters in the scope.
*/
pm_allocator_t allocator;

/** The IDs of the locals in the given scope. */
pm_locals_t locals;

Expand Down Expand Up @@ -645,6 +652,12 @@ struct pm_parser {
*/
uint32_t node_id;

/** The allocator used to allocate nodes and their fields. */
pm_allocator_t allocator;

/** The allocator used to allocate lists of block exits. */
pm_allocator_t block_exits_allocator;

/** The current state of the lexer. */
pm_lex_state_t lex_state;

Expand Down
4 changes: 3 additions & 1 deletion include/prism/util/pm_constant_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#define PRISM_CONSTANT_POOL_H

#include "prism/defines.h"
#include "prism/allocator.h"

#include <assert.h>
#include <stdbool.h>
Expand Down Expand Up @@ -54,10 +55,11 @@ void pm_constant_id_list_init(pm_constant_id_list_t *list);
/**
* Initialize a list of constant ids with a given capacity.
*
* @param allocator The allocator to use to allocate space for the list.
* @param list The list to initialize.
* @param capacity The initial capacity of the list.
*/
void pm_constant_id_list_init_capacity(pm_constant_id_list_t *list, size_t capacity);
void pm_constant_id_list_init_capacity(pm_allocator_t *allocator, pm_constant_id_list_t *list, size_t capacity);

/**
* Append a constant id to a list of constant ids. Returns false if any
Expand Down
4 changes: 3 additions & 1 deletion include/prism/util/pm_integer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define PRISM_NUMBER_H

#include "prism/defines.h"
#include "prism/allocator.h"
#include "prism/util/pm_buffer.h"

#include <assert.h>
Expand Down Expand Up @@ -77,12 +78,13 @@ typedef enum {
* has already been validated, as internal validation checks are not performed
* here.
*
* @param allocator The allocator to use to allocate memory for the integer.
* @param integer The integer to parse into.
* @param base The base of the integer.
* @param start The start of the string.
* @param end The end of the string.
*/
void pm_integer_parse(pm_integer_t *integer, pm_integer_base_t base, const uint8_t *start, const uint8_t *end);
void pm_integer_parse(pm_allocator_t *allocator, pm_integer_t *integer, pm_integer_base_t base, const uint8_t *start, const uint8_t *end);

/**
* Compare two integers. This function returns -1 if the left integer is less
Expand Down
3 changes: 2 additions & 1 deletion include/prism/util/pm_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define PRISM_STRING_H

#include "prism/defines.h"
#include "prism/allocator.h"

#include <assert.h>
#include <errno.h>
Expand Down Expand Up @@ -150,7 +151,7 @@ PRISM_EXPORTED_FUNCTION pm_string_init_result_t pm_string_file_init(pm_string_t
*
* @param string The string to ensure is owned.
*/
void pm_string_ensure_owned(pm_string_t *string);
void pm_string_ensure_owned(pm_allocator_t *allocator, pm_string_t *string);

/**
* Compare the underlying lengths and bytes of two strings. Returns 0 if the
Expand Down
120 changes: 120 additions & 0 deletions src/allocator.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#include "prism/allocator.h"

#define PM_ALLOCATOR_CAPACITY_DEFAULT 4096

/**
* Initialize an allocator with the given capacity.
*/
void
pm_allocator_init(pm_allocator_t *allocator, size_t capacity) {
char *memory = xmalloc(capacity);
if (memory == NULL) {
fprintf(stderr, "[pm_allocator_init] failed to allocate memory\n");
abort();
}

*allocator = (pm_allocator_t) {
.head = {
.next = NULL,
.start = memory,
.current = memory,
.end = memory + capacity
},
.tail = &allocator->head
};
}

/**
* Allocate memory from the given allocator.
*/
void *
pm_allocator_arena_alloc(pm_allocator_t *allocator, size_t size, size_t alignment) {
assert(size > 0);

char *result = allocator->tail->current;
uintptr_t delta = 0;
uintptr_t current = (uintptr_t) allocator->tail->current;
if (current % alignment != 0) {
delta = alignment - (current % alignment);
}
result += delta;

char *next = result + size;

if (next >= allocator->tail->end) {
size_t next_capacity = PM_ALLOCATOR_CAPACITY_DEFAULT;

// In case the requested size is larger than our default capacity, scale
// up just this node in the linked list to fit the allocation.
if (size > next_capacity) next_capacity = size;

char *memory = xmalloc(sizeof(pm_allocator_page_t) + next_capacity);
if (memory == NULL) {
fprintf(stderr, "[pm_allocator_arena_alloc] failed to allocate memory\n");
abort();
}

pm_allocator_page_t *next_allocator_page = (pm_allocator_page_t *) memory;
char *start = memory + sizeof(pm_allocator_page_t);

// Assume the alignment is correct because malloc should give us back
// the most relaxed alignment possible.
assert(((uintptr_t) start) % alignment == 0);

*next_allocator_page = (pm_allocator_page_t) {
.next = NULL,
.start = start,
.current = NULL, // set at the end
.end = start + next_capacity
};

allocator->tail->next = next_allocator_page;
allocator->tail = next_allocator_page;

result = allocator->tail->start;
next = result + size;
}

allocator->tail->current = next;
return result;
}

/**
* Allocate memory from the given allocator and zero out the memory.
*/
void *
pm_allocator_arena_calloc(pm_allocator_t *allocator, size_t count, size_t size, size_t alignment) {
assert(size > 0);

size_t product = count * size;
assert(product / size == count);

void *memory = pm_allocator_arena_alloc(allocator, product, alignment);
memset(memory, 0, product);

return memory;
}

/**
* Frees the internal memory associated with the allocator.
*/
void pm_allocator_free(pm_allocator_t *allocator) {
bool top_level = true;

for (pm_allocator_page_t *current = &allocator->head; current != NULL;) {
pm_allocator_page_t *previous = current;
current = current->next;

// If the memory block is immediately after the allocation of the
// allocator itself, then it came from pm_allocator_arena_alloc, so we
// should free it. Otherwise we assume it was embedded into another
// struct or allocated on the stack.
if (top_level) {
free(previous->start);
} else {
free(previous);
}

top_level = false;
}
}
Loading