KMEM_ALLOC(9F) Kernel Functions for Drivers KMEM_ALLOC(9F)
kmem_alloc, kmem_zalloc, kmem_free - allocate kernel memory
#include <sys/kmem.h> void *kmem_alloc
, int flag
); void *kmem_zalloc
, int flag
); void kmem_free
, size_t size
Architecture independent level 1 (DDI/DKI).
Number of bytes to allocate. flag
Determines whether caller can sleep for memory. Possible flags
to allow sleeping until memory is available, KM_NOSLEEP
to return NULL
if memory is not available even after
some reclamation attempts, and KM_NOSLEEP_LAZY
to return NULL
without reclamation attempts. KM_NOSLEEP_LAZY
is actually two
flags combined: (KM_NOSLEEP
), the latter flag
indicating not to attempt reclamation before giving up and
returning NULL. If any mention of KM_NOSLEEP
appears in this man
page by itself, it applies equally to KM_NOSLEEP_LAZY
as well. buf
Pointer to allocated memory.
function allocates size
bytes of kernel memory and
returns a pointer to the allocated memory. The allocated memory is at
least double-word aligned, so it can hold any C data structure. No
greater alignment can be assumed. flag
determines whether the caller can
sleep for memory. KM_SLEEP
allocations may sleep but are guaranteed to
allocations are guaranteed not
to sleep but may fail (return NULL
) if no memory is currently available. KM_NOSLEEP
will first attempt to aggressively reclaim memory from
otherwise unused blocks, while KM_NOSLEEP_LAZY
will not attempt any
reclamation. The initial contents of memory allocated using kmem_alloc()
are random garbage.
function is like kmem_alloc()
but returns zero-filled
function frees previously allocated kernel memory. The
buffer address and size must exactly match the original allocation.
Memory cannot be returned piecemeal.
If successful, kmem_alloc()
return a pointer to the
allocated memory. If KM_NOSLEEP
is set and memory cannot be allocated
without sleeping, kmem_alloc()
functions can be called from interrupt
context only if the KM_NOSLEEP
flag is set. They can be called from user
context with any valid flag
. The kmem_free()
function can be called from
from user, interrupt, or kernel context.
SEE ALSO copyout(9F)
, getrbuf(9F) Writing Device Drivers
Memory allocated using kmem_alloc()
is not paged. Available memory is
therefore limited by the total physical memory on the system. It is also
limited by the available kernel virtual address space, which is often the
more restrictive constraint on large-memory configurations.
Excessive use of kernel memory is likely to affect overall system
performance. Overcommitment of kernel memory will cause the system to
hang or panic.
Misuse of the kernel memory allocator, such as writing past the end of a
buffer, using a buffer after freeing it, freeing a buffer twice, or
freeing a null or invalid pointer, will corrupt the kernel heap and may
cause the system to corrupt data or panic.
The initial contents of memory allocated using kmem_alloc()
garbage. This random garbage may include secure kernel data. Therefore,
uninitialized kernel memory should be handled carefully. For example,
a potentially uninitialized buffer.
always returns NULL
, but if KM_SLEEP
is set, this
behavior is considered to be deprecated; the system may be configured to
explicitly panic in this case in lieu of returning NULL
. kmem_free(NULL, 0)
is legal, however.
November 20, 2019 KMEM_ALLOC(9F)