table of contents
|MALLOC(9)||Kernel Developer's Manual||MALLOC(9)|
kernel memory management routines
#include <sys/malloc.h>void *
malloc(unsigned long size, struct malloc_type *type, int flags); void
free(void *addr, struct malloc_type *type); void *
realloc(void *addr, unsigned long size, struct malloc_type *type, int flags); void *
reallocf(void *addr, unsigned long size, struct malloc_type *type, int flags);
MALLOC_DEFINE(type, shortdesc, longdesc);
malloc() function allocates uninitialized memory in kernel address space for an object whose size is specified by size. The
free() function releases memory at address addr that was previously allocated by
malloc() for re-use. The memory is not zeroed. If addr is
free() does nothing. The
realloc() 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,
NULLis returned and the memory referenced by addr is valid and unchanged. If addr is
realloc() function behaves identically to
malloc() for the specified size. The
reallocf() function is identical to
realloc() except that it will free the passed pointer when the requested memory cannot be allocated. Unlike its standard C library counterpart (malloc(3)), the kernel version takes two more arguments. The flags argument further qualifies
malloc()'s operational characteristics as follows:
- Causes the allocated memory to be set to all zeros.
- For allocations greater than page size, causes the allocated memory to be excluded from kernel core dumps.
reallocf() to return
NULLif the request cannot be immediately fulfilled due to resource shortage. Note that
M_NOWAITis required when running in an interrupt context.
- 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. The
reallocf() functions cannot return
- Indicates that the system can use its reserve of memory to satisfy the
request. This option should only be used in combination with
M_NOWAITwhen an allocation failure cannot be tolerated by the caller without catastrophic effects on the system.
M_NOWAITmust 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 struct malloc_type via the
/* sys/something/foo_extern.h */ MALLOC_DECLARE(M_FOOBUF); /* sys/something/foo_main.c */ MALLOC_DEFINE(M_FOOBUF, "foobuffers", "Buffers to foo data into the ether"); /* sys/something/foo_subr.c */ ... buf = malloc(sizeof(*buf), M_FOOBUF, M_NOWAIT);
MALLOC_DEFINE(), one must include
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. While it should not be relied upon, this information may be useful for optimizing the efficiency of memory use. Programmers should be careful not to confuse the malloc flags
M_WAITOKwith the mbuf(9) flags
reallocf() may not be called from fast interrupts handlers. When called from threaded interrupts, flags must contain
reallocf() may sleep when called with
free() never sleeps. Any calls to
malloc() (even with
free() when holding a vnode(9) interlock, will cause a LOR (Lock Order Reversal) due to the intertwining of VM Objects and Vnodes.
reallocf() functions return a kernel virtual address that is suitably aligned for storage of any type of object, or
NULLif the request could not be satisfied (implying that
DIAGNOSTICS¶A kernel compiled with the
INVARIANTSconfiguration option attempts to detect memory corruption caused by such things as writing outside the allocated area and imbalanced calls to the
free() functions. Failing consistency checks will cause a panic or a system console message.
SEE ALSO¶vmstat(8), contigmalloc(9), memguard(9), vnode(9)
|November 15, 2012||Debian|