'\" te .\" Copyright 1989 AT&T. Copyright (c) 2005, Sun Microsystems, Inc. All Rights Reserved. .\" Copyright 2017 Nexenta Systems, Inc. .\" 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] .TH MALLOC 3C "July 28, 2017" .SH NAME malloc, calloc, free, freezero, memalign, realloc, recallocarray, valloc, alloca \- memory allocator .SH SYNOPSIS .LP .nf #include \fBvoid *\fR\fBmalloc\fR(\fBsize_t\fR \fIsize\fR); .fi .LP .nf \fBvoid *\fR\fBcalloc\fR(\fBsize_t\fR \fInelem\fR, \fBsize_t\fR \fIelsize\fR); .fi .LP .nf \fBvoid\fR \fBfree\fR(\fBvoid *\fR\fIptr\fR); .fi .LP .nf \fBvoid\fR \fBfreezero\fR(\fBvoid *\fR\fIptr\fR, \fBsize_t\fR \fIsize\fR); .fi .LP .nf \fBvoid *\fR\fBmemalign\fR(\fBsize_t\fR \fIalignment\fR, \fBsize_t\fR \fIsize\fR); .fi .LP .nf \fBvoid *\fR\fBrealloc\fR(\fBvoid *\fR\fIptr\fR, \fBsize_t\fR \fIsize\fR); .fi .LP .nf \fBvoid *\fR\fBrecallocarray\fR(\fBvoid *\fR\fIptr\fR, \fBsize_t\fR \fIoldnelem\fR, \fBsize_t\fR \fInewnelem\fR, \fBsize_t\fR \fIelsize\fR); .fi .LP .nf \fBvoid *\fR\fBvalloc\fR(\fBsize_t\fR \fIsize\fR); .fi .LP .nf #include \fBvoid *\fR\fBalloca\fR(\fBsize_t\fR \fIsize\fR); .fi .SH DESCRIPTION .sp .LP The \fBmalloc()\fR and \fBfree()\fR functions provide a simple, general-purpose memory allocation package. The \fBmalloc()\fR function returns a pointer to a block of at least \fIsize\fR bytes suitably aligned for any use. If the space assigned by \fBmalloc()\fR is overrun, the results are undefined. .sp .LP The argument to \fBfree()\fR is a pointer to a block previously allocated by \fBmalloc()\fR, \fBcalloc()\fR, \fBrealloc()\fR, or \fBrecallocarray()\fR. After \fBfree()\fR 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 \fIptr\fR is a null pointer, no action occurs. If a random number is passed to \fBfree()\fR, the results are undefined. .sp .LP The \fBfreezero()\fR function is similar to the \fBfree()\fR function except it ensures memory is explicitly discarded. If \fIptr\fR is \fINULL\fR no action occurs. If \fIptr\fR is not \fINULL\fR the \fIsize\fR argument must be equal or smaller than the size of the earlier allocation that returned \fIptr\fR. \fBfreezero()\fR guarantees the memory range starting at \fIptr\fR with length \fIsize\fR is discarded while deallocating the whole object originally allocated. .sp .LP The \fBcalloc()\fR function allocates space for an array of \fInelem\fR elements of size \fIelsize\fR. The space is initialized to zeros. .sp .LP The \fBmemalign()\fR function allocates \fIsize\fR 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 \fIalignment\fR. The value of \fIalignment\fR must be a power of two and must be greater than or equal to the size of a word. .sp .LP The \fBrealloc()\fR function changes the size of the block pointed to by \fIptr\fR to \fIsize\fR bytes and returns a pointer to the (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 \fIptr\fR is \fINULL\fR, \fBrealloc()\fR behaves like \fBmalloc()\fR for the specified size. If \fIsize\fR is 0 and \fIptr\fR is not a null pointer, the space pointed to is freed. .sp .LP The \fBrecallocarray()\fR function is similar to \fBreallocarray()\fR except it ensures newly allocated memory is cleared similar to \fBcalloc()\fR. If \fIptr\fR is \fINULL\fR. \fIoldnelem\fR is ignored and the call is equivalent to \fBcalloc()\fR. If \fIptr\fR is not \fINULL\fR, \fIoldnelem\fR must be a value such that \fIoldnelem\fR * \fIelsize\fR is the size of the earlier allocation that returned \fIptr\fR, otherwise the behaviour is undefined. .sp .LP The \fBvalloc()\fR function has the same effect as \fBmalloc()\fR, except that the allocated memory will be aligned to a multiple of the value returned by \fBsysconf\fR(\fB_SC_PAGESIZE\fR). .sp .LP The \fBalloca()\fR function allocates \fIsize\fR 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 .sp .LP Upon successful completion, each of the allocation functions returns a pointer to space suitably aligned (after possible pointer coercion) for storage of any type of object. .sp .LP If there is no available memory, \fBmalloc()\fR, \fBcalloc()\fR, \fBrealloc()\fR, \fBrecallocarray()\fR, \fBmemalign()\fR, and \fBvalloc()\fR return a null pointer. When \fBrealloc()\fR is called with \fIsize\fR > 0 and returns \fINULL\fR, the block pointed to by \fIptr\fR is left intact. If \fIsize\fR, \fInelem\fR, or \fIelsize\fR is 0, either a null pointer or a unique pointer that can be passed to \fBfree()\fR is returned. .sp .LP If \fBmalloc()\fR, \fBcalloc()\fR, \fBrealloc()\fR, or \fBrecallocarray()\fR returns unsuccessfully, \fBerrno\fR will be set to indicate the error. The \fBfree()\fR and \fBfreezero()\fR functions do not set \fBerrno\fR. .SH ERRORS .sp .LP The \fBmalloc()\fR, \fBcalloc()\fR, and \fBrealloc()\fR functions will fail if: .sp .ne 2 .na \fB\fBENOMEM\fR\fR .ad .RS 10n The physical limits of the system are exceeded by \fIsize\fR bytes of memory which cannot be allocated. .RE .sp .ne 2 .na \fB\fBEAGAIN\fR\fR .ad .RS 10n There is not enough memory available to allocate \fIsize\fR bytes of memory; but the application could try again later. .RE .sp .LP The \fBrecallocarray()\fR function will fail if: .sp .ne 2 .na \fB\fBEINVAL\fR\fR .ad .RS 10n \fIptr\fR is not \fINULL\fR and multiplying \fIoldnelem\fR and \fIelsize\fR results in integer overflow. .RE .SH USAGE .sp .LP Portable applications should avoid using \fBvalloc()\fR but should instead use \fBmalloc()\fR or \fBmmap\fR(2). On systems with a large page size, the number of successful \fBvalloc()\fR operations might be 0. .sp .LP 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 \fBgetrlimit\fR(2) .sp .LP The \fBalloca()\fR function is machine-, compiler-, and most of all, system-dependent. Its use is strongly discouraged.