1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
.\"
.\" The contents of this file are subject to the terms of the
.\" Common Development and Distribution License (the "License").
.\" You may not use this file except in compliance with the License.
.\"
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
.\" or http://www.opensolaris.org/os/licensing.
.\" See the License for the specific language governing permissions
.\" and limitations under the License.
.\"
.\" When distributing Covered Code, include this CDDL HEADER in each
.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
.\" If applicable, add the following below this CDDL HEADER, with the
.\" fields enclosed by brackets "[]" replaced with your own identifying
.\" information: Portions Copyright [yyyy] [name of copyright owner]
.\"
.\"
.\" Copyright 1989 AT&T
.\" Copyright (c) 2005, Sun Microsystems, Inc. All Rights Reserved.
.\"
.Dd March 10, 2017
.Dt MALLOC 3C
.Os
.Sh NAME
.Nm malloc ,
.Nm calloc ,
.Nm free ,
.Nm memalign ,
.Nm realloc ,
.Nm reallocarray ,
.Nm valloc ,
.Nm alloca
.Nd memory allocator
.Sh SYNOPSIS
.In stdlib.h
.Ft void *
.Fo malloc
.Fa "size_t size"
.Fc
.Ft void *
.Fo calloc
.Fa "size_t nelem"
.Fa "size_t elsize"
.Fc
.Ft void
.Fo free
.Fa "void *ptr"
.Fc
.Ft void *
.Fo memalign
.Fa "size_t alignment"
.Fa "size_t size"
.Fc
.Ft void *
.Fo realloc
.Fa "void *ptr"
.Fa "size_t size"
.Fc
.Ft void *
.Fo reallocarray
.Fa "void *ptr"
.Fa "size_t nelem"
.Fa "size_t elsize"
.Fc
.Ft void *
.Fo valloc
.Fa "size_t size"
.Fc
.In alloca.h
.Ft void *
.Fo alloca
.Fa "size_t size"
.Fc
.Sh DESCRIPTION
The
.Fn malloc
and
.Fn free
functions provide a simple, general-purpose memory allocation package.
The
.Fn malloc
function returns a pointer to a block of at least
.Fa size
bytes suitably aligned for any use.
If the space assigned by
.Fn malloc
is overrun, the results are undefined.
.Pp
The argument to
.Fn free
is a pointer to a block previously allocated by
.Fn malloc ,
.Fn calloc ,
.Fn realloc ,
or
.Fn reallocarray .
After
.Fn free
is executed, this space is made available for further allocation by the
application, though not returned to the system.
Memory is returned to the system only upon termination of the application.
If
.Fa ptr
is a null pointer, no action occurs.
If a random number is passed to
.Fn free ,
the results are undefined.
.Pp
The
.Fn calloc
function allocates space for an array of
.Fa nelem
elements of size
.Fa elsize .
The space is initialized to zeros.
.Pp
The
.Fn memalign
function allocates
.Fa size
bytes on a specified alignment boundary and returns a pointer to the allocated
block.
The value of the returned address is guaranteed to be an even multiple of
.Fa alignment .
The value of
.Fa alignment
must be a power of two and must be greater than or equal to the size of a word.
.Pp
The
.Fn realloc
function changes the size of the block pointed to by
.Fa ptr
to
.Fa size
bytes and returns a pointer to the
.Pq possibly moved
block.
The contents will be unchanged up to the lesser of the new and old sizes.
If the new size of the block requires movement of the block, the space for the
previous instantiation of the block is freed.
If the new size is larger, the contents of the newly allocated portion of the
block are unspecified.
If
.Fa ptr
is
.Dv NULL ,
.Fn realloc
behaves like
.Fn malloc
for the specified size.
If
.Fa size
is 0 and
.Fa ptr
is not a null pointer, the space pointed to is freed.
.Pp
The
.Fn reallocarray
function is similar to
.Fn realloc ,
but operates on
.Fa nelem
elements of size
.Fa elsize
and checks for overflow in
.Fa nelem Ns * Ns Fa elsize
calculation.
.Pp
The
.Fn valloc
function has the same effect as
.Fn malloc ,
except that the allocated memory will be aligned to a multiple of the value
returned by
.Nm sysconf Ns Pq Dv _SC_PAGESIZE .
.Pp
The
.Fn alloca
function allocates
.Fa size
bytes of space in the stack frame of the caller, and returns a pointer to the
allocated block.
This temporary space is automatically freed when the caller returns.
If the allocated block is beyond the current stack limit, the resulting behavior
is undefined.
.Sh RETURN VALUES
Upon successful completion, each of the allocation functions returns a pointer
to space suitably aligned
.Pq after possible pointer coercion
for storage of any type of object.
.Pp
If there is no available memory,
.Fn malloc ,
.Fn realloc ,
.Fn reallocarray ,
.Fn memalign ,
.Fn valloc ,
and
.Fn calloc
return a null pointer.
.Pp
When
.Fn realloc
or
.Fn reallocarray
is called with
.Fa size
> 0 and returns
.Dv NULL ,
the block pointed to by
.Fa ptr
is left intact.
If
.Fa size ,
.Fa nelem ,
or
.Fa elsize
is 0, either a null pointer or a unique pointer that can be passed to
.Fn free
is returned.
.Pp
If
.Fn malloc ,
.Fn calloc ,
.Fn realloc ,
or
.Fn reallocarray
returns unsuccessfully,
.Va errno
will be set to indicate the error.
The
.Fn free
function does not set
.Va errno .
.Sh ERRORS
The
.Fn malloc ,
.Fn calloc ,
.Fn realloc ,
and
.Fn reallocarray
functions will fail if:
.Bl -tag -width "ENOMEM"
.It Er ENOMEM
The physical limits of the system are exceeded by
.Fa size
bytes of memory which cannot be allocated, or there's integer overflow in
.Fn reallocarray .
.It Er EAGAIN
There is not enough memory available to allocate
.Fa size
bytes of memory; but the application could try again later.
.El
.Sh USAGE
Portable applications should avoid using
.Fn valloc
but should instead use
.Fn malloc
or
.Xr mmap 2 .
On systems with a large page size, the number of successful
.Fn valloc
operations might be 0.
.Pp
These default memory allocation routines are safe for use in multithreaded
applications but are not scalable.
Concurrent accesses by multiple threads are single-threaded through the use of a
single lock.
Multithreaded applications that make heavy use of dynamic memory allocation
should be linked with allocation libraries designed for concurrent access, such
as
.Xr libumem 3LIB
or
.Xr libmtmalloc 3LIB .
Applications that want to avoid using heap allocations
.Pq with Xr brk 2
can do so by using either
.Xr libumem 3LIB
or
.Xr libmapmalloc 3LIB .
The allocation libraries
.Xr libmalloc 3LIB
and
.Xr libbsdmalloc 3LIB
are available for special needs.
.Pp
Comparative features of the various allocation libraries can be found in the
.Xr umem_alloc 3MALLOC
manual page.
.Sh INTERFACE STABILITY
The
.Fn malloc ,
.Fn calloc ,
.Fn free ,
.Fn realloc ,
.Fn valloc
functions are
.Sy Standard.
.Pp
The
.Fn reallocarray
function is
.Sy Committed .
.Pp
The
.Fn memalign
and
.Fn alloca
functions are
.Sy Stable .
.Sh MT-LEVEL
.Sy Safe.
.Sh SEE ALSO
.Xr brk 2 ,
.Xr getrlimit 2 ,
.Xr libbsdmalloc 3LIB ,
.Xr libmalloc 3LIB ,
.Xr libmapmalloc 3LIB ,
.Xr libmtmalloc 3LIB ,
.Xr libumem 3LIB ,
.Xr umem_alloc 3MALLOC ,
.Xr watchmalloc 3MALLOC ,
.Xr attributes 5
.Sh WARNINGS
Undefined results will occur if the size requested for a block of memory
exceeds the maximum size of a process's heap, which can be obtained with
.Xr getrlimit 2 .
.Pp
The
.Fn alloca
function is machine-, compiler-, and most of all, system-dependent.
Its use is strongly discouraged.
|