snmalloc
snmalloc copied to clipboard
sn_malloc not called?
I built snmalloc, and linked it to my app.exe, and try to "auto ptr = malloc(4);", but sn_malloc not called? What have I missed?
TARGET_LINK_LIBRARIES( ${LIB_NAME} PRIVATE
#mimalloc
PRIVATE snmalloc)
Maybe a detailed starter tutorial, or an example will help.
I also tried "Using snmalloc as header-only library", no luck either.
Hi, thanks for trying snmalloc!
This is expected behaviour. snmalloc
provides you with a lot of tools for building different allocators and not all consumers want to use it as the C malloc
. If you want to use it as a drop-in replacement for C malloc
or C++ new
then take a look in src/override
, there are files there that provide the malloc
and new
compatible behaviour. You can use these directly in your project.
If you just build snmalloc, then you will get a libsnmallocshim.so
. You can link this to your application or LD_PRELOAD
it to provide snmalloc-backed implementations of malloc
and friends.
@zhmt from your comment it looks like you are on Windows. I think you want the static library for your use case.
If you set CMAKE with:
-DSNMALLOC_STATIC_LIBRARY_PREFIX=""
This should build a static library that you can link that exposes the names malloc
etc. This builds a static library under the target:
snmallocshim-static
which will appear in the relevant build directory as
Debug\snmallocshim-static.lib
Release\snmallocshim-static.lib
The order of includes is fragile for getting the correct overriding behaviour on Windows. I think the static lib has to appear first in the include order. We'll try to get an example and add it to the docs.
You can also experiment with the static library with a non-empty prefix, and this will get you symbols like sn_malloc
, which you can also experiment with if you don't want to override the default symbols.
Hi, thanks for trying snmalloc!
This is expected behaviour.
snmalloc
provides you with a lot of tools for building different allocators and not all consumers want to use it as the Cmalloc
. If you want to use it as a drop-in replacement for Cmalloc
or C++new
then take a look insrc/override
, there are files there that provide themalloc
andnew
compatible behaviour. You can use these directly in your project.If you just build snmalloc, then you will get a
libsnmallocshim.so
. You can link this to your application orLD_PRELOAD
it to provide snmalloc-backed implementations ofmalloc
and friends.
I am trying snmalloc on windows. It is a little bit tricky on windows.
-DSNMALLOC_STATIC_LIBRARY_PREFIX=""
in main.cpp
//first line
#include "override/override.h"
main() {
auto ptr = malloc(4);
auto obj = new App();
}
# in cmake, link snmalloc as the first lib
TARGET_LINK_LIBRARIES(${LIB_NAME}exe PRIVATE
#mimalloc
PRIVATE snmallocshim-static
)
"opertor new " of snmalloc works now, but malloc no luck.
I have to pass buffers from threads to threads over queue, if malloc can also be hooked, that would be nice.
snmalloc fits my situation very well. Lots of messages and buffers are passed bettween threads, so jemalloc and mimalloc are not working perfectly. I count on snmalloc.
I dump the snmallocshim-static.lib, function malloc is not exported. Is this the root cause?
Here is the exported functions:
1 sn___malloc_end_pointer
1 sn_aligned_alloc
1 sn_calloc
1 sn_cfree
1 sn_free
1 sn_malloc
1 sn_malloc_usable_size
1 sn_memalign
1 sn_posix_memalign
1 sn_pvalloc
1 sn_realloc
1 sn_valloc
1 snmalloc_not_allocated
1 wmemcpy
The prefixing is controlled by the SNMALLOC_STATIC_LIBRARY_PREFIX
cmake variable. If you set this to "" then you should have malloc
and friends exposed from the static library. I don't know if this is sufficient for them to be used on Windows.
The prefixing is controlled by the
SNMALLOC_STATIC_LIBRARY_PREFIX
cmake variable. If you set this to "" then you should havemalloc
and friends exposed from the static library. I don't know if this is sufficient for them to be used on Windows.
Not sufficient . I made the malloc exported, it still doesn't work. ucrt has defined malloc, so linker will produce an error:
error LNK2005: free has been defined in snmallocshim-static.lib(new.cc.obj)
A tool like mimalloc-redirect.dll is needed. I will research later.
Thanks for you guys' help.
So Emery Berger's heap layers has a library for redirecting allocators on Windows: https://github.com/emeryberger/Heap-Layers/blob/f71407951a9b68882ad528d6200c2528e39bb31d/wrappers/winwrapper.cpp
This looks like we might be able to use this with snmalloc. I don't have capacity to do this myself, but I am happy to review if someone can get it working.