Skip to content
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

sn_malloc not called? #480

Open
zhmt opened this issue Mar 17, 2022 · 12 comments
Open

sn_malloc not called? #480

zhmt opened this issue Mar 17, 2022 · 12 comments

Comments

@zhmt
Copy link

zhmt commented Mar 17, 2022

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
Copy link
Author

zhmt commented Mar 17, 2022

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

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

@davidchisnall
Copy link
Collaborator

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.

@mjp41
Copy link
Member

mjp41 commented Mar 17, 2022

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

@zhmt
Copy link
Author

zhmt commented Mar 17, 2022

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
Copy link
Author

zhmt commented Mar 17, 2022

-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
Copy link
Author

zhmt commented Mar 17, 2022

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
Copy link
Author

zhmt commented Mar 17, 2022

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

@zhmt
Copy link
Author

zhmt commented Mar 17, 2022

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

@davidchisnall
Copy link
Collaborator

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.

@zhmt
Copy link
Author

zhmt commented Mar 17, 2022

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
Copy link
Author

zhmt commented Mar 17, 2022

Thanks for you guys' help.

@mjp41
Copy link
Member

mjp41 commented Mar 20, 2022

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.

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

No branches or pull requests

3 participants