xref: /original-bsd/sys/sys/malloc.h (revision 333da485)
1 /*
2  * Copyright (c) 1987, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)malloc.h	8.3 (Berkeley) 01/12/94
8  */
9 
10 #ifndef _SYS_MALLOC_H_
11 #define	_SYS_MALLOC_H_
12 
13 #define KMEMSTATS
14 
15 /*
16  * flags to malloc
17  */
18 #define	M_WAITOK	0x0000
19 #define	M_NOWAIT	0x0001
20 
21 /*
22  * Types of memory to be allocated
23  */
24 #define	M_FREE		0	/* should be on free list */
25 #define	M_MBUF		1	/* mbuf */
26 #define	M_DEVBUF	2	/* device driver memory */
27 #define	M_SOCKET	3	/* socket structure */
28 #define	M_PCB		4	/* protocol control block */
29 #define	M_RTABLE	5	/* routing tables */
30 #define	M_HTABLE	6	/* IMP host tables */
31 #define	M_FTABLE	7	/* fragment reassembly header */
32 #define	M_ZOMBIE	8	/* zombie proc status */
33 #define	M_IFADDR	9	/* interface address */
34 #define	M_SOOPTS	10	/* socket options */
35 #define	M_SONAME	11	/* socket name */
36 #define	M_NAMEI		12	/* namei path name buffer */
37 #define	M_GPROF		13	/* kernel profiling buffer */
38 #define	M_IOCTLOPS	14	/* ioctl data buffer */
39 #define	M_MAPMEM	15	/* mapped memory descriptors */
40 #define	M_CRED		16	/* credentials */
41 #define	M_PGRP		17	/* process group header */
42 #define	M_SESSION	18	/* session header */
43 #define	M_IOV		19	/* large iov's */
44 #define	M_MOUNT		20	/* vfs mount struct */
45 #define	M_FHANDLE	21	/* network file handle */
46 #define	M_NFSREQ	22	/* NFS request header */
47 #define	M_NFSMNT	23	/* NFS mount structure */
48 #define	M_NFSNODE	24	/* NFS vnode private part */
49 #define	M_VNODE		25	/* Dynamically allocated vnodes */
50 #define	M_CACHE		26	/* Dynamically allocated cache entries */
51 #define	M_DQUOT		27	/* UFS quota entries */
52 #define	M_UFSMNT	28	/* UFS mount structure */
53 #define	M_SHM		29	/* SVID compatible shared memory segments */
54 #define	M_VMMAP		30	/* VM map structures */
55 #define	M_VMMAPENT	31	/* VM map entry structures */
56 #define	M_VMOBJ		32	/* VM object structure */
57 #define	M_VMOBJHASH	33	/* VM object hash structure */
58 #define	M_VMPMAP	34	/* VM pmap */
59 #define	M_VMPVENT	35	/* VM phys-virt mapping entry */
60 #define	M_VMPAGER	36	/* XXX: VM pager struct */
61 #define	M_VMPGDATA	37	/* XXX: VM pager private data */
62 #define	M_FILE		38	/* Open file structure */
63 #define	M_FILEDESC	39	/* Open file descriptor table */
64 #define	M_LOCKF		40	/* Byte-range locking structures */
65 #define	M_PROC		41	/* Proc structures */
66 #define	M_SUBPROC	42	/* Proc sub-structures */
67 #define	M_SEGMENT	43	/* Segment for LFS */
68 #define	M_LFSNODE	44	/* LFS vnode private part */
69 #define	M_FFSNODE	45	/* FFS vnode private part */
70 #define	M_MFSNODE	46	/* MFS vnode private part */
71 #define	M_NQLEASE	47	/* Nqnfs lease */
72 #define	M_NQMHOST	48	/* Nqnfs host address table */
73 #define	M_NETADDR	49	/* Export host address structure */
74 #define	M_NFSSVC	50	/* Nfs server structure */
75 #define	M_NFSUID	51	/* Nfs uid mapping structure */
76 #define	M_NFSD		52	/* Nfs server daemon structure */
77 #define	M_IPMOPTS	53	/* internet multicast options */
78 #define	M_IPMADDR	54	/* internet multicast address */
79 #define	M_IFMADDR	55	/* link-level multicast address */
80 #define	M_MRTABLE	56	/* multicast routing tables */
81 #define M_ISOFSMNT	57	/* ISOFS mount structure */
82 #define M_ISOFSNODE	58	/* ISOFS vnode private part */
83 #define	M_TEMP		74	/* misc temporary data buffers */
84 #define	M_LAST		75	/* Must be last type + 1 */
85 
86 #define INITKMEMNAMES { \
87 	"free",		/* 0 M_FREE */ \
88 	"mbuf",		/* 1 M_MBUF */ \
89 	"devbuf",	/* 2 M_DEVBUF */ \
90 	"socket",	/* 3 M_SOCKET */ \
91 	"pcb",		/* 4 M_PCB */ \
92 	"routetbl",	/* 5 M_RTABLE */ \
93 	"hosttbl",	/* 6 M_HTABLE */ \
94 	"fragtbl",	/* 7 M_FTABLE */ \
95 	"zombie",	/* 8 M_ZOMBIE */ \
96 	"ifaddr",	/* 9 M_IFADDR */ \
97 	"soopts",	/* 10 M_SOOPTS */ \
98 	"soname",	/* 11 M_SONAME */ \
99 	"namei",	/* 12 M_NAMEI */ \
100 	"gprof",	/* 13 M_GPROF */ \
101 	"ioctlops",	/* 14 M_IOCTLOPS */ \
102 	"mapmem",	/* 15 M_MAPMEM */ \
103 	"cred",		/* 16 M_CRED */ \
104 	"pgrp",		/* 17 M_PGRP */ \
105 	"session",	/* 18 M_SESSION */ \
106 	"iov",		/* 19 M_IOV */ \
107 	"mount",	/* 20 M_MOUNT */ \
108 	"fhandle",	/* 21 M_FHANDLE */ \
109 	"NFS req",	/* 22 M_NFSREQ */ \
110 	"NFS mount",	/* 23 M_NFSMNT */ \
111 	"NFS node",	/* 24 M_NFSNODE */ \
112 	"vnodes",	/* 25 M_VNODE */ \
113 	"namecache",	/* 26 M_CACHE */ \
114 	"UFS quota",	/* 27 M_DQUOT */ \
115 	"UFS mount",	/* 28 M_UFSMNT */ \
116 	"shm",		/* 29 M_SHM */ \
117 	"VM map",	/* 30 M_VMMAP */ \
118 	"VM mapent",	/* 31 M_VMMAPENT */ \
119 	"VM object",	/* 32 M_VMOBJ */ \
120 	"VM objhash",	/* 33 M_VMOBJHASH */ \
121 	"VM pmap",	/* 34 M_VMPMAP */ \
122 	"VM pvmap",	/* 35 M_VMPVENT */ \
123 	"VM pager",	/* 36 M_VMPAGER */ \
124 	"VM pgdata",	/* 37 M_VMPGDATA */ \
125 	"file",		/* 38 M_FILE */ \
126 	"file desc",	/* 39 M_FILEDESC */ \
127 	"lockf",	/* 40 M_LOCKF */ \
128 	"proc",		/* 41 M_PROC */ \
129 	"subproc",	/* 42 M_SUBPROC */ \
130 	"LFS segment",	/* 43 M_SEGMENT */ \
131 	"LFS node",	/* 44 M_LFSNODE */ \
132 	"FFS node",	/* 45 M_FFSNODE */ \
133 	"MFS node",	/* 46 M_MFSNODE */ \
134 	"NQNFS Lease",	/* 47 M_NQLEASE */ \
135 	"NQNFS Host",	/* 48 M_NQMHOST */ \
136 	"Export Host",	/* 49 M_NETADDR */ \
137 	"NFS srvsock",	/* 50 M_NFSSVC */ \
138 	"NFS uid",	/* 51 M_NFSUID */ \
139 	"NFS daemon",	/* 52 M_NFSD */ \
140 	"ip_moptions",	/* 53 M_IPMOPTS */ \
141 	"in_multi",	/* 54 M_IPMADDR */ \
142 	"ether_multi",	/* 55 M_IFMADDR */ \
143 	"mrt",		/* 56 M_MRTABLE */ \
144 	"ISOFS mount",	/* 57 M_ISOFSMNT */ \
145 	"ISOFS node",	/* 58 M_ISOFSNODE */ \
146 	NULL, NULL, NULL, NULL, NULL, \
147 	NULL, NULL, NULL, NULL, NULL, \
148 	NULL, NULL, NULL, NULL, NULL, \
149 	"temp",		/* 74 M_TEMP */ \
150 }
151 
152 struct kmemstats {
153 	long	ks_inuse;	/* # of packets of this type currently in use */
154 	long	ks_calls;	/* total packets of this type ever allocated */
155 	long 	ks_memuse;	/* total memory held in bytes */
156 	u_short	ks_limblocks;	/* number of times blocked for hitting limit */
157 	u_short	ks_mapblocks;	/* number of times blocked for kernel map */
158 	long	ks_maxused;	/* maximum number ever used */
159 	long	ks_limit;	/* most that are allowed to exist */
160 	long	ks_size;	/* sizes of this thing that are allocated */
161 	long	ks_spare;
162 };
163 
164 /*
165  * Array of descriptors that describe the contents of each page
166  */
167 struct kmemusage {
168 	short ku_indx;		/* bucket index */
169 	union {
170 		u_short freecnt;/* for small allocations, free pieces in page */
171 		u_short pagecnt;/* for large allocations, pages alloced */
172 	} ku_un;
173 };
174 #define ku_freecnt ku_un.freecnt
175 #define ku_pagecnt ku_un.pagecnt
176 
177 /*
178  * Set of buckets for each size of memory block that is retained
179  */
180 struct kmembuckets {
181 	caddr_t kb_next;	/* list of free blocks */
182 	caddr_t kb_last;	/* last free block */
183 	long	kb_calls;	/* total calls to allocate this size */
184 	long	kb_total;	/* total number of blocks allocated */
185 	long	kb_totalfree;	/* # of free elements in this bucket */
186 	long	kb_elmpercl;	/* # of elements in this sized allocation */
187 	long	kb_highwat;	/* high water mark */
188 	long	kb_couldfree;	/* over high water mark and could free */
189 };
190 
191 #ifdef KERNEL
192 #define	MINALLOCSIZE	(1 << MINBUCKET)
193 #define BUCKETINDX(size) \
194 	(size) <= (MINALLOCSIZE * 128) \
195 		? (size) <= (MINALLOCSIZE * 8) \
196 			? (size) <= (MINALLOCSIZE * 2) \
197 				? (size) <= (MINALLOCSIZE * 1) \
198 					? (MINBUCKET + 0) \
199 					: (MINBUCKET + 1) \
200 				: (size) <= (MINALLOCSIZE * 4) \
201 					? (MINBUCKET + 2) \
202 					: (MINBUCKET + 3) \
203 			: (size) <= (MINALLOCSIZE* 32) \
204 				? (size) <= (MINALLOCSIZE * 16) \
205 					? (MINBUCKET + 4) \
206 					: (MINBUCKET + 5) \
207 				: (size) <= (MINALLOCSIZE * 64) \
208 					? (MINBUCKET + 6) \
209 					: (MINBUCKET + 7) \
210 		: (size) <= (MINALLOCSIZE * 2048) \
211 			? (size) <= (MINALLOCSIZE * 512) \
212 				? (size) <= (MINALLOCSIZE * 256) \
213 					? (MINBUCKET + 8) \
214 					: (MINBUCKET + 9) \
215 				: (size) <= (MINALLOCSIZE * 1024) \
216 					? (MINBUCKET + 10) \
217 					: (MINBUCKET + 11) \
218 			: (size) <= (MINALLOCSIZE * 8192) \
219 				? (size) <= (MINALLOCSIZE * 4096) \
220 					? (MINBUCKET + 12) \
221 					: (MINBUCKET + 13) \
222 				: (size) <= (MINALLOCSIZE * 16384) \
223 					? (MINBUCKET + 14) \
224 					: (MINBUCKET + 15)
225 
226 /*
227  * Turn virtual addresses into kmem map indicies
228  */
229 #define kmemxtob(alloc)	(kmembase + (alloc) * NBPG)
230 #define btokmemx(addr)	(((caddr_t)(addr) - kmembase) / NBPG)
231 #define btokup(addr)	(&kmemusage[((caddr_t)(addr) - kmembase) >> CLSHIFT])
232 
233 /*
234  * Macro versions for the usual cases of malloc/free
235  */
236 #if defined(KMEMSTATS) || defined(DIAGNOSTIC)
237 #define	MALLOC(space, cast, size, type, flags) \
238 	(space) = (cast)malloc((u_long)(size), type, flags)
239 #define FREE(addr, type) free((caddr_t)(addr), type)
240 
241 #else /* do not collect statistics */
242 #define	MALLOC(space, cast, size, type, flags) { \
243 	register struct kmembuckets *kbp = &bucket[BUCKETINDX(size)]; \
244 	long s = splimp(); \
245 	if (kbp->kb_next == NULL) { \
246 		(space) = (cast)malloc((u_long)(size), type, flags); \
247 	} else { \
248 		(space) = (cast)kbp->kb_next; \
249 		kbp->kb_next = *(caddr_t *)(space); \
250 	} \
251 	splx(s); \
252 }
253 
254 #define FREE(addr, type) { \
255 	register struct kmembuckets *kbp; \
256 	register struct kmemusage *kup = btokup(addr); \
257 	long s = splimp(); \
258 	if (1 << kup->ku_indx > MAXALLOCSAVE) { \
259 		free((caddr_t)(addr), type); \
260 	} else { \
261 		kbp = &bucket[kup->ku_indx]; \
262 		if (kbp->kb_next == NULL) \
263 			kbp->kb_next = (caddr_t)(addr); \
264 		else \
265 			*(caddr_t *)(kbp->kb_last) = (caddr_t)(addr); \
266 		*(caddr_t *)(addr) = NULL; \
267 		kbp->kb_last = (caddr_t)(addr); \
268 	} \
269 	splx(s); \
270 }
271 #endif /* do not collect statistics */
272 
273 extern struct kmemstats kmemstats[];
274 extern struct kmemusage *kmemusage;
275 extern char *kmembase;
276 extern struct kmembuckets bucket[];
277 extern void *malloc __P((unsigned long size, int type, int flags));
278 extern void free __P((void *addr, int type));
279 #endif /* KERNEL */
280 #endif /* !_SYS_MALLOC_H_ */
281