Skip to content

Commit da7670e

Browse files
committed
[malloc] Reuse previously allocated memory block and allow split
1 parent 06a5487 commit da7670e

File tree

1 file changed

+4
-19
lines changed

1 file changed

+4
-19
lines changed

src/usr/lib/stdlib.c

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -205,29 +205,24 @@ static union heap_entry *malloc_allocate_new_block(union heap_entry *block, size
205205
block->content.size = size;
206206
block->content.is_free = 1; // new block is free at init.
207207
heap_entry_count++;
208-
printf("[log] malloc_allocate_new_block: %x, %d\n", block, size);
209208
return block;
210209
}
211210

212211
static union heap_entry *malloc_split_block(union heap_entry *block, size_t size) {
213-
214212
// returns the first one of the two splitted blocks
215213

216214
// [ --------- old-free-block-------------- ]
217215
// [ allocating-block] [new-left-over-block]
218-
const int TODO_NEVER_SPLIT = 1;
219216
size_t left_over_size = block->content.size - size;
220-
if (TODO_NEVER_SPLIT || left_over_size < sizeof(union heap_entry)+8) {
217+
if (left_over_size < sizeof(union heap_entry)+8) {
221218
// do not split block if left-over block for less than 8 bytes allocation.
222219
// i.e. do not split block
223-
// printf("[log] malloc_split_block: %x, %d out of %d [no-split]\n", block, size, block->content.size);
224220
return block;
225221
}
226222
// allocate new-left-over-block
227223
union heap_entry *block_second = malloc_allocate_new_block((union heap_entry *)(((void*)block)+size), left_over_size);
228224
// resize first block
229225
block->content.size = size;
230-
printf("[log] malloc_split_block: %x, %d [split]\n", block, size);
231226
return block;
232227
}
233228

@@ -237,25 +232,21 @@ static void malloc_merge_onfree(union heap_entry *block) {
237232
// block.
238233
if (!block->content.is_free) return;
239234

240-
// TODO(scopeinfinity): implement
235+
// TODO(scopeinfinity): implementation pending.
241236
}
242237

243238
static union heap_entry *malloc_find_freeblock(size_t size) {
244-
if(size>200)
245-
printf("[log] malloc_find_freeblock: %d, block_count: %d\n", size, heap_entry_count);
246239
// size includes header size
247-
void* loc = _heap_start; // start
240+
void* loc = (void*)_heap_start; // start
248241
int block_id = 0;
249242
while(1) {
250-
// printf("[log] searching %d at %x\n", block_id, loc);
251243

252244
union heap_entry *block = loc;
253245
if (block_id==heap_entry_count) {
254246
// create new node
255247
return malloc_allocate_new_block(block, size);
256248
}
257-
const int TODO_ALWAYS_ALLOCATE_LAST = 1;
258-
if (TODO_ALWAYS_ALLOCATE_LAST || (!block->content.is_free) || block->content.size < size) {
249+
if ((!block->content.is_free) || block->content.size < size) {
259250
// block is not free
260251
// not sufficient memory in this block
261252
loc += block->content.size;
@@ -276,23 +267,17 @@ void* malloc(size_t size) {
276267
union heap_entry *header = malloc_find_freeblock(size);
277268

278269
benchmark_heap_inuse += size;
279-
header->content.size = size;
280270
header->content.is_free = 0; // false
281271
return (((void*)header)+sizeof(union heap_entry));
282272
}
283273

284-
// TODO(scopeinfinity): Cleanup before submit
285-
// - LOGO empty : 372 bytes
286-
// - Now: 188 bytes
287-
288274
void free(void* ptr) {
289275
if(ptr == NULL) return;
290276
// current version of malloc is non-optimal and doesn't
291277
// do any free operation.
292278
union heap_entry *header = ptr-sizeof(union heap_entry);
293279
if (header->content.is_free!=0) {
294280
// trying to free unallocated memory
295-
printf("[log] trying to free unallocated memory: %x", ptr);
296281
return;
297282
}
298283
header->content.is_free = 1; // true

0 commit comments

Comments
 (0)