1 .\" $NetBSD: malloc.3,v 1.38 2010/05/03 08:23:20 jruoho 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. All advertising materials mentioning features or use of this software
19 .\" must display the following acknowledgement:
20 .\" This product includes software developed by the University of
21 .\" California, Berkeley and its contributors.
22 .\" 4. Neither the name of the University nor the names of its contributors
23 .\" may be used to endorse or promote products derived from this software
24 .\" without specific prior written permission.
26 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 .\" @(#)malloc.3 8.1 (Berkeley) 6/4/93
39 .\" $FreeBSD: src/lib/libc/stdlib/malloc.3,v 1.73 2007/06/15 22:32:33 jasone Exp $
50 .Nd general purpose memory allocation functions
56 .Fn malloc "size_t size"
58 .Fn calloc "size_t number" "size_t size"
60 .Fn realloc "void *ptr" "size_t size"
62 .Fn reallocf "void *ptr" "size_t size"
70 bytes of uninitialized memory.
71 The allocated space is suitably aligned (after possible pointer coercion)
72 for storage of any type of object.
73 If the space is at least
77 the returned memory will be page boundary aligned as well.
81 function allocates space for
87 The result is identical to calling
91 with the exception that the allocated memory is explicitly initialized
96 function changes the size of the previously allocated memory referenced by
101 The contents of the memory are unchanged up to the lesser of the new and
103 If the new size is larger,
104 the value of the newly allocated portion of the memory is undefined.
105 Upon success, the memory referenced by
107 is freed and a pointer to the newly allocated memory is returned.
110 may move the memory allocation, resulting in a different return value than
118 function behaves identically to
120 for the specified size.
124 function call is identical to the realloc function call, except that it
125 will free the passed pointer when the requested memory cannot be allocated.
130 specific API designed to ease the problems with traditional coding styles
131 for realloc causing memory leaks in libraries.
135 function causes the allocated memory referenced by
137 to be made available for future allocations.
143 .Sh IMPLEMENTATION NOTES
146 implementation is based on a port of the
148 kernel slab allocator, appropriately modified for a user process
151 The slab allocator breaks memory allocations up to 8KB into 80 zones.
152 Each zone represents a fixed allocation size in multiples of some
154 The chunking is a power-of-2 but the fixed allocation size is not.
155 For example, a 1025-byte request is allocated out of the zone with a
156 chunking of 128, thus in multiples of 1152 bytes.
157 The minimum chunking, used for allocations in the 0-127 byte range,
158 is 8 bytes (16 of the 80 zones).
159 Beyond that the power-of-2 chunking is between 1/8 and 1/16 of the
160 minimum allocation size for any given zone.
162 As a special case any power-of-2-sized allocation within the zone
163 limit (8K) will be aligned to the same power-of-2 rather than that
164 zone's (smaller) chunking.
165 This is not something you can depend upon for
167 but it is used internally to optimize
168 .Xr posix_memalign 3 .
170 Each zone reserves memory in 64KB blocks.
171 Actual memory use tends to be significantly less as only the pages
172 actually needed are faulted in.
173 Allocations larger than 8K are managed using
175 and tracked with a hash table.
177 The zone mechanism results in well-fitted allocations with little
178 waste in a long-running environment which makes a lot of allocations.
179 Short-running environments which do not make many allocations will see
180 a bit of extra bloat due to the large number of zones but it will
181 be almost unnoticeable in the grand scheme of things.
182 To reduce bloat further the normal randomized start offset implemented
183 in the kernel version of the allocator to improve L1 cache fill is
184 disabled in the libc version.
186 The zone mechanism also has the nice side effect of greatly reducing
187 fragmentation over the original
191 is directly supported by keeping track of newly-allocated zones which
192 will be demand-zero'd by the system.
193 If the allocation is known to be zero'd we do not bother
196 If it is a reused allocation we
200 threading is supported by duplicating the primary structure.
203 which is unable to immediately acquire a mutex on the last primary
204 structure it used will switch to a different primary structure.
205 At the moment this is more of a quick hack than a solution, but it works.
211 functions return a pointer to the allocated memory if successful; otherwise
214 pointer is returned and
223 functions return a pointer, possibly identical to
225 to the allocated memory
226 if successful; otherwise a
228 pointer is returned, and
232 if the error was the result of an allocation failure.
235 function always leaves the original buffer intact
236 when an error occurs, whereas
238 deallocates it in this case.
242 function returns no value.
246 be careful to avoid the following idiom:
247 .Bd -literal -offset indent
248 if ((p = malloc(number * size)) == NULL)
249 err(EXIT_FAILURE, "malloc");
252 The multiplication may lead to an integer overflow.
259 must be used, be sure to test for overflow:
260 .Bd -literal -offset indent
261 if (size && number > SIZE_MAX / size) {
263 err(EXIT_FAILURE, "allocation");
269 one must be careful to avoid the following idiom:
270 .Bd -literal -offset indent
273 if ((p = realloc(p, nsize)) == NULL)
277 Do not adjust the variable describing how much memory has been allocated
278 until it is known that the allocation has been successful.
279 This can cause aberrant program behavior if the incorrect size value is used.
280 In most cases, the above example will also leak memory.
281 As stated earlier, a return value of
283 indicates that the old object still remains allocated.
284 Better code looks like this:
285 .Bd -literal -offset indent
288 if ((p2 = realloc(p, newsize)) == NULL) {
307 detect an error, a message will be printed to file descriptor
309 and the process will dump core.
331 function first appeared in
336 implementation is based on the kernel's slab allocator (see
337 .Xr posix_memalign 3 Ap s
338 .Sx IMPLEMENTATION NOTES ) .