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