2 .\" Copyright (c) 2008 Sun Microsystems, Inc. All Rights Reserved.
3 .\" Copyright (c) 2012 Joyent, Inc. All Rights Reserved.
4 .\" 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.
5 .\" 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.
6 .\" 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]
7 .TH UMEM_ALLOC 3MALLOC "Dec 9, 2017"
9 umem_alloc, umem_zalloc, umem_free, umem_nofail_callback \- fast, scalable
14 cc [ \fIflag \&.\|.\|.\fR ] \fIfile\fR\&.\|.\|. \fB-lumem\fR [ \fIlibrary \&.\|.\|.\fR ]
17 \fBvoid *\fR\fBumem_alloc\fR(\fBsize_t\fR \fIsize\fR, \fBint\fR \fIflags\fR);
22 \fBvoid *\fR\fBumem_zalloc\fR(\fBsize_t\fR \fIsize\fR, \fBint\fR \fIflags\fR);
27 \fBvoid\fR \fBumem_free\fR(\fBvoid *\fR\fIbuf\fR, \fBsize_t\fR \fIsize\fR);
32 \fBvoid\fR \fBumem_nofail_callback\fR(\fB(int (*\fR\fIcallback\fR)(void));
37 \fBvoid *\fR\fBmalloc\fR(\fBsize_t\fR \fIsize\fR);
42 \fBvoid *\fR\fBcalloc\fR(\fBsize_t\fR \fInelem\fR, \fBsize_t\fR \fIelsize\fR);
47 \fBvoid\fR \fBfree\fR(\fBvoid *\fR\fIptr\fR);
52 \fBvoid *\fR\fBmemalign\fR(\fBsize_t\fR \fIalignment\fR, \fBsize_t\fR \fIsize\fR);
57 \fBvoid *\fR\fBrealloc\fR(\fBvoid *\fR\fIptr\fR, \fBsize_t\fR \fIsize\fR);
62 \fBvoid *\fR\fBvalloc\fR(\fBsize_t\fR \fIsize\fR);
67 The \fBumem_alloc()\fR function returns a pointer to a block of \fIsize\fR
68 bytes suitably aligned for any variable type. The initial contents of memory
69 allocated using \fBumem_alloc()\fR is undefined. The \fIflags\fR argument
70 determines the behavior of \fBumem_alloc()\fR if it is unable to fulfill the
71 request. The \fIflags\fR argument can take the following values:
75 \fB\fBUMEM_DEFAULT\fR\fR
78 Return \fINULL\fR on failure.
84 \fB\fBUMEM_NOFAIL\fR\fR
87 Call an optional \fIcallback\fR (set with \fBumem_nofail_callback()\fR) on
88 failure. The \fIcallback\fR takes no arguments and can finish by:
93 returning \fBUMEM_CALLBACK_RETRY\fR, in which case the allocation will be
94 retried. If the allocation fails, the callback will be invoked again.
100 returning \fBUMEM_CALLBACK_EXIT\fR(\fIstatus\fR), in which case \fBexit\fR(2)
101 is invoked with \fIstatus\fR as its argument. The \fBexit()\fR function is
102 called only once. If multiple threads return from the \fBUMEM_NOFAIL\fR
103 callback with \fBUMEM_CALLBACK_EXIT\fR(\fIstatus\fR), one will call
104 \fBexit()\fR while the other blocks until \fBexit()\fR terminates the program.
110 invoking a context-changing function (\fBsetcontext\fR(2)) or a non-local jump
111 (\fBlongjmp\fR(3C) or \fBsiglongjmp\fR(3C)), or ending the current thread of
112 control (\fBthr_exit\fR(3C) or \fBpthread_exit\fR(3C)). The application is
113 responsible for any necessary cleanup. The state of \fBlibumem\fR remains
116 If no callback has been set or the callback has been set to \fINULL\fR,
117 \fBumem_alloc\fR(..., \fBUMEM_NOFAIL\fR) behaves as though the callback
118 returned \fBUMEM_CALLBACK_EXIT\fR(255).
120 The \fBlibumem\fR library can call callbacks from any place that a
121 \fBUMEM_NOFAIL\fR allocation is issued. In multithreaded applications,
122 callbacks are expected to perform their own concurrency management.
127 The function call \fBumem_alloc\fR(0, \fIflag\fR) always returns \fINULL\fR.
128 The function call \fBumem_free\fR(\fINULL\fR, 0) is allowed.
131 The \fBumem_zalloc()\fR function has the same semantics as \fBumem_alloc()\fR,
132 but the block of memory is initialized to zeros before it is returned.
135 The \fBumem_free()\fR function frees blocks previously allocated using
136 \fBumem_alloc()\fR and \fBumem_zalloc()\fR. The buffer address and size must
137 exactly match the original allocation. Memory must not be returned piecemeal.
140 The \fBumem_nofail_callback()\fR function sets the process-wide UMEM_NOFAIL
141 callback. See the description of UMEM_NOFAIL for more information.
144 The \fBmalloc()\fR, \fBcalloc()\fR, \fBfree()\fR, \fBmemalign()\fR,
145 \fBrealloc()\fR, and \fBvalloc()\fR functions are as described in
146 \fBmalloc\fR(3C). The \fBlibumem\fR library provides these functions for
147 backwards-compatibility with the standard functions.
148 .SH ENVIRONMENT VARIABLES
150 See \fBumem_debug\fR(3MALLOC) for environment variables that effect the
151 debugging features of the \fBlibumem\fR library.
155 \fB\fBUMEM_OPTIONS\fR\fR
158 Contains a list of comma-separated options. Unrecognized options are ignored.
159 The options that are supported are:
163 \fB\fBbackend\fR=\fBsbrk\fR\fR
167 \fB\fBbackend\fR=\fBmmap\fR\fR
170 Set the underlying function used to allocate memory. This option can be set to
171 \fBsbrk\fR (the default) for an \fBsbrk\fR(2)-based source or \fBmmap\fR for an
172 \fBmmap\fR(2)-based source. If set to a value that is not supported, \fBsbrk\fR
178 \fB\fBperthread_cache\fR=\fBsize\fR\fR
181 libumem allows for each thread to cache recently freed small allocations for
182 future allocations. The size argument, which accepts k, m, g, and t, suffixes
183 denotes the maximum amount of memory each thread can use for this purpose. The
184 default amount used is 1 MB. Any buffers in the per-thread cache are freed when
185 the thread exits. The efficacy of the per-thread cache can be determined with
186 the \fB::umastat\fR \fBmdb\fR(1) \fIdcmd\fR debugger command.
191 \fB\fBallocator\fR=\fBbest\fR\fR
195 \fB\fBallocator\fR=\fBfirst\fR\fR
199 \fB\fBallocator\fR=\fBinstant\fR\fR
203 \fB\fBallocator\fR=\fBnext\fR\fR
206 Set the underlying allocation strategy. The \fBbest\fR fit strategy tells
207 libumem to use the smallest free segment possible. The \fBinstant\fR fit
208 strategy approximates the best fit strategy in constant cpu time. The
209 \fBfirst\fR fit strategy takes the first free segment that can honor the
210 allocation. The \fBnext\fR fit strategy uses the next free segment after the
211 previously allocated one.
218 \fBExample 1 \fRUsing the \fBumem_alloc()\fR function.
225 char *buf = umem_alloc(1024, UMEM_DEFAULT);
228 fprintf(stderr, "out of memory\en");
231 /* cannot assume anything about buf's contents */
233 umem_free(buf, 1024);
239 \fBExample 2 \fRUsing the \fBumem_zalloc()\fR function
246 char *buf = umem_zalloc(1024, UMEM_DEFAULT);
249 fprintf(stderr, "out of memory\en");
252 /* buf contains zeros */
254 umem_free(buf, 1024);
260 \fBExample 3 \fRUsing UMEM_NOFAIL
269 * Note that the allocation code below does not have to
270 * check for umem_alloc() returning NULL
273 my_failure_handler(void)
275 (void) fprintf(stderr, "out of memory\en");
276 return (UMEM_CALLBACK_EXIT(255));
279 umem_nofail_callback(my_failure_handler);
284 for (i = 0; i < 100; i++)
285 buf[i] = umem_alloc(1024 * 1024, UMEM_NOFAIL);
287 for (i = 0; i < 100; i++)
288 umem_free(buf[i], 1024 * 1024);
294 \fBExample 4 \fRUsing UMEM_NOFAIL in a multithreaded application
303 start_func(void *the_arg)
305 int *info = (int *)the_arg;
306 char *buf = umem_alloc(1024 * 1024, UMEM_NOFAIL);
308 /* does not need to check for buf == NULL */
312 * if there were other UMEM_NOFAIL allocations,
313 * we would need to arrange for buf to be
314 * umem_free()ed upon failure.
317 umem_free(buf, 1024 * 1024);
322 my_failure_handler(void)
324 /* terminate the current thread with status NULL */
328 umem_nofail_callback(my_failure_handler);
335 (void) thr_create(NULL, NULL, start_func, &my_arg, 0,
338 while (thr_join(0, &tid, &status) != 0)
341 if (status == NULL) {
342 (void) fprintf(stderr, "thread %d ran out of memory\en",
351 See \fBattributes\fR(5) for descriptions of the following attributes:
359 ATTRIBUTE TYPE ATTRIBUTE VALUE
361 Interface Stability Committed
370 For \fBmalloc()\fR, \fBcalloc()\fR, \fBfree()\fR, \fBrealloc()\fR, and
371 \fBvalloc()\fR, see \fBstandards\fR(5).
374 \fBexit\fR(2), \fBmmap\fR(2), \fBsbrk\fR(2), \fBbsdmalloc\fR(3MALLOC),
375 \fBlibumem\fR(3LIB), \fBlongjmp\fR(3C), \fBmalloc\fR(3C),
376 \fBmalloc\fR(3MALLOC), \fBmapmalloc\fR(3MALLOC), \fBpthread_exit\fR(3C),
377 \fBthr_exit\fR(3C), \fBumem_cache_create\fR(3MALLOC),
378 \fBumem_debug\fR(3MALLOC), \fBwatchmalloc\fR(3MALLOC), \fBattributes\fR(5),
382 \fIModular Debugger Guide\fR:
385 https://illumos.org/books/mdb/
388 Any of the following can cause undefined results:
393 Passing a pointer returned from \fBumem_alloc()\fR or \fBumem_zalloc()\fR to
394 \fBfree()\fR or \fBrealloc()\fR.
400 Passing a pointer returned from \fBmalloc()\fR, \fBcalloc()\fR, \fBvalloc()\fR,
401 \fBmemalign()\fR, or \fBrealloc()\fR to \fBumem_free()\fR.
407 Writing past the end of a buffer allocated using \fBumem_alloc()\fR or
414 Performing \fBUMEM_NOFAIL\fR allocations from an \fBatexit\fR(3C) handler.
418 If the \fBUMEM_NOFAIL\fR callback performs \fBUMEM_NOFAIL\fR allocations,
419 infinite recursion can occur.
422 The following list compares the features of the \fBmalloc\fR(3C),
423 \fBbsdmalloc\fR(3MALLOC), \fBmalloc\fR(3MALLOC), \fBmtmalloc\fR(3MALLOC) , and
424 the \fBlibumem\fR functions.
429 The \fBmalloc\fR(3C), \fBbsdmalloc\fR(3MALLOC), and \fBmalloc\fR(3MALLOC)
430 functions have no support for concurrency. The \fBlibumem\fR and
431 \fBmtmalloc\fR(3MALLOC) functions support concurrent allocations.
437 The \fBbsdmalloc\fR(3MALLOC) functions afford better performance but are
444 The \fBmalloc\fR(3MALLOC) functions are space-efficient but have slower
451 The standard, fully SCD-compliant \fBmalloc\fR(3C) functions are a trade-off
452 between performance and space-efficiency.
458 The \fBmtmalloc\fR(3MALLOC) functions provide fast, concurrent \fBmalloc()\fR
459 implementations that are not space-efficient.
465 The \fBlibumem\fR functions provide a fast, concurrent allocation
466 implementation that in most cases is more space-efficient than
467 \fBmtmalloc\fR(3MALLOC).