MALLOC(9) | Kernel Developer's Manual | MALLOC(9) |
malloc
,
mallocarray
, free
—
kernel memory allocator
#include
<sys/types.h>
#include <sys/malloc.h>
void *
malloc
(size_t
size, int type,
int flags);
void *
mallocarray
(size_t
nmemb, size_t size,
int type,
int flags);
void
free
(void
*addr, int type,
size_t size);
The
malloc
()
function allocates uninitialized memory in kernel address space for an
object whose size is specified by size.
The
mallocarray
()
function is the same as malloc
(), but allocates
space for an array of nmemb objects and checks for
arithmetic overflow.
The
free
()
function releases memory at address addr that was
previously allocated by malloc
() or
mallocarray
() for re-use. The same object size
originally provided to malloc
() should be specified
by size, because free
() will
operate faster knowing this. If tracking the size is difficult, specify
size as 0. If addr is a null
pointer, no action occurs.
The flags argument affects the
operational characteristics of
malloc
()
and mallocarray
() as follows:
M_WAITOK
malloc
() may
call sleep to wait for resources to be released by other processes.M_NOWAIT
malloc
() to return
NULL
if the request cannot be immediately
fulfilled due to resource shortage.M_CANFAIL
M_WAITOK
case, if not enough memory is
available, return NULL
instead of calling
panic(9). If
mallocarray
() detects an overflow or
malloc
() detects an excessive allocation, return
NULL
instead of calling
panic(9).M_ZERO
One of M_NOWAIT
or
M_WAITOK
must be specified via the
flags argument.
The type argument broadly identifies the
kernel subsystem for which the allocated memory was needed, and is commonly
used to maintain statistics about kernel memory usage. These statistics can
be examined using vmstat(8) or
systat(1) if either of the kernel
options(4)
KMEMSTATS
or DEBUG
are
enabled.
The following types are currently defined:
M_FREE
M_DEVBUF
M_PCB
M_RTABLE
M_IFADDR
M_SOOPTS
M_SYSCTL
M_COUNTERS
M_IOCTLOPS
M_IOV
M_MOUNT
M_NFSREQ
M_NFSMNT
M_VNODE
M_CACHE
M_DQUOT
M_UFSMNT
M_SHM
M_VMMAP
M_SEM
M_DIRHASH
M_ACPI
M_VMPMAP
M_FILE
M_FILEDESC
M_SIGIO
M_PROC
M_SUBPROC
M_VCLUSTER
M_MFSNODE
M_NETADDR
M_NFSSVC
M_NFSD
M_IPMOPTS
M_IPMADDR
M_IFMADDR
M_MRTABLE
M_ISOFSMNT
M_ISOFSNODE
M_MSDOSFSMNT
M_MSDOSFSFAT
M_MSDOSFSNODE
M_TTYS
M_EXEC
M_MISCFSMNT
M_FUSEFS
M_PFKEY
M_TDB
M_XDATA
M_PAGEDEP
M_INODEDEP
M_NEWBLK
M_INDIRDEP
M_VMSWAP
M_UVMAMAP
M_UVMAOBJ
M_USB
M_USBDEV
M_USBHC
M_WITNESS
M_MEMDESC
M_CRYPTO_DATA
M_CREDENTIALS
M_EMULDATA
M_IP6OPT
M_IP6NDP
M_TEMP
M_NTFSMNT
M_NTFSNTNODE
M_NTFSFNODE
M_NTFSDIR
M_NTFSNTHASH
M_NTFSNTVATTR
M_NTFSRDATA
M_NTFSDECOMP
M_NTFSRUN
M_KEVENT
M_SYNCACHE
M_UDFMOUNT
M_UDFFENTRY
M_UDFFID
M_AGP
M_DRM
malloc
() and
mallocarray
() can be called during autoconf, from
process context, or from interrupt context if
M_NOWAIT
is passed via flags.
They can't be called from interrupt context if
M_WAITOK
is passed via
flags.
free
() can be called during autoconf, from
process context, or from interrupt context.
malloc
() and
mallocarray
() return a kernel virtual address that
is suitably aligned for storage of any type of object.
A kernel compiled with the DIAGNOSTIC
configuration option attempts to detect memory corruption caused by such
things as writing outside the allocated area and unbalanced calls to
malloc
() or mallocarray
(),
and free
(). Failing consistency checks will cause a
panic or a system console message:
February 7, 2019 | OpenBSD-current |