[util] fix region max alloc size

This commit is contained in:
Milo Turner 2020-02-08 08:37:14 -05:00
parent f36272b66f
commit efd832a3bd
2 changed files with 11 additions and 9 deletions

View File

@ -2,9 +2,9 @@
#include "region.h" #include "region.h"
#include <stdlib.h> #include <stdlib.h>
static inline void* underlying_alloc(size_t sz) static inline void* underlying_alloc(size_t siz)
{ {
void* p = malloc(sz); void* p = malloc(siz);
// printf("[region] allocate %zu ==> %p\n", sz, p); // printf("[region] allocate %zu ==> %p\n", sz, p);
return p; return p;
} }
@ -21,9 +21,10 @@ struct rgn_block {
char data[0]; char data[0];
}; };
void rgn_init(struct rgn* rgn, size_t pgsize) void rgn_init(struct rgn* rgn, size_t pg_size)
{ {
rgn->pgsize = pgsize; rgn->pg_size = pg_size;
rgn->max_alloc_size = pg_size - sizeof(struct rgn_block);
rgn->here = rgn->eob = NULL; rgn->here = rgn->eob = NULL;
rgn->unused_blocks = NULL; rgn->unused_blocks = NULL;
rgn->used_blocks = NULL; rgn->used_blocks = NULL;
@ -73,8 +74,8 @@ static inline void new_block(struct rgn* rgn)
if (b0 != NULL) { if (b0 != NULL) {
rgn->unused_blocks = b0->next; rgn->unused_blocks = b0->next;
} else { } else {
b0 = underlying_alloc(rgn->pgsize); b0 = underlying_alloc(rgn->pg_size);
b0->eob = ((char*) b0) + rgn->pgsize; b0->eob = ((char*) b0) + rgn->pg_size;
} }
use_block(rgn, b0); use_block(rgn, b0);
@ -83,7 +84,7 @@ static inline void new_block(struct rgn* rgn)
void* ralloc_assume_aligned(struct rgn* rgn, size_t siz) void* ralloc_assume_aligned(struct rgn* rgn, size_t siz)
{ {
// TODO: relax this // TODO: relax this
ASSERT(siz < rgn->pgsize, "allocation too big!"); ASSERT(siz <= rgn->max_alloc_size, "allocation too big!");
char* there = rgn->here + siz; char* there = rgn->here + siz;
if (there > rgn->eob) { if (there > rgn->eob) {
@ -100,7 +101,7 @@ void* ralloc_assume_aligned(struct rgn* rgn, size_t siz)
void rgn_print_stats(struct rgn* rgn, FILE* out) void rgn_print_stats(struct rgn* rgn, FILE* out)
{ {
fprintf(out, "[region stats] ======================\n"); fprintf(out, "[region stats] ======================\n");
fprintf(out, "[region stats] page size: %zu\n", rgn->pgsize); fprintf(out, "[region stats] page size: %zu\n", rgn->pg_size);
fprintf(out, "[region stats] space left in block: %zu\n", fprintf(out, "[region stats] space left in block: %zu\n",
(rgn->eob - rgn->here)); (rgn->eob - rgn->here));

View File

@ -5,7 +5,8 @@
struct rgn_block; struct rgn_block;
struct rgn { struct rgn {
size_t pgsize; size_t pg_size;
size_t max_alloc_size;
char* here; char* here;
char* eob; char* eob;
struct rgn_block* unused_blocks; struct rgn_block* unused_blocks;