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.49 2001/11/05 00:39:27 dd Exp $
43 .Nm malloc , calloc , realloc , free , reallocf
44 .Nd general purpose memory allocation functions
50 .Fn malloc "size_t size"
52 .Fn calloc "size_t number" "size_t size"
54 .Fn realloc "void *ptr" "size_t size"
56 .Fn reallocf "void *ptr" "size_t size"
62 .Fn \*(lp*_malloc_message\*(rp "char *p1" "char *p2" "char *p3" "char *p4"
69 The allocated space is suitably aligned (after possible pointer coercion)
70 for storage of any type of object.
71 If the space is at least
75 the returned memory will be page boundary aligned as well.
86 normally initialize the returned memory to zero bytes.
90 function allocates space for
96 The result is identical to calling
100 with the exception that the allocated memory is explicitly initialized
105 function changes the size of the previously allocated memory referenced by
110 The contents of the memory are unchanged up to the lesser of the new and
112 If the new size is larger,
113 the value of the newly allocated portion of the memory is undefined.
114 If the requested memory cannot be allocated,
117 the memory referenced by
119 is valid and unchanged.
126 function behaves identically to
128 for the specified size.
132 function call is identical to the realloc function call, except that it
133 will free the passed pointer when the requested memory cannot be allocated.
136 specific API designed to ease the problems with traditional coding styles
137 for realloc causing memory leaks in libraries.
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 allocation implementation.
154 The ``name'' of the file referenced by the symbolic link named
155 .Pa /etc/malloc.conf ,
156 the value of the environment variable
158 and the string pointed to by the global variable
160 will be interpreted, in that order, character by character as flags.
162 Most flags are single letters,
163 where uppercase indicates that the behavior is set, or on,
164 and lowercase means that the behavior is not set, or off.
165 .Bl -tag -width indent
167 All warnings (except for the warning about unknown
168 flags being set) become fatal.
169 The process will call
173 Each byte of new memory allocated by
178 as well as all memory returned by
183 will be initialized to 0xd0.
184 This options also sets the
187 This is intended for debugging and will impact performance negatively.
189 Pass a hint to the kernel about pages unused by the allocation functions.
190 This will help performance if the system is paging excessively. This
191 option is off by default.
197 functions to always reallocate memory even if the initial allocation was
199 This can substantially aid in compacting memory.
206 Consult the source for details on this option.
208 Attempting to allocate zero bytes will return a
212 (The default behavior is to make a minimal allocation and return a
214 This option is provided for System V compatibility.
215 This option is incompatible with the
219 Rather than return failure for any allocation function,
220 display a diagnostic message on stderr and cause the program to drop
223 This option should be set at compile time by including the following in
225 .Bd -literal -offset indent
226 _malloc_options = "X";
229 This option implicitly sets the
233 options, and then zeros out the bytes that were requested.
234 This is intended for debugging and will impact performance negatively.
236 Reduce the size of the cache by a factor of two.
237 The default cache size is 16 pages.
238 This option can be specified multiple times.
240 Double the size of the cache by a factor of two.
241 The default cache size is 16 pages.
242 This option can be specified multiple times.
249 options are intended for testing and debugging.
250 An application which changes its behavior when these options are used
253 To set a systemwide reduction of cache size, and to dump core whenever
256 .Bd -literal -offset indent
257 ln -s 'A<' /etc/malloc.conf
260 To specify in the source that a program does no return value checking
261 on calls to these functions:
262 .Bd -literal -offset indent
263 _malloc_options = "X";
266 The following environment variables affect the execution of the allocation
268 .Bl -tag -width ".Ev MALLOC_OPTIONS"
269 .It Ev MALLOC_OPTIONS
270 If the environment variable
272 is set, the characters it contains will be interpreted as flags to the
273 allocation functions.
280 functions return a pointer to the allocated memory if successful; otherwise
283 pointer is returned and
292 functions return a pointer, possibly identical to
294 to the allocated memory
295 if successful; otherwise a
297 pointer is returned, in which case the
300 is still available and intact.
301 In the case of memory allocation failure,
308 function returns no value.
309 .Sh DEBUGGING MALLOC PROBLEMS
310 The major difference between this implementation and other allocation
311 implementations is that the free pages are not accessed unless allocated,
312 and are aggressively returned to the kernel for reuse.
313 .Bd -ragged -offset indent
314 Most allocation implementations will store a data structure containing a
315 linked list in the free chunks of memory,
316 used to tie all the free memory together.
317 That can be suboptimal,
318 as every time the free-list is traversed,
319 the otherwise unused, and likely paged out,
320 pages are faulted into primary memory.
321 On systems which are paging,
322 this can result in a factor of five increase in the number of page-faults
326 A side effect of this architecture is that many minor transgressions on
327 the interface which would traditionally not be detected are in fact
328 detected. As a result, programs that have been running happily for
329 years may suddenly start to complain loudly, when linked with this
330 allocation implementation.
332 The first and most important thing to do is to set the
335 This option forces a coredump (if possible) at the first sign of trouble,
336 rather than the normal policy of trying to continue if at all possible.
338 It is probably also a good idea to recompile the program with suitable
339 options and symbols for debugger support.
341 If the program starts to give unusual results, coredump or generally behave
342 differently without emitting any of the messages listed in the next
343 section, it is likely because it depends on the storage being filled with
344 zero bytes. Try running it with
347 if that improves the situation, this diagnosis has been confirmed.
348 If the program still misbehaves,
349 the likely problem is accessing memory outside the allocated area,
350 more likely after than before the allocated area.
352 Alternatively, if the symptoms are not easy to reproduce, setting the
354 option may help provoke the problem.
356 In truly difficult cases, the
358 option, if supported by the kernel, can provide a detailed trace of
359 all calls made to these functions.
361 Unfortunately this implementation does not provide much detail about
362 the problems it detects, the performance impact for storing such information
363 would be prohibitive.
364 There are a number of allocation implementations available on the 'Net
365 which focus on detecting and pinpointing problems by trading performance
366 for extra sanity checks and detailed diagnostics.
367 .Sh DIAGNOSTIC MESSAGES
374 detect an error or warning condition,
375 a message will be printed to file descriptor STDERR_FILENO.
376 Errors will result in the process dumping core.
379 option is set, all warnings are treated as errors.
383 variable allows the programmer to override the function which emits
384 the text strings forming the errors and warnings if for some reason
385 the stderr filedescriptor is not suitable for this.
386 Please note that doing anything which tries to allocate memory in
387 this function will assure death of the process.
389 The following is a brief description of possible error messages and
393 .It "(ES): mumble mumble mumble"
394 The allocation functions were compiled with
396 defined, and an error was found during the additional error checking.
397 Consult the source code for further information.
398 .It "mmap(2) failed, check limits"
399 This most likely means that the system is dangerously overloaded or that
400 the process' limits are incorrectly specified.
401 .It "freelist is destroyed"
402 The internal free-list has been corrupted.
406 option was specified and an allocation of memory failed.
409 The following is a brief description of possible warning messages and
412 .It "chunk/page is already free"
413 The process attempted to
415 memory which had already been freed.
416 .It "junk pointer, ..."
417 A pointer specified to one of the allocation functions points outside the
418 bounds of the memory of which they are aware.
419 .It "malloc() has never been called"
420 No memory has been allocated,
421 yet something is being freed or
423 .It "modified (chunk-/page-) pointer"
424 The pointer passed to
429 .It "pointer to wrong page"
435 is trying to free does not reference a possible page.
437 A process has attempted to call an allocation function recursively.
438 This is not permitted. In particular, signal handlers should not
439 attempt to allocate memory.
440 .It "unknown char in MALLOC_OPTIONS"
441 An unknown option was specified.
444 option set, this warning is still only a warning.
452 .Pa /usr/share/doc/papers/malloc.ascii.gz
463 The present allocation implementation started out as a filesystem for a
464 drum attached to a 20bit binary challenged computer which was built
465 with discrete germanium transistors. It has since graduated to
466 handle primary storage rather than secondary.
467 It first appeared in its new shape and ability in
472 function first appeared in
475 .An Poul-Henning Kamp Aq phk@FreeBSD.org
477 The messages printed in case of problems provide no detail about the
480 It can be argued that returning a
482 pointer when asked to
483 allocate zero bytes is a silly response to a silly question.