MALLOC(9)

NAME
malloc, free - kernel memory allocator

SYNOPSIS
void *
malloc(unsigned long size, int type, int flags);
MALLOC(space, cast, unsigned long size, int type, int flags);
void
free(void *addr, int type);
FREE(void *addr, int type);

DESCRIPTION
The malloc() function allocates uninitialized memory in kernel address space for an object whose size is specified by size. free() releases memory at address addr that was previously allocated by malloc() for re-use. The MALLOC() macro variant is functionally equivalent to

    (space) = (cast)malloc((u_long)(size), type, flags)

and the FREE() macro variant is equivalent to

    free((caddr_t)(addr), type)

Unlike its standard C library counterpart (malloc(3)), the kernel version takes two more arguments. The flags argument further qualifies malloc()'s operational charateristics as follows:

M_NOWAIT
    Causes malloc() to return NULL if the request cannot be immediately fulfilled due to resource shortage. Otherwise, malloc() may call sleep to wait for resources to be released by other processes. If this flag is not set, malloc() will never return NULL. Note that M_WAITOK is conveniently defined to be 0, and hence maybe or'ed into the flags argument to indicate that it's Ok to wait for resources.

Currently, only one flag is defined.

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. The following types are currently defined:

M_FREE
    Should be on free list.
M_MBUF
    Mbuf memory.
M_DEVBUF
    Device driver memory.
M_SOCKET
    Socket structure.
M_PCB
    Protocol control block.
M_RTABLE
    Routing tables.
M_HTABLE
    IMP host tables.
M_FTABLE
    Fragment reassembly header.
M_ZOMBIE
    Zombie proc status
M_IFADDR
    Interface address.
M_SOOPTS
    Socket options.
M_SONAME
    Socket name.
M_NAMEI
    Namei path name buffer.
M_GPROF
    Kernel profiling buffer.
M_IOCTLOPS
    Ioctl data buffer.
M_MAPMEM
    Mapped memory descriptors.
M_CRED
    Credentials.
M_PGRP
    Process group header.
M_SESSION
    Session header.
M_IOV
    Large iov's.
M_MOUNT
    Vfs mount struct.
M_FHANDLE
    Network file handle.
M_NFSREQ
    NFS request header.
M_NFSMNT
    NFS mount structure.
M_NFSNODE
    NFS vnode private part.
M_VNODE
    Dynamically allocated vnodes.
M_CACHE
    Dynamically allocated cache entries.
M_DQUOT
    UFS quota entries.
M_UFSMNT
    UFS mount structure.
M_SHM
    SVID compatible shared memory segments.
M_VMMAP
    VM map structures.
M_VMMAPENT
    VM map entry structures.
M_VMOBJ
    VM object structure.
M_VMOBJHASH
    VM object hash structure.
M_VMPMAP
    VM pmap.
M_VMPVENT
    VM phys-virt mapping entry.
M_VMPAGER
    XXX: VM pager struct.
M_VMPGDATA
    XXX: VM pager private data.
M_FILE
    Open file structure.
M_FILEDESC
    Open file descriptor table.
M_LOCKF
    Byte-range locking structures.
M_PROC
    Proc structures.
M_SUBPROC
    Proc sub-structures.
M_SEGMENT
    Segment for LFS.
M_LFSNODE
    LFS vnode private part.
M_FFSNODE
    FFS vnode private part.
M_MFSNODE
    MFS vnode private part.
M_NQLEASE
    Nqnfs lease.
M_NQMHOST
    Nqnfs host address table.
M_NETADDR
    Export host address structure.
M_NFSSVC
    Nfs server structure.
M_NFSUID
    Nfs uid mapping structure.
M_NFSD
    Nfs server daemon structure.
M_IPMOPTS
    Internet multicast options.
M_IPMADDR
    Internet multicast address.
M_IFMADDR
    Link-level multicast address.
M_MRTABLE
    Multicast routing tables.
M_ISOFSMNT
    ISOFS mount structure.
M_ISOFSNODE
    ISOFS vnode private part.
M_MSDOSFSMNT
    MSDOS FS mount structure.
M_MSDOSFSFAT
    MSDOS FS fat table.
M_MSDOSFSNODE
    MSDOS FS vnode private part.
M_TTYS
    Allocated tty structures.
M_EXEC
    Argument lists & other mem used by exec.
M_MISCFSMNT
    Miscfs mount structures.
M_MISCFSNODE
    Miscfs vnode private part.
M_ADOSFSMNT
    Adosfs mount structures.
M_ADOSFSNODE
    Adosfs vnode private part.
M_ANODE
    Adosfs anode structures and tables.
M_IPQ
    IP packet queue entry.
M_AFS
    Andrew File System.
M_ADOSFSBITMAP
    Adosfs bitmap.
M_NFSSRVDESC
    NFS server descriptor.
M_NFSDIROFF
    NFS directory cookies.
M_NFSBIGFH
    NFS big filehandle.
M_EXT2FSNODE
    EXT2FS vnode private part.
M_VMSWAP
    VM swap structures.
M_VMPAGE
    VM page structures.
M_VMPBUCKET
    VM page buckets.
M_UVMAMAP
    UVM amap and related structs.
M_UVMAOBJ
    UVM aobj and related structs.
M_DMAMAP
    bus_dma(9) structures.
M_IPFLOW
    IP flow entries.
M_USB
    USB general.
M_USBDEV
    USB permanent.
M_POOL
    Memory pool(9) structures.
M_CFS
    Coda file system structures and tables.
M_TEMP
    Misc temporary data buffers.

Statistics based on the type argument is maintained only if the kernel option KMEMSTATS is used when compiling the kernel (the default in current NetBSD kernels) and can be examined by using ‘vmstat -m’.

RETURN VALUES
malloc() returns a kernel virtual address that is suitably aligned for storage of any type of object.

SEE ALSO
vmstat(8)

DIAGNOSTICS
A kernel compiled with the DIAGNOSTIC configuration option attempts to detect 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:

•   panic: “malloc - bogus type”
•   panic: “malloc: out of space in kmem_map”
•   panic: “malloc: allocation too large”
•   panic: “malloc: wrong bucket”
•   panic: “malloc: lost data”
•   panic: “free: unaligned addr”
•   panic: “free: duplicated free”
•   panic: “free: multiple frees”
•   panic: “init: minbucket too small/struct freelist too big”
•   “multiply freed item <addr>”
•   “Data modified on freelist: <data object description>”