|
| ||||||||||||||||||||||||
| M_ZERO | |
| Causes the allocated memory to be set to all zeros. | |
| M_NOWAIT | |
| Causes malloc, realloc, and reallocf 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. The malloc, realloc, and reallocf functions cannot return NULL if M_WAITOK is specified. | |
| M_USE_RESERVE | |
| Indicates that the system can dig into its reserve in order to obtain the requested memory. This option used to be called M_KERNEL but has been renamed to something more obvious. This option has been deprecated and is slowly being removed from the kernel, and so should not be used with any new programming. | |
Exactly one of either M_WAITOK 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
.Vt struct malloc_type
via the
MALLOC_DECLARE
and
MALLOC_DEFINE
macros.
/* 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 */
... MALLOC(buf, struct foo_buf *, sizeof *buf, M_FOOBUF, M_NOWAIT);
In order to use
MALLOC_DEFINE,
one must include
.In sys/param.h
(instead of
.In sys/types.h )
and
.In sys/kernel.h .
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_NOWAIT and M_WAITOK with the mbuf(9) flags M_DONTWAIT and M_TRYWAIT.
malloc, realloc and reallocf may not be called from fast interrupts handlers. When called from threaded interrupts, flags must contain M_NOWAIT.malloc, realloc and reallocf may sleep when called with M_WAITOK. free never sleeps.
Any calls to malloc (even with M_NOWAIT) or free when holding a vnode(9) interlock, will cause a LOR (Lock Order Reversal) due to the intertwining of VM Objects and Vnodes.
The malloc, realloc, and reallocf 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 was set).
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 malloc and free functions. Failing consistency checks will cause a panic or a system console message.
vmstat(8), contigmalloc(9), memguard(9), vnode(9)
| June 12, 2003 | MALLOC (9) |
Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.