MALLOC(9)                    OpenBSD Kernel Manual                   MALLOC(9)

NAME
     malloc - kernel memory allocator

SYNOPSIS
     #include <sys/types.h>
     #include <sys/malloc.h>

     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 mem-
     ory 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 ver-
     sion takes two more arguments.  The flags argument further qualifies
     malloc()'s operational characteristics as follows:

           M_NOWAIT
                Causes malloc() to return NULL if the request cannot be imme-
                diately 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 in-
                dicate 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_EXT2FSNODE    EXT2FS vnode private part.
           M_PFIL          Packer filter.
           M_PFKEY         Pfkey data.
           M_TDB           Transforms database.
           M_XDATA         IPsec data.


           M_VFS           VFS file systems.
           M_PAGEDEP       File page dependencies.
           M_INODEDEP      Inode dependencies.
           M_NEWBLK        New block allocation.
           M_BMSAFEMAP     Block or frag allocated from cyl group map.
           M_ALLOCDIRECT   Block or frag dependency for an inode.
           M_INDIRDEP      Indirect block dependencies.
           M_ALLOCINDIR    Block dependency for an indirect block.
           M_FREEFRAG      Previously used frag for an inode.
           M_FREEBLKS      Blocks freed from an inode.
           M_FREEFILE      Inode deallocated.
           M_DIRADD        New directory entry.
           M_MKDIR         New directory.
           M_DIRREM        Directory entry deleted.
           M_VMPBUCKET     VM page buckets.
           M_VMSWAP        VM swap structures.
           M_DISCQ         IPv6 discq.
           M_FRAGQ         IPv6 fragq.
           M_SECA          Sec Assoc.
           M_I6IFP         IPv6 if info.
           M_RAIDFRAME     Raidframe data.
           M_UVMAMAP       UVM amap and realted.
           M_UVMAOBJ       UVM aobj and realted.
           M_POOL          Pool memory.
           M_USB           USB general.
           M_USBDEV        USB device driver.
           M_USBHC         USB host controller.
           M_TEMP          Misc temporary data buffers.

     Statistics based on the type argument are maintained only if the kernel
     option KMEMSTATS is used when compiling the kernel (the default in
     current OpenBSD 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 memory corruption caused by such things as writing outside the al-
     located area and imbalanced calls to the malloc() and free() functions.
     Failing consistency checks will cause a panic or a system console mes-
     sage:

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

OpenBSD 2.6                      June 16, 1996                               3
