Skip to content

Commit f3631c3

Browse files
committed
Port the fix from #377 to emmmalloc.
Avoid using sbrk(0) in emmalloc too.
1 parent a1c7c2c commit f3631c3

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

emmalloc/emmalloc.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656

5757
// Defind by the linker to have the address of the start of the heap.
5858
extern unsigned char __heap_base;
59+
extern unsigned char __heap_end __attribute__((__weak__));
5960

6061
// Behavior of right shifting a signed integer is compiler implementation defined.
6162
static_assert((((int32_t)0x80000000U) >> 31) == -1, "This malloc implementation requires that right-shifting a signed integer produces a sign-extending (arithmetic) shift!");
@@ -545,7 +546,10 @@ static bool claim_more_memory(size_t numBytes)
545546
// If this is the first time we're called, see if we can use
546547
// the initial heap memory set up by wasm-ld.
547548
if (!listOfAllRegions) {
548-
unsigned char *heap_end = sbrk(0);
549+
unsigned char *heap_end = &__heap_end;
550+
if (heap_end == NULL)
551+
heap_end = (unsigned char*) ((size_t) &__heap_base + (PAGE_SIZE-1) & -PAGE_SIZE);
552+
549553
if (numBytes <= (size_t)(heap_end - &__heap_base)) {
550554
startPtr = &__heap_base;
551555
endPtr = heap_end;

0 commit comments

Comments
 (0)