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. 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 $
46 .Nd general purpose memory allocation functions
52 .Fn malloc "size_t size"
54 .Fn calloc "size_t number" "size_t size"
56 .Fn realloc "void *ptr" "size_t size"
58 .Fn reallocf "void *ptr" "size_t size"
66 bytes of uninitialized memory.
67 The allocated space is suitably aligned (after possible pointer coercion)
68 for storage of any type of object.
69 If the space is at least
73 the returned memory will be page boundary aligned as well.
77 function allocates space for
83 The result is identical to calling
87 with the exception that the allocated memory is explicitly initialized
92 function changes the size of the previously allocated memory referenced by
97 The contents of the memory are unchanged up to the lesser of the new and
99 If the new size is larger,
100 the value of the newly allocated portion of the memory is undefined.
101 Upon success, the memory referenced by
103 is freed and a pointer to the newly allocated memory is returned.
106 may move the memory allocation, resulting in a different return value than
114 function behaves identically to
116 for the specified size.
120 function call is identical to the realloc function call, except that it
121 will free the passed pointer when the requested memory cannot be allocated.
126 specific API designed to ease the problems with traditional coding styles
127 for realloc causing memory leaks in libraries.
131 function causes the allocated memory referenced by
133 to be made available for future allocations.
139 .Sh IMPLEMENTATION NOTES
142 implementation is based on a port of the
144 kernel slab allocator, appropriately modified for a user process
147 The slab allocator breaks memory allocations up to 8KB into 80 zones.
148 Each zone represents a fixed allocation size in multiples of some
150 The chunking is a power-of-2 but the fixed allocation size is not.
151 For example, a 1025-byte request is allocated out of the zone with a
152 chunking of 128, thus in multiples of 1152 bytes.
153 The minimum chunking, used for allocations in the 0-127 byte range,
154 is 8 bytes (16 of the 80 zones).
155 Beyond that the power-of-2 chunking is between 1/8 and 1/16 of the
156 minimum allocation size for any given zone.
158 As a special case any power-of-2-sized allocation within the zone
159 limit (8K) will be aligned to the same power-of-2 rather than that
160 zone's (smaller) chunking.
161 This is not something you can depend upon for
163 but it is used internally to optimize
164 .Xr posix_memalign 3 .
166 Each zone reserves memory in 64KB blocks.
167 Actual memory use tends to be significantly less as only the pages
168 actually needed are faulted in.
169 Allocations larger than 8K are managed using
171 and tracked with a hash table.
173 The zone mechanism results in well-fitted allocations with little
174 waste in a long-running environment which makes a lot of allocations.
175 Short-running environments which do not make many allocations will see
176 a bit of extra bloat due to the large number of zones but it will
177 be almost unnoticeable in the grand scheme of things.
178 To reduce bloat further the normal randomized start offset implemented
179 in the kernel version of the allocator to improve L1 cache fill is
180 disabled in the libc version.
182 The zone mechanism also has the nice side effect of greatly reducing
183 fragmentation over the original
187 is directly supported by keeping track of newly-allocated zones which
188 will be demand-zero'd by the system.
189 If the allocation is known to be zero'd we do not bother
192 If it is a reused allocation we
196 threading is supported by duplicating the primary structure.
199 which is unable to immediately acquire a mutex on the last primary
200 structure it used will switch to a different primary structure.
201 At the moment this is more of a quick hack than a solution, but it works.
207 functions return a pointer to the allocated memory if successful; otherwise
210 pointer is returned and
219 functions return a pointer, possibly identical to
221 to the allocated memory
222 if successful; otherwise a
224 pointer is returned, and
228 if the error was the result of an allocation failure.
231 function always leaves the original buffer intact
232 when an error occurs, whereas
234 deallocates it in this case.
238 function returns no value.
242 be careful to avoid the following idiom:
243 .Bd -literal -offset indent
244 if ((p = malloc(number * size)) == NULL)
245 err(EXIT_FAILURE, "malloc");
248 The multiplication may lead to an integer overflow.
255 must be used, be sure to test for overflow:
256 .Bd -literal -offset indent
257 if (size && number > SIZE_MAX / size) {
259 err(EXIT_FAILURE, "allocation");
265 one must be careful to avoid the following idiom:
266 .Bd -literal -offset indent
269 if ((p = realloc(p, nsize)) == NULL)
273 Do not adjust the variable describing how much memory has been allocated
274 until it is known that the allocation has been successful.
275 This can cause aberrant program behavior if the incorrect size value is used.
276 In most cases, the above example will also leak memory.
277 As stated earlier, a return value of
279 indicates that the old object still remains allocated.
280 Better code looks like this:
281 .Bd -literal -offset indent
284 if ((p2 = realloc(p, newsize)) == NULL) {
303 detect an error, a message will be printed to file descriptor
305 and the process will dump core.
328 function first appeared in
333 implementation is based on the kernel's slab allocator (see
334 .Xr posix_memalign 3 Ap s
335 .Sx IMPLEMENTATION NOTES ) .