1 /*	$OpenBSD: malloc.h,v 1.79 2005/07/03 20:14:00 drahn Exp $	*/
2 /*	$NetBSD: malloc.h,v 1.39 1998/07/12 19:52:01 augustss Exp $	*/
3 
4 /*
5  * Copyright (c) 1987, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)malloc.h	8.5 (Berkeley) 5/3/95
33  */
34 
35 #ifndef _SYS_MALLOC_H_
36 #define	_SYS_MALLOC_H_
37 
38 #define KERN_MALLOC_BUCKETS	1
39 #define KERN_MALLOC_BUCKET	2
40 #define KERN_MALLOC_KMEMNAMES	3
41 #define KERN_MALLOC_KMEMSTATS	4
42 #define KERN_MALLOC_MAXID	5
43 
44 #define CTL_KERN_MALLOC_NAMES { \
45 	{ 0, 0 }, \
46 	{ "buckets", CTLTYPE_STRING }, \
47 	{ "bucket", CTLTYPE_NODE }, \
48 	{ "kmemnames", CTLTYPE_STRING }, \
49 	{ "kmemstat", CTLTYPE_NODE }, \
50 }
51 
52 /*
53  * flags to malloc
54  */
55 #define	M_WAITOK	0x0000
56 #define	M_NOWAIT	0x0001
57 #define M_CANFAIL	0x0002
58 #define	M_ZERO		0x0004
59 
60 /*
61  * Types of memory to be allocated
62  */
63 #define	M_FREE		0	/* should be on free list */
64 #define	M_MBUF		1	/* mbuf */
65 #define	M_DEVBUF	2	/* device driver memory */
66 #define M_DEBUG		3	/* debug chunk */
67 #define	M_PCB		4	/* protocol control block */
68 #define	M_RTABLE	5	/* routing tables */
69 /* 6 - free */
70 #define	M_FTABLE	7	/* fragment reassembly header */
71 /* 8 - free */
72 #define	M_IFADDR	9	/* interface address */
73 #define	M_SOOPTS	10	/* socket options */
74 #define	M_SYSCTL	11	/* sysctl buffers (persistent storage) */
75 /* 12 - free */
76 /* 13 - free */
77 #define	M_IOCTLOPS	14	/* ioctl data buffer */
78 /* 15-18 - free */
79 #define	M_IOV		19	/* large iov's */
80 #define	M_MOUNT		20	/* vfs mount struct */
81 /* 21 - free */
82 #define	M_NFSREQ	22	/* NFS request header */
83 #define	M_NFSMNT	23	/* NFS mount structure */
84 #define	M_NFSNODE	24	/* NFS vnode private part */
85 #define	M_VNODE		25	/* Dynamically allocated vnodes */
86 #define	M_CACHE		26	/* Dynamically allocated cache entries */
87 #define	M_DQUOT		27	/* UFS quota entries */
88 #define	M_UFSMNT	28	/* UFS mount structure */
89 #define	M_SHM		29	/* SVID compatible shared memory segments */
90 #define	M_VMMAP		30	/* VM map structures */
91 #define	M_SEM		31	/* SVID compatible semaphores */
92 #define	M_DIRHASH	32	/* UFS dirhash */
93 /* 33 - free */
94 #define	M_VMPMAP	34	/* VM pmap */
95 /* 35-37 - free */
96 #define	M_FILE		38	/* Open file structure */
97 #define	M_FILEDESC	39	/* Open file descriptor table */
98 /* 40 - free */
99 #define	M_PROC		41	/* Proc structures */
100 #define	M_SUBPROC	42	/* Proc sub-structures */
101 #define	M_VCLUSTER	43	/* Cluster for VFS */
102 /* 45-46 - free */
103 #define	M_MFSNODE	46	/* MFS vnode private part */
104 /* 47-48 - free */
105 #define	M_NETADDR	49	/* Export host address structure */
106 #define	M_NFSSVC	50	/* Nfs server structure */
107 #define	M_NFSUID	51	/* Nfs uid mapping structure */
108 #define	M_NFSD		52	/* Nfs server daemon structure */
109 #define	M_IPMOPTS	53	/* internet multicast options */
110 #define	M_IPMADDR	54	/* internet multicast address */
111 #define	M_IFMADDR	55	/* link-level multicast address */
112 #define	M_MRTABLE	56	/* multicast routing tables */
113 #define	M_ISOFSMNT	57	/* ISOFS mount structure */
114 #define	M_ISOFSNODE	58	/* ISOFS vnode private part */
115 #define	M_MSDOSFSMNT	59	/* MSDOS FS mount structure */
116 #define	M_MSDOSFSFAT	60	/* MSDOS FS fat table */
117 #define	M_MSDOSFSNODE	61	/* MSDOS FS vnode private part */
118 #define	M_TTYS		62	/* allocated tty structures */
119 #define	M_EXEC		63	/* argument lists & other mem used by exec */
120 #define	M_MISCFSMNT	64	/* miscfs mount structures */
121 /* 65 - free */
122 #define	M_ADOSFSMNT	66	/* adosfs mount structures */
123 /* 67 - free */
124 #define	M_ANODE		68	/* adosfs anode structures and tables. */
125 /* 69-70 - free */
126 #define	M_ADOSFSBITMAP	71	/* adosfs bitmap */
127 #define	M_EXT2FSNODE	72	/* EXT2FS vnode private part */
128 /* 73 - free */
129 #define	M_PFKEY		74	/* pfkey data */
130 #define	M_TDB		75	/* Transforms database */
131 #define	M_XDATA		76	/* IPsec data */
132 /* 77 - free */
133 #define	M_PAGEDEP	78	/* File page dependencies */
134 #define	M_INODEDEP	79	/* Inode dependencies */
135 #define	M_NEWBLK	80	/* New block allocation */
136 /* 81-82 - free */
137 #define	M_INDIRDEP	83	/* Indirect block dependencies */
138 /* 84-91 - free */
139 #define M_VMSWAP	92	/* VM swap structures */
140 /* 93-96 - free */
141 #define	M_RAIDFRAME	97	/* RAIDframe data */
142 #define M_UVMAMAP	98	/* UVM amap and related */
143 #define M_UVMAOBJ	99	/* UVM aobj and related */
144 /* 100 - free */
145 #define	M_USB		101	/* USB general */
146 #define	M_USBDEV	102	/* USB device driver */
147 #define	M_USBHC		103	/* USB host controller */
148 /* 104 - free */
149 #define M_MEMDESC	105	/* Memory range */
150 /* 106-107 - free */
151 #define M_CRYPTO_DATA	108	/* Crypto framework data buffers (keys etc.) */
152 /* 109 - free */
153 #define M_CREDENTIALS	110	/* IPsec-related credentials and ID info */
154 #define M_PACKET_TAGS	111	/* Packet-attached information */
155 #define M_1394CTL	112	/* IEEE 1394 control structures */
156 #define M_1394DATA	113	/* IEEE 1394 data buffers */
157 #define	M_EMULDATA	114	/* Per-process emulation data */
158 /* 115-122 - free */
159 
160 /* KAME IPv6 */
161 #define	M_IP6OPT	123	/* IPv6 options */
162 #define	M_IP6NDP	124	/* IPv6 Neighbour Discovery */
163 #define	M_IP6RR		125	/* IPv6 Router Renumbering Prefix */
164 #define	M_RR_ADDR	126	/* IPv6 Router Renumbering Ifid */
165 #define	M_TEMP		127	/* misc temporary data buffers */
166 
167 #define	M_NTFSMNT	128	/* NTFS mount structure */
168 #define	M_NTFSNTNODE	129	/* NTFS ntnode information */
169 #define	M_NTFSFNODE	130	/* NTFS fnode information */
170 #define	M_NTFSDIR	131	/* NTFS dir buffer */
171 #define	M_NTFSNTHASH	132	/* NTFS ntnode hash tables */
172 #define	M_NTFSNTVATTR	133	/* NTFS file attribute information */
173 #define	M_NTFSRDATA	134	/* NTFS resident data */
174 #define	M_NTFSDECOMP	135	/* NTFS decompression temporary */
175 #define	M_NTFSRUN	136	/* NTFS vrun storage */
176 
177 #define	M_KEVENT	137	/* kqueue related */
178 
179 #define	M_BLUETOOTH	138	/* Bluetooth */
180 
181 #define M_BWMETER	139	/* Multicast upcall bw meters */
182 
183 #define M_UDFMOUNT	140	/* UDF mount */
184 #define M_UDFFENTRY	141	/* UDF file entry */
185 #define M_UDFFID	142	/* UDF file id */
186 
187 #define	M_LAST		143	/* Must be last type + 1 */
188 
189 #define	INITKMEMNAMES { \
190 	"free",		/* 0 M_FREE */ \
191 	"mbuf",		/* 1 M_MBUF */ \
192 	"devbuf",	/* 2 M_DEVBUF */ \
193 	"debug", 	/* 3 M_DEBUG */ \
194 	"pcb",		/* 4 M_PCB */ \
195 	"routetbl",	/* 5 M_RTABLE */ \
196 	NULL,		/* 6 */ \
197 	"fragtbl",	/* 7 M_FTABLE */ \
198 	NULL, \
199 	"ifaddr",	/* 9 M_IFADDR */ \
200 	"soopts",	/* 10 M_SOOPTS */ \
201 	"sysctl",	/* 11 M_SYSCTL */ \
202 	NULL, \
203 	NULL, \
204 	"ioctlops",	/* 14 M_IOCTLOPS */ \
205 	NULL, \
206 	NULL, \
207 	NULL, \
208 	NULL, \
209 	"iov",		/* 19 M_IOV */ \
210 	"mount",	/* 20 M_MOUNT */ \
211 	NULL, \
212 	"NFS req",	/* 22 M_NFSREQ */ \
213 	"NFS mount",	/* 23 M_NFSMNT */ \
214 	"NFS node",	/* 24 M_NFSNODE */ \
215 	"vnodes",	/* 25 M_VNODE */ \
216 	"namecache",	/* 26 M_CACHE */ \
217 	"UFS quota",	/* 27 M_DQUOT */ \
218 	"UFS mount",	/* 28 M_UFSMNT */ \
219 	"shm",		/* 29 M_SHM */ \
220 	"VM map",	/* 30 M_VMMAP */ \
221 	"sem",		/* 31 M_SEM */ \
222 	"dirhash",	/* 32 M_DIRHASH */ \
223 	NULL, \
224 	"VM pmap",	/* 34 M_VMPMAP */ \
225 	NULL,	/* 35 */ \
226 	NULL,	/* 36 */ \
227 	NULL,	/* 37 */ \
228 	"file",		/* 38 M_FILE */ \
229 	"file desc",	/* 39 M_FILEDESC */ \
230 	NULL,	/* 40 */ \
231 	"proc",		/* 41 M_PROC */ \
232 	"subproc",	/* 42 M_SUBPROC */ \
233 	"VFS cluster",	/* 43 M_VCLUSTER */ \
234 	NULL, \
235 	NULL, \
236 	"MFS node",	/* 46 M_MFSNODE */ \
237 	NULL, \
238 	NULL, \
239 	"Export Host",	/* 49 M_NETADDR */ \
240 	"NFS srvsock",	/* 50 M_NFSSVC */ \
241 	"NFS uid",	/* 51 M_NFSUID */ \
242 	"NFS daemon",	/* 52 M_NFSD */ \
243 	"ip_moptions",	/* 53 M_IPMOPTS */ \
244 	"in_multi",	/* 54 M_IPMADDR */ \
245 	"ether_multi",	/* 55 M_IFMADDR */ \
246 	"mrt",		/* 56 M_MRTABLE */ \
247 	"ISOFS mount",	/* 57 M_ISOFSMNT */ \
248 	"ISOFS node",	/* 58 M_ISOFSNODE */ \
249 	"MSDOSFS mount", /* 59 M_MSDOSFSMNT */ \
250 	"MSDOSFS fat",	/* 60 M_MSDOSFSFAT */ \
251 	"MSDOSFS node",	/* 61 M_MSDOSFSNODE */ \
252 	"ttys",		/* 62 M_TTYS */ \
253 	"exec",		/* 63 M_EXEC */ \
254 	"miscfs mount",	/* 64 M_MISCFSMNT */ \
255 	NULL, \
256 	"adosfs mount",	/* 66 M_ADOSFSMNT */ \
257 	NULL, \
258 	"adosfs anode",	/* 68 M_ANODE */ \
259 	NULL, \
260 	NULL, \
261 	"adosfs bitmap", /* 71 M_ADOSFSBITMAP */ \
262 	"EXT2FS node",	/* 72 M_EXT2FSNODE */ \
263 	NULL, \
264 	"pfkey data",	/* 74 M_PFKEY */ \
265 	"tdb",		/* 75 M_TDB */ \
266 	"xform_data",	/* 76 M_XDATA */ \
267 	NULL, \
268 	"pagedep",	/* 78 M_PAGEDEP */ \
269 	"inodedep",	/* 79 M_INODEDEP */ \
270 	"newblk",	/* 80 M_NEWBLK */ \
271 	NULL, \
272 	NULL, \
273 	"indirdep",	/* 83 M_INDIRDEP */ \
274 	NULL, NULL, NULL, NULL, \
275 	NULL, NULL, NULL, NULL, \
276 	"VM swap",	/* 92 M_VMSWAP */ \
277 	NULL, NULL, NULL, NULL, \
278 	"RAIDframe data", /* 97 M_RAIDFRAME */ \
279 	"UVM amap",	/* 98 M_UVMAMAP */ \
280 	"UVM aobj",	/* 99 M_UVMAOBJ */ \
281 	NULL, \
282 	"USB",		/* 101 M_USB */ \
283 	"USB device",	/* 102 M_USBDEV */ \
284 	"USB HC",	/* 103 M_USBHC */ \
285 	NULL, \
286 	"memdesc",	/* 105 M_MEMDESC */ \
287 	NULL,	/* 106 */ \
288 	NULL, \
289 	"crypto data",	/* 108 M_CRYPTO_DATA */ \
290 	NULL, \
291 	"IPsec creds",	/* 110 M_CREDENTIALS */ \
292 	"packet tags",	/* 111 M_PACKET_TAGS */ \
293 	"1394ctl",	/* 112 M_1394CTL */ \
294 	"1394data",	/* 113 M_1394DATA */ \
295 	"emuldata",	/* 114 M_EMULDATA */ \
296 	NULL, NULL, NULL, NULL, \
297 	NULL, NULL, NULL, NULL, \
298 	"ip6_options",	/* 123 M_IP6OPT */ \
299 	"NDP",		/* 124 M_IP6NDP */ \
300 	"ip6rr",	/* 125 M_IP6RR */ \
301 	"rp_addr",	/* 126 M_RR_ADDR */ \
302 	"temp",		/* 127 M_TEMP */ \
303 	"NTFS mount",	/* 128 M_NTFSMNT */ \
304 	"NTFS node",	/* 129 M_NTFSNTNODE */ \
305 	"NTFS fnode",	/* 130 M_NTFSFNODE */ \
306 	"NTFS dir",	/* 131 M_NTFSDIR */ \
307 	"NTFS hash tables",	/* 132 M_NTFSNTHASH */ \
308 	"NTFS file attr",	/* 133 M_NTFSNTVATTR */ \
309 	"NTFS resident data ",	/* 134 M_NTFSRDATA */ \
310 	"NTFS decomp",	/* 135 M_NTFSDECOMP */ \
311 	"NTFS vrun",	/* 136 M_NTFSRUN */ \
312 	"kqueue",	/* 137 M_KEVENT */ \
313 	"bluetooth",	/* 138 M_BLUETOOTH */ \
314 	"bwmeter",	/* 139 M_BWMETER */ \
315 	"UDF mount",	/* 140 M_UDFMOUNT */ \
316 	"UDF file entry",	/* 141 M_UDFFENTRY */ \
317 	"UDF file id",	/* 142 M_UDFFID */ \
318 }
319 
320 struct kmemstats {
321 	long	ks_inuse;	/* # of packets of this type currently in use */
322 	long	ks_calls;	/* total packets of this type ever allocated */
323 	long 	ks_memuse;	/* total memory held in bytes */
324 	u_short	ks_limblocks;	/* number of times blocked for hitting limit */
325 	u_short	ks_mapblocks;	/* number of times blocked for kernel map */
326 	long	ks_maxused;	/* maximum number ever used */
327 	long	ks_limit;	/* most that are allowed to exist */
328 	long	ks_size;	/* sizes of this thing that are allocated */
329 	long	ks_spare;
330 };
331 
332 /*
333  * Array of descriptors that describe the contents of each page
334  */
335 struct kmemusage {
336 	short ku_indx;		/* bucket index */
337 	union {
338 		u_short freecnt;/* for small allocations, free pieces in page */
339 		u_short pagecnt;/* for large allocations, pages alloced */
340 	} ku_un;
341 };
342 #define	ku_freecnt ku_un.freecnt
343 #define	ku_pagecnt ku_un.pagecnt
344 
345 /*
346  * Set of buckets for each size of memory block that is retained
347  */
348 struct kmembuckets {
349 	caddr_t   kb_next;	/* list of free blocks */
350 	caddr_t   kb_last;	/* last free block */
351 	u_int64_t kb_calls;	/* total calls to allocate this size */
352 	u_int64_t kb_total;	/* total number of blocks allocated */
353 	u_int64_t kb_totalfree;	/* # of free elements in this bucket */
354 	u_int64_t kb_elmpercl;	/* # of elements in this sized allocation */
355 	u_int64_t kb_highwat;	/* high water mark */
356 	u_int64_t kb_couldfree;	/* over high water mark and could free */
357 };
358 
359 #ifdef _KERNEL
360 #define	MINALLOCSIZE	(1 << MINBUCKET)
361 #define	BUCKETINDX(size) \
362 	((size) <= (MINALLOCSIZE * 128) \
363 		? (size) <= (MINALLOCSIZE * 8) \
364 			? (size) <= (MINALLOCSIZE * 2) \
365 				? (size) <= (MINALLOCSIZE * 1) \
366 					? (MINBUCKET + 0) \
367 					: (MINBUCKET + 1) \
368 				: (size) <= (MINALLOCSIZE * 4) \
369 					? (MINBUCKET + 2) \
370 					: (MINBUCKET + 3) \
371 			: (size) <= (MINALLOCSIZE* 32) \
372 				? (size) <= (MINALLOCSIZE * 16) \
373 					? (MINBUCKET + 4) \
374 					: (MINBUCKET + 5) \
375 				: (size) <= (MINALLOCSIZE * 64) \
376 					? (MINBUCKET + 6) \
377 					: (MINBUCKET + 7) \
378 		: (size) <= (MINALLOCSIZE * 2048) \
379 			? (size) <= (MINALLOCSIZE * 512) \
380 				? (size) <= (MINALLOCSIZE * 256) \
381 					? (MINBUCKET + 8) \
382 					: (MINBUCKET + 9) \
383 				: (size) <= (MINALLOCSIZE * 1024) \
384 					? (MINBUCKET + 10) \
385 					: (MINBUCKET + 11) \
386 			: (size) <= (MINALLOCSIZE * 8192) \
387 				? (size) <= (MINALLOCSIZE * 4096) \
388 					? (MINBUCKET + 12) \
389 					: (MINBUCKET + 13) \
390 				: (size) <= (MINALLOCSIZE * 16384) \
391 					? (MINBUCKET + 14) \
392 					: (MINBUCKET + 15))
393 
394 /*
395  * Turn virtual addresses into kmem map indicies
396  */
397 #define	kmemxtob(alloc)	(kmembase + (alloc) * NBPG)
398 #define	btokmemx(addr)	(((caddr_t)(addr) - kmembase) / NBPG)
399 #define	btokup(addr)	(&kmemusage[((caddr_t)(addr) - kmembase) >> PAGE_SHIFT])
400 
401 /*
402  * Macro versions for the usual cases of malloc/free
403  */
404 #if defined(KMEMSTATS) || defined(DIAGNOSTIC) || defined(_LKM) || defined(SMALL_KERNEL)
405 #define	MALLOC(space, cast, size, type, flags) \
406 	(space) = (cast)malloc((u_long)(size), type, flags)
407 #define	FREE(addr, type) free((caddr_t)(addr), type)
408 
409 #else /* do not collect statistics */
410 #define	MALLOC(space, cast, size, type, flags) do { \
411 	u_long kbp_size = (u_long)(size); \
412 	register struct kmembuckets *kbp = &bucket[BUCKETINDX(kbp_size)]; \
413 	long __s = splvm(); \
414 	if (kbp->kb_next == NULL) { \
415 		(space) = (cast)malloc(kbp_size, type, flags); \
416 	} else { \
417 		(space) = (cast)kbp->kb_next; \
418 		kbp->kb_next = *(caddr_t *)(space); \
419 	} \
420 	splx(__s); \
421 } while (0)
422 
423 #define	FREE(addr, type) do { \
424 	register struct kmembuckets *kbp; \
425 	register struct kmemusage *kup = btokup(addr); \
426 	long __s = splvm(); \
427 	if (1 << kup->ku_indx > MAXALLOCSAVE) { \
428 		free((caddr_t)(addr), type); \
429 	} else { \
430 		kbp = &bucket[kup->ku_indx]; \
431 		if (kbp->kb_next == NULL) \
432 			kbp->kb_next = (caddr_t)(addr); \
433 		else \
434 			*(caddr_t *)(kbp->kb_last) = (caddr_t)(addr); \
435 		*(caddr_t *)(addr) = NULL; \
436 		kbp->kb_last = (caddr_t)(addr); \
437 	} \
438 	splx(__s); \
439 } while(0)
440 #endif /* do not collect statistics */
441 
442 extern struct kmemstats kmemstats[];
443 extern struct kmemusage *kmemusage;
444 extern char *kmembase;
445 extern struct kmembuckets bucket[];
446 
447 extern void *malloc(unsigned long size, int type, int flags);
448 extern void free(void *addr, int type);
449 extern int sysctl_malloc(int *, u_int, void *, size_t *, void *, size_t,
450 			      struct proc *);
451 
452 size_t malloc_roundup(size_t);
453 
454 #ifdef MALLOC_DEBUG
455 int	debug_malloc(unsigned long, int, int, void **);
456 int	debug_free(void *, int);
457 void	debug_malloc_init(void);
458 void	debug_malloc_assert_allocated(void *, const char *);
459 #define DEBUG_MALLOC_ASSERT_ALLOCATED(addr) 			\
460 	debug_malloc_assert_allocated(addr, __func__)
461 
462 void	debug_malloc_print(void);
463 void	debug_malloc_printit(int (*)(const char *, ...), vaddr_t);
464 #endif /* MALLOC_DEBUG */
465 #endif /* _KERNEL */
466 #endif /* !_SYS_MALLOC_H_ */
467