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