2007-03-17 18:03:10 +00:00
'\" te
2015-08-22 19:40:44 +00:00
. \" Copyright (c) 2008 Sun Microsystems, Inc. All Rights Reserved.
. \" Copyright (c) 2012 Joyent, Inc. All Rights Reserved.
. \" 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 UMEM_ALLOC 3 MALLOC "Mar 24, 2008"
2007-03-17 18:03:10 +00:00
.SH NAME
2015-08-22 19:40:44 +00:00
umem_alloc, umem_zalloc, umem_free, umem_nofail_callback \- fast, scalable
memory allocation
2007-03-17 18:03:10 +00:00
.SH SYNOPSIS
.LP
.nf
2015-08-22 19:40:44 +00:00
cc [ \fI flag \& .\| .\| .\fR ] \fI file\fR \& .\| .\| . \fB -lumem\fR [ \fI library \& .\| .\| .\fR ]
2007-03-17 18:03:10 +00:00
#include <umem.h>
\fB void *\fR \fB umem_alloc\fR (\fB size_t\fR \fI size\fR , \fB int\fR \fI flags\fR );
.fi
2015-08-22 19:40:44 +00:00
2007-03-17 18:03:10 +00:00
.LP
.nf
\fB void *\fR \fB umem_zalloc\fR (\fB size_t\fR \fI size\fR , \fB int\fR \fI flags\fR );
.fi
2015-08-22 19:40:44 +00:00
2007-03-17 18:03:10 +00:00
.LP
.nf
\fB void\fR \fB umem_free\fR (\fB void *\fR \fI buf\fR , \fB size_t\fR \fI size\fR );
.fi
2015-08-22 19:40:44 +00:00
2007-03-17 18:03:10 +00:00
.LP
.nf
\fB void\fR \fB umem_nofail_callback\fR (\fB (int (*\fR \fI callback\fR )(void));
.fi
2015-08-22 19:40:44 +00:00
2007-03-17 18:03:10 +00:00
.LP
.nf
\fB void *\fR \fB malloc\fR (\fB size_t\fR \fI size\fR );
.fi
2015-08-22 19:40:44 +00:00
2007-03-17 18:03:10 +00:00
.LP
.nf
\fB void *\fR \fB calloc\fR (\fB size_t\fR \fI nelem\fR , \fB size_t\fR \fI elsize\fR );
.fi
2015-08-22 19:40:44 +00:00
2007-03-17 18:03:10 +00:00
.LP
.nf
\fB void\fR \fB free\fR (\fB void *\fR \fI ptr\fR );
.fi
2015-08-22 19:40:44 +00:00
2007-03-17 18:03:10 +00:00
.LP
.nf
\fB void *\fR \fB memalign\fR (\fB size_t\fR \fI alignment\fR , \fB size_t\fR \fI size\fR );
.fi
2015-08-22 19:40:44 +00:00
2007-03-17 18:03:10 +00:00
.LP
.nf
\fB void *\fR \fB realloc\fR (\fB void *\fR \fI ptr\fR , \fB size_t\fR \fI size\fR );
.fi
2015-08-22 19:40:44 +00:00
2007-03-17 18:03:10 +00:00
.LP
.nf
\fB void *\fR \fB valloc\fR (\fB size_t\fR \fI size\fR );
.fi
.SH DESCRIPTION
.sp
2015-08-22 19:40:44 +00:00
.LP
The \fB umem_alloc()\fR function returns a pointer to a block of \fI size\fR
bytes suitably aligned for any variable type. The initial contents of memory
allocated using \fB umem_alloc()\fR is undefined. The \fI flags\fR argument
determines the behavior of \fB umem_alloc()\fR if it is unable to fulfill the
request. The \fI flags\fR argument can take the following values:
2007-03-17 18:03:10 +00:00
.sp
.ne 2
.na
\fB \fB UMEM_DEFAULT\fR \fR
.ad
2015-08-22 19:40:44 +00:00
.RS 16 n
2007-03-17 18:03:10 +00:00
Return \fI NULL\fR on failure.
.RE
.sp
.ne 2
.na
\fB \fB UMEM_NOFAIL\fR \fR
.ad
2015-08-22 19:40:44 +00:00
.RS 16 n
Call an optional \fI callback\fR (set with \fB umem_nofail_callback()\fR ) on
failure. The \fI callback\fR takes no arguments and can finish by:
2007-03-17 18:03:10 +00:00
.RS +4
.TP
.ie t \(bu
.el o
2015-08-22 19:40:44 +00:00
returning \fB UMEM_CALLBACK_RETRY\fR , in which case the allocation will be
retried. If the allocation fails, the callback will be invoked again.
2007-03-17 18:03:10 +00:00
.RE
.RS +4
.TP
.ie t \(bu
.el o
2015-08-22 19:40:44 +00:00
returning \fB UMEM_CALLBACK_EXIT\fR (\fI status\fR ), in which case \fB exit\fR (2)
is invoked with \fI status\fR as its argument. The \fB exit()\fR function is
called only once. If multiple threads return from the \fB UMEM_NOFAIL\fR
callback with \fB UMEM_CALLBACK_EXIT\fR (\fI status\fR ), one will call
\fB exit()\fR while the other blocks until \fB exit()\fR terminates the program.
2007-03-17 18:03:10 +00:00
.RE
.RS +4
.TP
.ie t \(bu
.el o
2015-08-22 19:40:44 +00:00
invoking a context-changing function (\fB setcontext\fR (2)) or a non-local jump
(\fB longjmp\fR (3C) or \fB siglongjmp\fR (3C), or ending the current thread of
control (\fB thr_exit\fR (3C) or \fB pthread_exit\fR (3C). The application is
responsible for any necessary cleanup. The state of \fB libumem\fR remains
consistent.
2007-03-17 18:03:10 +00:00
.RE
2015-08-22 19:40:44 +00:00
If no callback has been set or the callback has been set to \fI NULL\fR ,
\fB umem_alloc\fR (..., \fB UMEM_NOFAIL\fR ) behaves as though the callback
returned \fB UMEM_CALLBACK_EXIT\fR (255).
2007-03-17 18:03:10 +00:00
.sp
2015-08-22 19:40:44 +00:00
The \fB libumem\fR library can call callbacks from any place that a
\fB UMEM_NOFAIL\fR allocation is issued. In multithreaded applications,
callbacks are expected to perform their own concurrency management.
2007-03-17 18:03:10 +00:00
.RE
.sp
.LP
2015-08-22 19:40:44 +00:00
The function call \fB umem_alloc\fR (0, \fI flag\fR ) always returns \fI NULL\fR .
The function call \fB umem_free\fR (\fI NULL\fR , 0) is allowed.
2007-03-17 18:03:10 +00:00
.sp
.LP
2015-08-22 19:40:44 +00:00
The \fB umem_zalloc()\fR function has the same semantics as \fB umem_alloc()\fR ,
but the block of memory is initialized to zeros before it is returned.
2007-03-17 18:03:10 +00:00
.sp
.LP
2015-08-22 19:40:44 +00:00
The \fB umem_free()\fR function frees blocks previously allocated using
\fB umem_alloc()\fR and \fB umem_zalloc()\fR . The buffer address and size must
exactly match the original allocation. Memory must not be returned piecemeal.
2007-03-17 18:03:10 +00:00
.sp
.LP
2015-08-22 19:40:44 +00:00
The \fB umem_nofail_callback()\fR function sets the process-wide UMEM_NOFAIL
callback. See the description of UMEM_NOFAIL for more information.
2007-03-17 18:03:10 +00:00
.sp
.LP
2015-08-22 19:40:44 +00:00
The \fB malloc()\fR , \fB calloc()\fR , \fB free()\fR , \fB memalign()\fR ,
\fB realloc()\fR , and \fB valloc()\fR functions are as described in
\fB malloc\fR (3C). The \fB libumem\fR library provides these functions for
backwards-compatibility with the standard functions.
.SH ENVIRONMENT VARIABLES
2007-03-17 18:03:10 +00:00
.sp
2015-08-22 19:40:44 +00:00
.LP
See \fB umem_debug\fR (3MALLOC) for environment variables that effect the
debugging features of the \fB libumem\fR library.
2007-03-17 18:03:10 +00:00
.sp
.ne 2
.na
2015-08-22 19:40:44 +00:00
\fB \fB UMEM_OPTIONS\fR \fR
2007-03-17 18:03:10 +00:00
.ad
2015-08-22 19:40:44 +00:00
.RS 16 n
Contains a list of comma-separated options. Unrecognized options are ignored.
The options that are supported are:
2007-03-17 18:03:10 +00:00
.sp
.ne 2
.na
\fB \fB backend\fR =\fB sbrk\fR \fR
.ad
.br
.na
\fB \fB backend\fR =\fB mmap\fR \fR
.ad
2015-08-22 19:40:44 +00:00
.RS 16 n
Set the underlying function used to allocate memory. This option can be set to
\fB sbrk\fR (the default) for an \fB sbrk\fR (2)-based source or \fB mmap\fR for an
\fB mmap\fR (2)-based source. If set to a value that is not supported, \fB sbrk\fR
will be used.
.RE
2007-03-17 18:03:10 +00:00
.sp
2015-08-22 19:40:44 +00:00
.ne 2
.na
\fB \fB perthread_cache\fR =\fB size\fR \fR
.ad
.RS 16 n
libumem allows for each thread to cache recently freed small allocations for
future allocations. The size argument, which accepts k, m, g, and t, suffixes
denotes the maximum amount of memory each thread can use for this purpose. The
default amount used is 1 MB. Any buffers in the per-thread cache are freed when
the thread exits. The efficacy of the per-thread cache can be determined with
the \fB ::umastat\fR \fB mdb\fR (1) \fI dcmd\fR debugger command.
.RE
2007-03-17 18:03:10 +00:00
2015-08-22 19:40:44 +00:00
.ne 2
.na
\fB \fB allocator\fR =\fB best\fR \fR
.ad
.br
.na
\fB \fB allocator\fR =\fB first\fR \fR
.ad
.br
.na
\fB \fB allocator\fR =\fB instant\fR \fR
.ad
.br
.na
\fB \fB allocator\fR =\fB next\fR \fR
.ad
.RS 16 n
Set the underlying allocation strategy. The \fB best\fR fit strategy tells
libumem to use the smallest free segment possible. The \fB instant\fR fit
strategy approximates the best fit strategy in constant cpu time. The
\fB first\fR fit strategy takes the first free segment that can honor the
allocation. The \fB next\fR fit strategy uses the next free segment after the
previously allocated one.
2007-03-17 18:03:10 +00:00
.RE
.RE
.SH EXAMPLES
.LP
\fB Example 1 \fR Using the \fB umem_alloc()\fR function.
2015-08-22 19:40:44 +00:00
.sp
2007-03-17 18:03:10 +00:00
.in +2
.nf
#include <stdio.h>
#include <umem.h>
\& ...
char *buf = umem_alloc(1024, UMEM_DEFAULT);
if (buf == NULL) {
2015-08-22 19:40:44 +00:00
fprintf(stderr, "out of memory\e n");
return (1);
2007-03-17 18:03:10 +00:00
}
/* cannot assume anything about buf's contents */
\& ...
umem_free(buf, 1024);
\& ...
.fi
.in -2
.LP
2015-08-22 19:40:44 +00:00
\fB Example 2 \fR Using the \fB umem_zalloc()\fR function
.sp
2007-03-17 18:03:10 +00:00
.in +2
.nf
#include <stdio.h>
#include <umem.h>
\& ...
char *buf = umem_zalloc(1024, UMEM_DEFAULT);
if (buf == NULL) {
2015-08-22 19:40:44 +00:00
fprintf(stderr, "out of memory\e n");
return (1);
2007-03-17 18:03:10 +00:00
}
/* buf contains zeros */
\& ...
umem_free(buf, 1024);
\& ...
.fi
.in -2
.LP
2015-08-22 19:40:44 +00:00
\fB Example 3 \fR Using UMEM_NOFAIL
.sp
2007-03-17 18:03:10 +00:00
.in +2
.nf
#include <stdlib.h>
#include <stdio.h>
#include <umem.h>
/*
2015-08-22 19:40:44 +00:00
* Note that the allocation code below does not have to
* check for umem_alloc() returning NULL
*/
2007-03-17 18:03:10 +00:00
int
my_failure_handler(void)
{
2015-08-22 19:40:44 +00:00
(void) fprintf(stderr, "out of memory\e n");
return (UMEM_CALLBACK_EXIT(255));
2007-03-17 18:03:10 +00:00
}
\& ...
umem_nofail_callback(my_failure_handler);
\& ...
int i;
char *buf[100];
for (i = 0; i < 100; i++)
2015-08-22 19:40:44 +00:00
buf[i] = umem_alloc(1024 * 1024, UMEM_NOFAIL);
2007-03-17 18:03:10 +00:00
\& ...
for (i = 0; i < 100; i++)
2015-08-22 19:40:44 +00:00
umem_free(buf[i], 1024 * 1024);
2007-03-17 18:03:10 +00:00
\& ...
.fi
.in -2
.LP
2015-08-22 19:40:44 +00:00
\fB Example 4 \fR Using UMEM_NOFAIL in a multithreaded application
.sp
2007-03-17 18:03:10 +00:00
.in +2
.nf
#define _REENTRANT
#include <thread.h>
#include <stdio.h>
#include <umem.h>
void *
start_func(void *the_arg)
{
2015-08-22 19:40:44 +00:00
int *info = (int *)the_arg;
char *buf = umem_alloc(1024 * 1024, UMEM_NOFAIL);
/* does not need to check for buf == NULL */
buf[0] = 0;
...
/*
* if there were other UMEM_NOFAIL allocations,
* we would need to arrange for buf to be
* umem_free()ed upon failure.
*/
...
umem_free(buf, 1024 * 1024);
return (the_arg);
2007-03-17 18:03:10 +00:00
}
\& ...
int
my_failure_handler(void)
{
2015-08-22 19:40:44 +00:00
/* terminate the current thread with status NULL */
thr_exit(NULL);
2007-03-17 18:03:10 +00:00
}
\& ...
umem_nofail_callback(my_failure_handler);
\& ...
int my_arg;
thread_t tid;
void *status;
(void) thr_create(NULL, NULL, start_func, &my_arg, 0,
2015-08-22 19:40:44 +00:00
NULL);
2007-03-17 18:03:10 +00:00
\& ...
while (thr_join(0, &tid, &status) != 0)
2015-08-22 19:40:44 +00:00
;
2007-03-17 18:03:10 +00:00
if (status == NULL) {
2015-08-22 19:40:44 +00:00
(void) fprintf(stderr, "thread %d ran out of memory\e n",
tid);
2007-03-17 18:03:10 +00:00
}
\& ...
.fi
.in -2
.SH ATTRIBUTES
.sp
.LP
2015-08-22 19:40:44 +00:00
See \fB attributes\fR (5) for descriptions of the following attributes:
.sp
2007-03-17 18:03:10 +00:00
.sp
.TS
2015-08-22 19:40:44 +00:00
box;
c | c
l | l .
ATTRIBUTE TYPE ATTRIBUTE VALUE
_
Interface Stability Committed
2007-03-17 18:03:10 +00:00
_
2015-08-22 19:40:44 +00:00
MT-Level MT-Safe
2007-03-17 18:03:10 +00:00
_
2015-08-22 19:40:44 +00:00
Standard See below.
2007-03-17 18:03:10 +00:00
.TE
.sp
2015-08-22 19:40:44 +00:00
.LP
For \fB malloc()\fR , \fB calloc()\fR , \fB free()\fR , \fB realloc()\fR , and
\fB valloc()\fR , see \fB standards\fR (5).
2007-03-17 18:03:10 +00:00
.SH SEE ALSO
2015-08-22 19:40:44 +00:00
.sp
2007-03-17 18:03:10 +00:00
.LP
2015-08-22 19:40:44 +00:00
\fB exit\fR (2), \fB mmap\fR (2), \fB sbrk\fR (2), \fB bsdmalloc\fR (3MALLOC),
\fB libumem\fR (3LIB), \fB longjmp\fR (3C), \fB malloc\fR (3C),
\fB malloc\fR (3MALLOC), \fB mapmalloc\fR (3MALLOC), \fB pthread_exit\fR (3C),
\fB thr_exit\fR (3C), \fB umem_cache_create\fR (3MALLOC),
\fB umem_debug\fR (3MALLOC), \fB watchmalloc\fR (3MALLOC), \fB attributes\fR (5),
2007-03-17 18:03:10 +00:00
\fB standards\fR (5)
.sp
.LP
2015-08-22 19:40:44 +00:00
\fI Solaris Modular Debugger Guide\fR
2007-03-17 18:03:10 +00:00
.SH WARNINGS
2015-08-22 19:40:44 +00:00
.sp
2007-03-17 18:03:10 +00:00
.LP
Any of the following can cause undefined results:
.RS +4
.TP
.ie t \(bu
.el o
2015-08-22 19:40:44 +00:00
Passing a pointer returned from \fB umem_alloc()\fR or \fB umem_zalloc()\fR to
\fB free()\fR or \fB realloc()\fR .
2007-03-17 18:03:10 +00:00
.RE
.RS +4
.TP
.ie t \(bu
.el o
2015-08-22 19:40:44 +00:00
Passing a pointer returned from \fB malloc()\fR , \fB calloc()\fR , \fB valloc()\fR ,
\fB memalign()\fR , or \fB realloc()\fR to \fB umem_free()\fR .
2007-03-17 18:03:10 +00:00
.RE
.RS +4
.TP
.ie t \(bu
.el o
2015-08-22 19:40:44 +00:00
Writing past the end of a buffer allocated using \fB umem_alloc()\fR or
\fB umem_zalloc()\fR
2007-03-17 18:03:10 +00:00
.RE
.RS +4
.TP
.ie t \(bu
.el o
2015-08-22 19:40:44 +00:00
Performing \fB UMEM_NOFAIL\fR allocations from an \fB atexit\fR (3C) handler.
2007-03-17 18:03:10 +00:00
.RE
.sp
.LP
2015-08-22 19:40:44 +00:00
If the \fB UMEM_NOFAIL\fR callback performs \fB UMEM_NOFAIL\fR allocations,
infinite recursion can occur.
.SH NOTES
2007-03-17 18:03:10 +00:00
.sp
2015-08-22 19:40:44 +00:00
.LP
The following list compares the features of the \fB malloc\fR (3C),
\fB bsdmalloc\fR (3MALLOC), \fB malloc\fR (3MALLOC), \fB mtmalloc\fR (3MALLOC) , and
the \fB libumem\fR functions.
2007-03-17 18:03:10 +00:00
.RS +4
.TP
.ie t \(bu
.el o
2015-08-22 19:40:44 +00:00
The \fB malloc\fR (3C), \fB bsdmalloc\fR (3MALLOC), and \fB malloc\fR (3MALLOC)
functions have no support for concurrency. The \fB libumem\fR and
\fB mtmalloc\fR (3MALLOC) functions support concurrent allocations.
2007-03-17 18:03:10 +00:00
.RE
.RS +4
.TP
.ie t \(bu
.el o
2015-08-22 19:40:44 +00:00
The \fB bsdmalloc\fR (3MALLOC) functions afford better performance but are
space-inefficient.
2007-03-17 18:03:10 +00:00
.RE
.RS +4
.TP
.ie t \(bu
.el o
2015-08-22 19:40:44 +00:00
The \fB malloc\fR (3MALLOC) functions are space-efficient but have slower
performance.
2007-03-17 18:03:10 +00:00
.RE
.RS +4
.TP
.ie t \(bu
.el o
2015-08-22 19:40:44 +00:00
The standard, fully SCD-compliant \fB malloc\fR (3C) functions are a trade-off
between performance and space-efficiency.
2007-03-17 18:03:10 +00:00
.RE
.RS +4
.TP
.ie t \(bu
.el o
2015-08-22 19:40:44 +00:00
The \fB mtmalloc\fR (3MALLOC) functions provide fast, concurrent \fB malloc()\fR
implementations that are not space-efficient.
2007-03-17 18:03:10 +00:00
.RE
.RS +4
.TP
.ie t \(bu
.el o
2015-08-22 19:40:44 +00:00
The \fB libumem\fR functions provide a fast, concurrent allocation
implementation that in most cases is more space-efficient than
2007-03-17 18:03:10 +00:00
\fB mtmalloc\fR (3MALLOC).
.RE