Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions system/lib/emmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,12 @@ static bool claim_more_memory(size_t numBytes)
validate_memory_regions();
#endif

// Make sure we send sbrk requests of aligned sizes. If we do not do that then
// it will not return contiguous regions, which leads to use creating more
// root regions below, inefficiently. (Note that we assume our alignment is
// identical to sbrk's, see the assumptions at the start of this file.)
numBytes = (size_t)ALIGN_UP(numBytes, MALLOC_ALIGNMENT);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the cast needed here? numBytes is already size_t.. and I imagine that MALLOC_ALIGNMENT is too, no?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cast is needed as the output of the macro is a pointer,

#define ALIGN_UP(ptr, alignment) ((uint8_t*)((((uintptr_t)(ptr)) + ((alignment)-1)) & ~((alignment)-1)))


// Claim memory via sbrk
uint8_t *startPtr = (uint8_t*)sbrk(numBytes);
if ((intptr_t)startPtr == -1)
Expand All @@ -510,6 +516,9 @@ static bool claim_more_memory(size_t numBytes)
uint8_t *previousSbrkEndAddress = listOfAllRegions ? listOfAllRegions->endPtr : 0;
if (startPtr == previousSbrkEndAddress)
{
#ifdef EMMALLOC_VERBOSE
MAIN_THREAD_ASYNC_EM_ASM(err('claim_more_memory: expanding previous'));
#endif
Region *prevEndSentinel = prev_region((Region*)startPtr);
assert(debug_region_is_consistent(prevEndSentinel));
assert(region_is_in_use(prevEndSentinel));
Expand All @@ -536,6 +545,9 @@ static bool claim_more_memory(size_t numBytes)
else
{
// Create a root region at the start of the heap block
#ifdef EMMALLOC_VERBOSE
MAIN_THREAD_ASYNC_EM_ASM(err('claim_more_memory: creating new root region'));
#endif
create_used_region(startPtr, sizeof(Region));

// Dynamic heap start region:
Expand Down Expand Up @@ -583,6 +595,11 @@ void emmalloc_blank_slate_from_orbit()
static void *attempt_allocate(Region *freeRegion, size_t alignment, size_t size)
{
ASSERT_MALLOC_IS_ACQUIRED();

#ifdef EMMALLOC_VERBOSE
MAIN_THREAD_ASYNC_EM_ASM(out('attempt_allocate(freeRegion=' + ptrToString($0) + ',alignment=' + Number($1) + ',size=' + Number($2) + ')'), freeRegion, alignment, size);
#endif

assert(freeRegion);
// Look at the next potential free region to allocate into.
// First, we should check if the free region has enough of payload bytes contained
Expand Down
8 changes: 4 additions & 4 deletions test/code_size/embind_val_wasm.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"a.html.gz": 431,
"a.js": 7498,
"a.js.gz": 3142,
"a.wasm": 11525,
"a.wasm.gz": 5766,
"total": 19696,
"total_gz": 9339
"a.wasm": 11533,
"a.wasm.gz": 5767,
"total": 19704,
"total_gz": 9340
}
8 changes: 4 additions & 4 deletions test/code_size/hello_wasm_worker_wasm.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"a.html.gz": 433,
"a.js": 667,
"a.js.gz": 458,
"a.wasm": 1855,
"a.wasm.gz": 1049,
"total": 3259,
"total_gz": 1940
"a.wasm": 1863,
"a.wasm.gz": 1051,
"total": 3267,
"total_gz": 1942
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl2_wasm.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"a.html.gz": 379,
"a.js": 4699,
"a.js.gz": 2419,
"a.wasm": 10467,
"a.wasm.gz": 6706,
"total": 15735,
"total_gz": 9504
"a.wasm": 10475,
"a.wasm.gz": 6710,
"total": 15743,
"total_gz": 9508
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl2_wasm2js.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 567,
"a.html.gz": 379,
"a.js": 17912,
"a.js.gz": 8067,
"a.js": 17921,
"a.js.gz": 8079,
"a.mem": 3171,
"a.mem.gz": 2713,
"total": 21650,
"total_gz": 11159
"total": 21659,
"total_gz": 11171
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl_wasm.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"a.html.gz": 379,
"a.js": 4185,
"a.js.gz": 2243,
"a.wasm": 10467,
"a.wasm.gz": 6706,
"total": 15221,
"total_gz": 9328
"a.wasm": 10475,
"a.wasm.gz": 6710,
"total": 15229,
"total_gz": 9332
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl_wasm2js.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 567,
"a.html.gz": 379,
"a.js": 17390,
"a.js.gz": 7895,
"a.js": 17399,
"a.js.gz": 7910,
"a.mem": 3171,
"a.mem.gz": 2713,
"total": 21128,
"total_gz": 10987
"total": 21137,
"total_gz": 11002
}