Skip to content

Commit b0fbaf7

Browse files
committed
Use split_block() helper in malloc()
malloc() previously contained open-coded logic for splitting a large block into two smaller ones. This is now replaced with a call to the split_block() helper introduced earlier. This removes duplicated code and ensures consistency by reusing the centralized split implementation, which also includes stronger validation and error handling.
1 parent 44eed33 commit b0fbaf7

File tree

1 file changed

+1
-12
lines changed

1 file changed

+1
-12
lines changed

lib/malloc.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -185,19 +185,8 @@ void *malloc(uint32_t size)
185185
}
186186

187187
if (!IS_USED(p) && GET_SIZE(p) >= size) {
188-
size_t remaining = GET_SIZE(p) - size;
189-
190188
/* Split block only if remainder is large enough to be useful */
191-
if (remaining >= sizeof(memblock_t) + MALLOC_MIN_SIZE) {
192-
memblock_t *new_block =
193-
(memblock_t *) ((size_t) p + sizeof(memblock_t) + size);
194-
new_block->next = p->next;
195-
new_block->size = remaining - sizeof(memblock_t);
196-
MARK_FREE(new_block);
197-
p->next = new_block;
198-
p->size = size;
199-
free_blocks_count++; /* New free block created */
200-
}
189+
split_block(p, size);
201190

202191
MARK_USED(p);
203192
if (unlikely(free_blocks_count <= 0)) {

0 commit comments

Comments
 (0)