Skip to content

wrap_dlmalloc #4603

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

Merged
merged 1 commit into from
Oct 20, 2016
Merged

wrap_dlmalloc #4603

merged 1 commit into from
Oct 20, 2016

Conversation

juj
Copy link
Collaborator

@juj juj commented Oct 5, 2016

Export system malloc() and free() as dlmalloc() and dlfree() so that applications can have an easier mechanism to just hook in between to the existing malloc and free, but still keep calling the original dlmalloc and dlfree.

@kripken
Copy link
Member

kripken commented Oct 5, 2016

I don't know about this. It adds two exported names to all builds, for a rather corner case, which we already have ways to do (provide a full dlmalloc, e.g. using dlmalloc.c, or use the tracing API). This would also be in danger of breaking should we e.g. switch to the musl allocator instead of separate dlmalloc, which is separate for historic reasons really.

@juj
Copy link
Collaborator Author

juj commented Oct 6, 2016

Hmm, what if we exported the functions as emscripten_builtin_malloc and emscripten_builtin_free then, instead of dlmalloc and dlfree?

@kripken
Copy link
Member

kripken commented Oct 6, 2016

Those names sound reasonable to me.

Can we do this as an option, though? Exporting them by default all the time sounds like overhead we shouldn't add. Unless this doesn't actually export them unless used?

@juj
Copy link
Collaborator Author

juj commented Oct 19, 2016

Rebased to resolve merge conflict.

There is no overhead in always exporting, at least not in added code size, since the symbols will only be linked in if code uses them. And because these are aliases to the same function, if code calls them they will not duplicate the implementation of malloc, but just refer to the original. Or are you concerned with the export table having two extra entries?

@kripken
Copy link
Member

kripken commented Oct 19, 2016

Sounds good about the overhead then.

Overall lgtm, except for the idea before to export them as emscripten_malloc or emscripten_builtin_malloc or such? Seems more future-proof.

…emscripten_builtin_free() so that applications can have an easier mechanism to just hook in between to the existing malloc and free, but still keep calling the original dlmalloc and dlfree. Bump version to 1.36.12.
@juj juj force-pushed the wrap_dlmalloc branch 2 times, most recently from 405f366 to 49674d7 Compare October 19, 2016 20:59
@juj
Copy link
Collaborator Author

juj commented Oct 19, 2016

Oh right, let's do the emscripten_builtin_malloc, forgot I proposed that. Updated the PR. I realize needs to bump the version (previous version also did, but didn't have that in) to rebuild libc in cache so it contains the alias names. How does this look now?

@kripken
Copy link
Member

kripken commented Oct 19, 2016

Nice, looks good.

@juj juj merged commit 2222503 into emscripten-core:incoming Oct 20, 2016
@evanw
Copy link
Contributor

evanw commented Nov 5, 2018

I just found this. I'd like to use emscripten_builtin_malloc and emscripten_builtin_free but they are currently impossible to use in real code. When changing out the memory allocator, you must override all four of malloc, free, calloc, and realloc. Only overriding some and not the others will result in heap corruption if both implementations are called.

Is there a reason why there isn't also emscripten_builtin_calloc and emscripten_builtin_realloc?

@juj
Copy link
Collaborator Author

juj commented Aug 24, 2021

I just found this. I'd like to use emscripten_builtin_malloc and emscripten_builtin_free but they are currently impossible to use in real code. When changing out the memory allocator, you must override all four of malloc, free, calloc, and realloc. Only overriding some and not the others will result in heap corruption if both implementations are called.

Is there a reason why there isn't also emscripten_builtin_calloc and emscripten_builtin_realloc?

It's been a long time - missed this question when it initially was proposed. There was no specific reason not to provide emscripten_builtin_calloc and emscripten_builtin_realloc as well, that would be a good idea to have.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants