r/cpp • u/greg7mdp C++ Dev • Apr 30 '17
A C++ version of Doug Lea's excellent malloc() implementation.
https://github.com/greg7mdp/dlmalloc/21
u/Jardik2 May 01 '17
I don't like the code. Reasons are:
The code still contains too much macros. Many times they are not needed. Having a constant
SIX_SIZE_T_SIZESwhich expands toXXXwhich expands toYYYwhich expands toZZZ. How is that simplier than writing6*sizeof(std::size_t), which will be equaly optimized at compile time? Some other macros are just there to test outdated and/or not standards conforming platforms, you should consider droping their support to simplify the code. What is the point ofABORT_ON_ASSERT_FAILURE? Shouldn't assert always lead to abort on failed condition? Why special macro calledDEBUG? DefiningNDEBUGis standard way used to disable assertions and they should be enabled otherwise.Lot of functions are just calculating something and then checking something by asserts. This can lead to unused variables warnings in non-DEBUG builds.
Custom locking functions could be replaced by C++11 stuff, removing more code.
Some functions use
intas indication oftrue/false. You should replace it bybool. This is C++ after all.POSIX requires
malloc,free,reallocto be thread-safe. Thus thismallocimplementation should be always thread-safe for those platforms without having an option to turn this off, as it would just cause issues as libraries rely on this fact and some could have threads invisible to users (such as glibc uses "hidden" threads to do async io on linux).
1
u/greg7mdp C++ Dev May 01 '17
Thank you, those are some valid points. I have defined USE_LOCKS to 1 by default, but left the option to set it to 0. This can be used also as a private allocator (see USE_SPACES) and in that case it could be useful to disable locking. Also I have removed many of the SIZE_T macros.
3
u/SeanMiddleditch May 01 '17
POSIX requires malloc, free, realloc to be thread-safe
Which is only relevant if one is actually using threads. Plenty of apps that operate in a more micro-service-like fashion don't use threads at all and benefit from disabling unused thread-safe features. Especially in this sort of allocator that relies on global locks for its thread-safety, rather than per-thread arenas/caches. Replacing the default
mallocand co with a single-threaded version is a common optimization for those classes of application.Other than that, though, I agree. We have a heavily-modified C++-ified copy of dlmalloc in our engine and those sorts of cleanups are exactly the kind of thing that transforms the allocator into something the average engineer can reasonably debug and improve. :)
1
u/Jardik2 May 02 '17
I can agree with this. One just need to be careful what other libraries you use, if they don't actually require thread safe malloc (as mentioned earlier, because of internal threads, that could allocate). Just for curiosity, there is
pthread-stubson Linux, which should make all pthread functions noops. I believe it is linked in by default. Code which requires threads then links withpthread, overriding those weak symbols and everything is then thread safe for the app, if needed.
23
u/ReDucTor Game Developer | quiz.cpp-perf.com May 01 '17
How does this differ from the C version? Do you have a diff?
It appears to be just copied and renamed to .cc then move a bunch of functions inside malloc_state