@@ -478,25 +478,20 @@ int emmalloc_validate_memory_regions()
478
478
return memoryError ;
479
479
}
480
480
481
- static bool claim_more_memory (size_t numBytes )
481
+ void * sbrk_aligned (uintptr_t alignment , intptr_t increment );
482
+
483
+ static bool claim_more_memory (size_t alignment , size_t numBytes )
482
484
{
483
485
#ifdef EMMALLOC_VERBOSE
484
- MAIN_THREAD_ASYNC_EM_ASM (out ('claim_more_memory(numBytes =' + Number ($0 )+ ')' ) , numBytes );
486
+ MAIN_THREAD_ASYNC_EM_ASM (out ('claim_more_memory(alignment =' + Number ($0 )+ ', numBytes=' + Number ( $1 ) + ')' ), alignment , numBytes );
485
487
#endif
486
488
487
489
#ifdef EMMALLOC_MEMVALIDATE
488
490
validate_memory_regions ();
489
491
#endif
490
492
491
- // Make sure we always send sbrk requests with the same alignment that sbrk()
492
- // allocates memory at. Otherwise we will not properly interpret returned memory
493
- // to form a seamlessly contiguous region with earlier root regions, which would
494
- // lead to inefficiently treating the sbrk()ed region to be a new disjoint root
495
- // region.
496
- numBytes = (size_t )ALIGN_UP (numBytes , MALLOC_ALIGNMENT );
497
-
498
493
// Claim memory via sbrk
499
- uint8_t * startPtr = (uint8_t * )sbrk ( numBytes );
494
+ uint8_t * startPtr = (uint8_t * )sbrk_aligned ( alignment , numBytes );
500
495
if ((intptr_t )startPtr == -1 )
501
496
{
502
497
#ifdef EMMALLOC_VERBOSE
@@ -508,7 +503,8 @@ static bool claim_more_memory(size_t numBytes)
508
503
MAIN_THREAD_ASYNC_EM_ASM (out ('claim_more_memory: claimed ' + ptrToString ($0 ) + ' - ' + ptrToString ($1 ) + ' (' + Number ($2 ) + ' bytes) via sbrk()' ), startPtr , startPtr + numBytes , numBytes );
509
504
#endif
510
505
assert (HAS_ALIGNMENT (startPtr , alignof(size_t )));
511
- uint8_t * endPtr = startPtr + numBytes ;
506
+ uint8_t * endPtr = ALIGN_UP (startPtr , alignment ) + numBytes ;
507
+ assert ((uintptr_t )sbrk (0 ) >= (uintptr_t )endPtr );
512
508
513
509
// Create a sentinel region at the end of the new heap block
514
510
Region * endSentinelRegion = (Region * )(endPtr - sizeof (Region ));
@@ -585,7 +581,7 @@ static void initialize_emmalloc_heap()
585
581
#endif
586
582
587
583
// Start with a tiny dynamic region.
588
- claim_more_memory (3 * sizeof (Region ));
584
+ claim_more_memory (MALLOC_ALIGNMENT , 3 * sizeof (Region ));
589
585
}
590
586
591
587
void emmalloc_blank_slate_from_orbit ()
@@ -810,24 +806,8 @@ static void *allocate_memory(size_t alignment, size_t size)
810
806
811
807
// We were unable to find a free memory region. Must sbrk() in more memory!
812
808
size_t numBytesToClaim = size + sizeof (Region )* 3 ;
813
- // Take into account the alignment as well. For typical alignment we don't
814
- // need to add anything here (so we do nothing if the alignment is equal to
815
- // MALLOC_ALIGNMENT), but it can matter if the alignment is very high. In that
816
- // case, not adding the alignment can lead to this sbrk not giving us enough
817
- // (in which case, the next attempt fails and will sbrk the same amount again,
818
- // potentially allocating a lot more memory than necessary).
819
- //
820
- // Note that this is not necessarily optimal, as the extra allocation size for
821
- // the alignment might not be needed (if we are lucky and already aligned),
822
- // and even if it helps us allocate it will not immediately be ready for reuse
823
- // (as it will be added to the currently-in-use region before us, so it is not
824
- // in a free list). As a compromise however it seems reasonable in practice as
825
- // a way to handle large aligned regions to avoid even worse waste.
826
- if (alignment > MALLOC_ALIGNMENT ) {
827
- numBytesToClaim += alignment ;
828
- }
829
809
assert (numBytesToClaim > size ); // 32-bit wraparound should not happen here, allocation size has been validated above!
830
- bool success = claim_more_memory (numBytesToClaim );
810
+ bool success = claim_more_memory (alignment , numBytesToClaim );
831
811
if (success )
832
812
return allocate_memory (alignment , size ); // Recurse back to itself to try again
833
813
0 commit comments