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