snmalloc icon indicating copy to clipboard operation
snmalloc copied to clipboard

sn_malloc not called?

Open zhmt opened this issue 2 years ago • 12 comments

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)

zhmt avatar Mar 17 '22 03:03 zhmt

Maybe a detailed starter tutorial, or an example will help.

I also tried "Using snmalloc as header-only library", no luck either.

zhmt avatar Mar 17 '22 03:03 zhmt

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.

davidchisnall avatar Mar 17 '22 09:03 davidchisnall

@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.

mjp41 avatar Mar 17 '22 10:03 mjp41

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.

I am trying snmalloc on windows. It is a little bit tricky on windows.

zhmt avatar Mar 17 '22 11:03 zhmt

-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.

zhmt avatar Mar 17 '22 12:03 zhmt

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.

zhmt avatar Mar 17 '22 12:03 zhmt

I dump the snmallocshim-static.lib, function malloc is not exported. Is this the root cause?

zhmt avatar Mar 17 '22 12:03 zhmt

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

zhmt avatar Mar 17 '22 12:03 zhmt

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.

davidchisnall avatar Mar 17 '22 13:03 davidchisnall

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.

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.

zhmt avatar Mar 17 '22 13:03 zhmt

Thanks for you guys' help.

zhmt avatar Mar 17 '22 13:03 zhmt

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.

mjp41 avatar Mar 20 '22 19:03 mjp41