1 .\" Copyright (c) 1980, 1991, 1993
2 .\" The Regents of the University of California. All rights reserved.
4 .\" This code is derived from software contributed to Berkeley by
5 .\" the American National Standards Committee X3, on Information
6 .\" Processing Systems.
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
11 .\" 1. Redistributions of source code must retain the above copyright
12 .\" notice, this list of conditions and the following disclaimer.
13 .\" 2. Redistributions in binary form must reproduce the above copyright
14 .\" notice, this list of conditions and the following disclaimer in the
15 .\" documentation and/or other materials provided with the distribution.
16 .\" 3. All advertising materials mentioning features or use of this software
17 .\" must display the following acknowledgement:
18 .\" This product includes software developed by the University of
19 .\" California, Berkeley and its contributors.
20 .\" 4. Neither the name of the University nor the names of its contributors
21 .\" may be used to endorse or promote products derived from this software
22 .\" without specific prior written permission.
24 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 .\" @(#)malloc.3 8.1 (Berkeley) 6/4/93
37 .\" $FreeBSD: src/lib/libc/stdlib/malloc.3,v 1.25.2.16 2003/01/06 17:10:45 trhodes Exp $
38 .\" $DragonFly: src/lib/libc/stdlib/malloc.3,v 1.8 2008/05/02 02:05:04 swildner Exp $
49 .Nd general purpose memory allocation functions
55 .Fn malloc "size_t size"
57 .Fn calloc "size_t number" "size_t size"
59 .Fn realloc "void *ptr" "size_t size"
61 .Fn reallocf "void *ptr" "size_t size"
72 The allocated space is suitably aligned (after possible pointer coercion)
73 for storage of any type of object.
74 If the space is at least
78 the returned memory will be page boundary aligned as well.
89 normally initialize the returned memory to zero bytes.
93 function allocates space for
99 The result is identical to calling
102 .Dq "number * size" ,
103 with the exception that the allocated memory is explicitly initialized
108 function changes the size of the previously allocated memory referenced by
113 The contents of the memory are unchanged up to the lesser of the new and
115 If the new size is larger,
116 the value of the newly allocated portion of the memory is undefined.
117 If the requested memory cannot be allocated,
120 the memory referenced by
122 is valid and unchanged.
129 function behaves identically to
131 for the specified size.
135 function call is identical to the realloc function call, except that it
136 will free the passed pointer when the requested memory cannot be allocated.
141 specific API designed to ease the problems with traditional coding styles
142 for realloc causing memory leaks in libraries.
146 function causes the allocated memory referenced by
148 to be made available for future allocations.
155 Once, when the first call is made to one of these memory allocation
156 routines, various flags will be set or reset, which affect the
157 workings of this allocation implementation.
159 The ``name'' of the file referenced by the symbolic link named
160 .Pa /etc/malloc.conf ,
161 the value of the environment variable
163 and the string pointed to by the global variable
165 will be interpreted, in that order, character by character as flags.
167 Most flags are single letters,
168 where uppercase indicates that the behavior is set, or on,
169 and lowercase means that the behavior is not set, or off.
170 .Bl -tag -width indent
172 All warnings (except for the warning about unknown
173 flags being set) become fatal.
174 The process will call
179 will dump statistics in a file called
182 This option requires the library to have been compiled with -DMALLOC_STATS in
183 order to have any effect.
185 Unused pages on the freelist are read and write protected to
186 cause a segmentation fault upon access.
188 Enable guard pages and chunk randomization.
189 Each page size or larger allocation is followed by a guard page that will
190 cause a segmentation fault upon any access.
191 Smaller than page size chunks are returned in a random order.
193 Pass a hint to the kernel about pages unused by the allocation functions.
194 This will help performance if the system is paging excessively. This
195 option is off by default.
197 Each byte of new memory allocated by
202 as well as all memory returned by
207 will be initialized to 0xd0.
208 This options also sets the
211 This is intended for debugging and will impact performance negatively.
213 Do not output warning messages when encountering possible corruption
216 Pointer sized allocations are aligned to the end of a page to catch
217 sizeof(ptr) errors where sizeof(*ptr) is meant.
219 Always reallocate when
221 is called, even if the initial allocation was big enough.
222 This can substantially aid in compacting memory.
229 Consult the source for details on this option.
231 Attempting to allocate zero bytes will return a
235 (The default behavior is to make a minimal allocation and return a
237 This option is provided for System V compatibility.
238 This option is incompatible with the
242 Rather than return failure for any allocation function,
243 display a diagnostic message on stderr and cause the program to drop
246 This option should be set at compile time by including the following in
248 .Bd -literal -offset indent
249 extern char *malloc_options;
250 malloc_options = "X";
253 This option implicitly sets the
257 options, and then zeros out the bytes that were requested.
258 This is intended for debugging and will impact performance negatively.
260 Reduce the size of the cache by a factor of two.
261 The default cache size is 16 pages.
262 This option can be specified multiple times.
264 Double the size of the cache by a factor of two.
265 The default cache size is 16 pages.
266 This option can be specified multiple times.
273 options are intended for testing and debugging.
274 An application which changes its behavior when these options are used
281 functions return a pointer to the allocated memory if successful; otherwise
284 pointer is returned and
293 functions return a pointer, possibly identical to
295 to the allocated memory
296 if successful; otherwise a
298 pointer is returned, and
302 if the error was the result of an allocation failure.
305 function always leaves the original buffer intact
306 when an error occurs, whereas
308 deallocates it in this case.
312 function returns no value.
314 The following environment variables affect the execution of the allocation
316 .Bl -tag -width ".Ev MALLOC_OPTIONS"
317 .It Ev MALLOC_OPTIONS
318 If the environment variable
320 is set, the characters it contains will be interpreted as flags to the
321 allocation functions.
324 To set a systemwide reduction of cache size, and to dump core whenever
326 .Bd -literal -offset indent
327 ln -s 'A<' /etc/malloc.conf
330 To specify in the source that a program does no return value checking
331 on calls to these functions:
332 .Bd -literal -offset indent
333 extern char *malloc_options;
334 malloc_options = "X";
336 .Sh DEBUGGING MALLOC PROBLEMS
337 The major difference between this implementation and other allocation
338 implementations is that the free pages are not accessed unless allocated,
339 and are aggressively returned to the kernel for reuse.
340 .Bd -ragged -offset indent
341 Most allocation implementations will store a data structure containing a
342 linked list in the free chunks of memory,
343 used to tie all the free memory together.
344 That can be suboptimal,
345 as every time the free-list is traversed,
346 the otherwise unused, and likely paged out,
347 pages are faulted into primary memory.
348 On systems which are paging,
349 this can result in a factor of five increase in the number of page-faults
353 A side effect of this architecture is that many minor transgressions on
354 the interface which would traditionally not be detected are in fact
355 detected. As a result, programs that have been running happily for
356 years may suddenly start to complain loudly, when linked with this
357 allocation implementation.
359 The first and most important thing to do is to set the
362 This option forces a coredump (if possible) at the first sign of trouble,
363 rather than the normal policy of trying to continue if at all possible.
365 It is probably also a good idea to recompile the program with suitable
366 options and symbols for debugger support.
368 If the program starts to give unusual results, coredump or generally behave
369 differently without emitting any of the messages listed in the next
370 section, it is likely because it depends on the storage being filled with
371 zero bytes. Try running it with
374 if that improves the situation, this diagnosis has been confirmed.
375 If the program still misbehaves,
376 the likely problem is accessing memory outside the allocated area,
377 more likely after than before the allocated area.
379 Alternatively, if the symptoms are not easy to reproduce, setting the
381 option may help provoke the problem.
383 In truly difficult cases, the
385 option, if supported by the kernel, can provide a detailed trace of
386 all calls made to these functions.
388 Unfortunately this implementation does not provide much detail about
389 the problems it detects, the performance impact for storing such information
390 would be prohibitive.
391 There are a number of allocation implementations available on the 'Net
392 which focus on detecting and pinpointing problems by trading performance
393 for extra sanity checks and detailed diagnostics.
394 .Sh DIAGNOSTIC MESSAGES
401 detect an error or warning condition,
402 a message will be printed to file descriptor STDERR_FILENO.
403 Errors will result in the process dumping core.
406 option is set, all warnings are treated as errors.
408 The following is a brief description of possible error messages and
411 .It "(ES): mumble mumble mumble"
412 The allocation functions were compiled with
414 defined, and an error was found during the additional error checking.
415 Consult the source code for further information.
416 .It "mmap(2) failed, check limits"
417 This most likely means that the system is dangerously overloaded or that
418 the process' limits are incorrectly specified.
419 .It "freelist is destroyed"
420 The internal free-list has been corrupted.
424 option was specified and an allocation of memory failed.
427 The following is a brief description of possible warning messages and
430 .It "chunk/page is already free"
431 The process attempted to
433 memory which had already been freed.
434 .It "junk pointer, ..."
435 A pointer specified to one of the allocation functions points outside the
436 bounds of the memory of which they are aware.
437 .It "malloc() has never been called"
438 No memory has been allocated,
439 yet something is being freed or
441 .It "modified (chunk-/page-) pointer"
442 The pointer passed to
447 .It "pointer to wrong page"
453 is trying to free does not reference a possible page.
455 A process has attempted to call an allocation function recursively.
456 This is not permitted. In particular, signal handlers should not
457 attempt to allocate memory.
458 .It "unknown char in MALLOC_OPTIONS"
459 An unknown option was specified.
462 option set, this warning is still only a warning.
470 .Pa /usr/share/doc/papers/malloc.ascii.gz
481 The present allocation implementation started out as a filesystem for a
482 drum attached to a 20bit binary challenged computer which was built
483 with discrete germanium transistors. It has since graduated to
484 handle primary storage rather than secondary.
485 It first appeared in its new shape and ability in
490 function first appeared in
493 .An Poul-Henning Kamp Aq phk@FreeBSD.org
495 The messages printed in case of problems provide no detail about the
498 It can be argued that returning a
500 pointer when asked to
501 allocate zero bytes is a silly response to a silly question.