malloc.h revision 2893
1/*
2 * Copyright (c) 1987, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 *	@(#)malloc.h	8.3 (Berkeley) 1/12/94
34 * $Id: malloc.h,v 1.3 1994/08/02 07:53:10 davidg Exp $
35 */
36
37#ifndef _SYS_MALLOC_H_
38#define	_SYS_MALLOC_H_
39
40#define KMEMSTATS
41
42/*
43 * flags to malloc
44 */
45#define	M_WAITOK	0x0000
46#define	M_NOWAIT	0x0001
47
48/*
49 * Types of memory to be allocated
50 */
51#define	M_FREE		0	/* should be on free list */
52#define	M_MBUF		1	/* mbuf */
53#define	M_DEVBUF	2	/* device driver memory */
54#define	M_SOCKET	3	/* socket structure */
55#define	M_PCB		4	/* protocol control block */
56#define	M_RTABLE	5	/* routing tables */
57#define	M_HTABLE	6	/* IMP host tables */
58#define	M_FTABLE	7	/* fragment reassembly header */
59#define	M_ZOMBIE	8	/* zombie proc status */
60#define	M_IFADDR	9	/* interface address */
61#define	M_SOOPTS	10	/* socket options */
62#define	M_SONAME	11	/* socket name */
63#define	M_NAMEI		12	/* namei path name buffer */
64#define	M_GPROF		13	/* kernel profiling buffer */
65#define	M_IOCTLOPS	14	/* ioctl data buffer */
66#define	M_MAPMEM	15	/* mapped memory descriptors */
67#define	M_CRED		16	/* credentials */
68#define	M_PGRP		17	/* process group header */
69#define	M_SESSION	18	/* session header */
70#define	M_IOV		19	/* large iov's */
71#define	M_MOUNT		20	/* vfs mount struct */
72#define	M_FHANDLE	21	/* network file handle */
73#define	M_NFSREQ	22	/* NFS request header */
74#define	M_NFSMNT	23	/* NFS mount structure */
75#define	M_NFSNODE	24	/* NFS vnode private part */
76#define	M_VNODE		25	/* Dynamically allocated vnodes */
77#define	M_CACHE		26	/* Dynamically allocated cache entries */
78#define	M_DQUOT		27	/* UFS quota entries */
79#define	M_UFSMNT	28	/* UFS mount structure */
80#define	M_SHM		29	/* SVID compatible shared memory segments */
81#define	M_VMMAP		30	/* VM map structures */
82#define	M_VMMAPENT	31	/* VM map entry structures */
83#define	M_VMOBJ		32	/* VM object structure */
84#define	M_VMOBJHASH	33	/* VM object hash structure */
85#define	M_VMPMAP	34	/* VM pmap */
86#define	M_VMPVENT	35	/* VM phys-virt mapping entry */
87#define	M_VMPAGER	36	/* XXX: VM pager struct */
88#define	M_VMPGDATA	37	/* XXX: VM pager private data */
89#define	M_FILE		38	/* Open file structure */
90#define	M_FILEDESC	39	/* Open file descriptor table */
91#define	M_LOCKF		40	/* Byte-range locking structures */
92#define	M_PROC		41	/* Proc structures */
93#define	M_SUBPROC	42	/* Proc sub-structures */
94#define	M_SEGMENT	43	/* Segment for LFS */
95#define	M_LFSNODE	44	/* LFS vnode private part */
96#define	M_FFSNODE	45	/* FFS vnode private part */
97#define	M_MFSNODE	46	/* MFS vnode private part */
98#define	M_NQLEASE	47	/* Nqnfs lease */
99#define	M_NQMHOST	48	/* Nqnfs host address table */
100#define	M_NETADDR	49	/* Export host address structure */
101#define	M_NFSSVC	50	/* Nfs server structure */
102#define	M_NFSUID	51	/* Nfs uid mapping structure */
103#define	M_NFSD		52	/* Nfs server daemon structure */
104#define	M_IPMOPTS	53	/* internet multicast options */
105#define	M_IPMADDR	54	/* internet multicast address */
106#define	M_IFMADDR	55	/* link-level multicast address */
107#define	M_MRTABLE	56	/* multicast routing tables */
108#define M_ISOFSMNT	57	/* ISOFS mount structure */
109#define M_ISOFSNODE	58	/* ISOFS vnode private part */
110#define M_MSDOSFSMNT	59	/* MSDOSFS mount structure */
111#define M_MSDOSFSNODE	60	/* MSDOSFS vnode private part */
112#define M_MSDOSFSFAT	61	/* MSDOSFS file allocation table */
113#define	M_TEMP		74	/* misc temporary data buffers */
114#define M_TTYS		75	/* tty data structures */
115#define	M_LAST		76	/* Must be last type + 1 */
116
117#define INITKMEMNAMES { \
118	"free",		/* 0 M_FREE */ \
119	"mbuf",		/* 1 M_MBUF */ \
120	"devbuf",	/* 2 M_DEVBUF */ \
121	"socket",	/* 3 M_SOCKET */ \
122	"pcb",		/* 4 M_PCB */ \
123	"routetbl",	/* 5 M_RTABLE */ \
124	"hosttbl",	/* 6 M_HTABLE */ \
125	"fragtbl",	/* 7 M_FTABLE */ \
126	"zombie",	/* 8 M_ZOMBIE */ \
127	"ifaddr",	/* 9 M_IFADDR */ \
128	"soopts",	/* 10 M_SOOPTS */ \
129	"soname",	/* 11 M_SONAME */ \
130	"namei",	/* 12 M_NAMEI */ \
131	"gprof",	/* 13 M_GPROF */ \
132	"ioctlops",	/* 14 M_IOCTLOPS */ \
133	"mapmem",	/* 15 M_MAPMEM */ \
134	"cred",		/* 16 M_CRED */ \
135	"pgrp",		/* 17 M_PGRP */ \
136	"session",	/* 18 M_SESSION */ \
137	"iov",		/* 19 M_IOV */ \
138	"mount",	/* 20 M_MOUNT */ \
139	"fhandle",	/* 21 M_FHANDLE */ \
140	"NFS req",	/* 22 M_NFSREQ */ \
141	"NFS mount",	/* 23 M_NFSMNT */ \
142	"NFS node",	/* 24 M_NFSNODE */ \
143	"vnodes",	/* 25 M_VNODE */ \
144	"namecache",	/* 26 M_CACHE */ \
145	"UFS quota",	/* 27 M_DQUOT */ \
146	"UFS mount",	/* 28 M_UFSMNT */ \
147	"shm",		/* 29 M_SHM */ \
148	"VM map",	/* 30 M_VMMAP */ \
149	"VM mapent",	/* 31 M_VMMAPENT */ \
150	"VM object",	/* 32 M_VMOBJ */ \
151	"VM objhash",	/* 33 M_VMOBJHASH */ \
152	"VM pmap",	/* 34 M_VMPMAP */ \
153	"VM pvmap",	/* 35 M_VMPVENT */ \
154	"VM pager",	/* 36 M_VMPAGER */ \
155	"VM pgdata",	/* 37 M_VMPGDATA */ \
156	"file",		/* 38 M_FILE */ \
157	"file desc",	/* 39 M_FILEDESC */ \
158	"lockf",	/* 40 M_LOCKF */ \
159	"proc",		/* 41 M_PROC */ \
160	"subproc",	/* 42 M_SUBPROC */ \
161	"LFS segment",	/* 43 M_SEGMENT */ \
162	"LFS node",	/* 44 M_LFSNODE */ \
163	"FFS node",	/* 45 M_FFSNODE */ \
164	"MFS node",	/* 46 M_MFSNODE */ \
165	"NQNFS Lease",	/* 47 M_NQLEASE */ \
166	"NQNFS Host",	/* 48 M_NQMHOST */ \
167	"Export Host",	/* 49 M_NETADDR */ \
168	"NFS srvsock",	/* 50 M_NFSSVC */ \
169	"NFS uid",	/* 51 M_NFSUID */ \
170	"NFS daemon",	/* 52 M_NFSD */ \
171	"ip_moptions",	/* 53 M_IPMOPTS */ \
172	"in_multi",	/* 54 M_IPMADDR */ \
173	"ether_multi",	/* 55 M_IFMADDR */ \
174	"mrt",		/* 56 M_MRTABLE */ \
175	"ISOFS mount",	/* 57 M_ISOFSMNT */ \
176	"ISOFS node",	/* 58 M_ISOFSNODE */ \
177	"MSDOSFS mount",/* 59 M_MSDOSFSMNT */ \
178	"MSDOSFS node",	/* 60 M_MSDOSFSNODE */ \
179	"MSDOSFS FAT",  /* 61 M_MSDOSFSFAR */ \
180	NULL, NULL, NULL, NULL, NULL, \
181	NULL, NULL, NULL, NULL, NULL, \
182	NULL, NULL, \
183	"temp",		/* 74 M_TEMP */ \
184	"ttys",		/* 75 M_TTYS */ \
185}
186
187struct kmemstats {
188	long	ks_inuse;	/* # of packets of this type currently in use */
189	long	ks_calls;	/* total packets of this type ever allocated */
190	long 	ks_memuse;	/* total memory held in bytes */
191	u_short	ks_limblocks;	/* number of times blocked for hitting limit */
192	u_short	ks_mapblocks;	/* number of times blocked for kernel map */
193	long	ks_maxused;	/* maximum number ever used */
194	long	ks_limit;	/* most that are allowed to exist */
195	long	ks_size;	/* sizes of this thing that are allocated */
196	long	ks_spare;
197};
198
199/*
200 * Array of descriptors that describe the contents of each page
201 */
202struct kmemusage {
203	short ku_indx;		/* bucket index */
204	union {
205		u_short freecnt;/* for small allocations, free pieces in page */
206		u_short pagecnt;/* for large allocations, pages alloced */
207	} ku_un;
208};
209#define ku_freecnt ku_un.freecnt
210#define ku_pagecnt ku_un.pagecnt
211
212/*
213 * Set of buckets for each size of memory block that is retained
214 */
215struct kmembuckets {
216	caddr_t kb_next;	/* list of free blocks */
217	caddr_t kb_last;	/* last free block */
218	long	kb_calls;	/* total calls to allocate this size */
219	long	kb_total;	/* total number of blocks allocated */
220	long	kb_totalfree;	/* # of free elements in this bucket */
221	long	kb_elmpercl;	/* # of elements in this sized allocation */
222	long	kb_highwat;	/* high water mark */
223	long	kb_couldfree;	/* over high water mark and could free */
224};
225
226#ifdef KERNEL
227#define	MINALLOCSIZE	(1 << MINBUCKET)
228#define BUCKETINDX(size) \
229	(size) <= (MINALLOCSIZE * 128) \
230		? (size) <= (MINALLOCSIZE * 8) \
231			? (size) <= (MINALLOCSIZE * 2) \
232				? (size) <= (MINALLOCSIZE * 1) \
233					? (MINBUCKET + 0) \
234					: (MINBUCKET + 1) \
235				: (size) <= (MINALLOCSIZE * 4) \
236					? (MINBUCKET + 2) \
237					: (MINBUCKET + 3) \
238			: (size) <= (MINALLOCSIZE* 32) \
239				? (size) <= (MINALLOCSIZE * 16) \
240					? (MINBUCKET + 4) \
241					: (MINBUCKET + 5) \
242				: (size) <= (MINALLOCSIZE * 64) \
243					? (MINBUCKET + 6) \
244					: (MINBUCKET + 7) \
245		: (size) <= (MINALLOCSIZE * 2048) \
246			? (size) <= (MINALLOCSIZE * 512) \
247				? (size) <= (MINALLOCSIZE * 256) \
248					? (MINBUCKET + 8) \
249					: (MINBUCKET + 9) \
250				: (size) <= (MINALLOCSIZE * 1024) \
251					? (MINBUCKET + 10) \
252					: (MINBUCKET + 11) \
253			: (size) <= (MINALLOCSIZE * 8192) \
254				? (size) <= (MINALLOCSIZE * 4096) \
255					? (MINBUCKET + 12) \
256					: (MINBUCKET + 13) \
257				: (size) <= (MINALLOCSIZE * 16384) \
258					? (MINBUCKET + 14) \
259					: (MINBUCKET + 15)
260
261/*
262 * Turn virtual addresses into kmem map indicies
263 */
264#define kmemxtob(alloc)	(kmembase + (alloc) * NBPG)
265#define btokmemx(addr)	(((caddr_t)(addr) - kmembase) / NBPG)
266#define btokup(addr)	(&kmemusage[((caddr_t)(addr) - kmembase) >> CLSHIFT])
267
268/*
269 * Macro versions for the usual cases of malloc/free
270 */
271#if defined(KMEMSTATS) || defined(DIAGNOSTIC)
272#define	MALLOC(space, cast, size, type, flags) \
273	(space) = (cast)malloc((u_long)(size), type, flags)
274#define FREE(addr, type) free((caddr_t)(addr), type)
275
276#else /* do not collect statistics */
277#define	MALLOC(space, cast, size, type, flags) { \
278	register struct kmembuckets *kbp = &bucket[BUCKETINDX(size)]; \
279	long s = splimp(); \
280	if (kbp->kb_next == NULL) { \
281		(space) = (cast)malloc((u_long)(size), type, flags); \
282	} else { \
283		(space) = (cast)kbp->kb_next; \
284		kbp->kb_next = *(caddr_t *)(space); \
285	} \
286	splx(s); \
287}
288
289#define FREE(addr, type) { \
290	register struct kmembuckets *kbp; \
291	register struct kmemusage *kup = btokup(addr); \
292	long s = splimp(); \
293	if (1 << kup->ku_indx > MAXALLOCSAVE) { \
294		free((caddr_t)(addr), type); \
295	} else { \
296		kbp = &bucket[kup->ku_indx]; \
297		if (kbp->kb_next == NULL) \
298			kbp->kb_next = (caddr_t)(addr); \
299		else \
300			*(caddr_t *)(kbp->kb_last) = (caddr_t)(addr); \
301		*(caddr_t *)(addr) = NULL; \
302		kbp->kb_last = (caddr_t)(addr); \
303	} \
304	splx(s); \
305}
306#endif /* do not collect statistics */
307
308extern struct kmemstats kmemstats[];
309extern struct kmemusage *kmemusage;
310extern char *kmembase;
311extern struct kmembuckets bucket[];
312extern void *malloc __P((unsigned long size, int type, int flags));
313extern void free __P((void *addr, int type));
314#endif /* KERNEL */
315#endif /* !_SYS_MALLOC_H_ */
316