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