DragonFly On-Line Manual Pages
KMALLOC(9) DragonFly Kernel Developer's Manual KMALLOC(9)
NAME
kmalloc, kfree, krealloc, kmalloc_raise_limit, MALLOC_DEFINE,
MALLOC_DECLARE - kernel memory management routines
SYNOPSIS
#include <sys/types.h>
#include <sys/malloc.h>
void *
kmalloc(unsigned long size, struct malloc_type *type, int flags);
void
kfree(void *addr, struct malloc_type *type);
void *
krealloc(void *addr, unsigned long size, struct malloc_type *type,
int flags);
void
kmalloc_raise_limit(struct malloc_type *type, size_t bytes);
MALLOC_DECLARE(type);
#include <sys/param.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
MALLOC_DEFINE(type, shortdesc, longdesc);
DESCRIPTION
The kmalloc() function allocates uninitialized memory in kernel address
space for an object whose size is specified by size.
The kfree() function releases memory at address addr that was previously
allocated by kmalloc() for re-use. The memory is not zeroed. The kernel
implementation of kfree() does not allow addr to be NULL.
The krealloc() function changes the size of the previously allocated
memory referenced by addr to size bytes. The contents of the memory are
unchanged up to the lesser of the new and old sizes. Note that the
returned value may differ from addr. If the requested memory cannot be
allocated, NULL is returned and the memory referenced by addr is valid
and unchanged. If addr is NULL, the krealloc() function behaves
identically to kmalloc() for the specified size.
kmalloc_raise_limit() is used to increase the internal pool limit to
bytes. Under most of the cases the default internal pool limit should be
more than enough, so this function is currently rarely used and must be
used with care.
Unlike its standard C library counterpart (malloc(3)), the kernel version
takes two more arguments. The flags argument further qualifies
kmalloc()'s operational characteristics as follows:
M_ZERO Causes the allocated memory to be set to all zeros.
M_NOWAIT
Causes kmalloc() and krealloc(), to return NULL if the request
cannot be immediately fulfilled due to resource shortage. Note
that M_NOWAIT is required when running in an interrupt context.
M_WAITOK
Indicates that it is OK to wait for resources. If the request
cannot be immediately fulfilled, the current process is put to
sleep to wait for resources to be released by other processes.
Before the internal pool limit is reached, the kmalloc() and
krealloc(), functions cannot return NULL if M_WAITOK is
specified. If the internal pool limit is reached and M_NULLOK is
not specified along with M_WAITOK, the system will panic. If the
internal pool limit is reached and M_NULLOK is specified along
with M_WAITOK, the kmalloc() and krealloc(), functions return
NULL instead of panicing the system.
M_INTWAIT
Indicates kmalloc() to dig into the system's reserved free pages
looking for enough room to perform the allocation. This is
typically used in interrupts where you cannot afford kmalloc() to
fail. Before the internal pool limit is reached, the kmalloc()
and krealloc(), functions cannot return NULL if M_INTWAIT is
specified. If the internal pool limit is reached and M_NULLOK is
not specified along with M_INTWAIT, the system will panic. If
the internal pool limit is reached and M_NULLOK is specified
along with M_INTWAIT, the kmalloc() and krealloc(), functions
return NULL instead of panicing the system.
M_USE_RESERVE
Indicates that the system can dig into its reserve in order to
obtain the requested memory.
M_POWEROF2
Rounds up the size to the nearest power of 2.
M_CACHEALIGN
Aligns to the CPU cache line size.
M_NULLOK
This flag is usually specified along with M_WAITOK or M_INTWAIT,
so when the interal pool limit is reached, kmalloc() and
krealloc(), functions will not panic the system, instead, NULL
will be returned. This flag is usually used on the kernel code
path that is triggered by user space programs' requests.
Exactly one of either M_WAITOK, M_INTWAIT or M_NOWAIT must be specified.
The type argument is used to perform statistics on memory usage, and for
basic sanity checks. It can be used to identify multiple allocations.
The statistics can be examined by `vmstat -m'.
A type is defined using the malloc_type_t typedef via the
MALLOC_DECLARE() and MALLOC_DEFINE() macros.
IMPLEMENTATION NOTES
The memory allocator allocates memory in chunks that have size a power of
two for requests up to the size of a page of memory. For larger
requests, one or more pages is allocated. The allocated memory will be
at least 8 bytes aligned. While it should not be relied upon, this
information may be useful for optimizing the efficiency of memory use.
RETURN VALUES
The kmalloc() and krealloc(), functions return a kernel virtual address
that is suitably aligned for storage of any type of object, or NULL if
the request could not be satisfied (implying that M_NOWAIT or M_NULLOK
was set).
EXAMPLES
/* sys/something/foo_extern.h */
MALLOC_DECLARE(M_FOOBUF);
/* sys/something/foo_main.c */
MALLOC_DEFINE(M_FOOBUF, "foobuffers", "Foo data");
/* sys/something/foo_subr.c */
...
buf = kmalloc(sizeof *buf, M_FOOBUF, M_NOWAIT);
DIAGNOSTICS
A kernel compiled with the INVARIANTS configuration option attempts to
detect memory corruption caused by such things as writing outside the
allocated area and imbalanced calls to the kmalloc() and kfree()
functions. Failing consistency checks will cause a panic or a system
console message.
SEE ALSO
vmstat(8), contigmalloc(9), kstrdup(9), memory(9), vnode(9)
DragonFly 5.7-DEVELOPMENT July 27, 2019 DragonFly 5.7-DEVELOPMENT