ned Productions – ptmalloc2

by . Last updated .

ptmalloc2 is one of the fastest dynamic memory allocators for multithreaded systems around and also causes remarkably low memory fragmentation. It isn't my code at all, but in the process of implementing the FXMemoryPool class in TnFOX I backported it from glibc to win32.

Therefore this edition of ptmalloc2 compiles for both Windows and POSIX Unix and to my knowledge is the only version of ptmalloc2 available for Windows.

Note: nedmalloc supersedes ptmalloc2 and is a better memory allocator in almost all scenarios

Doug Lea wrote the original dlmalloc and Wolfram Gloger added the per-thread arenas (renaming it to ptmalloc2) to enable the substantial performance gains under multithreaded conditions. I Niall Douglas merged & substantially improved the Win32 support from dlmalloc back into ptmalloc2. There is a report by Sun comparing dynamic allocators which shows just how good ptmalloc2 is. More information on how dlmalloc & ptmalloc2 work is on Doug Lea's website. If you want more info on all dynamic allocators, this is a good link. Also you may wish to read my notes on dynamic allocators in the TnFOX documentation.

Changes made:

  • Merged some features of the latest dlmalloc with glibc's ptmalloc2, including some assembler optimisations
  • Replaced the win32 mutex emulation code with inlined assembler or compiler intrinsics.
  • Substantially upgraded the win32/64 emulation of POSIX memory allocation functions. In particular much more memory is returned to the system when it's no longer in use.
  • Added the macro PTMALLOC_IN_CPPNAMESPACE which stops the defining of 'extern "C"'. This lets you put ptmalloc2 into a C++ namespace thus avoiding symbol collision at link time (especially when linking against glibc which also contains ptmalloc2). You will probably also want to define the USE_DL_PREFIX macro in real world use.

There are two versions of this allocator - the safe version and the unsafe version. The safe version is literally glibc's from the v2.3.3 release with the Unix memory management emulations patched in. The unsafe (and older) version is my attempt to merge the latest Doug Lea allocator v2.7.2 with ptmalloc2 (based on v2.7.0 of dlmalloc) - I only generated the safe version because I thought a mistake of mine in my merged version was causing a fault when it actually turned out to be something else entirely. TnFOX uses the safe version as of v0.80 onwards as performance will be sufficient.

Warning: When merging the two sets of source for v2.7.2 I may have made mistakes. I'm pretty confident that no memory corruption occurs after extensive testing, however I am less sure that all the memory freeable is actually freed when it should be. The v2.7.0 glibc port has the advantage of having been very heavily tested as the core allocator for Linux.

For the v2.7.2 port, my enhancements are licensed under the same license as Doug Lea's and Wolfram Gloger's ie; you can use this code for any purpose at all, commercial or non-commercial, closed or open source, without accreditation or any restriction of the production of derived works. For the v2.7.0 port is from glibc and so is purely LGPL.

10th February 2005: Released update of v2.7.0 safe fixing an occasional segfault under certain conditions
3rd May 2005: Released update of v2.7.0 safe adding Win64 support

Note that you should use nedmalloc in preference to ptmalloc2

Download v2.7.0 (safe version) of ptmalloc2 for Win32/64 and POSIX Unix (118Kb) Logo

Not recommended but still available if you want it:

Download v2.7.2 (unsafe version) of ptmalloc2 for win32 and POSIX Unix (70Kb)

Contact the webmaster: Niall Douglas @ webmaster2<at symbol> (Last updated: 2004-02-13 00:00:00 +0000 UTC)