]> git.saurik.com Git - apple/libc.git/blame_incremental - gen/malloc.3
Libc-320.1.3.tar.gz
[apple/libc.git] / gen / malloc.3
... / ...
CommitLineData
1.\" Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
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.\"
21.Dd November 21, 2002
22.Dt MALLOC 3
23.Os
24.Sh NAME
25.Nm malloc , calloc , valloc , realloc , free , malloc_size , malloc_good_size
26.Nd memory allocation
27.Sh SYNOPSIS
28.In stdlib.h
29.Ft void *
30.Fn malloc "size_t size"
31.Ft void *
32.Fn calloc "size_t count" "size_t size"
33.Ft void *
34.Fn valloc "size_t size"
35.Ft void *
36.Fn realloc "void *ptr" "size_t size"
37.Ft void
38.Fn free "void *ptr"
39.Ft size_t
40.Fn malloc_size "void *ptr"
41.Ft size_t
42.Fn malloc_good_size "size_t size"
43.Sh DESCRIPTION
44The
45.Fn malloc ,
46.Fn calloc ,
47.Fn valloc ,
48and
49.Fn realloc
50functions allocate memory.
51The allocated memory is aligned such that it can be used for any data type,
52including AltiVec-related types.
53The
54.Fn free
55function frees allocations that were created via the preceding allocation
56functions.
57The
58.Fn malloc_size
59and
60.Fn malloc_good_size
61functions provide information related to the amount of padding space at the end
62of allocations.
63.Pp
64The
65.Fn malloc
66function allocates
67.Fa size
68bytes of memory and returns a pointer to the allocated memory.
69.Fn malloc
70returns a
71.Dv NULL
72pointer if there is an error.
73.Pp
74The
75.Fn calloc
76function contiguously allocates enough space for
77.Fa count
78objects that are
79.Fa size
80bytes of memory each and returns a pointer to the allocated memory.
81The allocated memory is filled with bytes of value zero.
82.Fn calloc
83returns a
84.Dv NULL
85pointer if there is an error.
86.Pp
87The
88.Fn valloc
89function allocates
90.Fa size
91bytes of memory and returns a pointer to the allocated memory.
92The allocated memory is aligned on a page boundary.
93.Fn valloc
94returns a
95.Dv NULL
96pointer if there is an error.
97.Pp
98The
99.Fn realloc
100function tries to change the size of the allocation pointed to by
101.Fa ptr
102to
103.Fa size ,
104and return
105.Fa ptr .
106If there is not enough room to enlarge the memory allocation pointed to by
107.Fa ptr ,
108.Fn realloc
109creates a new allocation, copies as much of the old data pointed to by
110.Fa ptr
111as will fit to the new allocation, frees the old allocation, and returns a
112pointer to the allocated memory.
113.Fn realloc
114returns a
115.Dv NULL
116pointer if there is an error, and the allocation pointed to by
117.Fa ptr
118is still valid.
119.Pp
120The
121.Fn free
122function deallocates the memory allocation pointed to by
123.Fa ptr .
124.Pp
125The
126.Fn malloc_size
127function
128returns the size of the memory block that backs the allocation pointed to by
129.Fa ptr .
130The memory block size is always at least as large as the allocation it backs,
131and may be larger.
132.Pp
133The
134.Fn malloc_good_size
135function rounds
136.Fa size
137up to a value that the allocator implementation can allocate without adding any
138padding and returns that rounded up value.
139.Sh RETURN VALUES
140If successful, the
141.Fn malloc ,
142.Fn calloc ,
143and
144.Fn valloc
145functions return a pointer to allocated memory.
146If there is an error, they return a
147.Dv NULL
148pointer and set
149.Va errno
150to
151.Er ENOMEM .
152.Pp
153If successful, the
154.Fn realloc
155function returns a pointer to allocated memory.
156If there is an error, it returns a
157.Dv NULL
158pointer and sets
159.Va errno
160to
161.Er ENOMEM .
162.Pp
163The
164.Fn free
165function does not return a value.
166.Sh DEBUGGING ALLOCATION ERRORS
167A number of facilities are provided to aid in debugging allocation errors in
168applications.
169These facilities are primarily controlled via environment variables.
170The recognized environment variables and their meanings are documented below.
171.Sh ENVIRONMENT
172The following environment variables change the behavior of the
173allocation-related functions.
174.Bl -tag -width ".Ev MallocStackLoggingNoCompact"
175.It Ev MallocLogFile <f>
176Create/append messages to the given file path
177.Fa <f>
178instead of writing to the standard error.
179.It Ev MallocGuardEdges
180If set, add a guard page before and after each large block.
181.It Ev MallocDoNotProtectPrelude
182If set, do not add a guard page before large blocks,
183even if the
184.Ev MallocGuardEdges
185environment variable is set.
186.It Ev MallocDoNotProtectPostlude
187If set, do not add a guard page after large blocks,
188even if the
189.Ev MallocGuardEdges
190environment variable is set.
191.It Ev MallocStackLogging
192If set, record all stacks, so that tools like
193.Nm leaks
194can be used.
195.It Ev MallocStackLoggingNoCompact
196If set, record all stacks in a manner that is compatible with the
197.Nm malloc_history
198program.
199.It Ev MallocScribble
200If set, fill memory that has been deallocated with 0x55 bytes.
201This increases the likelihood that a program will fail due to accessing memory
202that is no longer allocated.
203.It Ev MallocCheckHeapStart <s>
204If set, specifies the number of allocations
205.Fa <s>
206to wait before begining periodic heap checks every
207.Fa <n>
208as specified by
209.Ev MallocCheckHeapEach .
210If
211.Ev MallocCheckHeapStart
212is set but
213.Ev MallocCheckHeapEach
214is not specified, the default check repetition is 1000.
215.It Ev MallocCheckHeapEach <n>
216If set, run a consistency check on the heap every
217.Fa <n>
218operations.
219.Ev MallocCheckHeapEach
220is only meaningful if
221.Ev MallocCheckHeapStart
222is also set.
223.It Ev MallocCheckHeapSleep <t>
224Sets the number of seconds to sleep (waiting for a debugger to attach) when
225.Ev MallocCheckHeapStart
226is set and a heap corruption is detected.
227The default is 100 seconds.
228Setting this to zero means not to sleep at all.
229Setting this to a negative number means to sleep (for the positive number of
230seconds) only the very first time a heap corruption is detected.
231.It Ev MallocCheckHeapAbort <b>
232When
233.Ev MallocCheckHeapStart
234is set and this is set to a non-zero value, causes
235.Xr abort 3
236to be called if a heap corruption is detected, instead of any sleeping.
237.It Ev MallocBadFreeAbort <b>
238If set to a non-zero value, causes
239.Xr abort 3
240to be called if the pointer passed to
241.Xr free 3
242was previously freed, or is otherwise illegal.
243.It Ev MallocHelp
244If set, print a list of environment variables that are paid heed to by the
245allocation-related functions, along with short descriptions.
246The list should correspond to this documentation.
247.El
248.Sh DIAGNOSTIC MESSAGES
249.Sh SEE ALSO
250.Xr leaks 1 ,
251.Xr malloc_history 1 ,
252.Xr abort 3
253.Pa /Developer/Documentation/ReleaseNotes/MallocOptions.html