malloc(9) - NetBSD Manual Pages

Command: Section: Arch: Collection:  
MALLOC(9)                    NetBSD Kernel Manual                    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); unsigned long malloc_roundup(unsigned long size); 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. malloc_roundup() returns the actual size of the allocation unit for the given value. free() releases memory at address addr that was previously allocated by malloc() for re-use. The MALLOC() macro variant is functionally equiva- lent 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() operational characteristics as follows: M_NOWAIT Causes malloc() to return NULL if the request cannot be immediately fulfilled due to resource shortage. If this flag is not set (see M_WAITOK), malloc() will never re- turn NULL. M_WAITOK By default, malloc() may call sleep(9) to wait for re- sources to be released by other processes, and this flag represents this behaviour. Note that M_WAITOK is conve- niently defined to be 0, and hence may be or'ed into the flags argument to indicate that it's ok to wait for re- sources. M_ZERO Causes the allocated memory to be set to all zeros. M_CANFAIL Changes behaviour for M_WAITOK case - if the requested memory size is bigger than malloc() can ever allocate, return failure, rather than calling panic(9). This is different to M_NOWAIT, since the call can still wait for resources. Rather than depending on M_CANFAIL, kernel code should do proper bound checking itself. This flag should only be used in cases where this is not feasible. Since it can hide real kernel bugs, it's usage is strongly discouraged. 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_TEMP Misc temporary data buffers. 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_CODA Coda file system structures and tables. M_FILECOREMNT Filecore FS mount structures. M_FILECORENODE Filecore FS vnode private part. M_RAIDFRAME RAIDframe structures and IO buffers. M_USBHC USB host controller. M_SECA security associations, key management. M_IP6OPT IPv6 options. M_IP6NDP IPv6 Neighbour Discovery. M_NTFS Windows NT file system structures. 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_IP6RR IPv6 Router Renumbering Prefix. M_RR_ADDR IPv6 Router Renumbering Ifid. M_SOFTINTR Softinterrupt structures. M_EMULDATA Per-process emulation data. M_1394CTL IEEE 1394 control structures. M_1394DATA IEEE 1394 data buffers. M_PIPE Pipe structures. M_AGP AGP memory. M_PROP Kernel properties structures. M_NEWDIRBLK Unclaimed new directory block (softdeps). M_SMBIOD SMB network id daemon. M_SMBCONN SMB connection id. M_SMBRQ SMB request. M_SMBDATA Miscellaneous SMB data. M_SMBSTR SMB string data. M_SMBTEMP Temporary SMB data. M_ICONV ICONV data. M_SMBNODE SMBFS node. M_SMBNODENAME SMBFS node name. M_SMBFSDATA SMBFS data. M_SMBFSHASH SMBFS hash table. M_SA Scheduler activations data structures 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.
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: + 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>''
SEE ALSO
vmstat(1) NetBSD 1.6 December 4, 2001 4
Powered by man-cgi (2024-03-20). Maintained for NetBSD by Kimmo Suominen. Based on man-cgi by Panagiotis Christias.