OpenBarnyard
 
Loading...
Searching...
No Matches
dlmalloc.h
Go to the documentation of this file.
1/*
2 This is a version (aka dlmalloc) of malloc/free/realloc written by
3 Doug Lea and released to the public domain, as explained at
4 http://creativecommons.org/publicdomain/zero/1.0/ Send questions,
5 comments, complaints, performance data, etc to dl@cs.oswego.edu
6
7* Version 2.8.6 Wed Aug 29 06:57:58 2012 Doug Lea
8 Note: There may be an updated version of this malloc obtainable at
9 ftp://gee.cs.oswego.edu/pub/misc/malloc.c
10 Check before installing!
11
12* Quickstart
13
14 This library is all in one file to simplify the most common usage:
15 ftp it, compile it (-O3), and link it into another program. All of
16 the compile-time options default to reasonable values for use on
17 most platforms. You might later want to step through various
18 compile-time and dynamic tuning options.
19
20 For convenience, an include file for code using this malloc is at:
21 ftp://gee.cs.oswego.edu/pub/misc/malloc-2.8.6.h
22 You don't really need this .h file unless you call functions not
23 defined in your system include files. The .h file contains only the
24 excerpts from this file needed for using this malloc on ANSI C/C++
25 systems, so long as you haven't changed compile-time options about
26 naming and tuning parameters. If you do, then you can create your
27 own malloc.h that does include all settings by cutting at the point
28 indicated below. Note that you may already by default be using a C
29 library containing a malloc that is based on some version of this
30 malloc (for example in linux). You might still want to use the one
31 in this file to customize settings or to avoid overheads associated
32 with library versions.
33
34* Vital statistics:
35
36 Supported pointer/size_t representation: 4 or 8 bytes
37 size_t MUST be an unsigned type of the same width as
38 pointers. (If you are using an ancient system that declares
39 size_t as a signed type, or need it to be a different width
40 than pointers, you can use a previous release of this malloc
41 (e.g. 2.7.2) supporting these.)
42
43 Alignment: 8 bytes (minimum)
44 This suffices for nearly all current machines and C compilers.
45 However, you can define MALLOC_ALIGNMENT to be wider than this
46 if necessary (up to 128bytes), at the expense of using more space.
47
48 Minimum overhead per allocated chunk: 4 or 8 bytes (if 4byte sizes)
49 8 or 16 bytes (if 8byte sizes)
50 Each malloced chunk has a hidden word of overhead holding size
51 and status information, and additional cross-check word
52 if FOOTERS is defined.
53
54 Minimum allocated size: 4-byte ptrs: 16 bytes (including overhead)
55 8-byte ptrs: 32 bytes (including overhead)
56
57 Even a request for zero bytes (i.e., malloc(0)) returns a
58 pointer to something of the minimum allocatable size.
59 The maximum overhead wastage (i.e., number of extra bytes
60 allocated than were requested in malloc) is less than or equal
61 to the minimum size, except for requests >= mmap_threshold that
62 are serviced via mmap(), where the worst case wastage is about
63 32 bytes plus the remainder from a system page (the minimal
64 mmap unit); typically 4096 or 8192 bytes.
65
66 Security: static-safe; optionally more or less
67 The "security" of malloc refers to the ability of malicious
68 code to accentuate the effects of errors (for example, freeing
69 space that is not currently malloc'ed or overwriting past the
70 ends of chunks) in code that calls malloc. This malloc
71 guarantees not to modify any memory locations below the base of
72 heap, i.e., static variables, even in the presence of usage
73 errors. The routines additionally detect most improper frees
74 and reallocs. All this holds as long as the static bookkeeping
75 for malloc itself is not corrupted by some other means. This
76 is only one aspect of security -- these checks do not, and
77 cannot, detect all possible programming errors.
78
79 If FOOTERS is defined nonzero, then each allocated chunk
80 carries an additional check word to verify that it was malloced
81 from its space. These check words are the same within each
82 execution of a program using malloc, but differ across
83 executions, so externally crafted fake chunks cannot be
84 freed. This improves security by rejecting frees/reallocs that
85 could corrupt heap memory, in addition to the checks preventing
86 writes to statics that are always on. This may further improve
87 security at the expense of time and space overhead. (Note that
88 FOOTERS may also be worth using with MSPACES.)
89
90 By default detected errors cause the program to abort (calling
91 "abort()"). You can override this to instead proceed past
92 errors by defining PROCEED_ON_ERROR. In this case, a bad free
93 has no effect, and a malloc that encounters a bad address
94 caused by user overwrites will ignore the bad address by
95 dropping pointers and indices to all known memory. This may
96 be appropriate for programs that should continue if at all
97 possible in the face of programming errors, although they may
98 run out of memory because dropped memory is never reclaimed.
99
100 If you don't like either of these options, you can define
101 CORRUPTION_ERROR_ACTION and USAGE_ERROR_ACTION to do anything
102 else. And if if you are sure that your program using malloc has
103 no errors or vulnerabilities, you can define INSECURE to 1,
104 which might (or might not) provide a small performance improvement.
105
106 It is also possible to limit the maximum total allocatable
107 space, using malloc_set_footprint_limit. This is not
108 designed as a security feature in itself (calls to set limits
109 are not screened or privileged), but may be useful as one
110 aspect of a secure implementation.
111
112 Thread-safety: NOT thread-safe unless USE_LOCKS defined non-zero
113 When USE_LOCKS is defined, each public call to malloc, free,
114 etc is surrounded with a lock. By default, this uses a plain
115 pthread mutex, win32 critical section, or a spin-lock if if
116 available for the platform and not disabled by setting
117 USE_SPIN_LOCKS=0. However, if USE_RECURSIVE_LOCKS is defined,
118 recursive versions are used instead (which are not required for
119 base functionality but may be needed in layered extensions).
120 Using a global lock is not especially fast, and can be a major
121 bottleneck. It is designed only to provide minimal protection
122 in concurrent environments, and to provide a basis for
123 extensions. If you are using malloc in a concurrent program,
124 consider instead using nedmalloc
125 (http://www.nedprod.com/programs/portable/nedmalloc/) or
126 ptmalloc (See http://www.malloc.de), which are derived from
127 versions of this malloc.
128
129 System requirements: Any combination of MORECORE and/or MMAP/MUNMAP
130 This malloc can use unix sbrk or any emulation (invoked using
131 the CALL_MORECORE macro) and/or mmap/munmap or any emulation
132 (invoked using CALL_MMAP/CALL_MUNMAP) to get and release system
133 memory. On most unix systems, it tends to work best if both
134 MORECORE and MMAP are enabled. On Win32, it uses emulations
135 based on VirtualAlloc. It also uses common C library functions
136 like memset.
137
138 Compliance: I believe it is compliant with the Single Unix Specification
139 (See http://www.unix.org). Also SVID/XPG, ANSI C, and probably
140 others as well.
141
142* Overview of algorithms
143
144 This is not the fastest, most space-conserving, most portable, or
145 most tunable malloc ever written. However it is among the fastest
146 while also being among the most space-conserving, portable and
147 tunable. Consistent balance across these factors results in a good
148 general-purpose allocator for malloc-intensive programs.
149
150 In most ways, this malloc is a best-fit allocator. Generally, it
151 chooses the best-fitting existing chunk for a request, with ties
152 broken in approximately least-recently-used order. (This strategy
153 normally maintains low fragmentation.) However, for requests less
154 than 256bytes, it deviates from best-fit when there is not an
155 exactly fitting available chunk by preferring to use space adjacent
156 to that used for the previous small request, as well as by breaking
157 ties in approximately most-recently-used order. (These enhance
158 locality of series of small allocations.) And for very large requests
159 (>= 256Kb by default), it relies on system memory mapping
160 facilities, if supported. (This helps avoid carrying around and
161 possibly fragmenting memory used only for large chunks.)
162
163 All operations (except malloc_stats and mallinfo) have execution
164 times that are bounded by a constant factor of the number of bits in
165 a size_t, not counting any clearing in calloc or copying in realloc,
166 or actions surrounding MORECORE and MMAP that have times
167 proportional to the number of non-contiguous regions returned by
168 system allocation routines, which is often just 1. In real-time
169 applications, you can optionally suppress segment traversals using
170 NO_SEGMENT_TRAVERSAL, which assures bounded execution even when
171 system allocators return non-contiguous spaces, at the typical
172 expense of carrying around more memory and increased fragmentation.
173
174 The implementation is not very modular and seriously overuses
175 macros. Perhaps someday all C compilers will do as good a job
176 inlining modular code as can now be done by brute-force expansion,
177 but now, enough of them seem not to.
178
179 Some compilers issue a lot of warnings about code that is
180 dead/unreachable only on some platforms, and also about intentional
181 uses of negation on unsigned types. All known cases of each can be
182 ignored.
183
184 For a longer but out of date high-level description, see
185 http://gee.cs.oswego.edu/dl/html/malloc.html
186
187* MSPACES
188 If MSPACES is defined, then in addition to malloc, free, etc.,
189 this file also defines mspace_malloc, mspace_free, etc. These
190 are versions of malloc routines that take an "mspace" argument
191 obtained using create_mspace, to control all internal bookkeeping.
192 If ONLY_MSPACES is defined, only these versions are compiled.
193 So if you would like to use this allocator for only some allocations,
194 and your system malloc for others, you can compile with
195 ONLY_MSPACES and then do something like...
196 static mspace mymspace = create_mspace(0,0); // for example
197 #define mymalloc(bytes) mspace_malloc(mymspace, bytes)
198
199 (Note: If you only need one instance of an mspace, you can instead
200 use "USE_DL_PREFIX" to relabel the global malloc.)
201
202 You can similarly create thread-local allocators by storing
203 mspaces as thread-locals. For example:
204 static __thread mspace tlms = 0;
205 void* tlmalloc(size_t bytes) {
206 if (tlms == 0) tlms = create_mspace(0, 0);
207 return mspace_malloc(tlms, bytes);
208 }
209 void tlfree(void* mem) { mspace_free(tlms, mem); }
210
211 Unless FOOTERS is defined, each mspace is completely independent.
212 You cannot allocate from one and free to another (although
213 conformance is only weakly checked, so usage errors are not always
214 caught). If FOOTERS is defined, then each chunk carries around a tag
215 indicating its originating mspace, and frees are directed to their
216 originating spaces. Normally, this requires use of locks.
217
218 ------------------------- Compile-time options ---------------------------
219
220Be careful in setting #define values for numerical constants of type
221size_t. On some systems, literal values are not automatically extended
222to size_t precision unless they are explicitly casted. You can also
223use the symbolic values MAX_SIZE_T, SIZE_T_ONE, etc below.
224
225WIN32 default: defined if _WIN32 defined
226 Defining WIN32 sets up defaults for MS environment and compilers.
227 Otherwise defaults are for unix. Beware that there seem to be some
228 cases where this malloc might not be a pure drop-in replacement for
229 Win32 malloc: Random-looking failures from Win32 GDI API's (eg;
230 SetDIBits()) may be due to bugs in some video driver implementations
231 when pixel buffers are malloc()ed, and the region spans more than
232 one VirtualAlloc()ed region. Because dlmalloc uses a small (64Kb)
233 default granularity, pixel buffers may straddle virtual allocation
234 regions more often than when using the Microsoft allocator. You can
235 avoid this by using VirtualAlloc() and VirtualFree() for all pixel
236 buffers rather than using malloc(). If this is not possible,
237 recompile this malloc with a larger DEFAULT_GRANULARITY. Note:
238 in cases where MSC and gcc (cygwin) are known to differ on WIN32,
239 conditions use _MSC_VER to distinguish them.
240
241DLMALLOC_EXPORT default: extern
242 Defines how public APIs are declared. If you want to export via a
243 Windows DLL, you might define this as
244 #define DLMALLOC_EXPORT extern __declspec(dllexport)
245 If you want a POSIX ELF shared object, you might use
246 #define DLMALLOC_EXPORT extern __attribute__((visibility("default")))
247
248MALLOC_ALIGNMENT default: (size_t)(2 * sizeof(void *))
249 Controls the minimum alignment for malloc'ed chunks. It must be a
250 power of two and at least 8, even on machines for which smaller
251 alignments would suffice. It may be defined as larger than this
252 though. Note however that code and data structures are optimized for
253 the case of 8-byte alignment.
254
255MSPACES default: 0 (TFALSE)
256 If TTRUE, compile in support for independent allocation spaces.
257 This is only supported if HAVE_MMAP is TTRUE.
258
259ONLY_MSPACES default: 0 (TFALSE)
260 If TTRUE, only compile in mspace versions, not regular versions.
261
262USE_LOCKS default: 0 (TFALSE)
263 Causes each call to each public routine to be surrounded with
264 pthread or WIN32 mutex lock/unlock. (If set TTRUE, this can be
265 overridden on a per-mspace basis for mspace versions.) If set to a
266 non-zero value other than 1, locks are used, but their
267 implementation is left out, so lock functions must be supplied manually,
268 as described below.
269
270USE_SPIN_LOCKS default: 1 iff USE_LOCKS and spin locks available
271 If TTRUE, uses custom spin locks for locking. This is currently
272 supported only gcc >= 4.1, older gccs on x86 platforms, and recent
273 MS compilers. Otherwise, posix locks or win32 critical sections are
274 used.
275
276USE_RECURSIVE_LOCKS default: not defined
277 If defined nonzero, uses recursive (aka reentrant) locks, otherwise
278 uses plain mutexes. This is not required for malloc proper, but may
279 be needed for layered allocators such as nedmalloc.
280
281LOCK_AT_FORK default: not defined
282 If defined nonzero, performs pthread_atfork upon initialization
283 to initialize child lock while holding parent lock. The implementation
284 assumes that pthread locks (not custom locks) are being used. In other
285 cases, you may need to customize the implementation.
286
287FOOTERS default: 0
288 If TTRUE, provide extra checking and dispatching by placing
289 information in the footers of allocated chunks. This adds
290 space and time overhead.
291
292INSECURE default: 0
293 If TTRUE, omit checks for usage errors and heap space overwrites.
294
295USE_DL_PREFIX default: NOT defined
296 Causes compiler to prefix all public routines with the string 'dl'.
297 This can be useful when you only want to use this malloc in one part
298 of a program, using your regular system malloc elsewhere.
299
300MALLOC_INSPECT_ALL default: NOT defined
301 If defined, compiles malloc_inspect_all and mspace_inspect_all, that
302 perform traversal of all heap space. Unless access to these
303 functions is otherwise restricted, you probably do not want to
304 include them in secure implementations.
305
306ABORT default: defined as abort()
307 Defines how to abort on failed checks. On most systems, a failed
308 check cannot die with an "assert" or even print an informative
309 message, because the underlying print routines in turn call malloc,
310 which will fail again. Generally, the best policy is to simply call
311 abort(). It's not very useful to do more than this because many
312 errors due to overwriting will show up as address faults (null, odd
313 addresses etc) rather than malloc-triggered checks, so will also
314 abort. Also, most compilers know that abort() does not return, so
315 can better optimize code conditionally calling it.
316
317PROCEED_ON_ERROR default: defined as 0 (TFALSE)
318 Controls whether detected bad addresses cause them to bypassed
319 rather than aborting. If set, detected bad arguments to free and
320 realloc are ignored. And all bookkeeping information is zeroed out
321 upon a detected overwrite of freed heap space, thus losing the
322 ability to ever return it from malloc again, but enabling the
323 application to proceed. If PROCEED_ON_ERROR is defined, the
324 static variable malloc_corruption_error_count is compiled in
325 and can be examined to see if errors have occurred. This option
326 generates slower code than the default abort policy.
327
328DEBUG default: NOT defined
329 The DEBUG setting is mainly intended for people trying to modify
330 this code or diagnose problems when porting to new platforms.
331 However, it may also be able to better isolate user errors than just
332 using runtime checks. The assertions in the check routines spell
333 out in more detail the assumptions and invariants underlying the
334 algorithms. The checking is fairly extensive, and will slow down
335 execution noticeably. Calling malloc_stats or mallinfo with DEBUG
336 set will attempt to check every non-mmapped allocated and free chunk
337 in the course of computing the summaries.
338
339ABORT_ON_ASSERT_FAILURE default: defined as 1 (TTRUE)
340 Debugging assertion failures can be nearly impossible if your
341 version of the assert macro causes malloc to be called, which will
342 lead to a cascade of further failures, blowing the runtime stack.
343 ABORT_ON_ASSERT_FAILURE cause assertions failures to call abort(),
344 which will usually make debugging easier.
345
346MALLOC_FAILURE_ACTION default: sets errno to ENOMEM, or no-op on win32
347 The action to take before "return 0" when malloc fails to be able to
348 return memory because there is none available.
349
350HAVE_MORECORE default: 1 (TTRUE) unless win32 or ONLY_MSPACES
351 True if this system supports sbrk or an emulation of it.
352
353MORECORE default: sbrk
354 The name of the sbrk-style system routine to call to obtain more
355 memory. See below for guidance on writing custom MORECORE
356 functions. The type of the argument to sbrk/MORECORE varies across
357 systems. It cannot be size_t, because it supports negative
358 arguments, so it is normally the signed type of the same width as
359 size_t (sometimes declared as "intptr_t"). It doesn't much matter
360 though. Internally, we only call it with arguments less than half
361 the max value of a size_t, which should work across all reasonable
362 possibilities, although sometimes generating compiler warnings.
363
364MORECORE_CONTIGUOUS default: 1 (TTRUE) if HAVE_MORECORE
365 If TTRUE, take advantage of fact that consecutive calls to MORECORE
366 with positive arguments always return contiguous increasing
367 addresses. This is TTRUE of unix sbrk. It does not hurt too much to
368 set it TTRUE anyway, since malloc copes with non-contiguities.
369 Setting it TFALSE when definitely non-contiguous saves time
370 and possibly wasted space it would take to discover this though.
371
372MORECORE_CANNOT_TRIM default: NOT defined
373 True if MORECORE cannot release space back to the system when given
374 negative arguments. This is generally necessary only if you are
375 using a hand-crafted MORECORE function that cannot handle negative
376 arguments.
377
378NO_SEGMENT_TRAVERSAL default: 0
379 If non-zero, suppresses traversals of memory segments
380 returned by either MORECORE or CALL_MMAP. This disables
381 merging of segments that are contiguous, and selectively
382 releasing them to the OS if unused, but bounds execution times.
383
384HAVE_MMAP default: 1 (TTRUE)
385 True if this system supports mmap or an emulation of it. If so, and
386 HAVE_MORECORE is not TTRUE, MMAP is used for all system
387 allocation. If set and HAVE_MORECORE is TTRUE as well, MMAP is
388 primarily used to directly allocate very large blocks. It is also
389 used as a backup strategy in cases where MORECORE fails to provide
390 space from system. Note: A single call to MUNMAP is assumed to be
391 able to unmap memory that may have be allocated using multiple calls
392 to MMAP, so long as they are adjacent.
393
394HAVE_MREMAP default: 1 on linux, else 0
395 If TTRUE realloc() uses mremap() to re-allocate large blocks and
396 extend or shrink allocation spaces.
397
398MMAP_CLEARS default: 1 except on WINCE.
399 True if mmap clears memory so calloc doesn't need to. This is TTRUE
400 for standard unix mmap using /dev/zero and on WIN32 except for WINCE.
401
402USE_BUILTIN_FFS default: 0 (i.e., not used)
403 Causes malloc to use the builtin ffs() function to compute indices.
404 Some compilers may recognize and intrinsify ffs to be faster than the
405 supplied C version. Also, the case of x86 using gcc is special-cased
406 to an asm instruction, so is already as fast as it can be, and so
407 this setting has no effect. Similarly for Win32 under recent MS compilers.
408 (On most x86s, the asm version is only slightly faster than the C version.)
409
410malloc_getpagesize default: derive from system includes, or 4096.
411 The system page size. To the extent possible, this malloc manages
412 memory from the system in page-size units. This may be (and
413 usually is) a function rather than a constant. This is ignored
414 if WIN32, where page size is determined using getSystemInfo during
415 initialization.
416
417NO_MALLINFO default: 0
418 If defined, don't compile "mallinfo". This can be a simple way
419 of dealing with mismatches between system declarations and
420 those in this file.
421
422MALLINFO_FIELD_TYPE default: size_t
423 The type of the fields in the mallinfo struct. This was originally
424 defined as "int" in SVID etc, but is more usefully defined as
425 size_t. The value is used only if HAVE_USR_INCLUDE_MALLOC_H is not set
426
427NO_MALLOC_STATS default: 0
428 If defined, don't compile "malloc_stats". This avoids calls to
429 fprintf and bringing in stdio dependencies you might not want.
430
431REALLOC_ZERO_BYTES_FREES default: not defined
432 This should be set if a call to realloc with zero bytes should
433 be the same as a call to free. Some people think it should. Otherwise,
434 since this malloc returns a unique pointer for malloc(0), so does
435 realloc(p, 0).
436
437LACKS_UNISTD_H, LACKS_FCNTL_H, LACKS_SYS_PARAM_H, LACKS_SYS_MMAN_H
438LACKS_STRINGS_H, LACKS_STRING_H, LACKS_SYS_TYPES_H, LACKS_ERRNO_H
439LACKS_STDLIB_H LACKS_SCHED_H LACKS_TIME_H default: NOT defined unless on WIN32
440 Define these if your system does not have these header files.
441 You might need to manually insert some of the declarations they provide.
442
443DEFAULT_GRANULARITY default: page size if MORECORE_CONTIGUOUS,
444 system_info.dwAllocationGranularity in WIN32,
445 otherwise 64K.
446 Also settable using mallopt(M_GRANULARITY, x)
447 The unit for allocating and deallocating memory from the system. On
448 most systems with contiguous MORECORE, there is no reason to
449 make this more than a page. However, systems with MMAP tend to
450 either require or encourage larger granularities. You can increase
451 this value to prevent system allocation functions to be called so
452 often, especially if they are slow. The value must be at least one
453 page and must be a power of two. Setting to 0 causes initialization
454 to either page size or win32 region size. (Note: In previous
455 versions of malloc, the equivalent of this option was called
456 "TOP_PAD")
457
458DEFAULT_TRIM_THRESHOLD default: 2MB
459 Also settable using mallopt(M_TRIM_THRESHOLD, x)
460 The maximum amount of unused top-most memory to keep before
461 releasing via malloc_trim in free(). Automatic trimming is mainly
462 useful in long-lived programs using contiguous MORECORE. Because
463 trimming via sbrk can be slow on some systems, and can sometimes be
464 wasteful (in cases where programs immediately afterward allocate
465 more large chunks) the value should be high enough so that your
466 overall system performance would improve by releasing this much
467 memory. As a rough guide, you might set to a value close to the
468 average size of a process (program) running on your system.
469 Releasing this much memory would allow such a process to run in
470 memory. Generally, it is worth tuning trim thresholds when a
471 program undergoes phases where several large chunks are allocated
472 and released in ways that can reuse each other's storage, perhaps
473 mixed with phases where there are no such chunks at all. The trim
474 value must be greater than page size to have any useful effect. To
475 disable trimming completely, you can set to MAX_SIZE_T. Note that the trick
476 some people use of mallocing a huge space and then freeing it at
477 program startup, in an attempt to reserve system memory, doesn't
478 have the intended effect under automatic trimming, since that memory
479 will immediately be returned to the system.
480
481DEFAULT_MMAP_THRESHOLD default: 256K
482 Also settable using mallopt(M_MMAP_THRESHOLD, x)
483 The request size threshold for using MMAP to directly service a
484 request. Requests of at least this size that cannot be allocated
485 using already-existing space will be serviced via mmap. (If enough
486 normal freed space already exists it is used instead.) Using mmap
487 segregates relatively large chunks of memory so that they can be
488 individually obtained and released from the host system. A request
489 serviced through mmap is never reused by any other request (at least
490 not directly; the system may just so happen to remap successive
491 requests to the same locations). Segregating space in this way has
492 the benefits that: Mmapped space can always be individually released
493 back to the system, which helps keep the system level memory demands
494 of a long-lived program low. Also, mapped memory doesn't become
495 `locked' between other chunks, as can happen with normally allocated
496 chunks, which means that even trimming via malloc_trim would not
497 release them. However, it has the disadvantage that the space
498 cannot be reclaimed, consolidated, and then used to service later
499 requests, as happens with normal chunks. The advantages of mmap
500 nearly always outweigh disadvantages for "large" chunks, but the
501 value of "large" may vary across systems. The default is an
502 empirically derived value that works well in most systems. You can
503 disable mmap by setting to MAX_SIZE_T.
504
505MAX_RELEASE_CHECK_RATE default: 4095 unless not HAVE_MMAP
506 The number of consolidated frees between checks to release
507 unused segments when freeing. When using non-contiguous segments,
508 especially with multiple mspaces, checking only for topmost space
509 doesn't always suffice to trigger trimming. To compensate for this,
510 free() will, with a period of MAX_RELEASE_CHECK_RATE (or the
511 current number of segments, if greater) try to release unused
512 segments to the OS when freeing chunks that result in
513 consolidation. The best value for this parameter is a compromise
514 between slowing down frees with relatively costly checks that
515 rarely trigger versus holding on to unused memory. To effectively
516 disable, set to MAX_SIZE_T. This may lead to a very slight speed
517 improvement at the expense of carrying around more memory.
518*/
519
520#ifdef TMEMORY_USE_DLMALLOC
521
522/* Version identifier to allow people to support multiple versions */
523#ifndef DLMALLOC_VERSION
524#define DLMALLOC_VERSION 20806
525#endif /* DLMALLOC_VERSION */
526
527#ifndef DLMALLOC_EXPORT
528#define DLMALLOC_EXPORT extern
529#endif
530
531#ifndef WIN32
532#ifdef _WIN32
533#define WIN32 1
534#endif /* _WIN32 */
535#ifdef _WIN32_WCE
536#define LACKS_FCNTL_H
537#define WIN32 1
538#endif /* _WIN32_WCE */
539#endif /* WIN32 */
540#ifdef WIN32
541#define WIN32_LEAN_AND_MEAN
542#include <windows.h>
543#include <tchar.h>
544#define HAVE_MMAP 1
545#define HAVE_MORECORE 0
546#define LACKS_UNISTD_H
547#define LACKS_SYS_PARAM_H
548#define LACKS_SYS_MMAN_H
549#define LACKS_STRING_H
550#define LACKS_STRINGS_H
551#define LACKS_SYS_TYPES_H
552#define LACKS_ERRNO_H
553#define LACKS_SCHED_H
554#ifndef MALLOC_FAILURE_ACTION
555#define MALLOC_FAILURE_ACTION
556#endif /* MALLOC_FAILURE_ACTION */
557#ifndef MMAP_CLEARS
558#ifdef _WIN32_WCE /* WINCE reportedly does not clear */
559#define MMAP_CLEARS 0
560#else
561#define MMAP_CLEARS 1
562#endif /* _WIN32_WCE */
563#endif /*MMAP_CLEARS */
564#endif /* WIN32 */
565
566#if defined(DARWIN) || defined(_DARWIN)
567/* Mac OSX docs advise not to use sbrk; it seems better to use mmap */
568#ifndef HAVE_MORECORE
569#define HAVE_MORECORE 0
570#define HAVE_MMAP 1
571/* OSX allocators provide 16 byte alignment */
572#ifndef MALLOC_ALIGNMENT
573#define MALLOC_ALIGNMENT ((size_t)16U)
574#endif
575#endif /* HAVE_MORECORE */
576#endif /* DARWIN */
577
578#ifndef LACKS_SYS_TYPES_H
579#include <sys/types.h> /* For size_t */
580#endif /* LACKS_SYS_TYPES_H */
581
582/* The maximum possible size_t value has all bits set */
583#define MAX_SIZE_T (~(size_t)0)
584
585#ifndef USE_LOCKS /* ensure TTRUE if spin or recursive locks set */
586#define USE_LOCKS ((defined(USE_SPIN_LOCKS) && USE_SPIN_LOCKS != 0) || \
587 (defined(USE_RECURSIVE_LOCKS) && USE_RECURSIVE_LOCKS != 0))
588#endif /* USE_LOCKS */
589
590#if USE_LOCKS /* Spin locks for gcc >= 4.1, older gcc on x86, MSC >= 1310 */
591#if ((defined(__GNUC__) && \
592 ((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)) || \
593 defined(__i386__) || defined(__x86_64__))) || \
594 (defined(_MSC_VER) && _MSC_VER>=1310))
595#ifndef USE_SPIN_LOCKS
596#define USE_SPIN_LOCKS 1
597#endif /* USE_SPIN_LOCKS */
598#elif USE_SPIN_LOCKS
599#error "USE_SPIN_LOCKS defined without implementation"
600#endif /* ... locks available... */
601#elif !defined(USE_SPIN_LOCKS)
602#define USE_SPIN_LOCKS 0
603#endif /* USE_LOCKS */
604
605#ifndef ONLY_MSPACES
606#define ONLY_MSPACES 0
607#endif /* ONLY_MSPACES */
608#ifndef MSPACES
609#if ONLY_MSPACES
610#define MSPACES 1
611#else /* ONLY_MSPACES */
612#define MSPACES 1
613#endif /* ONLY_MSPACES */
614#endif /* MSPACES */
615#ifndef MALLOC_ALIGNMENT
616#define MALLOC_ALIGNMENT ((size_t)(2 * sizeof(void *)))
617#endif /* MALLOC_ALIGNMENT */
618#ifndef FOOTERS
619#define FOOTERS 1
620#endif /* FOOTERS */
621#ifndef ABORT
622#define ABORT abort()
623#endif /* ABORT */
624#ifndef ABORT_ON_ASSERT_FAILURE
625#define ABORT_ON_ASSERT_FAILURE 1
626#endif /* ABORT_ON_ASSERT_FAILURE */
627#ifndef PROCEED_ON_ERROR
628#define PROCEED_ON_ERROR 0
629#endif /* PROCEED_ON_ERROR */
630
631#ifndef INSECURE
632#define INSECURE 0
633#endif /* INSECURE */
634#ifndef MALLOC_INSPECT_ALL
635#define MALLOC_INSPECT_ALL 0
636#endif /* MALLOC_INSPECT_ALL */
637#ifndef HAVE_MMAP
638#define HAVE_MMAP 1
639#endif /* HAVE_MMAP */
640#ifndef MMAP_CLEARS
641#define MMAP_CLEARS 1
642#endif /* MMAP_CLEARS */
643#ifndef HAVE_MREMAP
644#ifdef linux
645#define HAVE_MREMAP 1
646#define _GNU_SOURCE /* Turns on mremap() definition */
647#else /* linux */
648#define HAVE_MREMAP 0
649#endif /* linux */
650#endif /* HAVE_MREMAP */
651#ifndef MALLOC_FAILURE_ACTION
652#define MALLOC_FAILURE_ACTION errno = ENOMEM;
653#endif /* MALLOC_FAILURE_ACTION */
654#ifndef HAVE_MORECORE
655#if ONLY_MSPACES
656#define HAVE_MORECORE 0
657#else /* ONLY_MSPACES */
658#define HAVE_MORECORE 1
659#endif /* ONLY_MSPACES */
660#endif /* HAVE_MORECORE */
661#if !HAVE_MORECORE
662#define MORECORE_CONTIGUOUS 0
663#else /* !HAVE_MORECORE */
664#define MORECORE_DEFAULT sbrk
665#ifndef MORECORE_CONTIGUOUS
666#define MORECORE_CONTIGUOUS 1
667#endif /* MORECORE_CONTIGUOUS */
668#endif /* HAVE_MORECORE */
669#ifndef DEFAULT_GRANULARITY
670#if (MORECORE_CONTIGUOUS || defined(WIN32))
671#define DEFAULT_GRANULARITY (0) /* 0 means to compute in init_mparams */
672#else /* MORECORE_CONTIGUOUS */
673#define DEFAULT_GRANULARITY ((size_t)64U * (size_t)1024U)
674#endif /* MORECORE_CONTIGUOUS */
675#endif /* DEFAULT_GRANULARITY */
676#ifndef DEFAULT_TRIM_THRESHOLD
677#ifndef MORECORE_CANNOT_TRIM
678#define DEFAULT_TRIM_THRESHOLD ((size_t)2U * (size_t)1024U * (size_t)1024U)
679#else /* MORECORE_CANNOT_TRIM */
680#define DEFAULT_TRIM_THRESHOLD MAX_SIZE_T
681#endif /* MORECORE_CANNOT_TRIM */
682#endif /* DEFAULT_TRIM_THRESHOLD */
683#ifndef DEFAULT_MMAP_THRESHOLD
684#if HAVE_MMAP
685#define DEFAULT_MMAP_THRESHOLD ((size_t)256U * (size_t)1024U)
686#else /* HAVE_MMAP */
687#define DEFAULT_MMAP_THRESHOLD MAX_SIZE_T
688#endif /* HAVE_MMAP */
689#endif /* DEFAULT_MMAP_THRESHOLD */
690#ifndef MAX_RELEASE_CHECK_RATE
691#if HAVE_MMAP
692#define MAX_RELEASE_CHECK_RATE 4095
693#else
694#define MAX_RELEASE_CHECK_RATE MAX_SIZE_T
695#endif /* HAVE_MMAP */
696#endif /* MAX_RELEASE_CHECK_RATE */
697#ifndef USE_BUILTIN_FFS
698#define USE_BUILTIN_FFS 0
699#endif /* USE_BUILTIN_FFS */
700#ifndef NO_MALLINFO
701#define NO_MALLINFO 0
702#endif /* NO_MALLINFO */
703#ifndef MALLINFO_FIELD_TYPE
704#define MALLINFO_FIELD_TYPE size_t
705#endif /* MALLINFO_FIELD_TYPE */
706#ifndef NO_MALLOC_STATS
707#define NO_MALLOC_STATS 0
708#endif /* NO_MALLOC_STATS */
709#ifndef NO_SEGMENT_TRAVERSAL
710#define NO_SEGMENT_TRAVERSAL 0
711#endif /* NO_SEGMENT_TRAVERSAL */
712
713/*
714 mallopt tuning options. SVID/XPG defines four standard parameter
715 numbers for mallopt, normally defined in malloc.h. None of these
716 are used in this malloc, so setting them has no effect. But this
717 malloc does support the following options.
718*/
719
720#define M_TRIM_THRESHOLD (-1)
721#define M_GRANULARITY (-2)
722#define M_MMAP_THRESHOLD (-3)
723
724/* ------------------------ Mallinfo declarations ------------------------ */
725
726#if !NO_MALLINFO
727/*
728 This version of malloc supports the standard SVID/XPG mallinfo
729 routine that returns a struct containing usage properties and
730 statistics. It should work on any system that has a
731 /usr/include/malloc.h defining struct mallinfo. The main
732 declaration needed is the mallinfo struct that is returned (by-copy)
733 by mallinfo(). The malloinfo struct contains a bunch of fields that
734 are not even meaningful in this version of malloc. These fields are
735 are instead filled by mallinfo() with other numbers that might be of
736 interest.
737
738 HAVE_USR_INCLUDE_MALLOC_H should be set if you have a
739 /usr/include/malloc.h file that includes a declaration of struct
740 mallinfo. If so, it is included; else a compliant version is
741 declared below. These must be precisely the same for mallinfo() to
742 work. The original SVID version of this struct, defined on most
743 systems with mallinfo, declares all fields as ints. But some others
744 define as unsigned long. If your system defines the fields using a
745 type of different width than listed here, you MUST #include your
746 system version and #define HAVE_USR_INCLUDE_MALLOC_H.
747*/
748
749#ifdef HAVE_USR_INCLUDE_MALLOC_H
750#include "dlmalloc.h"
751#else /* HAVE_USR_INCLUDE_MALLOC_H */
752#ifndef STRUCT_MALLINFO_DECLARED
753/* HP-UX (and others?) redefines mallinfo unless _STRUCT_MALLINFO is defined */
754#define _STRUCT_MALLINFO
755#define STRUCT_MALLINFO_DECLARED 1
756struct mallinfo {
757 MALLINFO_FIELD_TYPE arena; /* non-mmapped space allocated from system */
758 MALLINFO_FIELD_TYPE ordblks; /* number of free chunks */
759 MALLINFO_FIELD_TYPE smblks; /* always 0 */
760 MALLINFO_FIELD_TYPE hblks; /* always 0 */
761 MALLINFO_FIELD_TYPE hblkhd; /* space in mmapped regions */
762 MALLINFO_FIELD_TYPE usmblks; /* maximum total allocated space */
763 MALLINFO_FIELD_TYPE fsmblks; /* always 0 */
764 MALLINFO_FIELD_TYPE uordblks; /* total allocated space */
765 MALLINFO_FIELD_TYPE fordblks; /* total free space */
766 MALLINFO_FIELD_TYPE keepcost; /* releasable (via malloc_trim) space */
767};
768#endif /* STRUCT_MALLINFO_DECLARED */
769#endif /* HAVE_USR_INCLUDE_MALLOC_H */
770#endif /* NO_MALLINFO */
771
772/*
773 Try to persuade compilers to inline. The most critical functions for
774 inlining are defined as macros, so these aren't used for them.
775*/
776
777#ifndef FORCEINLINE
778#if defined(__GNUC__)
779#define FORCEINLINE __inline __attribute__ ((always_inline))
780#elif defined(_MSC_VER)
781#define FORCEINLINE __forceinline
782#endif
783#endif
784#ifndef NOINLINE
785#if defined(__GNUC__)
786#define NOINLINE __attribute__ ((noinline))
787#elif defined(_MSC_VER)
788#define NOINLINE __declspec(noinline)
789#else
790#define NOINLINE
791#endif
792#endif
793
794#ifdef __cplusplus
795extern "C" {
796#ifndef FORCEINLINE
797#define FORCEINLINE inline
798#endif
799#endif /* __cplusplus */
800#ifndef FORCEINLINE
801#define FORCEINLINE
802#endif
803
804#if !ONLY_MSPACES
805
806 /* ------------------- Declarations of public routines ------------------- */
807
808#ifndef USE_DL_PREFIX
809#define dlmemalign memalign
810#define dlposix_memalign posix_memalign
811#define dlrealloc_in_place realloc_in_place
812#define dlvalloc valloc
813#define dlpvalloc pvalloc
814#define dlmallinfo mallinfo
815#define dlmallopt mallopt
816#define dlmalloc_trim malloc_trim
817#define dlmalloc_stats malloc_stats
818#define dlmalloc_usable_size malloc_usable_size
819#define dlmalloc_footprint malloc_footprint
820#define dlmalloc_max_footprint malloc_max_footprint
821#define dlmalloc_footprint_limit malloc_footprint_limit
822#define dlmalloc_set_footprint_limit malloc_set_footprint_limit
823#define dlmalloc_inspect_all malloc_inspect_all
824#define dlindependent_calloc independent_calloc
825#define dlindependent_comalloc independent_comalloc
826#define dlbulk_free bulk_free
827#endif /* USE_DL_PREFIX */
828
829/*
830 malloc(size_t n)
831 Returns a pointer to a newly allocated chunk of at least n bytes, or
832 null if no space is available, in which case errno is set to ENOMEM
833 on ANSI C systems.
834
835 If n is zero, malloc returns a minimum-sized chunk. (The minimum
836 size is 16 bytes on most 32bit systems, and 32 bytes on 64bit
837 systems.) Note that size_t is an unsigned type, so calls with
838 arguments that would be negative if signed are interpreted as
839 requests for huge amounts of space, which will often fail. The
840 maximum supported value of n differs across systems, but is in all
841 cases less than the maximum representable value of a size_t.
842*/
843 DLMALLOC_EXPORT void* dlmalloc(size_t);
844
845 /*
846 free(void* p)
847 Releases the chunk of memory pointed to by p, that had been previously
848 allocated using malloc or a related routine such as realloc.
849 It has no effect if p is null. If p was not malloced or already
850 freed, free(p) will by default cause the current program to abort.
851 */
852 DLMALLOC_EXPORT void dlfree(void*);
853
854 /*
855 calloc(size_t n_elements, size_t element_size);
856 Returns a pointer to n_elements * element_size bytes, with all locations
857 set to zero.
858 */
859 DLMALLOC_EXPORT void* dlcalloc(size_t, size_t);
860
861 /*
862 realloc(void* p, size_t n)
863 Returns a pointer to a chunk of size n that contains the same data
864 as does chunk p up to the minimum of (n, p's size) bytes, or null
865 if no space is available.
866
867 The returned pointer may or may not be the same as p. The algorithm
868 prefers extending p in most cases when possible, otherwise it
869 employs the equivalent of a malloc-copy-free sequence.
870
871 If p is null, realloc is equivalent to malloc.
872
873 If space is not available, realloc returns null, errno is set (if on
874 ANSI) and p is NOT freed.
875
876 if n is for fewer bytes than already held by p, the newly unused
877 space is lopped off and freed if possible. realloc with a size
878 argument of zero (re)allocates a minimum-sized chunk.
879
880 The old unix realloc convention of allowing the last-free'd chunk
881 to be used as an argument to realloc is not supported.
882 */
883 DLMALLOC_EXPORT void* dlrealloc(void*, size_t);
884
885 /*
886 realloc_in_place(void* p, size_t n)
887 Resizes the space allocated for p to size n, only if this can be
888 done without moving p (i.e., only if there is adjacent space
889 available if n is greater than p's current allocated size, or n is
890 less than or equal to p's size). This may be used instead of plain
891 realloc if an alternative allocation strategy is needed upon failure
892 to expand space; for example, reallocation of a buffer that must be
893 memory-aligned or cleared. You can use realloc_in_place to trigger
894 these alternatives only when needed.
895
896 Returns p if successful; otherwise null.
897 */
898 DLMALLOC_EXPORT void* dlrealloc_in_place(void*, size_t);
899
900 /*
901 memalign(size_t alignment, size_t n);
902 Returns a pointer to a newly allocated chunk of n bytes, aligned
903 in accord with the alignment argument.
904
905 The alignment argument should be a power of two. If the argument is
906 not a power of two, the nearest greater power is used.
907 8-byte alignment is guaranteed by normal malloc calls, so don't
908 bother calling memalign with an argument of 8 or less.
909
910 Overreliance on memalign is a sure way to fragment space.
911 */
912 DLMALLOC_EXPORT void* dlmemalign(size_t, size_t);
913
914 /*
915 int posix_memalign(void** pp, size_t alignment, size_t n);
916 Allocates a chunk of n bytes, aligned in accord with the alignment
917 argument. Differs from memalign only in that it (1) assigns the
918 allocated memory to *pp rather than returning it, (2) fails and
919 returns EINVAL if the alignment is not a power of two (3) fails and
920 returns ENOMEM if memory cannot be allocated.
921 */
922 DLMALLOC_EXPORT int dlposix_memalign(void**, size_t, size_t);
923
924 /*
925 valloc(size_t n);
926 Equivalent to memalign(pagesize, n), where pagesize is the page
927 size of the system. If the pagesize is unknown, 4096 is used.
928 */
929 DLMALLOC_EXPORT void* dlvalloc(size_t);
930
931 /*
932 mallopt(int parameter_number, int parameter_value)
933 Sets tunable parameters The format is to provide a
934 (parameter-number, parameter-value) pair. mallopt then sets the
935 corresponding parameter to the argument value if it can (i.e., so
936 long as the value is meaningful), and returns 1 if successful else
937 0. To workaround the fact that mallopt is specified to use int,
938 not size_t parameters, the value -1 is specially treated as the
939 maximum unsigned size_t value.
940
941 SVID/XPG/ANSI defines four standard param numbers for mallopt,
942 normally defined in malloc.h. None of these are use in this malloc,
943 so setting them has no effect. But this malloc also supports other
944 options in mallopt. See below for details. Briefly, supported
945 parameters are as follows (listed defaults are for "typical"
946 configurations).
947
948 Symbol param # default allowed param values
949 M_TRIM_THRESHOLD -1 2*1024*1024 any (-1 disables)
950 M_GRANULARITY -2 page size any power of 2 >= page size
951 M_MMAP_THRESHOLD -3 256*1024 any (or 0 if no MMAP support)
952 */
953 DLMALLOC_EXPORT int dlmallopt(int, int);
954
955 /*
956 malloc_footprint();
957 Returns the number of bytes obtained from the system. The total
958 number of bytes allocated by malloc, realloc etc., is less than this
959 value. Unlike mallinfo, this function returns only a precomputed
960 result, so can be called frequently to monitor memory consumption.
961 Even if locks are otherwise defined, this function does not use them,
962 so results might not be up to date.
963 */
964 DLMALLOC_EXPORT size_t dlmalloc_footprint(void);
965
966 /*
967 malloc_max_footprint();
968 Returns the maximum number of bytes obtained from the system. This
969 value will be greater than current footprint if deallocated space
970 has been reclaimed by the system. The peak number of bytes allocated
971 by malloc, realloc etc., is less than this value. Unlike mallinfo,
972 this function returns only a precomputed result, so can be called
973 frequently to monitor memory consumption. Even if locks are
974 otherwise defined, this function does not use them, so results might
975 not be up to date.
976 */
977 DLMALLOC_EXPORT size_t dlmalloc_max_footprint(void);
978
979 /*
980 malloc_footprint_limit();
981 Returns the number of bytes that the heap is allowed to obtain from
982 the system, returning the last value returned by
983 malloc_set_footprint_limit, or the maximum size_t value if
984 never set. The returned value reflects a permission. There is no
985 guarantee that this number of bytes can actually be obtained from
986 the system.
987 */
988 DLMALLOC_EXPORT size_t dlmalloc_footprint_limit();
989
990 /*
991 malloc_set_footprint_limit();
992 Sets the maximum number of bytes to obtain from the system, causing
993 failure returns from malloc and related functions upon attempts to
994 exceed this value. The argument value may be subject to page
995 rounding to an enforceable limit; this actual value is returned.
996 Using an argument of the maximum possible size_t effectively
997 disables checks. If the argument is less than or equal to the
998 current malloc_footprint, then all future allocations that require
999 additional system memory will fail. However, invocation cannot
1000 retroactively deallocate existing used memory.
1001 */
1002 DLMALLOC_EXPORT size_t dlmalloc_set_footprint_limit(size_t bytes);
1003
1004#if MALLOC_INSPECT_ALL
1005 /*
1006 malloc_inspect_all(void(*handler)(void *start,
1007 void *end,
1008 size_t used_bytes,
1009 void* callback_arg),
1010 void* arg);
1011 Traverses the heap and calls the given handler for each managed
1012 region, skipping all bytes that are (or may be) used for bookkeeping
1013 purposes. Traversal does not include include chunks that have been
1014 directly memory mapped. Each reported region begins at the start
1015 address, and continues up to but not including the end address. The
1016 first used_bytes of the region contain allocated data. If
1017 used_bytes is zero, the region is unallocated. The handler is
1018 invoked with the given callback argument. If locks are defined, they
1019 are held during the entire traversal. It is a bad idea to invoke
1020 other malloc functions from within the handler.
1021
1022 For example, to count the number of in-use chunks with size greater
1023 than 1000, you could write:
1024 static int count = 0;
1025 void count_chunks(void* start, void* end, size_t used, void* arg) {
1026 if (used >= 1000) ++count;
1027 }
1028 then:
1029 malloc_inspect_all(count_chunks, NULL);
1030
1031 malloc_inspect_all is compiled only if MALLOC_INSPECT_ALL is defined.
1032 */
1033 DLMALLOC_EXPORT void dlmalloc_inspect_all(void(*handler)(void*, void*, size_t, void*),
1034 void* arg);
1035
1036#endif /* MALLOC_INSPECT_ALL */
1037
1038#if !NO_MALLINFO
1039 /*
1040 mallinfo()
1041 Returns (by copy) a struct containing various summary statistics:
1042
1043 arena: current total non-mmapped bytes allocated from system
1044 ordblks: the number of free chunks
1045 smblks: always zero.
1046 hblks: current number of mmapped regions
1047 hblkhd: total bytes held in mmapped regions
1048 usmblks: the maximum total allocated space. This will be greater
1049 than current total if trimming has occurred.
1050 fsmblks: always zero
1051 uordblks: current total allocated space (normal or mmapped)
1052 fordblks: total free space
1053 keepcost: the maximum number of bytes that could ideally be released
1054 back to system via malloc_trim. ("ideally" means that
1055 it ignores page restrictions etc.)
1056
1057 Because these fields are ints, but internal bookkeeping may
1058 be kept as longs, the reported values may wrap around zero and
1059 thus be inaccurate.
1060 */
1061 DLMALLOC_EXPORT struct mallinfo dlmallinfo(void);
1062#endif /* NO_MALLINFO */
1063
1064 /*
1065 independent_calloc(size_t n_elements, size_t element_size, void* chunks[]);
1066
1067 independent_calloc is similar to calloc, but instead of returning a
1068 single cleared space, it returns an array of pointers to n_elements
1069 independent elements that can hold contents of size elem_size, each
1070 of which starts out cleared, and can be independently freed,
1071 realloc'ed etc. The elements are guaranteed to be adjacently
1072 allocated (this is not guaranteed to occur with multiple callocs or
1073 mallocs), which may also improve cache locality in some
1074 applications.
1075
1076 The "chunks" argument is optional (i.e., may be null, which is
1077 probably the most typical usage). If it is null, the returned array
1078 is itself dynamically allocated and should also be freed when it is
1079 no longer needed. Otherwise, the chunks array must be of at least
1080 n_elements in length. It is filled in with the pointers to the
1081 chunks.
1082
1083 In either case, independent_calloc returns this pointer array, or
1084 null if the allocation failed. If n_elements is zero and "chunks"
1085 is null, it returns a chunk representing an array with zero elements
1086 (which should be freed if not wanted).
1087
1088 Each element must be freed when it is no longer needed. This can be
1089 done all at once using bulk_free.
1090
1091 independent_calloc simplifies and speeds up implementations of many
1092 kinds of pools. It may also be useful when constructing large data
1093 structures that initially have a fixed number of fixed-sized nodes,
1094 but the number is not known at compile time, and some of the nodes
1095 may later need to be freed. For example:
1096
1097 struct Node { int item; struct Node* next; };
1098
1099 struct Node* build_list() {
1100 struct Node** pool;
1101 int n = read_number_of_nodes_needed();
1102 if (n <= 0) return 0;
1103 pool = (struct Node**)(independent_calloc(n, sizeof(struct Node), 0);
1104 if (pool == 0) die();
1105 // organize into a linked list...
1106 struct Node* first = pool[0];
1107 for (i = 0; i < n-1; ++i)
1108 pool[i]->next = pool[i+1];
1109 free(pool); // Can now free the array (or not, if it is needed later)
1110 return first;
1111 }
1112 */
1113 DLMALLOC_EXPORT void** dlindependent_calloc(size_t, size_t, void**);
1114
1115 /*
1116 independent_comalloc(size_t n_elements, size_t sizes[], void* chunks[]);
1117
1118 independent_comalloc allocates, all at once, a set of n_elements
1119 chunks with sizes indicated in the "sizes" array. It returns
1120 an array of pointers to these elements, each of which can be
1121 independently freed, realloc'ed etc. The elements are guaranteed to
1122 be adjacently allocated (this is not guaranteed to occur with
1123 multiple callocs or mallocs), which may also improve cache locality
1124 in some applications.
1125
1126 The "chunks" argument is optional (i.e., may be null). If it is null
1127 the returned array is itself dynamically allocated and should also
1128 be freed when it is no longer needed. Otherwise, the chunks array
1129 must be of at least n_elements in length. It is filled in with the
1130 pointers to the chunks.
1131
1132 In either case, independent_comalloc returns this pointer array, or
1133 null if the allocation failed. If n_elements is zero and chunks is
1134 null, it returns a chunk representing an array with zero elements
1135 (which should be freed if not wanted).
1136
1137 Each element must be freed when it is no longer needed. This can be
1138 done all at once using bulk_free.
1139
1140 independent_comallac differs from independent_calloc in that each
1141 element may have a different size, and also that it does not
1142 automatically clear elements.
1143
1144 independent_comalloc can be used to speed up allocation in cases
1145 where several structs or objects must always be allocated at the
1146 same time. For example:
1147
1148 struct Head { ... }
1149 struct Foot { ... }
1150
1151 void send_message(char* msg) {
1152 int msglen = strlen(msg);
1153 size_t sizes[3] = { sizeof(struct Head), msglen, sizeof(struct Foot) };
1154 void* chunks[3];
1155 if (independent_comalloc(3, sizes, chunks) == 0)
1156 die();
1157 struct Head* head = (struct Head*)(chunks[0]);
1158 char* body = (char*)(chunks[1]);
1159 struct Foot* foot = (struct Foot*)(chunks[2]);
1160 // ...
1161 }
1162
1163 In general though, independent_comalloc is worth using only for
1164 larger values of n_elements. For small values, you probably won't
1165 detect enough difference from series of malloc calls to bother.
1166
1167 Overuse of independent_comalloc can increase overall memory usage,
1168 since it cannot reuse existing noncontiguous small chunks that
1169 might be available for some of the elements.
1170 */
1171 DLMALLOC_EXPORT void** dlindependent_comalloc(size_t, size_t*, void**);
1172
1173 /*
1174 bulk_free(void* array[], size_t n_elements)
1175 Frees and clears (sets to null) each non-null pointer in the given
1176 array. This is likely to be faster than freeing them one-by-one.
1177 If footers are used, pointers that have been allocated in different
1178 mspaces are not freed or cleared, and the count of all such pointers
1179 is returned. For large arrays of pointers with poor locality, it
1180 may be worthwhile to sort this array before calling bulk_free.
1181 */
1182 DLMALLOC_EXPORT size_t dlbulk_free(void**, size_t n_elements);
1183
1184 /*
1185 pvalloc(size_t n);
1186 Equivalent to valloc(minimum-page-that-holds(n)), that is,
1187 round up n to nearest pagesize.
1188 */
1189 DLMALLOC_EXPORT void* dlpvalloc(size_t);
1190
1191 /*
1192 malloc_trim(size_t pad);
1193
1194 If possible, gives memory back to the system (via negative arguments
1195 to sbrk) if there is unused memory at the `high' end of the malloc
1196 pool or in unused MMAP segments. You can call this after freeing
1197 large blocks of memory to potentially reduce the system-level memory
1198 requirements of a program. However, it cannot guarantee to reduce
1199 memory. Under some allocation patterns, some large free blocks of
1200 memory will be locked between two used chunks, so they cannot be
1201 given back to the system.
1202
1203 The `pad' argument to malloc_trim represents the amount of free
1204 trailing space to leave untrimmed. If this argument is zero, only
1205 the minimum amount of memory to maintain internal data structures
1206 will be left. Non-zero arguments can be supplied to maintain enough
1207 trailing space to service future expected allocations without having
1208 to re-obtain memory from the system.
1209
1210 Malloc_trim returns 1 if it actually released any memory, else 0.
1211 */
1212 DLMALLOC_EXPORT int dlmalloc_trim(size_t);
1213
1214 /*
1215 malloc_stats();
1216 Prints on stderr the amount of space obtained from the system (both
1217 via sbrk and mmap), the maximum amount (which may be more than
1218 current if malloc_trim and/or munmap got called), and the current
1219 number of bytes allocated via malloc (or realloc, etc) but not yet
1220 freed. Note that this is the number of bytes allocated, not the
1221 number requested. It will be larger than the number requested
1222 because of alignment and bookkeeping overhead. Because it includes
1223 alignment wastage as being in use, this figure may be greater than
1224 zero even when no user-level chunks are allocated.
1225
1226 The reported current and maximum system memory can be inaccurate if
1227 a program makes other calls to system memory allocation functions
1228 (normally sbrk) outside of malloc.
1229
1230 malloc_stats prints only the most commonly interesting statistics.
1231 More information can be obtained by calling mallinfo.
1232 */
1233 DLMALLOC_EXPORT void dlmalloc_stats(void);
1234
1235 /*
1236 malloc_usable_size(void* p);
1237
1238 Returns the number of bytes you can actually use in
1239 an allocated chunk, which may be more than you requested (although
1240 often not) due to alignment and minimum size constraints.
1241 You can use this many bytes without worrying about
1242 overwriting other allocated objects. This is not a particularly great
1243 programming practice. malloc_usable_size can be more useful in
1244 debugging and assertions, for example:
1245
1246 p = malloc(n);
1247 assert(malloc_usable_size(p) >= 256);
1248 */
1249 size_t dlmalloc_usable_size(void*);
1250
1251#endif /* ONLY_MSPACES */
1252
1253#if MSPACES
1254
1255 /*
1256 mspace is an opaque type representing an independent
1257 region of space that supports mspace_malloc, etc.
1258 */
1259 typedef void* mspace;
1260
1261 /*
1262 create_mspace creates and returns a new independent space with the
1263 given initial capacity, or, if 0, the default granularity size. It
1264 returns null if there is no system memory available to create the
1265 space. If argument locked is non-zero, the space uses a separate
1266 lock to control access. The capacity of the space will grow
1267 dynamically as needed to service mspace_malloc requests. You can
1268 control the sizes of incremental increases of this space by
1269 compiling with a different DEFAULT_GRANULARITY or dynamically
1270 setting with mallopt(M_GRANULARITY, value).
1271 */
1272 DLMALLOC_EXPORT mspace create_mspace(size_t capacity, int locked);
1273
1274 /*
1275 destroy_mspace destroys the given space, and attempts to return all
1276 of its memory back to the system, returning the total number of
1277 bytes freed. After destruction, the results of access to all memory
1278 used by the space become undefined.
1279 */
1280 DLMALLOC_EXPORT size_t destroy_mspace(mspace msp);
1281
1282 /*
1283 create_mspace_with_base uses the memory supplied as the initial base
1284 of a new mspace. Part (less than 128*sizeof(size_t) bytes) of this
1285 space is used for bookkeeping, so the capacity must be at least this
1286 large. (Otherwise 0 is returned.) When this initial space is
1287 exhausted, additional memory will be obtained from the system.
1288 Destroying this space will deallocate all additionally allocated
1289 space (if possible) but not the initial base.
1290 */
1291 DLMALLOC_EXPORT mspace create_mspace_with_base(void* base, size_t capacity, int locked);
1292
1293 /*
1294 mspace_track_large_chunks controls whether requests for large chunks
1295 are allocated in their own untracked mmapped regions, separate from
1296 others in this mspace. By default large chunks are not tracked,
1297 which reduces fragmentation. However, such chunks are not
1298 necessarily released to the system upon destroy_mspace. Enabling
1299 tracking by setting to TTRUE may increase fragmentation, but avoids
1300 leakage when relying on destroy_mspace to release all memory
1301 allocated using this space. The function returns the previous
1302 setting.
1303 */
1304 DLMALLOC_EXPORT int mspace_track_large_chunks(mspace msp, int enable);
1305
1306
1307 /*
1308 mspace_malloc behaves as malloc, but operates within
1309 the given space.
1310 */
1311 DLMALLOC_EXPORT void* mspace_malloc(mspace msp, size_t bytes);
1312
1313 /*
1314 mspace_free behaves as free, but operates within
1315 the given space.
1316
1317 If compiled with FOOTERS==1, mspace_free is not actually needed.
1318 free may be called instead of mspace_free because freed chunks from
1319 any space are handled by their originating spaces.
1320 */
1321 DLMALLOC_EXPORT void mspace_free(mspace msp, void* mem);
1322
1323 /*
1324 mspace_realloc behaves as realloc, but operates within
1325 the given space.
1326
1327 If compiled with FOOTERS==1, mspace_realloc is not actually
1328 needed. realloc may be called instead of mspace_realloc because
1329 realloced chunks from any space are handled by their originating
1330 spaces.
1331 */
1332 DLMALLOC_EXPORT void* mspace_realloc(mspace msp, void* mem, size_t newsize);
1333
1334 /*
1335 mspace_calloc behaves as calloc, but operates within
1336 the given space.
1337 */
1338 DLMALLOC_EXPORT void* mspace_calloc(mspace msp, size_t n_elements, size_t elem_size);
1339
1340 /*
1341 mspace_memalign behaves as memalign, but operates within
1342 the given space.
1343 */
1344 DLMALLOC_EXPORT void* mspace_memalign(mspace msp, size_t alignment, size_t bytes);
1345
1346 /*
1347 mspace_independent_calloc behaves as independent_calloc, but
1348 operates within the given space.
1349 */
1350 DLMALLOC_EXPORT void** mspace_independent_calloc(mspace msp, size_t n_elements,
1351 size_t elem_size, void* chunks[]);
1352
1353 /*
1354 mspace_independent_comalloc behaves as independent_comalloc, but
1355 operates within the given space.
1356 */
1357 DLMALLOC_EXPORT void** mspace_independent_comalloc(mspace msp, size_t n_elements,
1358 size_t sizes[], void* chunks[]);
1359
1360 /*
1361 mspace_footprint() returns the number of bytes obtained from the
1362 system for this space.
1363 */
1364 DLMALLOC_EXPORT size_t mspace_footprint(mspace msp);
1365
1366 /*
1367 mspace_max_footprint() returns the peak number of bytes obtained from the
1368 system for this space.
1369 */
1370 DLMALLOC_EXPORT size_t mspace_max_footprint(mspace msp);
1371
1372
1373#if !NO_MALLINFO
1374 /*
1375 mspace_mallinfo behaves as mallinfo, but reports properties of
1376 the given space.
1377 */
1378 DLMALLOC_EXPORT struct mallinfo mspace_mallinfo(mspace msp);
1379#endif /* NO_MALLINFO */
1380
1381 /*
1382 malloc_usable_size(void* p) behaves the same as malloc_usable_size;
1383 */
1384 DLMALLOC_EXPORT size_t mspace_usable_size(const void* mem);
1385
1386 /*
1387 mspace_malloc_stats behaves as malloc_stats, but reports
1388 properties of the given space.
1389 */
1390 DLMALLOC_EXPORT void mspace_malloc_stats(mspace msp);
1391
1392 /*
1393 mspace_trim behaves as malloc_trim, but
1394 operates within the given space.
1395 */
1396 DLMALLOC_EXPORT int mspace_trim(mspace msp, size_t pad);
1397
1398 /*
1399 An alias for mallopt.
1400 */
1401 DLMALLOC_EXPORT int mspace_mallopt(int, int);
1402
1403 DLMALLOC_EXPORT void* get_mspace_from_ptr(void* ptr);
1404
1405#endif /* MSPACES */
1406
1407#ifdef __cplusplus
1408} /* end of extern "C" */
1409#endif /* __cplusplus */
1410
1411#endif // TMEMORY_USE_DLMALLOC