1 .\" $NetBSD: malloc.3,v 1.29 2009/05/18 09:00:02 wiz Exp $
3 .\" Copyright (c) 1980, 1991, 1993
4 .\" The Regents of the University of California. All rights reserved.
6 .\" This code is derived from software contributed to Berkeley by
7 .\" the American National Standards Committee X3, on Information
8 .\" Processing Systems.
10 .\" Redistribution and use in source and binary forms, with or without
11 .\" modification, are permitted provided that the following conditions
13 .\" 1. Redistributions of source code must retain the above copyright
14 .\" notice, this list of conditions and the following disclaimer.
15 .\" 2. Redistributions in binary form must reproduce the above copyright
16 .\" notice, this list of conditions and the following disclaimer in the
17 .\" documentation and/or other materials provided with the distribution.
18 .\" 3. Neither the name of the University nor the names of its contributors
19 .\" may be used to endorse or promote products derived from this software
20 .\" without specific prior written permission.
22 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 .\" @(#)malloc.3 8.1 (Berkeley) 6/4/93
35 .\" $FreeBSD: src/lib/libc/stdlib/malloc.3,v 1.73 2007/06/15 22:32:33 jasone Exp $
41 .Nm malloc , calloc , realloc , free
42 .Nd general purpose memory allocation functions
48 .Fn malloc "size_t size"
50 .Fn calloc "size_t number" "size_t size"
52 .Fn realloc "void *ptr" "size_t size"
62 bytes of uninitialized memory.
63 The allocated space is suitably aligned (after possible pointer coercion)
64 for storage of any type of object.
68 function allocates space for
74 The result is identical to calling
78 with the exception that the allocated memory is explicitly initialized
83 function changes the size of the previously allocated memory referenced by
88 The contents of the memory are unchanged up to the lesser of the new and
90 If the new size is larger,
91 the value of the newly allocated portion of the memory is undefined.
92 Upon success, the memory referenced by
94 is freed and a pointer to the newly allocated memory is returned.
97 may move the memory allocation, resulting in a different return value than
105 function behaves identically to
107 for the specified size.
111 one must be careful to avoid the following idiom:
113 .Bd -literal -offset indent
115 if ((p = realloc(p, nsize)) == NULL)
119 Do not adjust the variable describing how much memory has been allocated
120 until one knows the allocation has been successful.
121 This can cause aberrant program behavior if the incorrect size value is used.
122 In most cases, the above sample will also result in a leak of memory.
123 As stated earlier, a return value of
125 indicates that the old object still remains allocated.
126 Better code looks like this:
127 .Bd -literal -offset indent
129 if ((p2 = realloc(p, newsize)) == NULL) {
141 function causes the allocated memory referenced by
143 to be made available for future allocations.
150 Once, when the first call is made to one of these memory allocation
151 routines, various flags will be set or reset, which affect the
152 workings of this allocator implementation.
156 of the file referenced by the symbolic link named
157 .Pa /etc/malloc.conf ,
158 the value of the environment variable
160 and the string pointed to by the global variable
162 will be interpreted, in that order, character by character as flags.
164 Most flags are single letters,
165 where uppercase indicates that the behavior is set, or on,
166 and lowercase means that the behavior is not set, or off.
167 .Bl -tag -width indent
169 All warnings (except for the warning about unknown
170 flags being set) become fatal.
171 The process will call
177 when pages within a chunk are no longer in use, but the chunk as a whole cannot
179 This is primarily of use when swapping is a real possibility, due to the high
184 Each byte of new memory allocated by
187 will be initialized to 0xa5.
188 All memory returned by
191 will be initialized to 0x5a.
192 This is intended for debugging and will impact performance negatively.
194 Increase/decrease the virtual memory chunk size by a factor of two.
195 The default chunk size is 1 MB.
196 This option can be specified multiple times.
198 Increase/decrease the number of arenas by a factor of two.
199 The default number of arenas is four times the number of CPUs, or one if there
201 This option can be specified multiple times.
203 Various statistics are printed at program exit via an
206 This has the potential to cause deadlock for a multi-threaded process that exits
207 while one or more threads are executing in the memory allocation functions.
208 Therefore, this option should only be used with care; it is primarily intended
209 as a performance tuning aid during application development.
211 Increase/decrease the size of the allocation quantum by a factor of two.
212 The default quantum is the minimum allowed by the architecture (typically 8 or
214 This option can be specified multiple times.
216 Increase/decrease the size of the maximum size class that is a multiple of the
217 quantum by a factor of two.
218 Above this size, power-of-two spacing is used for size classes.
219 The default value is 512 bytes.
220 This option can be specified multiple times.
227 Consult the source for details on this option.
229 Attempting to allocate zero bytes will return a
231 pointer instead of a valid pointer.
232 (The default behavior is to make a minimal allocation and return a
234 This option is provided for System V compatibility.
235 This option is incompatible with the
239 Rather than return failure for any allocation function,
240 display a diagnostic message on
242 and cause the program to drop
245 This option should be set at compile time by including the following in
247 .Bd -literal -offset indent
248 _malloc_options = "X";
251 Each byte of new memory allocated by
254 will be initialized to 0.
255 Note that this initialization only happens once for each byte, so
257 does not zero memory that was previously allocated.
258 This is intended for debugging and will impact performance negatively.
265 options are intended for testing and debugging.
266 An application which changes its behavior when these options are used
268 .Sh IMPLEMENTATION NOTES
269 This allocator uses multiple arenas in order to reduce lock contention for
270 threaded programs on multi-processor systems.
271 This works well with regard to threading scalability, but incurs some costs.
272 There is a small fixed per-arena overhead, and additionally, arenas manage
273 memory completely independently of each other, which means a small fixed
274 increase in overall memory fragmentation.
275 These overheads are not generally an issue, given the number of arenas normally
277 Note that using substantially more arenas than the default is not likely to
278 improve performance, mainly due to reduced cache performance.
279 However, it may make sense to reduce the number of arenas if an application
280 does not make much use of the allocation functions.
282 Memory is conceptually broken into equal-sized chunks, where the chunk size is
283 a power of two that is greater than the page size.
284 Chunks are always aligned to multiples of the chunk size.
285 This alignment makes it possible to find metadata for user objects very
288 User objects are broken into three categories according to size: small, large,
290 Small objects are no larger than one half of a page.
291 Large objects are smaller than the chunk size.
292 Huge objects are a multiple of the chunk size.
293 Small and large objects are managed by arenas; huge objects are managed
294 separately in a single data structure that is shared by all threads.
295 Huge objects are used by applications infrequently enough that this single
296 data structure is not a scalability issue.
298 Each chunk that is managed by an arena tracks its contents in a page map as
299 runs of contiguous pages (unused, backing a set of small objects, or backing
301 The combination of chunk alignment and chunk page maps makes it possible to
302 determine all metadata regarding small and large allocations in constant time.
304 Small objects are managed in groups by page runs.
305 Each run maintains a bitmap that tracks which regions are in use.
306 Allocation requests that are no more than half the quantum (see the
308 option) are rounded up to the nearest power of two (typically 2, 4, or 8).
309 Allocation requests that are more than half the quantum, but no more than the
310 maximum quantum-multiple size class (see the
312 option) are rounded up to the nearest multiple of the quantum.
313 Allocation requests that are larger than the maximum quantum-multiple size
314 class, but no larger than one half of a page, are rounded up to the nearest
316 Allocation requests that are larger than half of a page, but small enough to
317 fit in an arena-managed chunk (see the
319 option), are rounded up to the nearest run size.
320 Allocation requests that are too large to fit in an arena-managed chunk are
321 rounded up to the nearest multiple of the chunk size.
323 Allocations are packed tightly together, which can be an issue for
324 multi-threaded applications.
325 If you need to assure that allocations do not suffer from cache line sharing,
326 round your allocation requests up to the nearest multiple of the cache line
328 .Sh DEBUGGING MALLOC PROBLEMS
329 The first thing to do is to set the
332 This option forces a coredump (if possible) at the first sign of trouble,
333 rather than the normal policy of trying to continue if at all possible.
335 It is probably also a good idea to recompile the program with suitable
336 options and symbols for debugger support.
338 If the program starts to give unusual results, coredump or generally behave
339 differently without emitting any of the messages mentioned in the next
340 section, it is likely because it depends on the storage being filled with
342 Try running it with the
345 if that improves the situation, this diagnosis has been confirmed.
346 If the program still misbehaves,
347 the likely problem is accessing memory outside the allocated area.
349 Alternatively, if the symptoms are not easy to reproduce, setting the
351 option may help provoke the problem.
353 In truly difficult cases, the
355 option, if supported by the kernel, can provide a detailed trace of
356 all calls made to these functions.
358 Unfortunately this implementation does not provide much detail about
359 the problems it detects; the performance impact for storing such information
360 would be prohibitive.
361 There are a number of allocator implementations available on the Internet
362 which focus on detecting and pinpointing problems by trading performance for
363 extra sanity checks and detailed diagnostics.
364 .Sh DIAGNOSTIC MESSAGES
365 If any of the memory allocation/deallocation functions detect an error or
366 warning condition, a message will be printed to file descriptor
368 Errors will result in the process dumping core.
371 option is set, all warnings are treated as errors.
375 variable allows the programmer to override the function which emits
376 the text strings forming the errors and warnings if for some reason
379 file descriptor is not suitable for this.
380 Please note that doing anything which tries to allocate memory in
381 this function is likely to result in a crash or deadlock.
383 All messages are prefixed by
384 .Dq Ao Ar progname Ac Ns Li \&: Pq malloc .
390 functions return a pointer to the allocated memory if successful; otherwise
393 pointer is returned and
400 function returns a pointer, possibly identical to
402 to the allocated memory
403 if successful; otherwise a
405 pointer is returned, and
409 if the error was the result of an allocation failure.
412 function always leaves the original buffer intact
413 when an error occurs.
417 function returns no value.
419 The following environment variables affect the execution of the allocation
421 .Bl -tag -width ".Ev MALLOC_OPTIONS"
422 .It Ev MALLOC_OPTIONS
423 If the environment variable
425 is set, the characters it contains will be interpreted as flags to the
426 allocation functions.
429 To dump core whenever a problem occurs:
431 .Bd -literal -offset indent
432 ln -s 'A' /etc/malloc.conf
435 To specify in the source that a program does no return value checking
436 on calls to these functions:
437 .Bd -literal -offset indent
438 _malloc_options = "X";