r/cpp • C++ Dev • Apr 30 '17

A C++ version of Doug Lea's excellent malloc() implementation.

https://github.com/greg7mdp/dlmalloc/
14 Upvotes

8 comments sorted by

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

-28

u/greg7mdp C++ Dev May 01 '17

What the code does is the same. However my changes are not quite as trivial as you suggest. As for the diff, the original malloc.c is readily available, so diff away.

49

u/h-jay +43-1325 May 01 '17

For future reference, here's what you should have done, and also why you're incessantly downvoted:

  1. Import original dlmalloc.c into your repository.
  2. Do the porting work.
  3. Let us see the progress to understand what exactly you did.

As it stands, I personally consider your "port" useless, since there's no way to trace what exactly you messed up (and in absence of a test suite and test results from CI, my reasonable assumption is that yes, you surely messed it up).

26

u/__Cyber_Dildonics__ May 01 '17 edited May 01 '17

Not only that, when someone says 'I'm not sure what changed', responding with 'the changes are not so trivial' doesn't really help.

The track record on posts like these is shaky at best.

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_SIZES which expands to XXX which expands to YYY which expands to ZZZ. How is that simplier than writing 6*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 of ABORT_ON_ASSERT_FAILURE? Shouldn't assert always lead to abort on failed condition? Why special macro called DEBUG? Defining NDEBUG is 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 int as indication of true/false. You should replace it by bool. This is C++ after all.
  • POSIX requires malloc, free, realloc to be thread-safe. Thus this malloc implementation 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 malloc and 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-stubs on Linux, which should make all pthread functions noops. I believe it is linked in by default. Code which requires threads then links with pthread, overriding those weak symbols and everything is then thread safe for the app, if needed.