'\" te .\" Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved. .\" Copyright 1989 AT&T .TH mtmalloc 3MALLOC "22 Mar 2013" "SunOS 5.11" "Memory Allocation Library Functions" .SH NAME mtmalloc, mallocctl \- MT hot memory allocator .SH SYNOPSIS .LP .nf #include cc -o a.out -lmtmalloc \fBvoid *\fR\fBmalloc\fR(\fBsize_t\fR \fIsize\fR); .fi .LP .nf \fBvoid\fR \fBfree\fR(\fBvoid *\fR\fIptr\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\fBvalloc\fR(\fBsize_t\fR \fIsize\fR); .fi .LP .nf \fBvoid\fR \fBmallocctl\fR(\fBint\fR \fIcmd\fR, \fBlong\fR \fIvalue\fR); .fi .SH DESCRIPTION .sp .LP The \fBmalloc()\fR and \fBfree()\fR functions provide a simple general-purpose memory allocation package that is suitable for use in high performance multithreaded applications. The suggested use of this library is in multithreaded applications; it can be used for single threaded applications, but there is no advantage in doing so. This library cannot be dynamically loaded with \fBdlopen\fR(3C) during runtime because there must be only one manager of the process heap. .sp .LP The \fBmalloc()\fR function returns a pointer to a block of at least \fIsize\fR bytes suitably aligned for any use. .sp .LP The argument to \fBfree()\fR is a pointer to a block previously allocated by \fBmalloc()\fR or \fBrealloc()\fR. After \fBfree()\fR is performed this space is available for further allocation. If \fIptr\fR is a null pointer, no action occurs. The \fBfree()\fR function does not set \fBerrno\fR. .sp .LP Undefined results will occur if the space assigned by \fBmalloc()\fR is overrun or if a random number is handed to \fBfree()\fR. A freed pointer that is passed to \fBfree()\fR will send a \fBSIGABRT\fR signal to the calling process. This behavior is controlled by \fBmallocctl()\fR. .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. Note that 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 \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 After possible pointer coercion, each allocation routine returns a pointer to a space that is suitably aligned for storage of any type of object. .sp .LP The \fBmalloc()\fR, \fBrealloc()\fR, \fBmemalign()\fR, and \fBvalloc()\fR functions will fail if there is not enough available memory. .sp .LP The \fBmallocctl()\fR function controls the behavior of the \fBmalloc\fR library. The options fall into two general classes, debugging options and performance options. .sp .ne 2 .mk .na \fB\fBMTDOUBLEFREE\fR\fR .ad .RS 18n .rt Allows double \fBfree\fR of a pointer. Setting \fIvalue\fR to \fB1\fR means yes and \fB0\fR means no. The default behavior of double \fBfree\fR results in a core dump. .RE .sp .ne 2 .mk .na \fB\fBMTDEBUGPATTERN\fR\fR .ad .RS 18n .rt Writes misaligned data into the buffer after \fBfree()\fR. When the buffer is reallocated, the contents are verified to ensure that there was no access to the buffer after the \fBfree\fR. If the buffer has been dirtied, a \fBSIGABRT\fR signal is delivered to the process. Setting \fIvalue\fR to \fB1\fR means yes and \fB0\fR means no. The default behavior is to \fInot\fR write misaligned data. The pattern used is \fB0xdeadbeef\fR. Use of this option results in a performance penalty. .RE .sp .ne 2 .mk .na \fB\fBMTINITBUFFER\fR\fR .ad .RS 18n .rt Writes misaligned data into the newly allocated buffer. This option is useful for detecting some accesses before initialization. Setting \fIvalue\fR to \fB1\fR means yes and \fB0\fR means no. The default behavior is to \fInot\fR write misaligned data to the newly allocated buffer. The pattern used is \fB0xbaddcafe\fR. Use of this option results in a performance penalty. .RE .sp .ne 2 .mk .na \fB\fBMTCHUNKSIZE\fR\fR .ad .RS 18n .rt This option changes the size of allocated memory when a pool has exhausted all available memory in the buffer. Increasing this value allocates more memory for the application. A substantial performance gain can occur because the library makes fewer calls to the OS for more memory. Acceptable number \fIvalue\fRs are between \fB9\fR and \fB256\fR. The default value is 9 for 32-bit code and 64 for 64-bit code. This value is multiplied by 8192. .RE .sp .ne 2 .mk .na \fB\fBMTEXCLUSIVE\fR\fR .ad .RS 18n .rt By default, \fBlibmtmalloc\fR allocates 2*\fBNCPUS\fR buckets from which allocations occur. Threads share buckets based on their thread ID. If \fBMTEXCLUSIVE\fR is invoked, then 4*\fBNCPUS\fR buckets are used. Threads with thread IDless than 2*\fBNCPUS\fR receive an exclusive bucket and thus do not need to use locks. Allocation performance for these buckets may be dramatically increased. One enabled, \fBMTEXCLUSIVE\fR cannot be disabled. This feature can also be enabled by setting the option \fBMTEXCLUSIVE\fR in the environment variable \fBMTMALLOC_OPTIONS\fR. See \fBlibmtmalloc\fR(3LIB). .RE .sp .ne 2 .mk .na \fB\fBMTREALFREE\fR\fR .ad .RS 18n .rt This option sets the threshold for calling \fBmadvise\fR(3C) with \fBMADV_FREE\fR. Calling \fBmadvise()\fR will result in the memory associated with the allocation being returned to the kernel. When freed, allocations greater than \fIvalue\fR*\fIpagesize\fR will have \fBmadvise()\fR called. If \fIvalue\fR is less than 2, it will be set to 2. .RE .SH RETURN VALUES .sp .LP If there is no available memory, \fBmalloc()\fR, \fBrealloc()\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 or \fBrealloc()\fR returns unsuccessfully, \fBerrno\fR will be set to indicate the error. .SH ERRORS .sp .LP The \fBmalloc()\fR and \fBrealloc()\fR functions will fail if: .sp .ne 2 .mk .na \fB\fBENOMEM\fR\fR .ad .RS 10n .rt The physical limits of the system are exceeded by \fIsize\fR bytes of memory which cannot be allocated. .RE .sp .ne 2 .mk .na \fB\fBEAGAIN\fR\fR .ad .RS 10n .rt There is not enough memory available to allocate \fIsize\fR bytes of memory; but the application could try again later. .RE .SH USAGE .sp .LP Comparative features of the various allocation libraries can be found in the \fBumem_alloc\fR(3MALLOC) manual page. .SH ATTRIBUTES .sp .LP See \fBattributes\fR(5) for descriptions of the following attributes: .sp .sp .TS tab() box; cw(2.75i) |cw(2.75i) lw(2.75i) |lw(2.75i) . ATTRIBUTE TYPEATTRIBUTE VALUE _ MT-LevelSafe .TE .SH SEE ALSO .sp .LP \fBbrk\fR(2), \fBgetrlimit\fR(2), \fBbsdmalloc\fR(3MALLOC), \fBdlopen\fR(3C), \fBlibmtmalloc\fR(3LIB), \fBmadvise\fR(3C), \fBmalloc\fR(3C), \fBmalloc\fR(3MALLOC), \fBmapmalloc\fR(3MALLOC), \fBsignal.h\fR(3HEAD), \fBumem_alloc\fR(3MALLOC), \fBwatchmalloc\fR(3MALLOC), \fBattributes\fR(5) .SH WARNINGS .sp .LP Undefined results will occur if the size requested for a block of memory exceeds the maximum size of a process's heap. This information may be obtained using \fBgetrlimit()\fR.