Fixed infinite growth of arena allocator
This commit is contained in:
parent
eb871068d8
commit
17dc475577
3 changed files with 20 additions and 11 deletions
|
|
@ -6,14 +6,14 @@
|
||||||
#define DEFAUL_REGION_SIZE 1048576 /* 8 * 1024 * 1024 = 8MB */
|
#define DEFAUL_REGION_SIZE 1048576 /* 8 * 1024 * 1024 = 8MB */
|
||||||
|
|
||||||
Region *new_region(size_t capacity) {
|
Region *new_region(size_t capacity) {
|
||||||
if (capacity == 0) {
|
if (capacity < DEFAUL_REGION_SIZE) {
|
||||||
capacity = DEFAUL_REGION_SIZE;
|
capacity = DEFAUL_REGION_SIZE;
|
||||||
}
|
}
|
||||||
Region *region = malloc(capacity);
|
Region *region = malloc(capacity + sizeof(Region));
|
||||||
assert(region != NULL);
|
assert(region != NULL);
|
||||||
*region = (Region){.next = NULL,
|
*region = (Region){.next = NULL,
|
||||||
.capacity = capacity + sizeof(Region),
|
.capacity = capacity,
|
||||||
.cursor = sizeof(Region) - 1};
|
.cursor = 0};
|
||||||
return region;
|
return region;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -39,8 +39,6 @@ void *arena_alloc(Arena *arena, size_t size) {
|
||||||
assert(region != NULL);
|
assert(region != NULL);
|
||||||
|
|
||||||
if (region->cursor + size > region->capacity) {
|
if (region->cursor + size > region->capacity) {
|
||||||
if (size < DEFAUL_REGION_SIZE)
|
|
||||||
size = DEFAUL_REGION_SIZE;
|
|
||||||
region->next = new_region(size);
|
region->next = new_region(size);
|
||||||
arena->end = region->next;
|
arena->end = region->next;
|
||||||
region = region->next;
|
region = region->next;
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ struct Region_s {
|
||||||
Region *next;
|
Region *next;
|
||||||
size_t capacity;
|
size_t capacity;
|
||||||
size_t cursor;
|
size_t cursor;
|
||||||
uintptr_t data[];
|
unsigned char data[];
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
|
||||||
11
src/main.c
11
src/main.c
|
|
@ -8,6 +8,17 @@
|
||||||
#include "shader.h"
|
#include "shader.h"
|
||||||
#include "window.h"
|
#include "window.h"
|
||||||
|
|
||||||
|
#include "arena_allocator.h"
|
||||||
|
|
||||||
|
/*int main(int argc, char *argv[]) {
|
||||||
|
Arena global_arena = arena_init(0);
|
||||||
|
for (int i = 0; i <= 1048576+100; i++) {
|
||||||
|
int *number = arena_alloc(&global_arena, sizeof(int));
|
||||||
|
*number = 42;
|
||||||
|
}
|
||||||
|
arena_deinit(&global_arena);
|
||||||
|
}*/
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
wn_window window = {0};
|
wn_window window = {0};
|
||||||
if (!wn_window_init(&window)) {
|
if (!wn_window_init(&window)) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue