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