xref: /netbsd/sys/sys/malloc.h (revision c4a72b64)
1 /*	$NetBSD: malloc.h,v 1.81 2002/11/02 06:24:34 yamt Exp $	*/
2 
3 /*
4  * Copyright (c) 1987, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)malloc.h	8.5 (Berkeley) 5/3/95
36  */
37 
38 #ifndef _SYS_MALLOC_H_
39 #define	_SYS_MALLOC_H_
40 
41 #if defined(_KERNEL_OPT)
42 #include "opt_kmemstats.h"
43 #include "opt_malloclog.h"
44 #include "opt_malloc_debug.h"
45 #include "opt_lockdebug.h"
46 #endif
47 
48 /*
49  * flags to malloc
50  */
51 #define	M_WAITOK	0x0000	/* can wait for resources */
52 #define	M_NOWAIT	0x0001	/* do not wait for resources */
53 #define	M_ZERO		0x0002	/* zero the allocation */
54 #define	M_CANFAIL	0x0004	/* can fail if requested memory can't ever
55 				 * be allocated */
56 /*
57  * Types of memory to be allocated (don't forget to update malloc.9)
58  */
59 #define	M_FREE		0	/* should be on free list */
60 #define	M_MBUF		1	/* mbuf */
61 #define	M_DEVBUF	2	/* device driver memory */
62 #define	M_SOCKET	3	/* socket structure */
63 #define	M_PCB		4	/* protocol control block */
64 #define	M_RTABLE	5	/* routing tables */
65 #define	M_HTABLE	6	/* IMP host tables */
66 #define	M_FTABLE	7	/* fragment reassembly header */
67 #define	M_ZOMBIE	8	/* zombie proc status */
68 #define	M_IFADDR	9	/* interface address */
69 #define	M_SOOPTS	10	/* socket options */
70 #define	M_SONAME	11	/* socket name */
71 #define	M_NAMEI		12	/* namei path name buffer */
72 #define	M_GPROF		13	/* kernel profiling buffer */
73 #define	M_IOCTLOPS	14	/* ioctl data buffer */
74 #define	M_MAPMEM	15	/* mapped memory descriptors */
75 #define	M_CRED		16	/* credentials */
76 #define	M_PGRP		17	/* process group header */
77 #define	M_SESSION	18	/* session header */
78 #define	M_IOV		19	/* large iov's */
79 #define	M_MOUNT		20	/* vfs mount struct */
80 #define	M_FHANDLE	21	/* network file handle */
81 #define	M_NFSREQ	22	/* NFS request header */
82 #define	M_NFSMNT	23	/* NFS mount structure */
83 #define	M_NFSNODE	24	/* NFS vnode private part */
84 #define	M_VNODE		25	/* Dynamically allocated vnodes */
85 #define	M_CACHE		26	/* Dynamically allocated cache entries */
86 #define	M_DQUOT		27	/* UFS quota entries */
87 #define	M_UFSMNT	28	/* UFS mount structure */
88 #define	M_SHM		29	/* SVID compatible shared memory segments */
89 #define	M_VMMAP		30	/* VM map structures */
90 #define	M_VMMAPENT	31	/* VM map entry structures */
91 #define	M_VMOBJ		32	/* VM object structure */
92 #define	M_VMOBJHASH	33	/* VM object hash structure */
93 #define	M_VMPMAP	34	/* VM pmap */
94 #define	M_VMPVENT	35	/* VM phys-virt mapping entry */
95 #define	M_VMPAGER	36	/* XXX: VM pager struct */
96 #define	M_VMPGDATA	37	/* XXX: VM pager private data */
97 #define	M_FILE		38	/* Open file structure */
98 #define	M_FILEDESC	39	/* Open file descriptor table */
99 #define	M_LOCKF		40	/* Byte-range locking structures */
100 #define	M_PROC		41	/* Proc structures */
101 #define	M_SUBPROC	42	/* Proc sub-structures */
102 #define	M_SEGMENT	43	/* Segment for LFS */
103 #define	M_LFSNODE	44	/* LFS vnode private part */
104 #define	M_FFSNODE	45	/* FFS vnode private part */
105 #define	M_MFSNODE	46	/* MFS vnode private part */
106 #define	M_NQLEASE	47	/* Nqnfs lease */
107 #define	M_NQMHOST	48	/* Nqnfs host address table */
108 #define	M_NETADDR	49	/* Export host address structure */
109 #define	M_NFSSVC	50	/* Nfs server structure */
110 #define	M_NFSUID	51	/* Nfs uid mapping structure */
111 #define	M_NFSD		52	/* Nfs server daemon structure */
112 #define	M_IPMOPTS	53	/* internet multicast options */
113 #define	M_IPMADDR	54	/* internet multicast address */
114 #define	M_IFMADDR	55	/* link-level multicast address */
115 #define	M_MRTABLE	56	/* multicast routing tables */
116 #define	M_ISOFSMNT	57	/* ISOFS mount structure */
117 #define	M_ISOFSNODE	58	/* ISOFS vnode private part */
118 #define	M_MSDOSFSMNT	59	/* MSDOS FS mount structure */
119 #define	M_MSDOSFSFAT	60	/* MSDOS FS fat table */
120 #define	M_MSDOSFSNODE	61	/* MSDOS FS vnode private part */
121 #define	M_TTYS		62	/* allocated tty structures */
122 #define	M_EXEC		63	/* argument lists & other mem used by exec */
123 #define	M_MISCFSMNT	64	/* miscfs mount structures */
124 #define	M_MISCFSNODE	65	/* miscfs vnode private part */
125 #define	M_ADOSFSMNT	66	/* adosfs mount structures */
126 #define	M_ADOSFSNODE	67	/* adosfs vnode private part */
127 #define	M_ANODE		68	/* adosfs anode structures and tables */
128 #define	M_IPQ		69	/* IP packet queue entry */
129 #define	M_AFS		70	/* Andrew File System */
130 #define	M_ADOSFSBITMAP	71	/* adosfs bitmap */
131 #define	M_NFSRVDESC	72	/* NFS server descriptor */
132 #define	M_NFSDIROFF	73	/* NFS directory cookies */
133 #define	M_NFSBIGFH	74	/* NFS big filehandle */
134 #define	M_EXT2FSNODE	75	/* EXT2FS vnode private part */
135 #define	M_VMSWAP	76	/* VM swap structures */
136 #define	M_VMPAGE	77	/* VM page structures */
137 #define	M_VMPBUCKET	78	/* VM page buckets */
138 /*
139  * Why are 79-81 empty?
140  */
141 #define	M_UVMAMAP	82	/* UVM amap and related structs */
142 #define	M_UVMAOBJ	83	/* UVM aobj and related structs */
143 #define	M_TEMP		84	/* misc temporary data buffers */
144 #define	M_DMAMAP	85	/* bus_dma(9) structures */
145 #define	M_IPFLOW	86	/* IP flow entries */
146 #define	M_USB		87	/* USB general */
147 #define	M_USBDEV	88	/* USB device driver */
148 #define	M_POOL		89	/* memory pool structs */
149 #define	M_CODA		90	/* Coda file system structures and tables */
150 #define	M_FILECOREMNT	91	/* Filecore FS mount structures */
151 #define	M_FILECORENODE	92	/* Filecore FS vnode private part */
152 #define	M_RAIDFRAME	93	/* RAIDframe structures */
153 #define	M_USBHC		94	/* USB host controller */
154 #define	M_SECA		95	/* security associations, key management */
155 #define	M_IP6OPT	96	/* IPv6 options */
156 #define	M_IP6NDP	97	/* IPv6 Neighbour Discovery */
157 #define	M_NTFS		98	/* Windows NT file system structures */
158 #define	M_PAGEDEP	99	/* File page dependencies */
159 #define	M_INODEDEP	100	/* Inode dependencies */
160 #define	M_NEWBLK	101	/* New block allocation */
161 #define	M_BMSAFEMAP	102	/* Block or frag allocated from cyl group map */
162 #define	M_ALLOCDIRECT	103	/* Block or frag dependency for an inode */
163 #define	M_INDIRDEP	104	/* Indirect block dependencies */
164 #define	M_ALLOCINDIR	105	/* Block dependency for an indirect block */
165 #define	M_FREEFRAG	106	/* Previously used frag for an inode */
166 #define	M_FREEBLKS	107	/* Blocks freed from an inode */
167 #define	M_FREEFILE	108	/* Inode deallocated */
168 #define	M_DIRADD	109	/* New directory entry */
169 #define	M_MKDIR		110	/* New directory */
170 #define	M_DIRREM	111 	/* Directory entry deleted */
171 #define	M_IP6RR		112	/* IPv6 Router Renumbering Prefix */
172 #define	M_RR_ADDR	113	/* IPv6 Router Renumbering Ifid */
173 #define	M_SOFTINTR	114	/* Softinterrupt structures */
174 #define	M_EMULDATA	115	/* Per-process emulation data */
175 #define	M_1394CTL	116	/* IEEE 1394 control structures */
176 #define	M_1394DATA	117	/* IEEE 1394 data buffers */
177 #define	M_PIPE		118	/* Pipe structures */
178 #define	M_AGP		119	/* AGP memory */
179 #define	M_PROP		120	/* Kernel properties structures */
180 #define	M_NEWDIRBLK	121	/* Unclaimed new dir block (softdeps) */
181 #define	M_SMBIOD	122	/* SMB network id daemon */
182 #define	M_SMBCONN	123	/* SMB connection */
183 #define	M_SMBRQ		124	/* SMB request */
184 #define	M_SMBDATA	125	/* Misc netsmb data */
185 #define	M_SMBSTR	126	/* netsmb string data */
186 #define	M_SMBTEMP	127	/* Temp netsmb data */
187 #define	M_ICONV		128	/* ICONV data */
188 #define	M_SMBNODE	129	/* SMBFS node */
189 #define	M_SMBNODENAME	130	/* SMBFS node name */
190 #define	M_SMBFSDATA	131	/* SMBFS private data */
191 #define	M_SMBFSHASH	132	/* SMBFS hash table */
192 #define	M_SA		133	/* Scheduler activations */
193 #define	M_KEVENT	134	/* kevents/knotes */
194 #define	M_LAST		135	/* Must be last type + 1 */
195 
196 /* added something?  don't forget to update malloc.9 */
197 
198 #define	INITKMEMNAMES { \
199 	"free",		/* 0 M_FREE */ \
200 	"mbuf",		/* 1 M_MBUF */ \
201 	"devbuf",	/* 2 M_DEVBUF */ \
202 	"socket",	/* 3 M_SOCKET */ \
203 	"pcb",		/* 4 M_PCB */ \
204 	"routetbl",	/* 5 M_RTABLE */ \
205 	"hosttbl",	/* 6 M_HTABLE */ \
206 	"fragtbl",	/* 7 M_FTABLE */ \
207 	"zombie",	/* 8 M_ZOMBIE */ \
208 	"ifaddr",	/* 9 M_IFADDR */ \
209 	"soopts",	/* 10 M_SOOPTS */ \
210 	"soname",	/* 11 M_SONAME */ \
211 	"namei",	/* 12 M_NAMEI */ \
212 	"gprof",	/* 13 M_GPROF */ \
213 	"ioctlops",	/* 14 M_IOCTLOPS */ \
214 	"mapmem",	/* 15 M_MAPMEM */ \
215 	"cred",		/* 16 M_CRED */ \
216 	"pgrp",		/* 17 M_PGRP */ \
217 	"session",	/* 18 M_SESSION */ \
218 	"iov",		/* 19 M_IOV */ \
219 	"mount",	/* 20 M_MOUNT */ \
220 	"fhandle",	/* 21 M_FHANDLE */ \
221 	"NFS req",	/* 22 M_NFSREQ */ \
222 	"NFS mount",	/* 23 M_NFSMNT */ \
223 	"NFS node",	/* 24 M_NFSNODE */ \
224 	"vnodes",	/* 25 M_VNODE */ \
225 	"namecache",	/* 26 M_CACHE */ \
226 	"UFS quota",	/* 27 M_DQUOT */ \
227 	"UFS mount",	/* 28 M_UFSMNT */ \
228 	"shm",		/* 29 M_SHM */ \
229 	"VM map",	/* 30 M_VMMAP */ \
230 	"VM mapent",	/* 31 M_VMMAPENT */ \
231 	"VM object",	/* 32 M_VMOBJ */ \
232 	"VM objhash",	/* 33 M_VMOBJHASH */ \
233 	"VM pmap",	/* 34 M_VMPMAP */ \
234 	"VM pvmap",	/* 35 M_VMPVENT */ \
235 	"VM pager",	/* 36 M_VMPAGER */ \
236 	"VM pgdata",	/* 37 M_VMPGDATA */ \
237 	"file",		/* 38 M_FILE */ \
238 	"file desc",	/* 39 M_FILEDESC */ \
239 	"lockf",	/* 40 M_LOCKF */ \
240 	"proc",		/* 41 M_PROC */ \
241 	"subproc",	/* 42 M_SUBPROC */ \
242 	"LFS segment",	/* 43 M_SEGMENT */ \
243 	"LFS node",	/* 44 M_LFSNODE */ \
244 	"FFS node",	/* 45 M_FFSNODE */ \
245 	"MFS node",	/* 46 M_MFSNODE */ \
246 	"NQNFS Lease",	/* 47 M_NQLEASE */ \
247 	"NQNFS Host",	/* 48 M_NQMHOST */ \
248 	"Export Host",	/* 49 M_NETADDR */ \
249 	"NFS srvsock",	/* 50 M_NFSSVC */ \
250 	"NFS uid",	/* 51 M_NFSUID */ \
251 	"NFS daemon",	/* 52 M_NFSD */ \
252 	"ip_moptions",	/* 53 M_IPMOPTS */ \
253 	"in_multi",	/* 54 M_IPMADDR */ \
254 	"ether_multi",	/* 55 M_IFMADDR */ \
255 	"mrt",		/* 56 M_MRTABLE */ \
256 	"ISOFS mount",	/* 57 M_ISOFSMNT */ \
257 	"ISOFS node",	/* 58 M_ISOFSNODE */ \
258 	"MSDOSFS mount", /* 59 M_MSDOSFSMNT */ \
259 	"MSDOSFS fat",	/* 60 M_MSDOSFSFAT */ \
260 	"MSDOSFS node",	/* 61 M_MSDOSFSNODE */ \
261 	"ttys",		/* 62 M_TTYS */ \
262 	"exec",		/* 63 M_EXEC */ \
263 	"miscfs mount",	/* 64 M_MISCFSMNT */ \
264 	"miscfs node",	/* 65 M_MISCFSNODE */ \
265 	"adosfs mount",	/* 66 M_ADOSFSMNT */ \
266 	"adosfs node",	/* 67 M_ADOSFSNODE */ \
267 	"adosfs anode",	/* 68 M_ANODE */ \
268 	"IP queue ent", /* 69 M_IPQ */ \
269 	"afs",		/* 70 M_AFS */ \
270 	"adosfs bitmap", /* 71 M_ADOSFSBITMAP */ \
271 	"NFS srvdesc",	/* 72 M_NFSRVDESC */ \
272 	"NFS diroff",	/* 73 M_NFSDIROFF */ \
273 	"NFS bigfh",	/* 74 M_NFSBIGFH */ \
274 	"EXT2FS node",  /* 75 M_EXT2FSNODE */ \
275 	"VM swap",	/* 76 M_VMSWAP */ \
276 	"VM page",	/* 77 M_VMPAGE */ \
277 	"VM page bucket", /* 78 M_VMPBUCKET */ \
278 	NULL,		/* 79 */ \
279 	NULL,		/* 80 */ \
280 	NULL,		/* 81 */ \
281 	"UVM amap",	/* 82 M_UVMAMAP */ \
282 	"UVM aobj",	/* 83 M_UVMAOBJ */ \
283 	"temp",		/* 84 M_TEMP */ \
284 	"DMA map",	/* 85 M_DMAMAP */ \
285 	"IP flow",	/* 86 M_IPFLOW */ \
286 	"USB",		/* 87 M_USB */ \
287 	"USB device",	/* 88 M_USBDEV */ \
288 	"Pool",		/* 89 M_POOL */ \
289 	"coda",		/* 90 M_CODA */ \
290 	"filecore mount", /* 91 M_FILECOREMNT */ \
291 	"filecore node", /* 92 M_FILECORENODE */ \
292 	"RAIDframe",	/* 93 M_RAIDFRAME */ \
293 	"USB HC",	/* 94 M_USBHC */ \
294 	"key mgmt",	/* 95 M_SECA */ \
295 	"ip6_options",	/* 96 M_IP6OPT */ \
296 	"NDP",		/* 97 M_IP6NDP */ \
297 	"NTFS",		/* 98 M_NTFS */ \
298 	"pagedep",	/* 99 M_PAGEDEP */ \
299 	"inodedep",	/* 100 M_INODEDEP */ \
300 	"newblk",	/* 101 M_NEWBLK */ \
301 	"bmsafemap",	/* 102 M_BMSAFEMAP */ \
302 	"allocdirect",	/* 103 M_ALLOCDIRECT */ \
303 	"indirdep",	/* 104 M_INDIRDEP */ \
304 	"allocindir",	/* 105 M_ALLOCINDIR */ \
305 	"freefrag",	/* 106 M_FREEFRAG */ \
306 	"freeblks",	/* 107 M_FREEBLKS */ \
307 	"freefile",	/* 108 M_FREEFILE */ \
308 	"diradd",	/* 109 M_DIRADD */ \
309 	"mkdir",	/* 110 M_MKDIR */ \
310 	"dirrem",	/* 111 M_DIRREM */ \
311 	"ip6rr",	/* 112 M_IP6RR */ \
312 	"rp_addr",	/* 113 M_RR_ADDR */ \
313 	"softintr",	/* 114 M_SOFTINTR */ \
314 	"emuldata",	/* 115 M_EMULDATA */ \
315 	"1394ctl",	/* 116 M_1394CTL */ \
316 	"1394data",	/* 117 M_1394DATA */ \
317 	"pipe",		/* 118 M_PIPE */ \
318 	"AGP",		/* 119 M_AGP */ \
319 	"prop",		/* 120 M_PROP */ \
320 	"newdirblk",	/* 121 M_NEWDIRBLK */ \
321 	"smbiod",	/* 122 M_SMBIOD */ \
322 	"smbconn",	/* 123 M_SMBCONN */ \
323 	"smbrq",	/* 124 M_SMBRQ */ \
324 	"smbdata",	/* 125 M_SMBDATA */ \
325 	"smbstr",	/* 126 M_SMBDATA */ \
326 	"smbtemp",	/* 127 M_SMBTEMP */ \
327 	"iconv",	/* 128 M_ICONV */ \
328 	"smbnode",	/* 129 M_SMBNODE */ \
329 	"smbnodename",	/* 130 M_SMBNODENAME */ \
330 	"smbfsdata",	/* 131 M_SMBFSDATA */ \
331 	"smbfshash",	/* 132 M_SMBFSHASH */ \
332 	"sa",		/* 133 M_SA */ \
333 	"kevent",	/* 134 M_KEVENT */ \
334 	NULL,		/* 135 */ \
335 }
336 
337 struct kmemstats {
338 	u_long	ks_inuse;	/* # of packets of this type currently in use */
339 	u_long	ks_calls;	/* total packets of this type ever allocated */
340 	u_long 	ks_memuse;	/* total memory held in bytes */
341 	u_short	ks_limblocks;	/* number of times blocked for hitting limit */
342 	u_short	ks_mapblocks;	/* number of times blocked for kernel map */
343 	u_long	ks_maxused;	/* maximum number ever used */
344 	u_long	ks_limit;	/* most that are allowed to exist */
345 	u_long	ks_size;	/* sizes of this thing that are allocated */
346 	u_long	ks_spare;
347 };
348 
349 /*
350  * Array of descriptors that describe the contents of each page
351  */
352 struct kmemusage {
353 	short ku_indx;		/* bucket index */
354 	union {
355 		u_short freecnt;/* for small allocations, free pieces in page */
356 		u_short pagecnt;/* for large allocations, pages alloced */
357 	} ku_un;
358 };
359 #define	ku_freecnt ku_un.freecnt
360 #define	ku_pagecnt ku_un.pagecnt
361 
362 /*
363  * Set of buckets for each size of memory block that is retained
364  */
365 struct kmembuckets {
366 	caddr_t kb_next;	/* list of free blocks */
367 	caddr_t kb_last;	/* last free block */
368 	long	kb_calls;	/* total calls to allocate this size */
369 	long	kb_total;	/* total number of blocks allocated */
370 	long	kb_totalfree;	/* # of free elements in this bucket */
371 	long	kb_elmpercl;	/* # of elements in this sized allocation */
372 	long	kb_highwat;	/* high water mark */
373 	long	kb_couldfree;	/* over high water mark and could free */
374 };
375 
376 #ifdef _KERNEL
377 #define	MINALLOCSIZE	(1 << MINBUCKET)
378 #define	BUCKETINDX(size) \
379 	((size) <= (MINALLOCSIZE * 128) \
380 		? (size) <= (MINALLOCSIZE * 8) \
381 			? (size) <= (MINALLOCSIZE * 2) \
382 				? (size) <= (MINALLOCSIZE * 1) \
383 					? (MINBUCKET + 0) \
384 					: (MINBUCKET + 1) \
385 				: (size) <= (MINALLOCSIZE * 4) \
386 					? (MINBUCKET + 2) \
387 					: (MINBUCKET + 3) \
388 			: (size) <= (MINALLOCSIZE* 32) \
389 				? (size) <= (MINALLOCSIZE * 16) \
390 					? (MINBUCKET + 4) \
391 					: (MINBUCKET + 5) \
392 				: (size) <= (MINALLOCSIZE * 64) \
393 					? (MINBUCKET + 6) \
394 					: (MINBUCKET + 7) \
395 		: (size) <= (MINALLOCSIZE * 2048) \
396 			? (size) <= (MINALLOCSIZE * 512) \
397 				? (size) <= (MINALLOCSIZE * 256) \
398 					? (MINBUCKET + 8) \
399 					: (MINBUCKET + 9) \
400 				: (size) <= (MINALLOCSIZE * 1024) \
401 					? (MINBUCKET + 10) \
402 					: (MINBUCKET + 11) \
403 			: (size) <= (MINALLOCSIZE * 8192) \
404 				? (size) <= (MINALLOCSIZE * 4096) \
405 					? (MINBUCKET + 12) \
406 					: (MINBUCKET + 13) \
407 				: (size) <= (MINALLOCSIZE * 16384) \
408 					? (MINBUCKET + 14) \
409 					: (MINBUCKET + 15))
410 
411 /*
412  * Turn virtual addresses into kmem map indicies
413  */
414 #define	kmemxtob(alloc)	(kmembase + (alloc) * NBPG)
415 #define	btokmemx(addr)	(((caddr_t)(addr) - kmembase) / NBPG)
416 #define	btokup(addr)	(&kmemusage[((caddr_t)(addr) - kmembase) >> PGSHIFT])
417 
418 /*
419  * Macro versions for the usual cases of malloc/free
420  */
421 #if defined(KMEMSTATS) || defined(DIAGNOSTIC) || defined(_LKM) || \
422     defined(MALLOCLOG) || defined(LOCKDEBUG) || defined(MALLOC_NOINLINE)
423 #define	MALLOC(space, cast, size, type, flags) \
424 	(space) = (cast)malloc((u_long)(size), (type), (flags))
425 #define	FREE(addr, type) free((caddr_t)(addr), (type))
426 
427 #else /* do not collect statistics */
428 #define	MALLOC(space, cast, size, type, flags)				\
429 do {									\
430 	register struct kmembuckets *__kbp = &bucket[BUCKETINDX((size))]; \
431 	long __s = splvm();						\
432 	if (__kbp->kb_next == NULL) {					\
433 		(space) = (cast)malloc((u_long)(size), (type), (flags)); \
434 		splx(__s);						\
435 	} else {							\
436 		(space) = (cast)__kbp->kb_next;				\
437 		__kbp->kb_next = *(caddr_t *)(space);			\
438 		splx(__s);						\
439 		if ((flags) & M_ZERO)					\
440 			memset((space), 0, (size));			\
441 	}								\
442 } while (/* CONSTCOND */ 0)
443 
444 #define	FREE(addr, type)						\
445 do {									\
446 	register struct kmembuckets *__kbp;				\
447 	register struct kmemusage *__kup = btokup((addr));		\
448 	long __s = splvm();						\
449 	if (1 << __kup->ku_indx > MAXALLOCSAVE) {			\
450 		free((caddr_t)(addr), (type));				\
451 	} else {							\
452 		__kbp = &bucket[__kup->ku_indx];			\
453 		if (__kbp->kb_next == NULL)				\
454 			__kbp->kb_next = (caddr_t)(addr);		\
455 		else							\
456 			*(caddr_t *)(__kbp->kb_last) = (caddr_t)(addr);	\
457 		*(caddr_t *)(addr) = NULL;				\
458 		__kbp->kb_last = (caddr_t)(addr);			\
459 	}								\
460 	splx(__s);							\
461 } while(/* CONSTCOND */ 0)
462 #endif /* do not collect statistics */
463 
464 extern struct kmemstats		kmemstats[];
465 extern struct kmemusage		*kmemusage;
466 extern char			*kmembase;
467 extern struct kmembuckets	bucket[];
468 
469 #ifdef MALLOCLOG
470 void	*_malloc(unsigned long size, int type, int flags,
471 	    const char *file, long line);
472 void	_free(void *addr, int type, const char *file, long line);
473 #define	malloc(size, type, flags) \
474 	    _malloc((size), (type), (flags), __FILE__, __LINE__)
475 #define	free(addr, type) \
476 	    _free((addr), (type), __FILE__, __LINE__)
477 #else
478 void	*malloc(unsigned long size, int type, int flags);
479 void	free(void *addr, int type);
480 #endif /* MALLOCLOG */
481 
482 #ifdef MALLOC_DEBUG
483 int	debug_malloc(unsigned long, int, int, void **);
484 int	debug_free(void *, int);
485 void	debug_malloc_init(void);
486 
487 void	debug_malloc_print(void);
488 void	debug_malloc_printit(void (*)(const char *, ...), vaddr_t);
489 #endif /* MALLOC_DEBUG */
490 
491 void	*realloc(void *curaddr, unsigned long newsize, int type,
492 	    int flags);
493 unsigned long
494 	malloc_roundup(unsigned long);
495 #endif /* _KERNEL */
496 #endif /* !_SYS_MALLOC_H_ */
497