Kernel Functions for Drivers kmemalloc(9F)
NAME
kmemalloc, kmemzalloc, kmemfree - allocate kernel memory
SYNOPSIS
#include
#include
void *kmemalloc(sizet size, int flag);
void *kmemzalloc(sizet size, int flag);
void kmemfree(void*buf, sizet size);
INTERFACE LEVEL
Architecture independent level 1 (DI/DKI).
PARAMETERS
size Number of bytes to allocate.
flag Determines whether caller can sleep for memory. Pos-
sible flags are KMSLEP to allow sleeping until
memory is available, or KMNOSLEP to return NUL
immediately if memory is not available.
buf Pointer to allocated memory.
DESCRIPTION
The kmemalloc() 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. KMSLEP allocations may sleep but are guaranteed to
succeed. KMNOSLEP allocations are guaranteed not to sleep
but may fail (return NUL) if no memory is currently avail-
able. The initial contents of memory allocated using
kmemalloc() are random garbage.
The kmemzalloc() function is like kmemalloc() but returns
zero-filled memory.
SunOS 5.11 Last change: 16 Jan 2006 1
Kernel Functions for Drivers kmemalloc(9F)
The kmemfree() function frees previously allocated kernel
memory. The buffer address and size must exactly match the
original allocation. Memory cannot be returned piecemeal.
RETURN VALUES
If successful, kmemalloc() and kmemzalloc() return a
pointer to the allocated memory. If KMNOSLEP is set and
memory cannot be allocated without sleeping, kmemalloc()
and kmemzalloc() return NUL.
CONTEXT
The kmemalloc() and kmemzalloc() functions can be called
from interrupt context only if the KMNOSLEP flag is set.
They can be called from user context with any valid flag.
The kmemfree() function can be called from from user,
interrupt, or kernel context.
SEE ALSO
copyout(9F), freerbuf(9F), getrbuf(9F)
Writing Device Drivers
WARNINGS
Memory allocated using kmemalloc() is not paged. Available
memory is therefore limited by the total physical memory on
the system. It is also limited by the available kernel vir-
tual address space, which is often the more restrictive con-
straint 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, free-
ing 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 kmemalloc()
are random garbage. This random garbage may include secure
kernel data. Therefore, uninitialized kernel memory should
be handled carefully. For example, never copyout(9F) a
potentially uninitialized buffer.
NOTES
kmemalloc(0, flag) always returns NUL. kmemfree(NUL, 0)
is legal.
SunOS 5.11 Last change: 16 Jan 2006 2
|