-
Notifications
You must be signed in to change notification settings - Fork 164
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
Cluster444
wants to merge
10
commits into
ruby:main
Choose a base branch
from
Cluster444:allocator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+484
−360
Open
Arena Allocator #3231
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b230027
An arena allocator
kddnewton 5b78702
Temporary
kddnewton 3f74a5a
Temporary
kddnewton 19cdc86
Temporary
kddnewton c6d5247
Temporary
kddnewton 9d155d0
Temporary
kddnewton 3382cf1
Temporary
kddnewton ac6abc2
work on string fields
XrXr 2b9d06e
dont free integer nodes that are arena alloced
Cluster444 57b85f8
Lift up ignored nodes to make it a little more clear what the excepti…
Cluster444 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -164,6 +164,7 @@ | |
* #endif | ||
* ``` | ||
*/ | ||
|
||
#ifdef PRISM_XALLOCATOR | ||
#include "prism_xallocator.h" | ||
#else | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.