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