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.4 2004/07/08 00:27:11 hmp Exp $
44 .Nm malloc , calloc , realloc , free , reallocf
45 .Nd general purpose memory allocation functions
51 .Fn malloc "size_t size"
53 .Fn calloc "size_t number" "size_t size"
55 .Fn realloc "void *ptr" "size_t size"
57 .Fn reallocf "void *ptr" "size_t size"
68 The allocated space is suitably aligned (after possible pointer coercion)
69 for storage of any type of object.
70 If the space is at least
74 the returned memory will be page boundary aligned as well.
85 normally initialize the returned memory to zero bytes.
89 function allocates space for
95 The result is identical to calling
99 with the exception that the allocated memory is explicitly initialized
104 function changes the size of the previously allocated memory referenced by
109 The contents of the memory are unchanged up to the lesser of the new and
111 If the new size is larger,
112 the value of the newly allocated portion of the memory is undefined.
113 If the requested memory cannot be allocated,
116 the memory referenced by
118 is valid and unchanged.
125 function behaves identically to
127 for the specified size.
131 function call is identical to the realloc function call, except that it
132 will free the passed pointer when the requested memory cannot be allocated.
137 specific API designed to ease the problems with traditional coding styles
138 for realloc causing memory leaks in libraries.
142 function causes the allocated memory referenced by
144 to be made available for future allocations.
151 Once, when the first call is made to one of these memory allocation
152 routines, various flags will be set or reset, which affect the
153 workings of this allocation implementation.
155 The ``name'' of the file referenced by the symbolic link named
156 .Pa /etc/malloc.conf ,
157 the value of the environment variable
159 and the string pointed to by the global variable
161 will be interpreted, in that order, character by character as flags.
163 Most flags are single letters,
164 where uppercase indicates that the behavior is set, or on,
165 and lowercase means that the behavior is not set, or off.
166 .Bl -tag -width indent
168 All warnings (except for the warning about unknown
169 flags being set) become fatal.
170 The process will call
174 Each byte of new memory allocated by
179 as well as all memory returned by
184 will be initialized to 0xd0.
185 This options also sets the
188 This is intended for debugging and will impact performance negatively.
190 Pass a hint to the kernel about pages unused by the allocation functions.
191 This will help performance if the system is paging excessively. This
192 option is off by default.
198 functions to always reallocate memory even if the initial allocation was
200 This can substantially aid in compacting memory.
207 Consult the source for details on this option.
209 Attempting to allocate zero bytes will return a
213 (The default behavior is to make a minimal allocation and return a
215 This option is provided for System V compatibility.
216 This option is incompatible with the
220 Rather than return failure for any allocation function,
221 display a diagnostic message on stderr and cause the program to drop
224 This option should be set at compile time by including the following in
226 .Bd -literal -offset indent
227 extern char *malloc_options;
228 malloc_options = "X";
231 This option implicitly sets the
235 options, and then zeros out the bytes that were requested.
236 This is intended for debugging and will impact performance negatively.
238 Reduce the size of the cache by a factor of two.
239 The default cache size is 16 pages.
240 This option can be specified multiple times.
242 Double the size of the cache by a factor of two.
243 The default cache size is 16 pages.
244 This option can be specified multiple times.
251 options are intended for testing and debugging.
252 An application which changes its behavior when these options are used
255 To set a systemwide reduction of cache size, and to dump core whenever
258 .Bd -literal -offset indent
259 ln -s 'A<' /etc/malloc.conf
262 To specify in the source that a program does no return value checking
263 on calls to these functions:
264 .Bd -literal -offset indent
265 extern char *malloc_options;
266 malloc_options = "X";
269 The following environment variables affect the execution of the allocation
271 .Bl -tag -width ".Ev MALLOC_OPTIONS"
272 .It Ev MALLOC_OPTIONS
273 If the environment variable
275 is set, the characters it contains will be interpreted as flags to the
276 allocation functions.
283 functions return a pointer to the allocated memory if successful; otherwise
286 pointer is returned and
295 functions return a pointer, possibly identical to
297 to the allocated memory
298 if successful; otherwise a
300 pointer is returned, and
304 if the error was the result of an allocation failure.
307 function always leaves the original buffer intact
308 when an error occurs, whereas
310 deallocates it in this case.
314 function returns no value.
315 .Sh DEBUGGING MALLOC PROBLEMS
316 The major difference between this implementation and other allocation
317 implementations is that the free pages are not accessed unless allocated,
318 and are aggressively returned to the kernel for reuse.
319 .Bd -ragged -offset indent
320 Most allocation implementations will store a data structure containing a
321 linked list in the free chunks of memory,
322 used to tie all the free memory together.
323 That can be suboptimal,
324 as every time the free-list is traversed,
325 the otherwise unused, and likely paged out,
326 pages are faulted into primary memory.
327 On systems which are paging,
328 this can result in a factor of five increase in the number of page-faults
332 A side effect of this architecture is that many minor transgressions on
333 the interface which would traditionally not be detected are in fact
334 detected. As a result, programs that have been running happily for
335 years may suddenly start to complain loudly, when linked with this
336 allocation implementation.
338 The first and most important thing to do is to set the
341 This option forces a coredump (if possible) at the first sign of trouble,
342 rather than the normal policy of trying to continue if at all possible.
344 It is probably also a good idea to recompile the program with suitable
345 options and symbols for debugger support.
347 If the program starts to give unusual results, coredump or generally behave
348 differently without emitting any of the messages listed in the next
349 section, it is likely because it depends on the storage being filled with
350 zero bytes. Try running it with
353 if that improves the situation, this diagnosis has been confirmed.
354 If the program still misbehaves,
355 the likely problem is accessing memory outside the allocated area,
356 more likely after than before the allocated area.
358 Alternatively, if the symptoms are not easy to reproduce, setting the
360 option may help provoke the problem.
362 In truly difficult cases, the
364 option, if supported by the kernel, can provide a detailed trace of
365 all calls made to these functions.
367 Unfortunately this implementation does not provide much detail about
368 the problems it detects, the performance impact for storing such information
369 would be prohibitive.
370 There are a number of allocation implementations available on the 'Net
371 which focus on detecting and pinpointing problems by trading performance
372 for extra sanity checks and detailed diagnostics.
373 .Sh DIAGNOSTIC MESSAGES
380 detect an error or warning condition,
381 a message will be printed to file descriptor STDERR_FILENO.
382 Errors will result in the process dumping core.
385 option is set, all warnings are treated as errors.
387 The following is a brief description of possible error messages and
391 .It "(ES): mumble mumble mumble"
392 The allocation functions were compiled with
394 defined, and an error was found during the additional error checking.
395 Consult the source code for further information.
396 .It "mmap(2) failed, check limits"
397 This most likely means that the system is dangerously overloaded or that
398 the process' limits are incorrectly specified.
399 .It "freelist is destroyed"
400 The internal free-list has been corrupted.
404 option was specified and an allocation of memory failed.
407 The following is a brief description of possible warning messages and
410 .It "chunk/page is already free"
411 The process attempted to
413 memory which had already been freed.
414 .It "junk pointer, ..."
415 A pointer specified to one of the allocation functions points outside the
416 bounds of the memory of which they are aware.
417 .It "malloc() has never been called"
418 No memory has been allocated,
419 yet something is being freed or
421 .It "modified (chunk-/page-) pointer"
422 The pointer passed to
427 .It "pointer to wrong page"
433 is trying to free does not reference a possible page.
435 A process has attempted to call an allocation function recursively.
436 This is not permitted. In particular, signal handlers should not
437 attempt to allocate memory.
438 .It "unknown char in MALLOC_OPTIONS"
439 An unknown option was specified.
442 option set, this warning is still only a warning.
450 .Pa /usr/share/doc/papers/malloc.ascii.gz
461 The present allocation implementation started out as a filesystem for a
462 drum attached to a 20bit binary challenged computer which was built
463 with discrete germanium transistors. It has since graduated to
464 handle primary storage rather than secondary.
465 It first appeared in its new shape and ability in
470 function first appeared in
473 .An Poul-Henning Kamp Aq phk@FreeBSD.org
475 The messages printed in case of problems provide no detail about the
478 It can be argued that returning a
480 pointer when asked to
481 allocate zero bytes is a silly response to a silly question.