add man pages from opensolaris

This commit is contained in:
Wez Furlong 2007-03-17 18:03:10 +00:00
parent 8f2686cc81
commit a6a20ffacf
3 changed files with 1615 additions and 0 deletions

525
umem_alloc.3 Normal file
View file

@ -0,0 +1,525 @@
'\" te
.\" CDDL HEADER START
.\"
.\" 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]
.\"
.\" CDDL HEADER END
.\" Copyright (c) 2002, Sun Microsystems, Inc. All Rights Reserved.
.TH umem_alloc 3MALLOC "26 Aug 2002" "SunOS 5.11" "Memory Allocation Library Functions"
.SH NAME
umem_alloc, umem_zalloc, umem_free, umem_nofail_callback \- fast, scalable memory allocation
.SH SYNOPSIS
.LP
.nf
cc [ \fIflag \&.\|.\|.\fR ] \fIfile\fR\&.\|.\|. \fB-lumem\fR [ \fIlibrary \&.\|.\|.\fR ]
#include <umem.h>
\fBvoid *\fR\fBumem_alloc\fR(\fBsize_t\fR \fIsize\fR, \fBint\fR \fIflags\fR);
.fi
.LP
.nf
\fBvoid *\fR\fBumem_zalloc\fR(\fBsize_t\fR \fIsize\fR, \fBint\fR \fIflags\fR);
.fi
.LP
.nf
\fBvoid\fR \fBumem_free\fR(\fBvoid *\fR\fIbuf\fR, \fBsize_t\fR \fIsize\fR);
.fi
.LP
.nf
\fBvoid\fR \fBumem_nofail_callback\fR(\fB(int (*\fR\fIcallback\fR)(void));
.fi
.LP
.nf
\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\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
.SH DESCRIPTION
.LP
The \fBumem_alloc()\fR function returns a pointer to a block of \fIsize\fR bytes suitably aligned for any variable type. The initial contents of memory allocated using \fBumem_alloc()\fR is undefined. The \fIflags\fR argument determines
the behavior of \fBumem_alloc()\fR if it is unable to fulfill the request. The \fIflags\fR argument can take the following values:
.sp
.sp
.ne 2
.mk
.na
\fB\fBUMEM_DEFAULT\fR\fR
.ad
.RS 14n
.rt
Return \fINULL\fR on failure.
.sp
.RE
.sp
.ne 2
.mk
.na
\fB\fBUMEM_NOFAIL\fR\fR
.ad
.RS 14n
.rt
Call an optional \fIcallback\fR (set with \fBumem_nofail_callback()\fR) on failure. The \fIcallback\fR takes no arguments and can finish by:
.sp
.sp
.RS +4
.TP
.ie t \(bu
.el o
returning \fBUMEM_CALLBACK_RETRY\fR, in which case the allocation will be retried. If the allocation fails, the callback will be invoked again.
.sp
.RE
.sp
.RS +4
.TP
.ie t \(bu
.el o
returning \fBUMEM_CALLBACK_EXIT\fR(\fIstatus\fR), in which case
\fBexit\fR(2) is invoked with \fIstatus\fR
as its argument. The \fBexit()\fR function is called only once. If multiple threads return from the \fBUMEM_NOFAIL\fR callback with \fBUMEM_CALLBACK_EXIT\fR(\fIstatus\fR), one will call \fBexit()\fR while the other blocks until \fBexit()\fR terminates the program.
.sp
.RE
.sp
.RS +4
.TP
.ie t \(bu
.el o
invoking a context-changing function (
\fBsetcontext\fR(2)) or a non-local jump (
\fBlongjmp\fR(3C) or
\fBsiglongjmp\fR(3C), or ending the current thread of control (
\fBthr_exit\fR(3C) or
\fBpthread_exit\fR(3C). The application is responsible for any necessary cleanup. The state of \fBlibumem\fR remains consistent.
.sp
.RE
If no callback has been set or the callback has been set to \fINULL\fR, \fBumem_alloc\fR(..., \fBUMEM_NOFAIL\fR) behaves as though the callback returned \fBUMEM_CALLBACK_EXIT\fR(255).
.sp
.sp
The \fBlibumem\fR library can call callbacks from any place that a \fBUMEM_NOFAIL\fR allocation is issued. In multithreaded applications, callbacks are expected to perform their own concurrency management.
.sp
.RE
.LP
The function call \fBumem_alloc\fR(0, \fIflag\fR) always returns \fINULL\fR. The function call \fBumem_free\fR(\fINULL\fR, 0) is allowed.
.sp
.LP
The \fBumem_zalloc()\fR function has the same semantics as \fBumem_alloc()\fR, but the block of memory is initialized to zeros before it is returned.
.sp
.LP
The \fBumem_free()\fR function frees blocks previously allocated using \fBumem_alloc()\fR and \fBumem_zalloc()\fR. The buffer address and size must exactly match the original allocation. Memory must not be returned piecemeal.
.sp
.LP
The \fBumem_nofail_callback()\fR function sets the process-wide UMEM_NOFAIL callback. See the description of UMEM_NOFAIL for more information.
.sp
.LP
The \fBmalloc()\fR, \fBcalloc()\fR, \fBfree()\fR, \fBmemalign()\fR, \fBrealloc()\fR, and \fBvalloc()\fR functions are are as described in
\fBmalloc\fR(3C). The \fBlibumem\fR library provides these functions for backwards-compatibility with the standard functions.
.sp
.SH ENVIRONMENT VARIABLES
.LP
See
\fBumem_debug\fR(3MALLOC) for environment variables that effect the debugging features of the \fBlibumem\fR library.
.sp
.sp
.ne 2
.mk
.na
\fBUMEM_OPTIONS\fR
.ad
.RS 14n
.rt
Contains a list of comma-separated options. Unrecognized options are ignored. The options that are supported are:
.sp
.sp
.ne 2
.mk
.na
\fB\fBbackend\fR=\fBsbrk\fR\fR
.ad
.br
.na
\fB\fBbackend\fR=\fBmmap\fR\fR
.ad
.RS 14n
.rt
Set the underlying function used to allocate memory. This option can be set to \fBsbrk\fR (the default) for an
\fBsbrk\fR(2)-based source or \fBmmap\fR for an
\fBmmap\fR(2)-based
source. If set to a value that is not supported, \fBsbrk\fR will be used.
.sp
.RE
.RE
.SH EXAMPLES
.LP
\fBExample 1 \fRUsing the \fBumem_alloc()\fR function.
.LP
.in +2
.nf
#include <stdio.h>
#include <umem.h>
\&...
char *buf = umem_alloc(1024, UMEM_DEFAULT);
if (buf == NULL) {
fprintf(stderr, "out of memory\en");
return (1);
}
/* cannot assume anything about buf's contents */
\&...
umem_free(buf, 1024);
\&...
.fi
.in -2
.LP
\fBExample 2 \fRUsing the \fBumem_zalloc()\fR function
.LP
.in +2
.nf
#include <stdio.h>
#include <umem.h>
\&...
char *buf = umem_zalloc(1024, UMEM_DEFAULT);
if (buf == NULL) {
fprintf(stderr, "out of memory\en");
return (1);
}
/* buf contains zeros */
\&...
umem_free(buf, 1024);
\&...
.fi
.in -2
.LP
\fBExample 3 \fRUsing UMEM_NOFAIL
.LP
.in +2
.nf
#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
*/
int
my_failure_handler(void)
{
(void) fprintf(stderr, "out of memory\en");
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);
\&...
.fi
.in -2
.LP
\fBExample 4 \fRUsing UMEM_NOFAIL in a multithreaded application
.LP
.in +2
.nf
#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);
}
\&...
int
my_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\en",
tid);
}
\&...
.fi
.in -2
.SH ATTRIBUTES
.LP
See
\fBattributes\fR(5) for descriptions of the following attributes:
.sp
.LP
.sp
.TS
tab() box;
cw(2.75i) |cw(2.75i)
lw(2.75i) |lw(2.75i)
.
ATTRIBUTE TYPEATTRIBUTE VALUE
_
Interface StabilitySee below.
_
MT-LevelMT-Safe
.TE
.LP
The \fBmalloc()\fR, \fBcalloc()\fR, \fBfree()\fR, \fBrealloc()\fR, and \fBvalloc()\fR functions are Standard. The \fBmemalign()\fR function is Stable. The \fBumem_alloc()\fR, \fBumem_zalloc()\fR, \fBumem_free()\fR, and \fBumem_nofail_callback()\fR functions are Evolving.
.sp
.SH SEE ALSO
.LP
\fBexit\fR(2),
\fBmmap\fR(2),
\fBsbrk\fR(2),
\fBbsdmalloc\fR(3MALLOC),
\fBlibumem\fR(3LIB),
\fBlongjmp\fR(3C),
\fBmalloc\fR(3C),
\fBmalloc\fR(3MALLOC),
\fBmapmalloc\fR(3MALLOC),
\fBpthread_exit\fR(3C),
\fBthr_exit\fR(3C),
\fBumem_cache_create\fR(3MALLOC),
\fBumem_debug\fR(3MALLOC),
\fBwatchmalloc\fR(3MALLOC),
\fBattributes\fR(5),
\fBstandards\fR(5)
.sp
.LP
.sp
.SH WARNINGS
.LP
Any of the following can cause undefined results:
.sp
.sp
.RS +4
.TP
.ie t \(bu
.el o
Passing a pointer returned from \fBumem_alloc()\fR or \fBumem_zalloc()\fR to \fBfree()\fR or \fBrealloc()\fR.
.sp
.RE
.sp
.RS +4
.TP
.ie t \(bu
.el o
Passing a pointer returned from \fBmalloc()\fR, \fBcalloc()\fR, \fBvalloc()\fR, \fBmemalign()\fR, or \fBrealloc()\fR to \fBumem_free()\fR.
.sp
.RE
.sp
.RS +4
.TP
.ie t \(bu
.el o
Writing past the end of a buffer allocated using \fBumem_alloc()\fR or \fBumem_zalloc()\fR
.sp
.RE
.sp
.RS +4
.TP
.ie t \(bu
.el o
Performing \fBUMEM_NOFAIL\fR allocations from an
\fBatexit\fR(3C) handler.
.sp
.RE
.LP
If the \fBUMEM_NOFAIL\fR callback performs \fBUMEM_NOFAIL\fR allocations, infinite recursion can occur.
.sp
.SH NOTES
.LP
The following list compares the features of the
\fBmalloc\fR(3C),
\fBbsdmalloc\fR(3MALLOC),
\fBmalloc\fR(3MALLOC),
\fBmtmalloc\fR(3MALLOC) , and the \fBlibumem\fR
functions.
.sp
.sp
.RS +4
.TP
.ie t \(bu
.el o
The
\fBmalloc\fR(3C),
\fBbsdmalloc\fR(3MALLOC), and
\fBmalloc\fR(3MALLOC) functions have no support for concurrency. The \fBlibumem\fR and
\fBmtmalloc\fR(3MALLOC)
functions support concurrent allocations.
.sp
.RE
.sp
.RS +4
.TP
.ie t \(bu
.el o
The
\fBbsdmalloc\fR(3MALLOC) functions afford better performance but are space-inefficient.
.sp
.RE
.sp
.RS +4
.TP
.ie t \(bu
.el o
The
\fBmalloc\fR(3MALLOC) functions are space-efficient but have slower performance.
.sp
.RE
.sp
.RS +4
.TP
.ie t \(bu
.el o
The standard, fully SCD-compliant
\fBmalloc\fR(3C) functions are a trade-off between performance and space-efficiency.
.sp
.RE
.sp
.RS +4
.TP
.ie t \(bu
.el o
The
\fBmtmalloc\fR(3MALLOC) functions provide fast, concurrent \fBmalloc()\fR implementations that are not space-efficient.
.sp
.RE
.sp
.RS +4
.TP
.ie t \(bu
.el o
The \fBlibumem\fR functions provide a fast, concurrent allocation implementation that in most cases is more space-efficient than
\fBmtmalloc\fR(3MALLOC).
.sp
.RE

815
umem_cache_create.3 Normal file
View file

@ -0,0 +1,815 @@
'\" te
.\" CDDL HEADER START
.\"
.\" 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]
.\"
.\" CDDL HEADER END
.\" Copyright (c) 2003, Sun Microsystems, Inc. All Rights Reserved.
.TH umem_cache_create 3MALLOC "4 Nov 2003" "SunOS 5.11" "Memory Allocation Library Functions"
.SH NAME
umem_cache_create, umem_cache_destroy, umem_cache_alloc, umem_cache_free \- allocation cache manipulation
.SH SYNOPSIS
.LP
.nf
cc [ \fIflag \&.\|.\|.\fR ] \fIfile\fR\&.\|.\|. \fB-lumem\fR [ \fIlibrary \&.\|.\|.\fR ]
#include <umem.h>
\fBumem_cache_t *\fR\fBumem_cache_create\fR(\fBchar *\fR\fIdebug_name\fR, \fBsize_t\fR \fIbufsize\fR,
\fBsize_t\fR \fIalign\fR, \fBumem_constructor_t *\fR\fIconstructor\fR,
\fBumem_destructor_t *\fR\fIdestructor\fR, \fBumem_reclaim_t *\fR\fIreclaim\fR,
\fBvoid *\fR\fIcallback_data\fR, \fBvmem_t *\fR\fIsource\fR, \fBint\fR \fIcflags\fR);
.fi
.LP
.nf
\fBvoid\fR \fBumem_cache_destroy\fR(\fBumem_cache_t *\fR\fIcache\fR);
.fi
.LP
.nf
\fBvoid *\fR\fBumem_cache_alloc\fR(\fBumem_cache_t *\fR\fIcache\fR, \fBint\fR \fIflags\fR);
.fi
.LP
.nf
\fBvoid\fR \fBumem_cache_free\fR(\fBumem_cache_t *\fR\fIcache\fR, \fBvoid *\fR\fIbuffer\fR);
.fi
.SH DESCRIPTION
.LP
These functions create, destroy, and use an "object cache". An object cache is a collection of buffers of a single size, with optional content caching enabled by the use of callbacks (see \fBCache Callbacks\fR). Object caches are MT-Safe. Multiple allocations and freeing of
memory from different threads can proceed simultaneously. Object caches are faster and use less space per buffer than
\fBmalloc\fR(3MALLOC) and
\fBumem_alloc\fR(3MALLOC). For more information about object caching, see "The Slab Allocator: An Object-Caching Kernel Memory Allocator" and "Magazines
and vmem: Extending the Slab Allocator to Many CPUs and Arbitrary Resources".
.sp
.LP
The \fBumem_cache_create()\fR function creates object caches. Once a cache has been created, objects can be requested from and returned to the cache using \fBumem_cache_alloc()\fR and \fBumem_cache_free()\fR, respectively. A cache with no outstanding
buffers can be destroyed with \fBumem_cache_destroy()\fR.
.sp
.SS Creating and Destroying Caches
.LP
The \fBumem_cache_create()\fR function creates a cache of objects and takes as arguments the following:
.sp
.sp
.ne 2
.mk
.na
\fB\fIdebug_name\fR\fR
.ad
.RS 15n
.rt
A human-readable name for debugging purposes.
.sp
.RE
.sp
.ne 2
.mk
.na
\fB\fIbufsize\fR\fR
.ad
.RS 15n
.rt
The size, in bytes, of the buffers in this cache.
.sp
.RE
.sp
.ne 2
.mk
.na
\fB\fIalign\fR\fR
.ad
.RS 15n
.rt
The minimum alignment required for buffers in this cache. This parameter must be a power of 2. If 0, it is replaced with the minimum required alignment for the current architecture.
.sp
.RE
.sp
.ne 2
.mk
.na
\fB\fIconstructor\fR\fR
.ad
.RS 15n
.rt
The callback to construct an object.
.sp
.RE
.sp
.ne 2
.mk
.na
\fB\fIdestructor\fR\fR
.ad
.RS 15n
.rt
The callback to destroy an object.
.sp
.RE
.sp
.ne 2
.mk
.na
\fB\fIreclaim\fR\fR
.ad
.RS 15n
.rt
The callback to reclaim objects.
.sp
.RE
.sp
.ne 2
.mk
.na
\fB\fIcallback_data\fR\fR
.ad
.RS 15n
.rt
An opaque pointer passed to the callbacks.
.sp
.RE
.sp
.ne 2
.mk
.na
\fB\fIsource\fR\fR
.ad
.RS 15n
.rt
This parameter must be \fINULL\fR.
.sp
.RE
.sp
.ne 2
.mk
.na
\fB\fIcflags\fR\fR
.ad
.RS 15n
.rt
This parameter must be either 0 or \fBUMC_NODEBUG\fR. If \fBUMC_NODEBUG\fR, all debugging features are disabled for this cache. See
\fBumem_debug\fR(3MALLOC).
.sp
.RE
.LP
Each cache can have up to three associated callbacks:
.sp
.LP
.in +2
.nf
int constructor(void *buffer, void *callback_data, int flags);
void destructor(void *buffer, void *callback_data);
void reclaim(void *callback_data);
.fi
.in -2
.LP
The \fIcallback_data\fR argument is always equal to the value passed to \fBumem_cache_create()\fR, thereby allowing a client to use the same callback functions for multiple caches, but with customized behavior.
.sp
.LP
The reclaim callback is called when the umem function is requesting more memory from the operating system. This callback can be used by clients who retain objects longer than they are strictly needed (for example, caching non-active state). A typical reclaim callback might return to the
cache ten per cent of the unneeded buffers.
.sp
.LP
The constructor and destructor callbacks enable the management of buffers with the constructed state. The constructor takes as arguments a buffer with undefined contents, some callback data, and the flags to use for any allocations. This callback should transform the buffer into the constructed
state.
.sp
.LP
The destructor callback takes as an argument a constructed object and prepares it for return to the general pool of memory. The destructor should undo any state that the constructor created. For debugging, the destructor can also check that the buffer is in the constructed state, to catch
incorrectly freed buffers. See
\fBumem_debug\fR(3MALLOC) for further information on debugging support.
.sp
.LP
The \fBumem_cache_destroy()\fR function destroys an object cache. If the cache has any outstanding allocations, the behavior is undefined.
.sp
.SS Allocating Objects
.LP
The \fBumem_cache_alloc()\fR function takes as arguments:
.sp
.sp
.ne 2
.mk
.na
\fB\fIcache\fR\fR
.ad
.RS 7n
.rt
a cache pointer
.sp
.RE
.sp
.ne 2
.mk
.na
\fB\fIflags\fR\fR
.ad
.RS 7n
.rt
flags that determine the behavior if \fBumem_cache_alloc()\fR is unable to fulfill the allocation request
.sp
.RE
.LP
If successful, \fBumem_cache_alloc()\fR returns a pointer to the beginning of an object of \fIbufsize\fR length.
.sp
.LP
There are three cases to consider:
.sp
.sp
.RS +4
.TP
.ie t \(bu
.el o
A new buffer needed to be allocated. If the cache was created with a constructor, it is applied to the buffer and the resulting object is returned.
.sp
.RE
.sp
.RS +4
.TP
.ie t \(bu
.el o
The object cache was able to use a previously freed buffer. If the cache was created with a constructor, the object is returned unchanged from when it was freed.
.sp
.RE
.sp
.RS +4
.TP
.ie t \(bu
.el o
The allocation of a new buffer failed. The \fIflags\fR argument determines the behavior:
.sp
.sp
.ne 2
.mk
.na
\fB\fBUMEM_DEFAULT\fR\fR
.ad
.RS 14n
.rt
The \fBumem_cache_alloc()\fR function returns \fINULL\fR if the allocation fails.
.sp
.RE
.sp
.ne 2
.mk
.na
\fB\fBUMEM_NOFAIL\fR\fR
.ad
.RS 14n
.rt
The \fBumem_cache_alloc()\fR function cannot return \fINULL\fR. A callback is used to determine what action occurs. See
\fBumem_alloc\fR(3MALLOC) for more information.
.sp
.RE
.RE
.SS Freeing Objects
.LP
The \fBumem_cache_free()\fR function takes as arguments:
.sp
.sp
.ne 2
.mk
.na
\fB\fIcache\fR\fR
.ad
.RS 7n
.rt
a cache pointer
.sp
.RE
.sp
.ne 2
.mk
.na
\fB\fIbuf\fR\fR
.ad
.RS 7n
.rt
a pointer previously returned from \fBumem_cache_alloc()\fR. This argument must not be \fINULL\fR.
.sp
.RE
.LP
If the cache was created with a constructor callback, the object must be returned to the constructed state before it is freed.
.sp
.LP
Undefined behavior results if an object is freed multiple times, if an object is modified after it is freed, or if an object is freed to a cache other than the one from which it was allocated.
.sp
.SS Caches with Constructors
.LP
When a constructor callback is in use, there is essentially a contract between the cache and its clients. The cache guarantees that all objects returned from \fBumem_cache_alloc()\fR will be in the constructed state, and the client guarantees that it will return the object
to the constructed state before handing it to \fBumem_cache_free()\fR.
.sp
.SH RETURN VALUES
.LP
Upon failure, the \fBumem_cache_create()\fR function returns a null pointer.
.sp
.SH ERRORS
.LP
The \fBumem_cache_create()\fR function will fail if:
.sp
.sp
.ne 2
.mk
.na
\fB\fBEAGAIN\fR\fR
.ad
.RS 8n
.rt
There is not enough memory available to allocate the cache data structure.
.sp
.RE
.sp
.ne 2
.mk
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 8n
.rt
The \fIdebug_name\fR argument is \fINULL\fR, the \fIalign\fR argument is not a power of two or is larger than the system pagesize, or the \fIbufsize\fR argument is 0.
.sp
.RE
.sp
.ne 2
.mk
.na
\fB\fBENOMEM\fR\fR
.ad
.RS 8n
.rt
The \fBlibumem\fR library could not be initialized, or the \fIbufsize\fR argument is too large and its use would cause integer overflow to occur.
.sp
.RE
.SH EXAMPLES
.LP
\fBExample 1 \fRUse a fixed-size structure with no constructor callback.
.LP
.in +2
.nf
#include <umem.h>
typedef struct my_obj {
long my_data1;
} my_obj_t;
/*
* my_objs can be freed at any time. The contents of
* my_data1 is undefined at allocation time.
*/
umem_cache_t *my_obj_cache;
\&...
my_obj_cache = umem_cache_create("my_obj", sizeof (my_obj_t),
0, NULL, NULL, NULL, NULL, NULL, 0);
\&...
my_obj_t *cur = umem_cache_alloc(my_obj_cache, UMEM_DEFAULT);
\&...
/* use cur */
\&...
umem_cache_free(my_obj_cache, cur);
\&...
.fi
.in -2
.LP
\fBExample 2 \fRUse an object with a mutex.
.LP
.in +2
.nf
#define _REENTRANT
#include <synch.h>
#include <umem.h>
typedef struct my_obj {
mutex_t my_mutex;
long my_data;
} my_obj_t;
/*
* my_objs can only be freed when my_mutex is unlocked.
*/
int
my_obj_constructor(void *buf, void *ignored, int flags)
{
my_obj_t *myobj = buf;
(void) mutex_init(&my_obj->my_mutex, USYNC_THREAD, NULL);
return (0);
}
void
my_obj_destructor(void *buf, void *ignored)
{
my_obj_t *myobj = buf;
(void) mutex_destroy(&my_obj->my_mutex);
}
umem_cache_t *my_obj_cache;
\&...
my_obj_cache = umem_cache_create("my_obj", sizeof (my_obj_t),
0, my_obj_constructor, my_obj_destructor, NULL, NULL,
NULL, 0);
\&...
my_obj_t *cur = umem_cache_alloc(my_obj_cache, UMEM_DEFAULT);
cur->my_data = 0; /* cannot assume anything about my_data */
\&...
umem_cache_free(my_obj_cache, cur);
\&...
.fi
.in -2
.LP
\fBExample 3 \fRUse a more complex object with a mutex.
.LP
.in +2
.nf
#define _REENTRANT
#include <assert.h>
#include <synch.h>
#include <umem.h>
typedef struct my_obj {
mutex_t my_mutex;
cond_t my_cv;
struct bar *my_barlist;
unsigned my_refcount;
} my_obj_t;
/*
* my_objs can only be freed when my_barlist == NULL,
* my_refcount == 0, there are no waiters on my_cv, and
* my_mutex is unlocked.
*/
int
my_obj_constructor(void *buf, void *ignored, int flags)
{
my_obj_t *myobj = buf;
(void) mutex_init(&my_obj->my_mutex, USYNC_THREAD, NULL);
(void) cond_init(&my_obj->my_cv, USYNC_THREAD, NULL);
myobj->my_barlist = NULL;
myobj->my_refcount = 0;
return (0);
}
void
my_obj_destructor(void *buf, void *ignored)
{
my_obj_t *myobj = buf;
assert(myobj->my_refcount == 0);
assert(myobj->my_barlist == NULL);
(void) cond_destroy(&my_obj->my_cv);
(void) mutex_destroy(&my_obj->my_mutex);
}
umem_cache_t *my_obj_cache;
\&...
my_obj_cache = umem_cache_create("my_obj", sizeof (my_obj_t),
0, my_obj_constructor, my_obj_destructor, NULL, NULL,
NULL, 0);
\&...
my_obj_t *cur = umem_cache_alloc(my_obj_cache, UMEM_DEFAULT);
\&...
/* use cur */
\&...
umem_cache_free(my_obj_cache, cur);
\&...
.fi
.in -2
.LP
\fBExample 4 \fRUse objects with a subordinate buffer while reusing callbacks.
.LP
.in +2
.nf
#include assert.h>
#include umem.h>
typedef struct my_obj {
char *my_buffer;
size_t my_size;
} my_obj_t;
/*
* my_size and the my_buffer pointer should never be changed
*/
int
my_obj_constructor(void *buf, void *arg, int flags)
{
size_t sz = (size_t)arg;
my_obj_t *myobj = buf;
if ((myobj->my_buffer = umem_alloc(sz, flags)) == NULL)
return (1);
my_size = sz;
return (0);
}
void
my_obj_destructor(void *buf, void *arg)
{
size_t sz = (size_t)arg;
my_obj_t *myobj = buf;
assert(sz == buf->my_size);
umem_free(myobj->my_buffer, sz);
}
\&...
umem_cache_t *my_obj_4k_cache;
umem_cache_t *my_obj_8k_cache;
\&...
my_obj_cache_4k = umem_cache_create("my_obj_4k", sizeof (my_obj_t),
0, my_obj_constructor, my_obj_destructor, NULL,
(void *)4096, NULL, 0);
my_obj_cache_8k = umem_cache_create("my_obj_8k", sizeof (my_obj_t),
0, my_obj_constructor, my_obj_destructor, NULL,
(void *)8192, NULL, 0);
\&...
my_obj_t *my_obj_4k = umem_cache_alloc(my_obj_4k_cache,
UMEM_DEFAULT);
my_obj_t *my_obj_8k = umem_cache_alloc(my_obj_8k_cache,
UMEM_DEFAULT);
/* no assumptions should be made about the contents
of the buffers */
\&...
/* make sure to return them to the correct cache */
umem_cache_free(my_obj_4k_cache, my_obj_4k);
umem_cache_free(my_obj_8k_cache, my_obj_8k);
\&...
.fi
.in -2
.LP
See the \fBEXAMPLES\fR section of
\fBumem_alloc\fR(3MALLOC) for examples involving the \fBUMEM_NOFAIL\fR flag.
.sp
.SH ATTRIBUTES
.LP
See
\fBattributes\fR(5) for descriptions of the following attributes:
.sp
.LP
.sp
.TS
tab() box;
cw(2.75i) |cw(2.75i)
lw(2.75i) |lw(2.75i)
.
ATTRIBUTE TYPEATTRIBUTE VALUE
_
Interface StabilityEvolving
_
MT-LevelMT-Safe
.TE
.SH SEE ALSO
.LP
\fBsetcontext\fR(2),
\fBatexit\fR(3C),
\fBlibumem\fR(3LIB),
\fBlongjmp\fR(3C),
\fBswapcontext\fR(3C),
\fBthr_exit\fR(3C),
\fBumem_alloc\fR(3MALLOC),
\fBumem_debug\fR(3MALLOC),
\fBattributes\fR(5)
.sp
.LP
Bonwick, Jeff, "The Slab Allocator: An Object-Caching Kernel Memory Allocator", Proceedings of the Summer 1994 Usenix Conference.
.sp
.LP
Bonwick, Jeff and Jonathan Adams, "Magazines and vmem: Extending the Slab Allocator to Many CPUs and Arbitrary Resources", Proceedings of the Summer 2001 Usenix Conference.
.sp
.SH WARNINGS
.LP
Any of the following can cause undefined results:
.sp
.sp
.RS +4
.TP
.ie t \(bu
.el o
Destroying a cache that has outstanding allocated buffers.
.sp
.RE
.sp
.RS +4
.TP
.ie t \(bu
.el o
Using a cache after it has been destroyed.
.sp
.RE
.sp
.RS +4
.TP
.ie t \(bu
.el o
Calling \fBumem_cache_free()\fR on the same buffer multiple times.
.sp
.RE
.sp
.RS +4
.TP
.ie t \(bu
.el o
Passing a \fINULL\fR pointer to \fBumem_cache_free()\fR.
.sp
.RE
.sp
.RS +4
.TP
.ie t \(bu
.el o
Writing past the end of a buffer.
.sp
.RE
.sp
.RS +4
.TP
.ie t \(bu
.el o
Reading from or writing to a buffer after it has been freed.
.sp
.RE
.sp
.RS +4
.TP
.ie t \(bu
.el o
Performing \fBUMEM_NOFAIL\fR allocations from an
\fBatexit\fR(3C) handler.
.sp
.RE
.LP
Per-cache callbacks can be called from a variety of contexts. The use of functions that modify the active context, such as
\fBsetcontext\fR(2),
\fBswapcontext\fR(3C), and
\fBthr_exit\fR(3C),
or functions that are unsafe for use in multithreaded applications, such as
\fBlongjmp\fR(3C) and
\fBsiglongjmp\fR(3C), result in undefined behavior.
.sp
.LP
A constructor callback that performs allocations must pass its \fIflags\fR argument unchanged to
\fBumem_alloc\fR(3MALLOC) and \fBumem_cache_alloc()\fR. Any allocations made with a different flags argument results in undefined behavior. The constructor must correctly handle the failure of any allocations it makes.
.sp
.SH NOTES
.LP
Object caches make the following guarantees about objects:
.sp
.sp
.RS +4
.TP
.ie t \(bu
.el o
If the cache has a constructor callback, it is applied to every object before it is returned from \fBumem_cache_alloc()\fR for the first time.
.sp
.RE
.sp
.RS +4
.TP
.ie t \(bu
.el o
If the cache has a constructor callback, an object passed to \fBumem_cache_free()\fR and later returned from \fBumem_cache_alloc()\fR is not modified between the two events.
.sp
.RE
.sp
.RS +4
.TP
.ie t \(bu
.el o
If the cache has a destructor, it is applied to all objects before their underlying storage is returned.
.sp
.RE
.LP
No other guarantees are made. In particular, even if there are buffers recently freed to the cache, \fBumem_cache_alloc()\fR can fail.
.sp

275
umem_debug.3 Normal file
View file

@ -0,0 +1,275 @@
'\" te
.\" CDDL HEADER START
.\"
.\" 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]
.\"
.\" CDDL HEADER END
.\" Copyright (c) 2002, Sun Microsystems, Inc. All Rights Reserved.
.TH umem_debug 3MALLOC "26 July 2002" "SunOS 5.11" "Memory Allocation Library Functions"
.SH NAME
umem_debug \- debugging features of the umem library
.SH SYNOPSIS
.LP
.nf
\fBcc\fR [ \fIflag\fR\&.\|.\|. ] \fIfile\fR\&.\|.\|. \fB-lumem\fR [ \fIlibrary\fR\&.\|.\|. ]
#include <\fBumem.h\fR>
.fi
.SH DESCRIPTION
.LP
The \fBlibumem\fR library provides debugging features that detect memory leaks, buffer overruns, multiple frees, use of uninitialized data, use of freed data, and many other common programming errors. The activation of the run-time debugging features is controlled by environment variables.
.sp
.LP
When the library detects an error, it writes a description of the error to an internal buffer that is readable with the \fB::umem_status\fR
\fBmdb\fR(1) \fIdcmd\fR and then calls
\fBabort\fR(3C).
.sp
.SH ENVIRONMENT VARIABLES
.sp
.ne 2
.mk
.na
\fBUMEM_DEBUG\fR
.ad
.RS 14n
.rt
This variable contains a list of comma-separated options. Unrecognized options are ignored. Possible options include:
.sp
.sp
.ne 2
.mk
.na
\fB\fBaudit\fR[=\fIframes\fR]\fR
.ad
.RS 18n
.rt
This option enables the recording of auditing information, including thread ID, high-resolution time stamp, and stack trace for the last action (allocation or free) on every allocation. If transaction logging
(see UMEM_LOGGING) is enabled, this auditing information is also logged.
.sp
.sp
The \fIframes\fR parameter sets the number of stack frames recorded in the auditing structure. The upper bound for frames is implementation-defined. If a larger value is requested, the upper bound is used instead.
.sp
.sp
If \fIframes\fR is not specified or is not an integer, the default value of 15 is used.
.sp
.sp
This option also enables the \fBguards\fR option.
.sp
.RE
.sp
.ne 2
.mk
.na
\fB\fBcontents\fR[=\fIcount\fR]\fR
.ad
.RS 18n
.rt
If auditing and contents logging (see UMEM_LOGGING) are enabled, the first \fIcount\fR bytes of each buffer are logged when they are freed. If a buffer is shorter than \fIcount\fR bytes, it is logged in its entirety.
.sp
.sp
If \fIcount\fR is not specified or is not an integer, the default value of 256 is used.
.sp
.RE
.sp
.ne 2
.mk
.na
\fB\fBdefault\fR\fR
.ad
.RS 18n
.rt
This option is equivalent to \fBaudit\fR,\fBcontents\fR,\fBguards\fR.
.sp
.RE
.sp
.ne 2
.mk
.na
\fB\fBguards\fR\fR
.ad
.RS 18n
.rt
This option enables filling allocated and freed buffers with special patterns to help detect the use of uninitialized data and previously freed buffers. It also enables an 8-byte redzone after each buffer that contains \fB0xfeedfacefeedfaceULL\fR.
.sp
.sp
When an object is freed, it is filled with \fB0xdeadbeef\fR. When an object is allocated, the \fB0xdeadbeef\fR pattern is verified and replaced with \fB0xbaddcafe\fR. The redzone is checked every time a buffer is allocated or freed.
.sp
.sp
For caches with either constructors or destructors, or both,
\fBumem_cache_alloc\fR(3MALLOC) and
\fBumem_cache_free\fR(3MALLOC) apply the cache's constructor and destructor, respectively, instead of caching constructed objects. The presence of
\fBassert\fR(3C)s
in the destructor verifying that the buffer is in the constructed state can be used to detect any objects returned in an improper state. See
\fBumem_cache_create\fR(3MALLOC) for
details.
.sp
.RE
.sp
.ne 2
.mk
.na
\fB\fBverbose\fR\fR
.ad
.RS 18n
.rt
The library writes error descriptions to standard error before aborting. These messages are not localized.
.sp
.RE
.RE
.sp
.ne 2
.mk
.na
\fBUMEM_LOGGING\fR
.ad
.RS 14n
.rt
To be enabled, this variable should be set to a comma-separated list of in-memory logs. The logs available are:
.sp
.sp
.ne 2
.mk
.na
\fB\fBtransaction\fR[=\fIsize\fR]\fR
.ad
.RS 20n
.rt
If the \fBaudit\fR debugging option is set (see \fBUMEM_DEBUG\fR), the audit structures from previous transactions are entered into this log.
.sp
.RE
.sp
.ne 2
.mk
.na
\fB\fBcontents\fR[=\fIsize\fR]\fR
.ad
.RS 20n
.rt
If the \fBaudit\fR debugging option is set, the contents of objects are recorded in this log as they are freed.
.sp
.sp
If the "contents" debugging option was not set, 256 bytes of each freed buffer are saved.
.sp
.RE
.sp
.ne 2
.mk
.na
\fB\fBfail\fR[=\fIsize\fR]\fR
.ad
.RS 20n
.rt
Records are entered into this log for every failed allocation.
.sp
.RE
For any of these options, if \fIsize\fR is not specified, the default value of 64k is used. The \fIsize\fR parameter must be an integer that can be qualified with K, M, G, or T to specify kilobytes, megabytes, gigabytes, or terabytes, respectively.
.sp
.sp
Logs that are not listed or that have either a size of 0 or an invalid size are disabled.
.sp
.sp
The log is disabled if during initialization the requested amount of storage cannot be allocated.
.sp
.RE
.SH ATTRIBUTES
.LP
See
\fBattributes\fR(5) for descriptions of the following attributes:
.sp
.LP
.sp
.TS
tab() box;
cw(2.75i) |cw(2.75i)
lw(2.75i) |lw(2.75i)
.
ATTRIBUTE TYPEATTRIBUTE VALUE
_
Interface StabilityUnstable
_
MT-LevelMT-Safe
.TE
.SH SEE ALSO
.LP
\fBmdb\fR(1),
\fBabort\fR(3C),
\fBsignal\fR(3C),
\fBumem_cache_create\fR(3MALLOC),
\fBattributes\fR(5)
.sp
.LP
.sp
.SH WARNINGS
.LP
When \fBlibumem\fR aborts the process using
\fBabort\fR(3C), any existing signal handler for \fBSIGABRT\fR is called. If the signal handler performs allocations, undefined
behavior can result.
.sp
.SH NOTES
.LP
Some of the debugging features work only for allocations smaller than 16 kilobytes in size. Allocations larger than 16 kilobytes could have reduced support.
.sp
.LP
Activating any of the library's debugging features could significantly increase the library's memory footprint and decrease its performance.
.sp