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