• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

sysdeps/H25-Jul-2004-627363

COPYRIGHTH A D01-Jan-2006976 2016

ChangeLogH A D05-Jun-20066 KiB187128

MakefileH A D03-May-20226.8 KiB222142

READMEH A D06-Nov-20047.3 KiB193140

arena.cH A D05-Nov-200421.2 KiB801570

hooks.cH A D05-Nov-200418.3 KiB641489

lran2.hH A D05-Dec-1996837 5237

malloc-stats.cH A D08-Aug-20045.1 KiB16289

malloc.cH A D04-Nov-2004167.9 KiB5,4402,549

malloc.hH A D08-Aug-20049.8 KiB292159

t-test.hH A D06-Nov-20042.7 KiB144109

t-test1.cH A D04-Nov-20045.7 KiB286235

t-test2.cH A D04-Nov-20044.6 KiB232185

tst-mallocstate.cH A D18-Jan-20021.9 KiB8346

tst-mstats.cH A D08-Aug-20042.7 KiB10163

README

1ptmalloc2 - a multi-thread malloc implementation
2================================================
3
4Wolfram Gloger (wg@malloc.de)
5
6Nov 2004
7
8
9Introduction
10============
11
12This package is a modified version of Doug Lea's malloc-2.7.1pre
13implementation (available seperately from ftp://g.oswego.edu/pub/misc)
14that I adapted for multiple threads, while trying to avoid lock
15contention as much as possible.  Many thanks should go to Doug Lea
16(dl@cs.oswego.edu) for the great original malloc implementation.
17
18As part of the GNU C library, the source files are available under the
19GNU Library General Public License (see the comments in the files).
20But as part of this stand-alone package, the code is also available
21under the (probably less restrictive) conditions described in the file
22'COPYRIGHT'.  In any case, there is no warranty whatsoever for this
23package.
24
25The current distribution should be available from:
26
27http://www.malloc.de/malloc/ptmalloc2.tar.gz
28
29
30Compilation
31===========
32
33It should be possible to build ptmalloc2 on any UN*X-like system that
34implements the sbrk(), mmap(), munmap() and mprotect() calls.  If
35mmap() is not available, it is only possible to produce a
36non-threadsafe implementation.  Since there are now several source
37files, a library (libmalloc.a) is generated.  See the Makefile for
38examples of the compile-time options.
39
40Note that support for non-ANSI compilers is no longer a significant
41goal.
42
43Several example targets are provided in the Makefile:
44
45 o Posix threads (pthreads), compile with "make posix"
46
47 o Posix threads with explicit initialization, compile with
48   "make posix-explicit" (known to be required on HPUX)
49
50 o Posix threads without "tsd data hack" (see below), compile with
51   "make posix-with-tsd"
52
53 o Solaris threads, compile with "make solaris"
54
55 o SGI sproc() threads, compile with "make sproc"
56
57 o no threads, compile with "make nothreads"
58
59For Linux:
60
61 o make "linux-pthread" (almost the same as "make posix")
62
63Note that some compilers need special flags for multi-threaded code,
64e.g. with Solaris cc with Posix threads, one should use:
65
66% make posix SYS_FLAGS='-mt'
67
68Some additional targets, ending in `-libc', are also provided in the
69Makefile, to compare performance of the test programs to the case when
70linking with the standard malloc implementation in libc.
71
72A potential problem remains: If any of the system-specific functions
73for getting/setting thread-specific data or for locking a mutex call
74one of the malloc-related functions internally, the implementation
75cannot work at all due to infinite recursion.  One example seems to be
76Solaris 2.4.  I would like to hear if this problem occurs on other
77systems, and whether similar workarounds could be applied.
78
79For Posix threads, too, an optional hack like that has been integrated
80(activated when defining USE_TSD_DATA_HACK) which depends on
81`pthread_t' being convertible to an integral type (which is of course
82not generally guaranteed).  USE_TSD_DATA_HACK is now the default
83because I haven't yet found a non-glibc pthreads system where this
84hack is _not_ needed.
85
86*NEW* and _important_: In (currently) one place in the ptmalloc2
87source, a write memory barrier is needed, named
88atomic_write_barrier().  This macro needs to be defined at the end of
89malloc-machine.h.  For gcc, a fallback in the form of a full memory
90barrier is already defined, but you may need to add another definition
91if you don't use gcc.
92
93Usage
94=====
95
96Just link libmalloc.a into your application.
97
98Some wicked systems (e.g. HPUX apparently) won't let malloc call _any_
99thread-related functions before main().  On these systems,
100USE_STARTER=2 must be defined during compilation (see "make
101posix-explicit" above) and the global initialization function
102ptmalloc_init() must be called explitly, preferably at the start of
103main().
104
105Otherwise, when using ptmalloc2, no special precautions are necessary.
106
107Link order is important
108=======================
109
110On some systems, when overriding malloc and linking against shared
111libraries, the link order becomes very important.  E.g., when linking
112C++ programs on Solaris, don't rely on libC being included by default,
113but instead put `-lthread' behind `-lC' on the command line:
114
115  CC ... libmalloc.a -lC -lthread
116
117This is because there are global constructors in libC that need
118malloc/ptmalloc, which in turn needs to have the thread library to be
119already initialized.
120
121Debugging hooks
122===============
123
124All calls to malloc(), realloc(), free() and memalign() are routed
125through the global function pointers __malloc_hook, __realloc_hook,
126__free_hook and __memalign_hook if they are not NULL (see the malloc.h
127header file for declarations of these pointers).  Therefore the malloc
128implementation can be changed at runtime, if care is taken not to call
129free() or realloc() on pointers obtained with a different
130implementation than the one currently in effect.  (The easiest way to
131guarantee this is to set up the hooks before any malloc call, e.g.
132with a function pointed to by the global variable
133__malloc_initialize_hook).
134
135A useful application of the hooks is built-in into ptmalloc2: The
136implementation is usually very unforgiving with respect to misuse,
137such as free()ing a pointer twice or free()ing a pointer not obtained
138with malloc() (these will typically crash the application
139immediately).  To debug in such situations, you can set the
140environment variable `MALLOC_CHECK_' (note the trailing underscore).
141Performance will suffer somewhat, but you will get more controlled
142behaviour in the case of misuse.  If MALLOC_CHECK_=0, wrong free()s
143will be silently ignored, if MALLOC_CHECK_=1, diagnostics will be
144printed on stderr, and if MALLOC_CHECK_=2, abort() will be called on
145any error.
146
147You can now also tune other malloc parameters (normally adjused via
148mallopt() calls from the application) with environment variables:
149
150    MALLOC_TRIM_THRESHOLD_    for deciding to shrink the heap (in bytes)
151
152    MALLOC_TOP_PAD_           how much extra memory to allocate on
153                              each system call (in bytes)
154
155    MALLOC_MMAP_THRESHOLD_    min. size for chunks allocated via
156                              mmap() (in bytes)
157
158    MALLOC_MMAP_MAX_          max. number of mmapped regions to use
159
160Tests
161=====
162
163Two testing applications, t-test1 and t-test2, are included in this
164source distribution.  Both perform pseudo-random sequences of
165allocations/frees, and can be given numeric arguments (all arguments
166are optional):
167
168% t-test[12] <n-total> <n-parallel> <n-allocs> <size-max> <bins>
169
170    n-total = total number of threads executed (default 10)
171    n-parallel = number of threads running in parallel (2)
172    n-allocs = number of malloc()'s / free()'s per thread (10000)
173    size-max = max. size requested with malloc() in bytes (10000)
174    bins = number of bins to maintain
175
176The first test `t-test1' maintains a completely seperate pool of
177allocated bins for each thread, and should therefore show full
178parallelism.  On the other hand, `t-test2' creates only a single pool
179of bins, and each thread randomly allocates/frees any bin.  Some lock
180contention is to be expected in this case, as the threads frequently
181cross each others arena.
182
183Performance results from t-test1 should be quite repeatable, while the
184behaviour of t-test2 depends on scheduling variations.
185
186Conclusion
187==========
188
189I'm always interested in performance data and feedback, just send mail
190to ptmalloc@malloc.de.
191
192Good luck!
193