MALLOC(9)                    NetBSD Kernel Manual                    MALLOC(9)

NNAAMMEE
     mmaalllloocc, ffrreeee - kernel memory allocator

SSYYNNOOPPSSIISS
     _v_o_i_d _*
     mmaalllloocc(_u_n_s_i_g_n_e_d _l_o_n_g _s_i_z_e, _i_n_t _t_y_p_e, _i_n_t _f_l_a_g_s);

     MMAALLLLOOCC(_s_p_a_c_e, _c_a_s_t, _u_n_s_i_g_n_e_d _l_o_n_g _s_i_z_e, _i_n_t _t_y_p_e, _i_n_t _f_l_a_g_s);

     _v_o_i_d
     ffrreeee(_v_o_i_d _*_a_d_d_r, _i_n_t _t_y_p_e);

     FFRREEEE(_v_o_i_d _*_a_d_d_r, _i_n_t _t_y_p_e);

DDEESSCCRRIIPPTTIIOONN
     The mmaalllloocc() function allocates uninitialized memory in kernel address
     space for an object whose size is specified by _s_i_z_e. ffrreeee() releases mem-
     ory at address _a_d_d_r that was previously allocated by mmaalllloocc() for re-use.
     The MMAALLLLOOCC() macro variant is functionally equivalent to

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

     and the FFRREEEE() 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 _f_l_a_g_s argument further qualifies
     mmaalllloocc()'s operational charateristics as follows:

           M_NOWAIT
             Causes mmaalllloocc() to return NULL if the request cannot be immedi-
             ately fulfilled due to resource shortage.  Otherwise, mmaalllloocc()
             may call sleep to wait for resources to be released by other pro-
             cesses.  If this flag is not set, mmaalllloocc() will never return
             NULL. Note that M_WAITOK is conveniently defined to be 0, and
             hence maybe or'ed into the _f_l_a_g_s argument to indicate that it's
             Ok to wait for resources.

     Currently, only one flag is defined.

     The _t_y_p_e 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_DIROFF        NFS directory cookies.
           M_NFSBIGFH      NFS big filehandle.
           M_EXT2FSNODE    EXT2FS vnode private part.
           M_VMSWAP        VM swap structures.
           M_TEMP          Misc temporary data buffers.

     Statistics based on the _t_y_p_e argument is maintained only if the kernel
     option KMEMSTATS is used when compiling the kernel (the default in
     currentNetBSD kernels) and can be examined by using `vmstat -m'.

RREETTUURRNN VVAALLUUEESS
     mmaalllloocc() returns a kernel virtual address that is suitably aligned for
     storage of any type of object.

SSEEEE AALLSSOO
     vmstat(8)

DDIIAAGGNNOOSSTTIICCSS
     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 mmaalllloocc() and ffrreeee() func-
     tions. Failing consistency checks will cause a panic or a system console
     message:

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

NetBSD                           Jun 16, 1996                                3
