]> git.saurik.com Git - apple/libc.git/blame - gen/malloc.3
Libc-763.11.tar.gz
[apple/libc.git] / gen / malloc.3
CommitLineData
224c7076 1.\" Copyright (c) 2006 Apple Computer, Inc. All rights reserved.
9385eb3d
A
2.\"
3.\" @APPLE_LICENSE_HEADER_START@
4.\"
5.\" The contents of this file constitute Original Code as defined in and
6.\" are subject to the Apple Public Source License Version 1.1 (the
7.\" "License"). You may not use this file except in compliance with the
8.\" License. Please obtain a copy of the License at
9.\" http://www.apple.com/publicsource and read it before using this file.
10.\"
11.\" This Original Code and all software distributed under the License are
12.\" distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
13.\" EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
14.\" INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
15.\" FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
16.\" License for the specific language governing rights and limitations
17.\" under the License.
18.\"
19.\" @APPLE_LICENSE_HEADER_END@
20.\"
34e8f829 21.Dd Aug 13, 2008
9385eb3d
A
22.Dt MALLOC 3
23.Os
24.Sh NAME
224c7076
A
25.Nm calloc ,
26.Nm free ,
27.Nm malloc ,
28.Nm realloc ,
29.Nm reallocf ,
30.Nm valloc
9385eb3d
A
31.Nd memory allocation
32.Sh SYNOPSIS
33.In stdlib.h
34.Ft void *
224c7076
A
35.Fo calloc
36.Fa "size_t count"
37.Fa "size_t size"
38.Fc
39.Ft void
40.Fo free
41.Fa "void *ptr"
42.Fc
9385eb3d 43.Ft void *
224c7076
A
44.Fo malloc
45.Fa "size_t size"
46.Fc
9385eb3d 47.Ft void *
224c7076
A
48.Fo realloc
49.Fa "void *ptr"
50.Fa "size_t size"
51.Fc
9385eb3d 52.Ft void *
224c7076
A
53.Fo reallocf
54.Fa "void *ptr"
55.Fa "size_t size"
56.Fc
59e0d9fe 57.Ft void *
224c7076
A
58.Fo valloc
59.Fa "size_t size"
60.Fc
9385eb3d
A
61.Sh DESCRIPTION
62The
63.Fn malloc ,
64.Fn calloc ,
65.Fn valloc ,
59e0d9fe 66.Fn realloc ,
9385eb3d 67and
59e0d9fe 68.Fn reallocf
9385eb3d
A
69functions allocate memory.
70The allocated memory is aligned such that it can be used for any data type,
224c7076 71including AltiVec- and SSE-related types.
9385eb3d
A
72The
73.Fn free
74function frees allocations that were created via the preceding allocation
75functions.
9385eb3d
A
76.Pp
77The
78.Fn malloc
79function allocates
80.Fa size
81bytes of memory and returns a pointer to the allocated memory.
9385eb3d
A
82.Pp
83The
84.Fn calloc
85function contiguously allocates enough space for
86.Fa count
87objects that are
88.Fa size
89bytes of memory each and returns a pointer to the allocated memory.
90The allocated memory is filled with bytes of value zero.
9385eb3d
A
91.Pp
92The
93.Fn valloc
94function allocates
95.Fa size
96bytes of memory and returns a pointer to the allocated memory.
97The allocated memory is aligned on a page boundary.
9385eb3d
A
98.Pp
99The
100.Fn realloc
101function tries to change the size of the allocation pointed to by
102.Fa ptr
103to
104.Fa size ,
224c7076 105and returns
9385eb3d
A
106.Fa ptr .
107If there is not enough room to enlarge the memory allocation pointed to by
108.Fa ptr ,
109.Fn realloc
110creates a new allocation, copies as much of the old data pointed to by
111.Fa ptr
112as will fit to the new allocation, frees the old allocation, and returns a
113pointer to the allocated memory.
224c7076
A
114If
115.Fa ptr
116is
117.Dv NULL ,
9385eb3d 118.Fn realloc
224c7076
A
119is identical to a call to
120.Fn malloc
121for
122.Fa size
123bytes.
124If
125.Fa size
126is zero and
9385eb3d 127.Fa ptr
224c7076
A
128is not
129.Dv NULL ,
130a new, minimum sized object is allocated and the original object is freed.
1f2f436a
A
131When extending a region allocated with calloc(3), realloc(3) does not guarantee
132that the additional memory is also zero-filled.
9385eb3d
A
133.Pp
134The
59e0d9fe
A
135.Fn reallocf
136function is identical to the
137.Fn realloc
138function, except that it
139will free the passed pointer when the requested memory cannot be allocated.
140This is a
141.Fx
142specific API designed to ease the problems with traditional coding styles
143for realloc causing memory leaks in libraries.
144.Pp
145The
9385eb3d
A
146.Fn free
147function deallocates the memory allocation pointed to by
34e8f829
A
148.Fa ptr . If
149.Fa ptr
150is a NULL pointer, no operation is performed.
9385eb3d 151.Sh RETURN VALUES
224c7076 152If successful,
9385eb3d 153.Fn calloc ,
224c7076
A
154.Fn malloc ,
155.Fn realloc ,
156.Fn reallocf ,
9385eb3d
A
157and
158.Fn valloc
159functions return a pointer to allocated memory.
160If there is an error, they return a
161.Dv NULL
162pointer and set
163.Va errno
164to
165.Er ENOMEM .
166.Pp
224c7076
A
167For
168.Fn realloc ,
169the input pointer is still valid if reallocation failed.
170For
171.Fn reallocf ,
172the input pointer will have been freed if reallocation failed.
9385eb3d
A
173.Pp
174The
175.Fn free
176function does not return a value.
177.Sh DEBUGGING ALLOCATION ERRORS
178A number of facilities are provided to aid in debugging allocation errors in
179applications.
180These facilities are primarily controlled via environment variables.
181The recognized environment variables and their meanings are documented below.
182.Sh ENVIRONMENT
183The following environment variables change the behavior of the
184allocation-related functions.
185.Bl -tag -width ".Ev MallocStackLoggingNoCompact"
186.It Ev MallocLogFile <f>
187Create/append messages to the given file path
188.Fa <f>
189instead of writing to the standard error.
190.It Ev MallocGuardEdges
191If set, add a guard page before and after each large block.
192.It Ev MallocDoNotProtectPrelude
193If set, do not add a guard page before large blocks,
194even if the
195.Ev MallocGuardEdges
196environment variable is set.
197.It Ev MallocDoNotProtectPostlude
198If set, do not add a guard page after large blocks,
199even if the
200.Ev MallocGuardEdges
201environment variable is set.
202.It Ev MallocStackLogging
203If set, record all stacks, so that tools like
204.Nm leaks
205can be used.
206.It Ev MallocStackLoggingNoCompact
207If set, record all stacks in a manner that is compatible with the
208.Nm malloc_history
209program.
34e8f829
A
210.It Ev MallocStackLoggingDirectory
211If set, records stack logs to the directory specified instead of saving them to the default location (/tmp).
9385eb3d 212.It Ev MallocScribble
34e8f829
A
213If set, fill memory that has been allocated with 0xaa bytes.
214This increases the likelihood that a program making assumptions about the contents of
215freshly allocated memory will fail.
216Also if set, fill memory that has been deallocated with 0x55 bytes.
9385eb3d
A
217This increases the likelihood that a program will fail due to accessing memory
218that is no longer allocated.
219.It Ev MallocCheckHeapStart <s>
220If set, specifies the number of allocations
221.Fa <s>
222to wait before begining periodic heap checks every
223.Fa <n>
224as specified by
225.Ev MallocCheckHeapEach .
226If
227.Ev MallocCheckHeapStart
228is set but
229.Ev MallocCheckHeapEach
230is not specified, the default check repetition is 1000.
231.It Ev MallocCheckHeapEach <n>
232If set, run a consistency check on the heap every
233.Fa <n>
234operations.
235.Ev MallocCheckHeapEach
236is only meaningful if
237.Ev MallocCheckHeapStart
238is also set.
239.It Ev MallocCheckHeapSleep <t>
240Sets the number of seconds to sleep (waiting for a debugger to attach) when
241.Ev MallocCheckHeapStart
242is set and a heap corruption is detected.
243The default is 100 seconds.
244Setting this to zero means not to sleep at all.
245Setting this to a negative number means to sleep (for the positive number of
246seconds) only the very first time a heap corruption is detected.
247.It Ev MallocCheckHeapAbort <b>
248When
249.Ev MallocCheckHeapStart
250is set and this is set to a non-zero value, causes
251.Xr abort 3
252to be called if a heap corruption is detected, instead of any sleeping.
224c7076
A
253.It Ev MallocErrorAbort
254If set, causes
9385eb3d 255.Xr abort 3
224c7076
A
256to be called if an error was encountered in
257.Xr malloc 3
258or
9385eb3d 259.Xr free 3
224c7076
A
260, such as a calling
261.Xr free 3
262on a pointer previously freed.
34e8f829
A
263.It Ev MallocCorruptionAbort
264Similar to
265.Ev
266MallocErrorAbort
267but will not abort in out of memory conditions, making it more useful to catch
268only those errors which will cause memory corruption.
269MallocCorruptionAbort is always set on 64-bit processes.
9385eb3d
A
270.It Ev MallocHelp
271If set, print a list of environment variables that are paid heed to by the
272allocation-related functions, along with short descriptions.
273The list should correspond to this documentation.
274.El
275.Sh DIAGNOSTIC MESSAGES
276.Sh SEE ALSO
277.Xr leaks 1 ,
278.Xr malloc_history 1 ,
224c7076 279.Xr abort 3 ,
34e8f829
A
280.Xr malloc_size 3 ,
281.Xr malloc_zone_malloc 3 ,
282.Xr posix_memalign 3 ,
283.Xr libgmalloc 3