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