Skip to content

Commit f18fdaf

Browse files
committed
Merge pull request #135 from mpetri/fix_hugepage_alloc
fixed hugepage alloc when extending last free block
2 parents 9d6ba7b + b3dafff commit f18fdaf

File tree

1 file changed

+43
-3
lines changed

1 file changed

+43
-3
lines changed

lib/memory_management.cpp

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ block_markused(mm_block_t* ptr)
297297
void
298298
hugepage_allocator::coalesce_block(mm_block_t* block)
299299
{
300+
//std::cout << "coalesce_block()" << std::endl;
300301
mm_block_t* newblock = block;
301302
if (block_nextfree(block,m_top)) {
302303
mm_block_t* next = block_next(block,m_top);
@@ -323,14 +324,19 @@ hugepage_allocator::coalesce_block(mm_block_t* block)
323324
void
324325
hugepage_allocator::split_block(mm_block_t* bptr,size_t size)
325326
{
327+
//std::cout << "split_block("<< (void*)bptr << ")" << std::endl;
326328
size_t blocksize = UNMASK_SIZE(bptr->size);
329+
//std::cout << "cur_block_size = " << blocksize << std::endl;
327330
/* only split if we get at least a small block
328331
out of it */
329332
int64_t newblocksize = ALIGNSPLIT(blocksize - ALIGN(size+MM_BLOCK_OVERHEAD));
333+
//std::cout << "new_block_size = " << newblocksize << std::endl;
330334
if (newblocksize >= (int64_t)SPLIT_THRESHOLD) {
331335
/* update blocksize of old block */
336+
//std::cout << "block_update = " << blocksize-newblocksize << std::endl;
332337
block_update(bptr,blocksize-newblocksize);
333338
mm_block_t* newblock = (mm_block_t*)((char*)bptr+(blocksize-newblocksize));
339+
//std::cout << "new block ptr = " << (void*)newblock << std::endl;
334340
block_update(newblock,newblocksize);
335341
coalesce_block(newblock);
336342
}
@@ -353,6 +359,7 @@ hugepage_allocator::hsbrk(size_t size)
353359
mm_block_t*
354360
hugepage_allocator::new_block(size_t size)
355361
{
362+
//std::cout << "new_block(" << size << ")" << std::endl;
356363
size = ALIGN(size+MM_BLOCK_OVERHEAD);
357364
if (size < MIN_BLOCKSIZE) size = MIN_BLOCKSIZE;
358365
mm_block_t* ptr = (mm_block_t*) hsbrk(size);
@@ -364,9 +371,14 @@ mm_block_t*
364371
hugepage_allocator::last_block()
365372
{
366373
mm_block_t* last = nullptr;
374+
//std::cout << "m_top = " << (void*)m_top << std::endl;
375+
//std::cout << "m_base = " << (void*)m_base << std::endl;
367376
if (m_top != m_base) {
368377
mm_block_foot_t* fptr = (mm_block_foot_t*)(m_top - sizeof(size_t));
378+
//std::cout << "foot of last = " << (void*)fptr << std::endl;
379+
//std::cout << "size of last = " << UNMASK_SIZE(fptr->size) << std::endl;
369380
last = (mm_block_t*)(((uint8_t*)fptr) - UNMASK_SIZE(fptr->size) + sizeof(size_t));
381+
//std::cout << "last = " << (void*)last << std::endl;
370382
}
371383
return last;
372384
}
@@ -394,6 +406,7 @@ hugepage_allocator::print_heap()
394406
void
395407
hugepage_allocator::remove_from_free_set(mm_block_t* block)
396408
{
409+
//std::cout << "remove_from_free_set()" << std::endl;
397410
auto eq_range = m_free_large.equal_range(block->size);
398411
// find the block amoung the blocks with equal size
399412
auto itr = eq_range.first;
@@ -414,12 +427,16 @@ hugepage_allocator::remove_from_free_set(mm_block_t* block)
414427
void
415428
hugepage_allocator::insert_into_free_set(mm_block_t* block)
416429
{
417-
m_free_large.insert({block->size,block});
430+
//std::cout << "insert_into_free_set("<< (void*)block << "," << UNMASK_SIZE(block->size) << ")" << std::endl;
431+
//std::cout << "insert_into_free_set("<< (void*)block << "," << block->size << ")" << std::endl;
432+
m_free_large.insert( {block->size,block});
418433
}
419434

420435
mm_block_t*
421436
hugepage_allocator::find_free_block(size_t size_in_bytes)
422437
{
438+
//std::cout << "find_free_block(" << size_in_bytes << ")" << std::endl;
439+
423440
mm_block_t* bptr = nullptr;
424441
auto free_block = m_free_large.lower_bound(size_in_bytes);
425442
if (free_block != m_free_large.end()) {
@@ -432,44 +449,57 @@ hugepage_allocator::find_free_block(size_t size_in_bytes)
432449
void*
433450
hugepage_allocator::mm_alloc(size_t size_in_bytes)
434451
{
452+
//std::cout << "ALLOC(" << size_in_bytes << ")" << std::endl;
435453
mm_block_t* bptr = nullptr;
436454
if ((bptr=find_free_block(size_in_bytes + MM_BLOCK_OVERHEAD)) != nullptr) {
455+
//std::cout << "found free block = " << (void*)bptr << std::endl;
437456
block_markused(bptr);
438457
/* split if we have a block too large for us? */
439458
split_block(bptr,size_in_bytes);
440459
} else {
460+
//std::cout << "no free block found that is big enough!" << std::endl;
441461
// check if last block is free
462+
//std::cout << "check last block" << std::endl;
442463
bptr = last_block();
443464
if (bptr && block_isfree(bptr)) {
465+
//std::cout << "last block is free. -> extend!" << std::endl;
444466
// extent last block as it is free
445467
size_t blockdatasize = block_getdatasize(bptr);
446468
size_t needed = ALIGN(size_in_bytes - blockdatasize);
447469
hsbrk(needed);
448470
remove_from_free_set(bptr);
449471
block_update(bptr,blockdatasize+needed+sizeof(size_t)+sizeof(mm_block_foot_t));
450-
insert_into_free_set(bptr);
472+
//insert_into_free_set(bptr);
451473
block_markused(bptr);
452474
} else {
453475
bptr = new_block(size_in_bytes);
454476
}
455477
}
478+
//print_heap();
479+
//void* ptr = block_data(bptr);
480+
//std::cout << "return ptr = " << ptr << std::endl;
456481
return block_data(bptr);
457482
}
458483

459484
void
460485
hugepage_allocator::mm_free(void* ptr)
461486
{
487+
//print_heap();
488+
//std::cout << "FREE(" << ptr << ")" << std::endl;
462489
if (ptr) {
463490
mm_block_t* bptr = block_cur(ptr);
464491
block_markfree(bptr);
465492
/* coalesce if needed. otherwise just add */
466493
coalesce_block(bptr);
467494
}
495+
//print_heap();
468496
}
469497

470498
void*
471499
hugepage_allocator::mm_realloc(void* ptr, size_t size)
472500
{
501+
//print_heap();
502+
//std::cout << "REALLOC(" << ptr << "," << size << ")" << std::endl;
473503
/* handle special cases first */
474504
if (ptr==NULL) return mm_alloc(size);
475505
if (size==0) {
@@ -481,17 +511,22 @@ hugepage_allocator::mm_realloc(void* ptr, size_t size)
481511
bool need_malloc = 0;
482512
size_t blockdatasize = block_getdatasize(bptr);
483513
/* we do nothing if the size is equal to the block */
484-
if (size == blockdatasize)
514+
if (size == blockdatasize) {
515+
//std::cout << "return ptr = " << ptr << std::endl;
485516
return ptr; /* do nothing if size fits already */
517+
}
486518
if (size < blockdatasize) {
487519
/* we shrink */
488520
/* do we shrink enough to perform a split? */
521+
//std::cout << "shrink!" << std::endl;
489522
split_block(bptr,size);
490523
} else {
524+
//std::cout << "expand!" << std::endl;
491525
/* we expand */
492526
/* if the next block is free we could use it! */
493527
mm_block_t* next = block_next(bptr,m_top);
494528
if (!next) {
529+
//std::cout << "no next! -> expand!" << std::endl;
495530
// we are the last block so we just expand
496531
blockdatasize = block_getdatasize(bptr);
497532
size_t needed = ALIGN(size - blockdatasize);
@@ -500,6 +535,7 @@ hugepage_allocator::mm_realloc(void* ptr, size_t size)
500535
return block_data(bptr);
501536
} else {
502537
// we are not the last block
538+
//std::cout << "try combine next" << std::endl;
503539
if (next && block_isfree(next)) {
504540
/* do we have enough space if we use the next block */
505541
if (blockdatasize + UNMASK_SIZE(next->size) >= size) {
@@ -514,6 +550,7 @@ hugepage_allocator::mm_realloc(void* ptr, size_t size)
514550
}
515551
} else {
516552
/* try combing the previous block if free */
553+
//std::cout << "try combine prev" << std::endl;
517554
mm_block_t* prev = block_prev(bptr,m_first_block);
518555
if (prev && block_isfree(prev)) {
519556
if (blockdatasize + UNMASK_SIZE(prev->size) >= size) {
@@ -535,11 +572,14 @@ hugepage_allocator::mm_realloc(void* ptr, size_t size)
535572
}
536573
}
537574
if (need_malloc) {
575+
//std::cout << "need_alloc in REALLOC!" << std::endl;
538576
void* newptr = mm_alloc(size);
539577
memcpy(newptr,ptr,size);
540578
mm_free(ptr);
541579
ptr = newptr;
542580
}
581+
//print_heap();
582+
//std::cout << "return ptr = " << ptr << std::endl;
543583
return ptr;
544584
}
545585

0 commit comments

Comments
 (0)