-
Notifications
You must be signed in to change notification settings - Fork 252
Use __heap_base by dlmalloc #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
aa698a1
93476e4
5b72609
72a6db1
7d1e5ab
8dbe4e2
403d4a9
f858b48
2e4eee3
5e70f07
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4560,6 +4560,11 @@ static void* tmalloc_small(mstate m, size_t nb) { | |
|
|
||
| #if !ONLY_MSPACES | ||
|
|
||
| #if __wasilibc_unmodified_upstream // Forward declaration of try_init_allocator. | ||
| #else | ||
| void try_init_allocator(void); | ||
| #endif | ||
|
|
||
| void* dlmalloc(size_t bytes) { | ||
| /* | ||
| Basic algorithm: | ||
|
|
@@ -4588,6 +4593,13 @@ void* dlmalloc(size_t bytes) { | |
| ensure_initialization(); /* initialize in sys_alloc if not using locks */ | ||
| #endif | ||
|
|
||
| #if __wasilibc_unmodified_upstream // Try to initialize the allocator. | ||
| #else | ||
| if(!is_initialized(gm)) { | ||
| try_init_allocator(); | ||
| } | ||
| #endif | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it work to call this from within In that case, it shouldn't do
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmm, I've though about it, but there are some clues:
So, yes, it possible to move top chunk initialization into What do you think?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for looking into that! I think what you have right now looks like a reasonable approach then. |
||
|
|
||
| if (!PREACTION(gm)) { | ||
| void* mem; | ||
| size_t nb; | ||
|
|
@@ -5197,6 +5209,43 @@ static void internal_inspect_all(mstate m, | |
| } | ||
| #endif /* MALLOC_INSPECT_ALL */ | ||
|
|
||
| #ifdef __wasilibc_unmodified_upstream // Define a function that initializes the initial state of dlmalloc | ||
| #else | ||
|
mikevoronov marked this conversation as resolved.
|
||
| /* ------------------ Exported try_init_allocator -------------------- */ | ||
|
|
||
| extern unsigned char __heap_base; /* Symbol marking the end of data, bss and explicit stack, provided by wasm-ld. */ | ||
|
|
||
| // Initialize the initial state of dlmalloc to be able to use free memory between __heap_base and initial. | ||
| void try_init_allocator(void) { | ||
| // Check that it is a first-time initialization. | ||
| if (is_initialized(gm)) { | ||
|
mikevoronov marked this conversation as resolved.
Outdated
|
||
| return; | ||
| } | ||
|
|
||
| char *base = (char *)&__heap_base; | ||
| // Calls sbrk(0) that returns the initial memory position. | ||
| char *init = (char *)CALL_MORECORE(0); | ||
| int initial_heap_size = init - base; | ||
|
|
||
| // Check that initial heap is long enough to serve a minimal allocation request. | ||
| if(initial_heap_size <= MIN_CHUNK_SIZE + TOP_FOOT_SIZE + MALLOC_ALIGNMENT) { | ||
| return; | ||
| } | ||
|
|
||
| // Initialize mstate. | ||
| ensure_initialization(); | ||
|
|
||
| // Initialize the dlmalloc internal state. | ||
| gm->least_addr = base; | ||
| gm->seg.base = base; | ||
| gm->seg.size = initial_heap_size; | ||
| gm->magic = mparams.magic; | ||
| gm->release_checks = MAX_RELEASE_CHECK_RATE; | ||
| init_bins(gm); | ||
| init_top(gm, (mchunkptr)base, initial_heap_size - TOP_FOOT_SIZE); | ||
| } | ||
| #endif | ||
|
|
||
| /* ------------------ Exported realloc, memalign, etc -------------------- */ | ||
|
|
||
| #if !ONLY_MSPACES | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1013,6 +1013,7 @@ trunc | |
| truncate | ||
| truncf | ||
| truncl | ||
| try_init_allocator | ||
| tsearch | ||
| twalk | ||
| uname | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ __floatsitf | |
| __floatunsitf | ||
| __getf2 | ||
| __gttf2 | ||
| __heap_base | ||
| __letf2 | ||
| __lttf2 | ||
| __netf2 | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.