2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1987, 1993
5 * The Regents of the University of California.
6 * Copyright (c) 2005, 2009 Robert N. M. Watson
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)malloc.h 8.5 (Berkeley) 5/3/95
37 #ifndef _SYS_MALLOC_H_
38 #define _SYS_MALLOC_H_
40 #include <sys/param.h>
42 #include <sys/systm.h>
44 #include <sys/queue.h>
45 #include <sys/_lock.h>
46 #include <sys/_mutex.h>
47 #include <machine/_limits.h>
49 #define MINALLOCSIZE UMA_SMALLEST_UNIT
54 #define M_NOWAIT 0x0001 /* do not block */
55 #define M_WAITOK 0x0002 /* ok to block */
56 #define M_ZERO 0x0100 /* bzero the allocation */
57 #define M_NOVM 0x0200 /* don't ask VM for pages */
58 #define M_USE_RESERVE 0x0400 /* can alloc out of reserve memory */
59 #define M_NODUMP 0x0800 /* don't dump pages in this allocation */
60 #define M_FIRSTFIT 0x1000 /* Only for vmem, fast fit. */
61 #define M_BESTFIT 0x2000 /* Only for vmem, low fragmentation. */
63 #define M_MAGIC 877983977 /* time when first defined :-) */
66 #define M_ZERO_INVARIANTS M_ZERO
68 #define M_ZERO_INVARIANTS 0
73 * Two malloc type structures are present: malloc_type, which is used by a
74 * type owner to declare the type, and malloc_type_internal, which holds
75 * malloc-owned statistics and other ABI-sensitive fields, such as the set of
76 * malloc statistics indexed by the compile-time MAXCPU constant.
77 * Applications should avoid introducing dependence on the allocator private
78 * data layout and size.
80 * The malloc_type ks_next field is protected by malloc_mtx. Other fields in
81 * malloc_type are static after initialization so unsynchronized.
83 * Statistics in malloc_type_stats are written only when holding a critical
84 * section and running on the CPU associated with the index into the stat
85 * array, but read lock-free resulting in possible (minor) races, which the
86 * monitoring app should take into account.
88 struct malloc_type_stats {
89 uint64_t mts_memalloced; /* Bytes allocated on CPU. */
90 uint64_t mts_memfreed; /* Bytes freed on CPU. */
91 uint64_t mts_numallocs; /* Number of allocates on CPU. */
92 uint64_t mts_numfrees; /* number of frees on CPU. */
93 uint64_t mts_size; /* Bitmask of sizes allocated on CPU. */
94 uint64_t _mts_reserved1; /* Reserved field. */
95 uint64_t _mts_reserved2; /* Reserved field. */
96 uint64_t _mts_reserved3; /* Reserved field. */
100 * Index definitions for the mti_probes[] array.
102 #define DTMALLOC_PROBE_MALLOC 0
103 #define DTMALLOC_PROBE_FREE 1
104 #define DTMALLOC_PROBE_MAX 2
106 struct malloc_type_internal {
107 uint32_t mti_probes[DTMALLOC_PROBE_MAX];
108 /* DTrace probe ID array. */
110 struct malloc_type_stats mti_stats[MAXCPU];
114 * Public data structure describing a malloc type. Private data is hung off
115 * of ks_handle to avoid encoding internal malloc(9) data structures in
116 * modules, which will statically allocate struct malloc_type.
119 struct malloc_type *ks_next; /* Next in global chain. */
120 u_long ks_magic; /* Detect programmer error. */
121 const char *ks_shortdesc; /* Printable type name. */
122 void *ks_handle; /* Priv. data, was lo_class. */
126 * Statistics structure headers for user space. The kern.malloc sysctl
127 * exposes a structure stream consisting of a stream header, then a series of
128 * malloc type headers and statistics structures (quantity maxcpus). For
129 * convenience, the kernel will provide the current value of maxcpus at the
130 * head of the stream.
132 #define MALLOC_TYPE_STREAM_VERSION 0x00000001
133 struct malloc_type_stream_header {
134 uint32_t mtsh_version; /* Stream format version. */
135 uint32_t mtsh_maxcpus; /* Value of MAXCPU for stream. */
136 uint32_t mtsh_count; /* Number of records. */
137 uint32_t _mtsh_pad; /* Pad/reserved field. */
140 #define MALLOC_MAX_NAME 32
141 struct malloc_type_header {
142 char mth_name[MALLOC_MAX_NAME];
146 #define MALLOC_DEFINE(type, shortdesc, longdesc) \
147 struct malloc_type type[1] = { \
148 { NULL, M_MAGIC, shortdesc, NULL } \
150 SYSINIT(type##_init, SI_SUB_KMEM, SI_ORDER_THIRD, malloc_init, \
152 SYSUNINIT(type##_uninit, SI_SUB_KMEM, SI_ORDER_ANY, \
155 #define MALLOC_DECLARE(type) \
156 extern struct malloc_type type[1]
158 MALLOC_DECLARE(M_CACHE);
159 MALLOC_DECLARE(M_DEVBUF);
160 MALLOC_DECLARE(M_TEMP);
163 * XXX this should be declared in <sys/uio.h>, but that tends to fail
164 * because <sys/uio.h> is included in a header before the source file
165 * has a chance to include <sys/malloc.h> to get MALLOC_DECLARE() defined.
167 MALLOC_DECLARE(M_IOV);
169 extern struct mtx malloc_mtx;
172 * Function type used when iterating over the list of malloc types.
174 typedef void malloc_type_list_func_t(struct malloc_type *, void *);
176 void contigfree(void *addr, unsigned long size, struct malloc_type *type);
177 void *contigmalloc(unsigned long size, struct malloc_type *type, int flags,
178 vm_paddr_t low, vm_paddr_t high, unsigned long alignment,
179 vm_paddr_t boundary) __malloc_like __result_use_check
180 __alloc_size(1) __alloc_align(6);
181 void *contigmalloc_domain(unsigned long size, struct malloc_type *type,
182 int domain, int flags, vm_paddr_t low, vm_paddr_t high,
183 unsigned long alignment, vm_paddr_t boundary)
184 __malloc_like __result_use_check __alloc_size(1) __alloc_align(6);
185 void free(void *addr, struct malloc_type *type);
186 void free_domain(void *addr, struct malloc_type *type);
187 void *malloc(size_t size, struct malloc_type *type, int flags) __malloc_like
188 __result_use_check __alloc_size(1);
190 #define malloc(size, type, flags) ({ \
191 void *_malloc_item; \
192 size_t _size = (size); \
193 if (__builtin_constant_p(size) && __builtin_constant_p(flags) &&\
194 ((flags) & M_ZERO)) { \
195 _malloc_item = malloc(_size, type, (flags) &~ M_ZERO); \
196 if (((flags) & M_WAITOK) || _malloc_item != NULL) \
197 bzero(_malloc_item, _size); \
199 _malloc_item = malloc(_size, type, flags); \
205 void *malloc_domain(size_t size, struct malloc_type *type, int domain,
206 int flags) __malloc_like __result_use_check __alloc_size(1);
207 void *mallocarray(size_t nmemb, size_t size, struct malloc_type *type,
208 int flags) __malloc_like __result_use_check
210 void malloc_init(void *);
211 int malloc_last_fail(void);
212 void malloc_type_allocated(struct malloc_type *type, unsigned long size);
213 void malloc_type_freed(struct malloc_type *type, unsigned long size);
214 void malloc_type_list(malloc_type_list_func_t *, void *);
215 void malloc_uninit(void *);
216 void *realloc(void *addr, size_t size, struct malloc_type *type, int flags)
217 __result_use_check __alloc_size(2);
218 void *reallocf(void *addr, size_t size, struct malloc_type *type, int flags)
219 __result_use_check __alloc_size(2);
221 struct malloc_type *malloc_desc2type(const char *desc);
224 * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
225 * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
227 #define MUL_NO_OVERFLOW (1UL << (sizeof(size_t) * 8 / 2))
229 WOULD_OVERFLOW(size_t nmemb, size_t size)
232 return ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
233 nmemb > 0 && __SIZE_T_MAX / nmemb < size);
235 #undef MUL_NO_OVERFLOW
238 #endif /* !_SYS_MALLOC_H_ */