From: Eric Wong <e@80x24.org>
To: misc@80x24.org
Subject: dlmalloc has mspace support
Date: Mon, 21 Jul 2014 02:35:59 +0000
Message-ID: <20140721023559.GA20717@dcvr.yhbt.net> (raw)
Apparently newish versions of the venerable dlmalloc has an mspace API.
This could prove promising for some servers which dynamically
spawn/destroy independent applications and would benefit from different
arenas.
Quoted from ftp://gee.cs.oswego.edu/pub/misc/malloc.c
-----------------------------------------------------------------------
* MSPACES
If MSPACES is defined, then in addition to malloc, free, etc.,
this file also defines mspace_malloc, mspace_free, etc. These
are versions of malloc routines that take an "mspace" argument
obtained using create_mspace, to control all internal bookkeeping.
If ONLY_MSPACES is defined, only these versions are compiled.
So if you would like to use this allocator for only some allocations,
and your system malloc for others, you can compile with
ONLY_MSPACES and then do something like...
static mspace mymspace = create_mspace(0,0); // for example
#define mymalloc(bytes) mspace_malloc(mymspace, bytes)
(Note: If you only need one instance of an mspace, you can instead
use "USE_DL_PREFIX" to relabel the global malloc.)
You can similarly create thread-local allocators by storing
mspaces as thread-locals. For example:
static __thread mspace tlms = 0;
void* tlmalloc(size_t bytes) {
if (tlms == 0) tlms = create_mspace(0, 0);
return mspace_malloc(tlms, bytes);
}
void tlfree(void* mem) { mspace_free(tlms, mem); }
Unless FOOTERS is defined, each mspace is completely independent.
You cannot allocate from one and free to another (although
conformance is only weakly checked, so usage errors are not always
caught). If FOOTERS is defined, then each chunk carries around a tag
indicating its originating mspace, and frees are directed to their
originating spaces. Normally, this requires use of locks.
-----------------------------------------------------------------------