| Skip Navigation Links | |
| Exit Print View | |
![]() | man pages section 3: Basic Library Functions Oracle Solaris 11 Information Library |
enable_extended_FILE_stdio(3C)
posix_spawnattr_getschedparam(3C)
posix_spawnattr_getschedpolicy(3C)
posix_spawnattr_getsigdefault(3C)
posix_spawnattr_getsigignore_np(3C)
posix_spawnattr_getsigmask(3C)
posix_spawnattr_setschedparam(3C)
posix_spawnattr_setschedpolicy(3C)
posix_spawnattr_setsigdefault(3C)
posix_spawnattr_setsigignore_np(3C)
posix_spawnattr_setsigmask(3C)
posix_spawn_file_actions_addclose(3C)
posix_spawn_file_actions_addclosefrom_np(3C)
posix_spawn_file_actions_adddup2(3C)
posix_spawn_file_actions_addopen(3C)
posix_spawn_file_actions_destroy(3C)
posix_spawn_file_actions_init(3C)
pthread_attr_getdetachstate(3C)
pthread_attr_getinheritsched(3C)
pthread_attr_getschedparam(3C)
pthread_attr_getschedpolicy(3C)
pthread_attr_setdetachstate(3C)
pthread_attr_setinheritsched(3C)
pthread_attr_setschedparam(3C)
pthread_attr_setschedpolicy(3C)
pthread_barrierattr_destroy(3C)
pthread_barrierattr_getpshared(3C)
pthread_barrierattr_setpshared(3C)
pthread_condattr_getpshared(3C)
pthread_condattr_setpshared(3C)
pthread_cond_reltimedwait_np(3C)
pthread_key_create_once_np(3C)
pthread_mutexattr_getprioceiling(3C)
pthread_mutexattr_getprotocol(3C)
pthread_mutexattr_getpshared(3C)
pthread_mutexattr_getrobust(3C)
pthread_mutexattr_setprioceiling(3C)
pthread_mutexattr_setprotocol(3C)
pthread_mutexattr_setpshared(3C)
pthread_mutexattr_setrobust(3C)
pthread_mutex_getprioceiling(3C)
pthread_mutex_reltimedlock_np(3C)
pthread_mutex_setprioceiling(3C)
pthread_rwlockattr_destroy(3C)
pthread_rwlockattr_getpshared(3C)
pthread_rwlockattr_setpshared(3C)
pthread_rwlock_reltimedrdlock_np(3C)
pthread_rwlock_reltimedwrlock_np(3C)
pthread_rwlock_timedrdlock(3C)
pthread_rwlock_timedwrlock(3C)
rctlblk_get_enforced_value(3C)
- fast, scalable memory allocation
cc [flag … ]file…-lumem [library … ] #include <umem.h>void *umem_alloc(size_tsize,intflags);
void *umem_zalloc(size_tsize,intflags);
voidumem_free(void *buf,size_tsize);
voidumem_nofail_callback((int (*callback)(void));
void *malloc(size_tsize);
void *calloc(size_tnelem,size_telsize);
voidfree(void *ptr);
void *memalign(size_talignment,size_tsize);
void *realloc(void *ptr,size_tsize);
void *valloc(size_tsize);
Theumem_alloc() function returns a pointer to a block ofsize bytessuitably aligned for any variable type. The initial contents of memory allocatedusingumem_alloc() is undefined. Theflags argument determines the behavior ofumem_alloc() ifit is unable to fulfill the request. Theflags argument can takethe following values:
ReturnNULL on failure.
Call an optionalcallback (set withumem_nofail_callback()) on failure. Thecallback takes no arguments and can finish by:
returningUMEM_CALLBACK_RETRY, in which case the allocation will be retried. If the allocation fails, the callback will be invoked again.
returningUMEM_CALLBACK_EXIT(status), in which caseexit(2) is invoked withstatus as its argument. Theexit() function is called only once. If multiple threads return from theUMEM_NOFAIL callback withUMEM_CALLBACK_EXIT(status), one will callexit() while the other blocks untilexit() terminates the program.
invoking a context-changing function (setcontext(2)) or a non-local jump (longjmp(3C) orsiglongjmp(3C), or ending the current thread of control (thr_exit(3C) orpthread_exit(3C). The application is responsible for any necessary cleanup. The state oflibumem remains consistent.
If no callback has been set or the callback has been set toNULL,umem_alloc(...,UMEM_NOFAIL) behaves as though the callback returnedUMEM_CALLBACK_EXIT(255).
Thelibumem library can call callbacks from any place that aUMEM_NOFAIL allocation is issued. In multithreaded applications, callbacks are expected to perform their own concurrency management.
The function callumem_alloc(0,flag) always returnsNULL. The function callumem_free(NULL,0) is allowed.
Theumem_zalloc() function has the same semantics asumem_alloc(), but the blockof memory is initialized to zeros before it is returned.
Theumem_free() function frees blocks previously allocated usingumem_alloc() andumem_zalloc(). Thebuffer address and size must exactly match the original allocation. Memory mustnot be returned piecemeal.
Theumem_nofail_callback() function sets the process-wide UMEM_NOFAIL callback. See the description ofUMEM_NOFAIL for more information.
Themalloc(),calloc(),free(),memalign(),realloc(), andvalloc() functions are as describedinmalloc(3C). Thelibumem library provides these functions for backwards-compatibility with thestandard functions.
Seeumem_debug(3MALLOC) for environment variables that effect the debugging features of thelibumem library.
Contains a list of comma-separated options. Unrecognized options are ignored. The options that are supported are:
Example 1 Using theumem_alloc() function.
#include <stdio.h>#include <umem.h>...char *buf = umem_alloc(1024, UMEM_DEFAULT);if (buf == NULL) { fprintf(stderr, "out of memory\n"); return (1);}/* cannot assume anything about buf's contents */...umem_free(buf, 1024);...Example 2 Using theumem_zalloc() function
#include <stdio.h>#include <umem.h>...char *buf = umem_zalloc(1024, UMEM_DEFAULT);if (buf == NULL) { fprintf(stderr, "out of memory\n"); return (1);}/* buf contains zeros */...umem_free(buf, 1024);...Example 3 Using UMEM_NOFAIL
#include <stdlib.h>#include <stdio.h>#include <umem.h>/* * Note that the allocation code below does not have to * check for umem_alloc() returning NULL */intmy_failure_handler(void){ (void) fprintf(stderr, "out of memory\n"); return (UMEM_CALLBACK_EXIT(255));}...umem_nofail_callback(my_failure_handler);...int i;char *buf[100];for (i = 0; i < 100; i++) buf[i] = umem_alloc(1024 * 1024, UMEM_NOFAIL);...for (i = 0; i < 100; i++) umem_free(buf[i], 1024 * 1024);...Example 4 Using UMEM_NOFAIL in a multithreaded application
#define _REENTRANT#include <thread.h>#include <stdio.h>#include <umem.h> void *start_func(void *the_arg){ 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);}...intmy_failure_handler(void){ /* terminate the current thread with status NULL */ thr_exit(NULL);}...umem_nofail_callback(my_failure_handler);...int my_arg; thread_t tid;void *status; (void) thr_create(NULL, NULL, start_func, &my_arg, 0, NULL);...while (thr_join(0, &tid, &status) != 0) ; if (status == NULL) { (void) fprintf(stderr, "thread %d ran out of memory\n", tid);}...Seeattributes(5) for descriptions of the following attributes:
|
Formalloc(),calloc(),free(),realloc(), andvalloc(), seestandards(5).
exit(2),mmap(2),sbrk(2),bsdmalloc(3MALLOC),libumem(3LIB),longjmp(3C),malloc(3C),malloc(3MALLOC),mapmalloc(3MALLOC),pthread_exit(3C),thr_exit(3C),umem_cache_create(3MALLOC),umem_debug(3MALLOC),watchmalloc(3MALLOC),attributes(5),standards(5)
Oracle Solaris Modular Debugger Guide
Any of the following can cause undefined results:
Passing a pointer returned fromumem_alloc() orumem_zalloc() tofree() orrealloc().
Passing a pointer returned frommalloc(),calloc(),valloc(),memalign(), orrealloc() toumem_free().
Writing past the end of a buffer allocated usingumem_alloc() orumem_zalloc()
PerformingUMEM_NOFAIL allocations from anatexit(3C) handler.
If theUMEM_NOFAIL callback performsUMEM_NOFAIL allocations, infinite recursion can occur.
The following list compares the features of themalloc(3C),bsdmalloc(3MALLOC),malloc(3MALLOC),mtmalloc(3MALLOC), and thelibumem functions.
Themalloc(3C),bsdmalloc(3MALLOC), andmalloc(3MALLOC) functions have no support for concurrency. Thelibumem andmtmalloc(3MALLOC) functions support concurrent allocations.
Thebsdmalloc(3MALLOC) functions afford better performance but are space-inefficient.
Themalloc(3MALLOC) functions are space-efficient but have slower performance.
The standard, fully SCD-compliantmalloc(3C) functions are a trade-off between performance and space-efficiency.
Themtmalloc(3MALLOC) functions provide fast, concurrentmalloc() implementations that are not space-efficient.
Thelibumem functions provide a fast, concurrent allocation implementation that in most cases is more space-efficient thanmtmalloc(3MALLOC).
Copyright © 2011, Oracle and/or its affiliates. All rights reserved.Legal Notices | ![]() ![]() |