Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

contigmalloc(9) [freebsd man page]

CONTIGMALLOC(9) 					   BSD Kernel Developer's Manual					   CONTIGMALLOC(9)

NAME
contigmalloc, contigfree -- manage contiguous kernel physical memory SYNOPSIS
#include <sys/types.h> #include <sys/malloc.h> void * contigmalloc(unsigned long size, struct malloc_type *type, int flags, vm_paddr_t low, vm_paddr_t high, unsigned long alignment, vm_paddr_t boundary); void contigfree(void *addr, unsigned long size, struct malloc_type *type); DESCRIPTION
The contigmalloc() function allocates size bytes of contiguous physical memory that is aligned to alignment bytes, and which does not cross a boundary of boundary bytes. If successful, the allocation will reside between physical addresses low and high. The returned pointer points to a wired kernel virtual address range of size bytes allocated from the kernel virtual address (KVA) map. The flags parameter modifies contigmalloc()'s behaviour as follows: M_ZERO Causes the allocated physical memory to be zero filled. M_NOWAIT Causes contigmalloc() to return NULL if the request cannot be immediately fulfilled due to resource shortage. Other flags (if present) are ignored. The contigfree() function deallocates memory allocated by a previous call to contigmalloc(). IMPLEMENTATION NOTES
The contigmalloc() function does not sleep waiting for memory resources to be freed up, but instead actively reclaims pages before giving up. However, unless M_NOWAIT is specified, it may select a page for reclamation that must first be written to backing storage, causing it to sleep. The contigfree() function does not accept NULL as an address input, unlike free(9). RETURN VALUES
The contigmalloc() function returns a kernel virtual address if allocation succeeds, or NULL otherwise. EXAMPLES
void *p; p = contigmalloc(8192, M_DEVBUF, M_ZERO, 0, (1L << 22), 32 * 1024, 1024 * 1024); Ask for 8192 bytes of zero-filled memory residing between physical address 0 and 4194303 inclusive, aligned to a 32K boundary and not cross- ing a 1M address boundary. DIAGNOSTICS
The contigmalloc() function will panic if size is zero, or if alignment or boundary is not a power of two. SEE ALSO
malloc(9), memguard(9) BSD
January 29, 2015 BSD

Check Out this Related Man Page

MALLOC(9)						   BSD Kernel Developer's Manual						 MALLOC(9)

NAME
malloc, free, realloc, reallocf, MALLOC_DEFINE, MALLOC_DECLARE -- kernel memory management routines SYNOPSIS
#include <sys/types.h> #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_DECLARE(type); #include <sys/param.h> #include <sys/malloc.h> #include <sys/kernel.h> MALLOC_DEFINE(type, shortdesc, longdesc); DESCRIPTION
The 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 NULL, then 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, NULL is returned and the memory referenced by addr is valid and unchanged. If addr is NULL, the realloc() function behaves iden- tically 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: M_ZERO Causes the allocated memory to be set to all zeros. M_NODUMP For allocations greater than page size, causes the allocated memory to be excluded from kernel core dumps. 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 use its reserve of memory to satisfy the request. This option should only be used in combination with M_NOWAIT when an allocation failure cannot be tolerated by the caller without catastrophic effects on the system. 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 alloca- tions. The statistics can be examined by 'vmstat -m'. A type is defined using 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 */ ... buf = malloc(sizeof(*buf), M_FOOBUF, M_NOWAIT); In order to use MALLOC_DEFINE(), one must include <sys/param.h> (instead of <sys/types.h>) and <sys/kernel.h>. CONTEXT
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. 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. RETURN VALUES
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). 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 malloc() and free() functions. Failing consistency checks will cause a panic or a system console message. SEE ALSO
vmstat(8), contigmalloc(9), memguard(9), vnode(9) BSD
January 16, 2014 BSD
Man Page