r-lyeh-archived / ltalloc

LightweighT Almost Lock-Less Oriented for C++ programs memory allocator
BSD 3-Clause "New" or "Revised" License
164 stars 16 forks source link

p_thread_callback_ltalloc #3

Closed gjaegy closed 6 years ago

gjaegy commented 7 years ago

Hi, I am facing a small issue. I am using ltalloc STL allocation (.hpp), for some reason I need to include ltalloc.cc in different projects.

One of them is build into a .lib (static library), and it's imported/linked with in another project, which include ltalloc.cc as well.

I get the following link error:

libpgmD.lib(ltalloc.obj) : error LNK2005: p_thread_callback_ltalloc already defined in ltalloc.obj

Now, I can solve that by simply commented out the whole block in ltalloc.cc:

`static void NTAPI on_tls_callback(PVOID h, DWORD reason, PVOID pv) { h; pv; if (reason == DLL_THREAD_DETACH) release_thread_cache(0); }

pragma comment(linker, "/INCLUDE:" CODE3264("_","") "p_thread_callback_ltalloc")

pragma const_seg(".CRT$XLL")

extern CPPCODE("C") const PIMAGE_TLS_CALLBACK p_thread_callback_ltalloc = on_tls_callback;

pragma const_seg()`

It seems this code is not really used, any reason why I shouldn't remove it ?

Have I missed anything ?

thanks !

r-lyeh-archived commented 7 years ago

This is a custom (hacky) code from the original code that commands all threads to execute a callback whenever they're created and/or destructed. You should not be altering this as it may have undesirable effects (like memory leaks for example). A likely fix would be creating ltalloc2.cc that uses p_thread_callback_ltalloc2 instead: first lib should link against ltalloc.cc and 2nd one against ltalloc2.cc. Another fix could be switching to DLLs instead (in the first place, having to link against the same lib twice in the very same executable seems odd to me :)

gjaegy commented 7 years ago

Thanks a lot for your answer. Basically I am using OpenPGM, which I slightly modified to rely on ltalloc. This library can only be compiled as static library (otherwise would need modificiation). I am then linking against that OpenPGM static library in another DLL, which itself uses ltalloc as well (and therefore include ltalloc.cc). I haven't found any workaroud.

So, I have now modified the ltalloc.cc used in OpenPGM according to your recommendation and that works :)

Wouldn't it be a better way now to implement TLS using MSVC++ 2017 ? I know TLS is not part of the c++ standard (not sure which revision TBH)..

thanks a lot in any case !!

r-lyeh commented 6 years ago

Hye @gjaegy . I dont think there is still a better choice for tls callbacks right now (as of Jul 2018). I might re-open the issue if I find anything relevant though :) thanks for the feedback :)