Commit | Line | Data |
---|---|---|
b688fc87 WJ |
1 | /* |
2 | * Copyright (c) 1987 Regents of the University of California. | |
3 | * 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 7.25 (Berkeley) 5/15/91 | |
34 | */ | |
35 | ||
36 | #ifndef _MALLOC_H_ | |
37 | #define _MALLOC_H_ | |
38 | ||
39 | #define KMEMSTATS | |
40 | ||
41 | /* | |
42 | * flags to malloc | |
43 | */ | |
44 | #define M_WAITOK 0x0000 | |
45 | #define M_NOWAIT 0x0001 | |
46 | ||
47 | /* | |
48 | * Types of memory to be allocated | |
49 | */ | |
50 | #define M_FREE 0 /* should be on free list */ | |
51 | #define M_MBUF 1 /* mbuf */ | |
52 | #define M_DEVBUF 2 /* device driver memory */ | |
53 | #define M_SOCKET 3 /* socket structure */ | |
54 | #define M_PCB 4 /* protocol control block */ | |
55 | #define M_RTABLE 5 /* routing tables */ | |
56 | #define M_HTABLE 6 /* IMP host tables */ | |
57 | #define M_FTABLE 7 /* fragment reassembly header */ | |
58 | #define M_ZOMBIE 8 /* zombie proc status */ | |
59 | #define M_IFADDR 9 /* interface address */ | |
60 | #define M_SOOPTS 10 /* socket options */ | |
61 | #define M_SONAME 11 /* socket name */ | |
62 | #define M_NAMEI 12 /* namei path name buffer */ | |
63 | #define M_GPROF 13 /* kernel profiling buffer */ | |
64 | #define M_IOCTLOPS 14 /* ioctl data buffer */ | |
65 | #define M_SUPERBLK 15 /* super block data */ | |
66 | #define M_CRED 16 /* credentials */ | |
67 | #define M_PGRP 17 /* process group header */ | |
68 | #define M_SESSION 18 /* session header */ | |
69 | #define M_IOV 19 /* large iov's */ | |
70 | #define M_MOUNT 20 /* vfs mount struct */ | |
71 | #define M_FHANDLE 21 /* network file handle */ | |
72 | #define M_NFSREQ 22 /* NFS request header */ | |
73 | #define M_NFSMNT 23 /* NFS mount structure */ | |
74 | #define M_VNODE 24 /* Dynamically allocated vnodes */ | |
75 | #define M_CACHE 25 /* Dynamically allocated cache entries */ | |
76 | #define M_DQUOT 26 /* UFS quota entries */ | |
77 | #define M_UFSMNT 27 /* UFS mount structure */ | |
78 | #define M_MAPMEM 28 /* mapped memory descriptors */ | |
79 | #define M_SHM 29 /* SVID compatible shared memory segments */ | |
80 | #define M_VMMAP 30 /* VM map structures */ | |
81 | #define M_VMMAPENT 31 /* VM map entry structures */ | |
82 | #define M_VMOBJ 32 /* VM object structure */ | |
83 | #define M_VMOBJHASH 33 /* VM object hash structure */ | |
84 | #define M_VMPMAP 34 /* VM pmap */ | |
85 | #define M_VMPVENT 35 /* VM phys-virt mapping entry */ | |
86 | #define M_VMPAGER 36 /* XXX: VM pager struct */ | |
87 | #define M_VMPGDATA 37 /* XXX: VM pager private data */ | |
88 | #define M_FILE 38 /* Open file structure */ | |
89 | #define M_FILEDESC 39 /* Open file descriptor table */ | |
90 | #define M_LOCKF 40 /* Byte-range locking structures */ | |
91 | #define M_PROC 41 /* Proc structures */ | |
92 | #define M_SUBPROC 42 /* Proc sub-structures */ | |
93 | #define M_TEMP 49 /* misc temporary data buffers */ | |
94 | #define M_LAST 50 | |
95 | ||
96 | #define INITKMEMNAMES { \ | |
97 | "free", /* 0 M_FREE */ \ | |
98 | "mbuf", /* 1 M_MBUF */ \ | |
99 | "devbuf", /* 2 M_DEVBUF */ \ | |
100 | "socket", /* 3 M_SOCKET */ \ | |
101 | "pcb", /* 4 M_PCB */ \ | |
102 | "routetbl", /* 5 M_RTABLE */ \ | |
103 | "hosttbl", /* 6 M_HTABLE */ \ | |
104 | "fragtbl", /* 7 M_FTABLE */ \ | |
105 | "zombie", /* 8 M_ZOMBIE */ \ | |
106 | "ifaddr", /* 9 M_IFADDR */ \ | |
107 | "soopts", /* 10 M_SOOPTS */ \ | |
108 | "soname", /* 11 M_SONAME */ \ | |
109 | "namei", /* 12 M_NAMEI */ \ | |
110 | "gprof", /* 13 M_GPROF */ \ | |
111 | "ioctlops", /* 14 M_IOCTLOPS */ \ | |
112 | "superblk", /* 15 M_SUPERBLK */ \ | |
113 | "cred", /* 16 M_CRED */ \ | |
114 | "pgrp", /* 17 M_PGRP */ \ | |
115 | "session", /* 18 M_SESSION */ \ | |
116 | "iov", /* 19 M_IOV */ \ | |
117 | "mount", /* 20 M_MOUNT */ \ | |
118 | "fhandle", /* 21 M_FHANDLE */ \ | |
119 | "NFS req", /* 22 M_NFSREQ */ \ | |
120 | "NFS mount", /* 23 M_NFSMNT */ \ | |
121 | "vnodes", /* 24 M_VNODE */ \ | |
122 | "namecache", /* 25 M_CACHE */ \ | |
123 | "UFS quota", /* 26 M_DQUOT */ \ | |
124 | "UFS mount", /* 27 M_UFSMNT */ \ | |
125 | "mapmem", /* 28 M_MAPMEM */ \ | |
126 | "shm", /* 29 M_SHM */ \ | |
127 | "VM map", /* 30 M_VMMAP */ \ | |
128 | "VM mapent", /* 31 M_VMMAPENT */ \ | |
129 | "VM object", /* 32 M_VMOBJ */ \ | |
130 | "VM objhash", /* 33 M_VMOBJHASH */ \ | |
131 | "VM pmap", /* 34 M_VMPMAP */ \ | |
132 | "VM pvmap", /* 35 M_VMPVENT */ \ | |
133 | "VM pager", /* 36 M_VMPAGER */ \ | |
134 | "VM pgdata", /* 37 M_VMPGDATA */ \ | |
135 | "file", /* 38 M_FILE */ \ | |
136 | "file desc", /* 39 M_FILEDESC */ \ | |
137 | "lockf", /* 40 M_LOCKF */ \ | |
138 | "proc", /* 41 M_PROC */ \ | |
139 | "subproc", /* 42 M_PROC */ \ | |
140 | 0, 0, 0, 0, 0, 0, \ | |
141 | "temp", /* 49 M_TEMP */ \ | |
142 | } | |
143 | ||
144 | struct kmemstats { | |
145 | long ks_inuse; /* # of packets of this type currently in use */ | |
146 | long ks_calls; /* total packets of this type ever allocated */ | |
147 | long ks_memuse; /* total memory held in bytes */ | |
148 | u_short ks_limblocks; /* number of times blocked for hitting limit */ | |
149 | u_short ks_mapblocks; /* number of times blocked for kernel map */ | |
150 | long ks_maxused; /* maximum number ever used */ | |
151 | long ks_limit; /* most that are allowed to exist */ | |
152 | }; | |
153 | ||
154 | /* | |
155 | * Array of descriptors that describe the contents of each page | |
156 | */ | |
157 | struct kmemusage { | |
158 | short ku_indx; /* bucket index */ | |
159 | union { | |
160 | u_short freecnt;/* for small allocations, free pieces in page */ | |
161 | u_short pagecnt;/* for large allocations, pages alloced */ | |
162 | } ku_un; | |
163 | }; | |
164 | #define ku_freecnt ku_un.freecnt | |
165 | #define ku_pagecnt ku_un.pagecnt | |
166 | ||
167 | /* | |
168 | * Set of buckets for each size of memory block that is retained | |
169 | */ | |
170 | struct kmembuckets { | |
171 | caddr_t kb_next; /* list of free blocks */ | |
172 | long kb_calls; /* total calls to allocate this size */ | |
173 | long kb_total; /* total number of blocks allocated */ | |
174 | long kb_totalfree; /* # of free elements in this bucket */ | |
175 | long kb_elmpercl; /* # of elements in this sized allocation */ | |
176 | long kb_highwat; /* high water mark */ | |
177 | long kb_couldfree; /* over high water mark and could free */ | |
178 | }; | |
179 | ||
180 | #ifdef KERNEL | |
181 | #define MINALLOCSIZE (1 << MINBUCKET) | |
182 | #define BUCKETINDX(size) \ | |
183 | (size) <= (MINALLOCSIZE * 128) \ | |
184 | ? (size) <= (MINALLOCSIZE * 8) \ | |
185 | ? (size) <= (MINALLOCSIZE * 2) \ | |
186 | ? (size) <= (MINALLOCSIZE * 1) \ | |
187 | ? (MINBUCKET + 0) \ | |
188 | : (MINBUCKET + 1) \ | |
189 | : (size) <= (MINALLOCSIZE * 4) \ | |
190 | ? (MINBUCKET + 2) \ | |
191 | : (MINBUCKET + 3) \ | |
192 | : (size) <= (MINALLOCSIZE* 32) \ | |
193 | ? (size) <= (MINALLOCSIZE * 16) \ | |
194 | ? (MINBUCKET + 4) \ | |
195 | : (MINBUCKET + 5) \ | |
196 | : (size) <= (MINALLOCSIZE * 64) \ | |
197 | ? (MINBUCKET + 6) \ | |
198 | : (MINBUCKET + 7) \ | |
199 | : (size) <= (MINALLOCSIZE * 2048) \ | |
200 | ? (size) <= (MINALLOCSIZE * 512) \ | |
201 | ? (size) <= (MINALLOCSIZE * 256) \ | |
202 | ? (MINBUCKET + 8) \ | |
203 | : (MINBUCKET + 9) \ | |
204 | : (size) <= (MINALLOCSIZE * 1024) \ | |
205 | ? (MINBUCKET + 10) \ | |
206 | : (MINBUCKET + 11) \ | |
207 | : (size) <= (MINALLOCSIZE * 8192) \ | |
208 | ? (size) <= (MINALLOCSIZE * 4096) \ | |
209 | ? (MINBUCKET + 12) \ | |
210 | : (MINBUCKET + 13) \ | |
211 | : (size) <= (MINALLOCSIZE * 16384) \ | |
212 | ? (MINBUCKET + 14) \ | |
213 | : (MINBUCKET + 15) | |
214 | ||
215 | /* | |
216 | * Turn virtual addresses into kmem map indicies | |
217 | */ | |
218 | #define kmemxtob(alloc) (kmembase + (alloc) * NBPG) | |
219 | #define btokmemx(addr) (((caddr_t)(addr) - kmembase) / NBPG) | |
220 | #define btokup(addr) (&kmemusage[((caddr_t)(addr) - kmembase) >> CLSHIFT]) | |
221 | ||
222 | /* | |
223 | * Macro versions for the usual cases of malloc/free | |
224 | */ | |
225 | #ifdef KMEMSTATS | |
226 | #define MALLOC(space, cast, size, type, flags) \ | |
227 | (space) = (cast)malloc((u_long)(size), type, flags) | |
228 | #define FREE(addr, type) free((caddr_t)(addr), type) | |
229 | ||
230 | #else /* do not collect statistics */ | |
231 | #define MALLOC(space, cast, size, type, flags) { \ | |
232 | register struct kmembuckets *kbp = &bucket[BUCKETINDX(size)]; \ | |
233 | long s = splimp(); \ | |
234 | if (kbp->kb_next == NULL) { \ | |
235 | (space) = (cast)malloc((u_long)(size), type, flags); \ | |
236 | } else { \ | |
237 | (space) = (cast)kbp->kb_next; \ | |
238 | kbp->kb_next = *(caddr_t *)(space); \ | |
239 | } \ | |
240 | splx(s); \ | |
241 | } | |
242 | ||
243 | #define FREE(addr, type) { \ | |
244 | register struct kmembuckets *kbp; \ | |
245 | register struct kmemusage *kup = btokup(addr); \ | |
246 | long s = splimp(); \ | |
247 | if (1 << kup->ku_indx > MAXALLOCSAVE) { \ | |
248 | free((caddr_t)(addr), type); \ | |
249 | } else { \ | |
250 | kbp = &bucket[kup->ku_indx]; \ | |
251 | *(caddr_t *)(addr) = kbp->kb_next; \ | |
252 | kbp->kb_next = (caddr_t)(addr); \ | |
253 | } \ | |
254 | splx(s); \ | |
255 | } | |
256 | #endif /* do not collect statistics */ | |
257 | ||
258 | extern struct kmemstats kmemstats[]; | |
259 | extern struct kmemusage *kmemusage; | |
260 | extern char *kmembase; | |
261 | extern struct kmembuckets bucket[]; | |
262 | extern void *malloc __P((unsigned long size, int type, int flags)); | |
263 | extern void free __P((void *addr, int type)); | |
264 | #endif /* KERNEL */ | |
265 | #endif /* !_MALLOC_H_ */ |