File tree Expand file tree Collapse file tree 2 files changed +9
-3
lines changed Expand file tree Collapse file tree 2 files changed +9
-3
lines changed Original file line number Diff line number Diff line change @@ -79,6 +79,7 @@ typedef struct arena_block {
7979typedef 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 */
Original file line number Diff line number Diff 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 ;
You can’t perform that action at this time.
0 commit comments