-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
emmalloc: Add an option to not define the standard exports (#20487)
With this PR if emmalloc.c is built with -DEMMALLOC_NO_STD_EXPORTS then we do not define malloc, free, etc. That means we only provide emmalloc_malloc, emmalloc_free, etc., the prefixed versions. They can then be used alongside another malloc impl. This will be useful in a later PR that adds a two-tiered allocator: a fast multithreaded one, and underneath it emmalloc, which will function as the "system allocator" for it. That is, emmalloc will play the role of VirtualAlloc on windows or mmap on POSIX, a way for the main allocator to get system memory. (We can't just use sbrk for that purpose because we also want to free memory to the system.) For that goal, emmalloc seems suitable as it is compact (we don't need it to be super-fast; this is the system allocator that will be called rarely, compared to the fast one before it). And for emmalloc to be used like that we need this PR so that we can build emmalloc alongside another allocator (that other allocator will define malloc etc. itself). Helps #18369
- Loading branch information
Showing
3 changed files
with
94 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include <assert.h> | ||
#include <emscripten/console.h> | ||
#include <emscripten/emmalloc.h> | ||
|
||
int main() { | ||
// Verify we can call both malloc and emmalloc_malloc, and that those are | ||
// different functions, unless TEST_EMMALLOC_IS_MALLOC is set (in that case, | ||
// emmalloc is malloc because we let emmalloc define the standard exports like | ||
// malloc). | ||
|
||
// We have allocated nothing so far, but there may be some initial allocation | ||
// from startup. | ||
size_t initial = emmalloc_dynamic_heap_size(); | ||
emscripten_console_logf("initial: %zu\n", initial); | ||
|
||
const size_t ONE_MB = 1024 * 1024; | ||
void* one = malloc(ONE_MB); | ||
assert(one); | ||
#ifndef TEST_EMMALLOC_IS_MALLOC | ||
// We have allocated using malloc, but not emmalloc, so emmalloc reports no | ||
// change in usage. | ||
assert(emmalloc_dynamic_heap_size() == initial); | ||
#else | ||
// malloc == emmalloc_malloc, so emmalloc will report additional usage (of the | ||
// size of the allocation, or perhaps more if it overallocated as an | ||
// optimization). | ||
assert(emmalloc_dynamic_heap_size() >= initial + ONE_MB); | ||
#endif | ||
|
||
void* two = emmalloc_malloc(ONE_MB); | ||
assert(two); | ||
// We have allocated using emmalloc, so now emmalloc definitely reports usage. | ||
assert(emmalloc_dynamic_heap_size() >= initial + ONE_MB); | ||
|
||
emscripten_console_log("success"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters