Skip to content

Commit 0a196ad

Browse files
committed
Add per-arena block sizing and safe top-block growth
This grows using max(DEFAULT_ARENA_SIZE, block_size, request) and sets per-arena block_size from initial capacity.
1 parent fb956c1 commit 0a196ad

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/defs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ typedef struct arena_block {
7979
typedef struct {
8080
arena_block_t *head;
8181
int total_bytes; /* Track total allocation for profiling */
82+
int block_size; /* Default block size for new blocks */
8283
} arena_t;
8384

8485
/* string-based hash map definitions */

src/globals.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ arena_t *arena_init(int initial_capacity)
144144
}
145145
arena->head = arena_block_create(initial_capacity);
146146
arena->total_bytes = initial_capacity;
147+
/* Use the initial capacity as the default block size for future growth. */
148+
arena->block_size = initial_capacity;
147149
return arena;
148150
}
149151

@@ -166,9 +168,12 @@ void *arena_alloc(arena_t *arena, int size)
166168
size = (size + PTR_SIZE - 1) & ~(PTR_SIZE - 1);
167169

168170
if (!arena->head || arena->head->offset + size > arena->head->capacity) {
169-
/* Need a new block: choose capacity = max(DEFAULT_ARENA_SIZE, size) */
170-
int new_capacity =
171-
(size > DEFAULT_ARENA_SIZE ? size : DEFAULT_ARENA_SIZE);
171+
/* Need a new block: choose capacity = max(DEFAULT_ARENA_SIZE,
172+
* arena->block_size, size) */
173+
int base =
174+
(arena->block_size > DEFAULT_ARENA_SIZE ? arena->block_size
175+
: DEFAULT_ARENA_SIZE);
176+
int new_capacity = (size > base ? size : base);
172177
arena_block_t *new_block = arena_block_create(new_capacity);
173178
new_block->next = arena->head;
174179
arena->head = new_block;

0 commit comments

Comments
 (0)