1139825Simp/*- 21541Srgrimes * Copyright (c) 1987, 1993 3146747Srwatson * The Regents of the University of California. 4191267Srwatson * Copyright (c) 2005, 2009 Robert N. M. Watson 5146747Srwatson * All rights reserved. 61541Srgrimes * 71541Srgrimes * Redistribution and use in source and binary forms, with or without 81541Srgrimes * modification, are permitted provided that the following conditions 91541Srgrimes * are met: 101541Srgrimes * 1. Redistributions of source code must retain the above copyright 111541Srgrimes * notice, this list of conditions and the following disclaimer. 121541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 131541Srgrimes * notice, this list of conditions and the following disclaimer in the 141541Srgrimes * documentation and/or other materials provided with the distribution. 151541Srgrimes * 4. Neither the name of the University nor the names of its contributors 161541Srgrimes * may be used to endorse or promote products derived from this software 171541Srgrimes * without specific prior written permission. 181541Srgrimes * 191541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 201541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 211541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 221541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 231541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 241541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 251541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 261541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 271541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 281541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 291541Srgrimes * SUCH DAMAGE. 301541Srgrimes * 3114481Shsu * @(#)malloc.h 8.5 (Berkeley) 5/3/95 3250477Speter * $FreeBSD: stable/10/sys/sys/malloc.h 328276 2018-01-23 04:37:31Z kp $ 331541Srgrimes */ 341541Srgrimes 351541Srgrimes#ifndef _SYS_MALLOC_H_ 361541Srgrimes#define _SYS_MALLOC_H_ 371541Srgrimes 38146765Srwatson#include <sys/param.h> 39103349Sbde#include <sys/queue.h> 40103349Sbde#include <sys/_lock.h> 41103349Sbde#include <sys/_mutex.h> 42328276Skp#include <machine/_limits.h> 4392654Sjeff 44103351Sbde#define MINALLOCSIZE UMA_SMALLEST_UNIT 4534266Sjulian 461541Srgrimes/* 4742957Sdillon * flags to malloc. 481541Srgrimes */ 4951168Sbde#define M_NOWAIT 0x0001 /* do not block */ 50119129Ssam#define M_WAITOK 0x0002 /* ok to block */ 51112063Sphk#define M_ZERO 0x0100 /* bzero the allocation */ 52112063Sphk#define M_NOVM 0x0200 /* don't ask VM for pages */ 53112063Sphk#define M_USE_RESERVE 0x0400 /* can alloc out of reserve memory */ 54230623Skmacy#define M_NODUMP 0x0800 /* don't dump pages in this allocation */ 55252330Sjeff#define M_FIRSTFIT 0x1000 /* Only for vmem, fast fit. */ 56252330Sjeff#define M_BESTFIT 0x2000 /* Only for vmem, low fragmentation. */ 571541Srgrimes 5831559Sbde#define M_MAGIC 877983977 /* time when first defined :-) */ 5930817Sphk 60146747Srwatson/* 61146747Srwatson * Two malloc type structures are present: malloc_type, which is used by a 62146747Srwatson * type owner to declare the type, and malloc_type_internal, which holds 63146747Srwatson * malloc-owned statistics and other ABI-sensitive fields, such as the set of 64146747Srwatson * malloc statistics indexed by the compile-time MAXCPU constant. 65146747Srwatson * Applications should avoid introducing dependence on the allocator private 66146747Srwatson * data layout and size. 67146747Srwatson * 68146747Srwatson * The malloc_type ks_next field is protected by malloc_mtx. Other fields in 69146747Srwatson * malloc_type are static after initialization so unsynchronized. 70146747Srwatson * 71146747Srwatson * Statistics in malloc_type_stats are written only when holding a critical 72146747Srwatson * section and running on the CPU associated with the index into the stat 73146747Srwatson * array, but read lock-free resulting in possible (minor) races, which the 74146747Srwatson * monitoring app should take into account. 75146747Srwatson */ 76146747Srwatsonstruct malloc_type_stats { 77147984Srwatson uint64_t mts_memalloced; /* Bytes allocated on CPU. */ 78147984Srwatson uint64_t mts_memfreed; /* Bytes freed on CPU. */ 79147984Srwatson uint64_t mts_numallocs; /* Number of allocates on CPU. */ 80147984Srwatson uint64_t mts_numfrees; /* number of frees on CPU. */ 81147984Srwatson uint64_t mts_size; /* Bitmask of sizes allocated on CPU. */ 82147984Srwatson uint64_t _mts_reserved1; /* Reserved field. */ 83147984Srwatson uint64_t _mts_reserved2; /* Reserved field. */ 84147984Srwatson uint64_t _mts_reserved3; /* Reserved field. */ 85146747Srwatson}; 86146747Srwatson 87179222Sjb/* 88179222Sjb * Index definitions for the mti_probes[] array. 89179222Sjb */ 90179222Sjb#define DTMALLOC_PROBE_MALLOC 0 91179222Sjb#define DTMALLOC_PROBE_FREE 1 92179222Sjb#define DTMALLOC_PROBE_MAX 2 93179222Sjb 94146747Srwatsonstruct malloc_type_internal { 95179222Sjb uint32_t mti_probes[DTMALLOC_PROBE_MAX]; 96179222Sjb /* DTrace probe ID array. */ 97210564Smdf u_char mti_zone; 98146747Srwatson struct malloc_type_stats mti_stats[MAXCPU]; 99146747Srwatson}; 100146747Srwatson 101146747Srwatson/* 102191267Srwatson * Public data structure describing a malloc type. Private data is hung off 103191267Srwatson * of ks_handle to avoid encoding internal malloc(9) data structures in 104191267Srwatson * modules, which will statically allocate struct malloc_type. 105146747Srwatson */ 10630282Sphkstruct malloc_type { 107146747Srwatson struct malloc_type *ks_next; /* Next in global chain. */ 108146747Srwatson u_long ks_magic; /* Detect programmer error. */ 109146747Srwatson const char *ks_shortdesc; /* Printable type name. */ 110146747Srwatson void *ks_handle; /* Priv. data, was lo_class. */ 1111541Srgrimes}; 1121541Srgrimes 113147984Srwatson/* 114147984Srwatson * Statistics structure headers for user space. The kern.malloc sysctl 115147984Srwatson * exposes a structure stream consisting of a stream header, then a series of 116147984Srwatson * malloc type headers and statistics structures (quantity maxcpus). For 117147984Srwatson * convenience, the kernel will provide the current value of maxcpus at the 118147984Srwatson * head of the stream. 119147984Srwatson */ 120147984Srwatson#define MALLOC_TYPE_STREAM_VERSION 0x00000001 121147984Srwatsonstruct malloc_type_stream_header { 122147984Srwatson uint32_t mtsh_version; /* Stream format version. */ 123147984Srwatson uint32_t mtsh_maxcpus; /* Value of MAXCPU for stream. */ 124147984Srwatson uint32_t mtsh_count; /* Number of records. */ 125147984Srwatson uint32_t _mtsh_pad; /* Pad/reserved field. */ 126147984Srwatson}; 127147984Srwatson 128147984Srwatson#define MALLOC_MAX_NAME 32 129147984Srwatsonstruct malloc_type_header { 130147984Srwatson char mth_name[MALLOC_MAX_NAME]; 131147984Srwatson}; 132147984Srwatson 13355205Speter#ifdef _KERNEL 134146747Srwatson#define MALLOC_DEFINE(type, shortdesc, longdesc) \ 135146747Srwatson struct malloc_type type[1] = { \ 136191267Srwatson { NULL, M_MAGIC, shortdesc, NULL } \ 137146747Srwatson }; \ 138146747Srwatson SYSINIT(type##_init, SI_SUB_KMEM, SI_ORDER_SECOND, malloc_init, \ 139146747Srwatson type); \ 140146747Srwatson SYSUNINIT(type##_uninit, SI_SUB_KMEM, SI_ORDER_ANY, \ 141149397Sphk malloc_uninit, type) 14230278Sphk 14330309Sphk#define MALLOC_DECLARE(type) \ 14441054Speter extern struct malloc_type type[1] 14530309Sphk 14651167SbdeMALLOC_DECLARE(M_CACHE); 14751167SbdeMALLOC_DECLARE(M_DEVBUF); 14851167SbdeMALLOC_DECLARE(M_TEMP); 14952904Sshin 15052904SshinMALLOC_DECLARE(M_IP6OPT); /* for INET6 */ 15152904SshinMALLOC_DECLARE(M_IP6NDP); /* for INET6 */ 15230278Sphk 1531541Srgrimes/* 154184210Sdes * Deprecated macro versions of not-quite-malloc() and free(). 155184210Sdes */ 156184210Sdes#define MALLOC(space, cast, size, type, flags) \ 157184210Sdes ((space) = (cast)malloc((u_long)(size), (type), (flags))) 158184210Sdes#define FREE(addr, type) free((addr), (type)) 159184210Sdes 160184210Sdes/* 16131558Sbde * XXX this should be declared in <sys/uio.h>, but that tends to fail 16231558Sbde * because <sys/uio.h> is included in a header before the source file 16331558Sbde * has a chance to include <sys/malloc.h> to get MALLOC_DECLARE() defined. 16431558Sbde */ 16531558SbdeMALLOC_DECLARE(M_IOV); 16631558Sbde 167103351Sbdeextern struct mtx malloc_mtx; 168103351Sbde 169179222Sjb/* 170179222Sjb * Function type used when iterating over the list of malloc types. 171179222Sjb */ 172179222Sjbtypedef void malloc_type_list_func_t(struct malloc_type *, void *); 173179222Sjb 17493008Sbdevoid contigfree(void *addr, unsigned long size, struct malloc_type *type); 17593008Sbdevoid *contigmalloc(unsigned long size, struct malloc_type *type, int flags, 176112569Sjake vm_paddr_t low, vm_paddr_t high, unsigned long alignment, 177232356Sjhb vm_paddr_t boundary) __malloc_like; 17892719Salfredvoid free(void *addr, struct malloc_type *type); 179187961Sdasvoid *malloc(unsigned long size, struct malloc_type *type, int flags) __malloc_like; 180328276Skpvoid *mallocarray(size_t nmemb, size_t size, struct malloc_type *type, 181328276Skp int flags) __malloc_like __result_use_check; 18292719Salfredvoid malloc_init(void *); 183106305Sphkint malloc_last_fail(void); 184132379Sgreenvoid malloc_type_allocated(struct malloc_type *type, unsigned long size); 185132379Sgreenvoid malloc_type_freed(struct malloc_type *type, unsigned long size); 186179222Sjbvoid malloc_type_list(malloc_type_list_func_t *, void *); 18792719Salfredvoid malloc_uninit(void *); 18893008Sbdevoid *realloc(void *addr, unsigned long size, struct malloc_type *type, 18993008Sbde int flags); 19093008Sbdevoid *reallocf(void *addr, unsigned long size, struct malloc_type *type, 19193008Sbde int flags); 192153880Spjd 193153880Spjdstruct malloc_type *malloc_desc2type(const char *desc); 194328276Skp 195328276Skp/* 196328276Skp * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX 197328276Skp * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW 198328276Skp */ 199328276Skp#define MUL_NO_OVERFLOW (1UL << (sizeof(size_t) * 8 / 2)) 200328276Skpstatic inline bool 201328276SkpWOULD_OVERFLOW(size_t nmemb, size_t size) 202328276Skp{ 203328276Skp 204328276Skp return ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && 205328276Skp nmemb > 0 && __SIZE_T_MAX / nmemb < size); 206328276Skp} 207328276Skp#undef MUL_NO_OVERFLOW 20855205Speter#endif /* _KERNEL */ 20913642Sbde 2101541Srgrimes#endif /* !_SYS_MALLOC_H_ */ 211