]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. | |
7 | * | |
8 | * This file contains Original Code and/or Modifications of Original Code | |
9 | * as defined in and that are subject to the Apple Public Source License | |
10 | * Version 2.0 (the 'License'). You may not use this file except in | |
11 | * compliance with the License. Please obtain a copy of the License at | |
12 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
13 | * file. | |
14 | * | |
15 | * The Original Code and all software distributed under the License are | |
16 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
17 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
18 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
19 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
20 | * Please see the License for the specific language governing rights and | |
21 | * limitations under the License. | |
22 | * | |
23 | * @APPLE_LICENSE_HEADER_END@ | |
24 | */ | |
25 | /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */ | |
26 | /* | |
27 | * Copyright (c) 1987, 1991, 1993 | |
28 | * The Regents of the University of California. All rights reserved. | |
29 | * | |
30 | * Redistribution and use in source and binary forms, with or without | |
31 | * modification, are permitted provided that the following conditions | |
32 | * are met: | |
33 | * 1. Redistributions of source code must retain the above copyright | |
34 | * notice, this list of conditions and the following disclaimer. | |
35 | * 2. Redistributions in binary form must reproduce the above copyright | |
36 | * notice, this list of conditions and the following disclaimer in the | |
37 | * documentation and/or other materials provided with the distribution. | |
38 | * 3. All advertising materials mentioning features or use of this software | |
39 | * must display the following acknowledgement: | |
40 | * This product includes software developed by the University of | |
41 | * California, Berkeley and its contributors. | |
42 | * 4. Neither the name of the University nor the names of its contributors | |
43 | * may be used to endorse or promote products derived from this software | |
44 | * without specific prior written permission. | |
45 | * | |
46 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
47 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
48 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
49 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
50 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
51 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
52 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
53 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
54 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
55 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
56 | * SUCH DAMAGE. | |
57 | * | |
58 | * @(#)kern_malloc.c 8.4 (Berkeley) 5/20/95 | |
59 | */ | |
60 | ||
61 | #include <sys/param.h> | |
62 | #include <sys/malloc.h> | |
63 | ||
64 | #include <sys/socket.h> | |
65 | #include <sys/socketvar.h> | |
66 | ||
67 | #include <net/route.h> | |
68 | ||
69 | #include <netinet/in.h> | |
70 | #include <netinet/in_systm.h> | |
71 | #include <netinet/ip.h> | |
72 | #include <netinet/in_pcb.h> | |
73 | ||
74 | #include <sys/proc.h> | |
75 | #include <sys/mount.h> | |
76 | #include <sys/vnode.h> | |
77 | #include <sys/namei.h> | |
78 | #include <sys/file.h> | |
79 | #include <sys/filedesc.h> | |
80 | #include <sys/tty.h> | |
81 | #include <sys/quota.h> | |
82 | ||
83 | #include <ufs/ufs/inode.h> | |
84 | ||
85 | #include <hfs/hfs_cnode.h> | |
86 | #include <isofs/cd9660/cd9660_node.h> | |
87 | ||
88 | #include <miscfs/volfs/volfs.h> | |
89 | ||
90 | #include <nfs/rpcv2.h> | |
91 | #include <nfs/nfsproto.h> | |
92 | #include <nfs/nfsnode.h> | |
93 | #include <nfs/nfsmount.h> | |
94 | #include <nfs/nqnfs.h> | |
95 | ||
96 | #include <vfs/vfs_journal.h> | |
97 | ||
98 | #include <mach/mach_types.h> | |
99 | ||
100 | #include <kern/zalloc.h> | |
101 | #include <kern/kalloc.h> | |
102 | ||
103 | struct kmemstats kmemstats[M_LAST]; | |
104 | char *memname[] = INITKMEMNAMES; | |
105 | ||
106 | struct kmzones { | |
107 | size_t kz_elemsize; | |
108 | void *kz_zalloczone; | |
109 | #define KMZ_CREATEZONE ((void *)-2) | |
110 | #define KMZ_LOOKUPZONE ((void *)-1) | |
111 | #define KMZ_MALLOC ((void *)0) | |
112 | #define KMZ_SHAREZONE ((void *)1) | |
113 | } kmzones[M_LAST] = { | |
114 | #define SOS(sname) sizeof (struct sname) | |
115 | #define SOX(sname) -1 | |
116 | -1, 0, /* 0 M_FREE */ | |
117 | MSIZE, KMZ_CREATEZONE, /* 1 M_MBUF */ | |
118 | 0, KMZ_MALLOC, /* 2 M_DEVBUF */ | |
119 | SOS(socket), KMZ_CREATEZONE, /* 3 M_SOCKET */ | |
120 | SOS(inpcb), KMZ_LOOKUPZONE, /* 4 M_PCB */ | |
121 | M_MBUF, KMZ_SHAREZONE, /* 5 M_RTABLE */ | |
122 | M_MBUF, KMZ_SHAREZONE, /* 6 M_HTABLE */ | |
123 | M_MBUF, KMZ_SHAREZONE, /* 7 M_FTABLE */ | |
124 | SOS(rusage), KMZ_CREATEZONE, /* 8 M_ZOMBIE */ | |
125 | 0, KMZ_MALLOC, /* 9 M_IFADDR */ | |
126 | M_MBUF, KMZ_SHAREZONE, /* 10 M_SOOPTS */ | |
127 | 0, KMZ_MALLOC, /* 11 M_SONAME */ | |
128 | MAXPATHLEN, KMZ_CREATEZONE, /* 12 M_NAMEI */ | |
129 | 0, KMZ_MALLOC, /* 13 M_GPROF */ | |
130 | 0, KMZ_MALLOC, /* 14 M_IOCTLOPS */ | |
131 | 0, KMZ_MALLOC, /* 15 M_MAPMEM */ | |
132 | SOS(ucred), KMZ_CREATEZONE, /* 16 M_CRED */ | |
133 | SOS(pgrp), KMZ_CREATEZONE, /* 17 M_PGRP */ | |
134 | SOS(session), KMZ_CREATEZONE, /* 18 M_SESSION */ | |
135 | SOS(iovec), KMZ_LOOKUPZONE, /* 19 M_IOV */ | |
136 | SOS(mount), KMZ_CREATEZONE, /* 20 M_MOUNT */ | |
137 | 0, KMZ_MALLOC, /* 21 M_FHANDLE */ | |
138 | SOS(nfsreq), KMZ_CREATEZONE, /* 22 M_NFSREQ */ | |
139 | SOS(nfsmount), KMZ_CREATEZONE, /* 23 M_NFSMNT */ | |
140 | SOS(nfsnode), KMZ_CREATEZONE, /* 24 M_NFSNODE */ | |
141 | SOS(vnode), KMZ_CREATEZONE, /* 25 M_VNODE */ | |
142 | SOS(namecache), KMZ_CREATEZONE, /* 26 M_CACHE */ | |
143 | SOX(dquot), KMZ_LOOKUPZONE, /* 27 M_DQUOT */ | |
144 | SOX(ufsmount), KMZ_LOOKUPZONE, /* 28 M_UFSMNT */ | |
145 | 0, KMZ_MALLOC, /* 29 M_CGSUM */ | |
146 | 0, KMZ_MALLOC, /* 30 M_VMMAP */ | |
147 | 0, KMZ_MALLOC, /* 31 M_VMMAPENT */ | |
148 | 0, KMZ_MALLOC, /* 32 M_VMOBJ */ | |
149 | 0, KMZ_MALLOC, /* 33 M_VMOBJHASH */ | |
150 | 0, KMZ_MALLOC, /* 34 M_VMPMAP */ | |
151 | 0, KMZ_MALLOC, /* 35 M_VMPVENT */ | |
152 | 0, KMZ_MALLOC, /* 36 M_VMPAGER */ | |
153 | 0, KMZ_MALLOC, /* 37 M_VMPGDATA */ | |
154 | SOS(file), KMZ_CREATEZONE, /* 38 M_FILE */ | |
155 | SOS(filedesc), KMZ_CREATEZONE, /* 39 M_FILEDESC */ | |
156 | SOX(lockf), KMZ_CREATEZONE, /* 40 M_LOCKF */ | |
157 | SOS(proc), KMZ_CREATEZONE, /* 41 M_PROC */ | |
158 | SOS(pcred), KMZ_CREATEZONE, /* 42 M_SUBPROC */ | |
159 | 0, KMZ_MALLOC, /* 43 M_SEGMENT */ | |
160 | M_FFSNODE, KMZ_SHAREZONE, /* 44 M_LFSNODE */ | |
161 | SOS(inode), KMZ_CREATEZONE, /* 45 M_FFSNODE */ | |
162 | M_FFSNODE, KMZ_SHAREZONE, /* 46 M_MFSNODE */ | |
163 | SOS(nqlease), KMZ_CREATEZONE, /* 47 M_NQLEASE */ | |
164 | SOS(nqm), KMZ_CREATEZONE, /* 48 M_NQMHOST */ | |
165 | 0, KMZ_MALLOC, /* 49 M_NETADDR */ | |
166 | SOX(nfssvc_sock), | |
167 | KMZ_CREATEZONE, /* 50 M_NFSSVC */ | |
168 | SOS(nfsuid), KMZ_CREATEZONE, /* 51 M_NFSUID */ | |
169 | SOX(nfsrvcache), | |
170 | KMZ_CREATEZONE, /* 52 M_NFSD */ | |
171 | SOX(ip_moptions), | |
172 | KMZ_LOOKUPZONE, /* 53 M_IPMOPTS */ | |
173 | SOX(in_multi), KMZ_LOOKUPZONE, /* 54 M_IPMADDR */ | |
174 | SOX(ether_multi), | |
175 | KMZ_LOOKUPZONE, /* 55 M_IFMADDR */ | |
176 | SOX(mrt), KMZ_CREATEZONE, /* 56 M_MRTABLE */ | |
177 | SOX(iso_mnt), KMZ_LOOKUPZONE, /* 57 M_ISOFSMNT */ | |
178 | SOS(iso_node), KMZ_CREATEZONE, /* 58 M_ISOFSNODE */ | |
179 | SOS(nfsrv_descript), | |
180 | KMZ_CREATEZONE, /* 59 M_NFSRVDESC */ | |
181 | SOS(nfsdmap), KMZ_CREATEZONE, /* 60 M_NFSDIROFF */ | |
182 | SOS(fhandle), KMZ_LOOKUPZONE, /* 61 M_NFSBIGFH */ | |
183 | 0, KMZ_MALLOC, /* 62 M_MSDOSFSMNT */ | |
184 | 0, KMZ_MALLOC, /* 63 M_MSDOSFSFAT */ | |
185 | 0, KMZ_MALLOC, /* 64 M_MSDOSFSNODE */ | |
186 | SOS(tty), KMZ_CREATEZONE, /* 65 M_TTYS */ | |
187 | 0, KMZ_MALLOC, /* 66 M_EXEC */ | |
188 | 0, KMZ_MALLOC, /* 67 M_MISCFSMNT */ | |
189 | 0, KMZ_MALLOC, /* 68 M_MISCFSNODE */ | |
190 | 0, KMZ_MALLOC, /* 69 M_ADOSFSMNT */ | |
191 | 0, KMZ_MALLOC, /* 70 M_ADOSFSNODE */ | |
192 | 0, KMZ_MALLOC, /* 71 M_ANODE */ | |
193 | SOX(buf), KMZ_CREATEZONE, /* 72 M_BUFHDR */ | |
194 | (NDFILE * OFILESIZE), | |
195 | KMZ_CREATEZONE, /* 73 M_OFILETABL */ | |
196 | MCLBYTES, KMZ_CREATEZONE, /* 74 M_MCLUST */ | |
197 | SOX(hfsmount), KMZ_LOOKUPZONE, /* 75 M_HFSMNT */ | |
198 | SOS(cnode), KMZ_CREATEZONE, /* 76 M_HFSNODE */ | |
199 | SOS(filefork), KMZ_CREATEZONE, /* 77 M_HFSFORK */ | |
200 | SOX(volfs_mntdata), KMZ_LOOKUPZONE, /* 78 M_VOLFSMNT */ | |
201 | SOS(volfs_vndata), KMZ_CREATEZONE, /* 79 M_VOLFSNODE */ | |
202 | 0, KMZ_MALLOC, /* 80 M_TEMP */ | |
203 | 0, KMZ_MALLOC, /* 81 M_SECA */ | |
204 | 0, KMZ_MALLOC, /* 82 M_DEVFS */ | |
205 | 0, KMZ_MALLOC, /* 83 M_IPFW */ | |
206 | 0, KMZ_MALLOC, /* 84 M_UDFNODE */ | |
207 | 0, KMZ_MALLOC, /* 85 M_UDFMOUNT */ | |
208 | 0, KMZ_MALLOC, /* 86 M_IP6NDP */ | |
209 | 0, KMZ_MALLOC, /* 87 M_IP6OPT */ | |
210 | 0, KMZ_MALLOC, /* 88 M_IP6MISC */ | |
211 | 0, KMZ_MALLOC, /* 89 M_TSEGQ */ | |
212 | 0, KMZ_MALLOC, /* 90 M_IGMP */ | |
213 | SOS(journal), KMZ_CREATEZONE, /* 91 M_JNL_JNL */ | |
214 | SOS(transaction), KMZ_CREATEZONE, /* 92 M_JNL_TR */ | |
215 | #undef SOS | |
216 | #undef SOX | |
217 | }; | |
218 | ||
219 | ||
220 | /* | |
221 | * Initialize the kernel memory allocator | |
222 | */ | |
223 | void | |
224 | kmeminit(void) | |
225 | { | |
226 | struct kmzones *kmz; | |
227 | ||
228 | if ((sizeof(kmzones)/sizeof(kmzones[0])) != (sizeof(memname)/sizeof(memname[0]))) { | |
229 | panic("kmeminit: kmzones has %d elements but memname has %d\n", | |
230 | (sizeof(kmzones)/sizeof(kmzones[0])), (sizeof(memname)/sizeof(memname[0]))); | |
231 | } | |
232 | ||
233 | kmz = kmzones; | |
234 | while (kmz < &kmzones[M_LAST]) { | |
235 | /* XXX */ | |
236 | if (kmz->kz_elemsize == -1) | |
237 | ; | |
238 | else | |
239 | /* XXX */ | |
240 | if (kmz->kz_zalloczone == KMZ_CREATEZONE) { | |
241 | kmz->kz_zalloczone = zinit(kmz->kz_elemsize, | |
242 | 1024 * 1024, PAGE_SIZE, | |
243 | memname[kmz - kmzones]); | |
244 | } | |
245 | else if (kmz->kz_zalloczone == KMZ_LOOKUPZONE) | |
246 | kmz->kz_zalloczone = kalloc_zone(kmz->kz_elemsize); | |
247 | ||
248 | kmz++; | |
249 | } | |
250 | ||
251 | kmz = kmzones; | |
252 | while (kmz < &kmzones[M_LAST]) { | |
253 | /* XXX */ | |
254 | if (kmz->kz_elemsize == -1) | |
255 | ; | |
256 | else | |
257 | /* XXX */ | |
258 | if (kmz->kz_zalloczone == KMZ_SHAREZONE) { | |
259 | kmz->kz_zalloczone = | |
260 | kmzones[kmz->kz_elemsize].kz_zalloczone; | |
261 | kmz->kz_elemsize = | |
262 | kmzones[kmz->kz_elemsize].kz_elemsize; | |
263 | } | |
264 | ||
265 | kmz++; | |
266 | } | |
267 | } | |
268 | ||
269 | #define MDECL(reqlen) \ | |
270 | union { \ | |
271 | struct _mhead hdr; \ | |
272 | char _m[(reqlen) + sizeof (struct _mhead)]; \ | |
273 | } | |
274 | ||
275 | struct _mhead { | |
276 | size_t mlen; | |
277 | char dat[0]; | |
278 | }; | |
279 | ||
280 | #define ZEROSIZETOKEN 0xFADEDFAD | |
281 | ||
282 | void *_MALLOC( | |
283 | size_t size, | |
284 | int type, | |
285 | int flags) | |
286 | { | |
287 | MDECL(size) *mem; | |
288 | size_t memsize = sizeof (*mem); | |
289 | ||
290 | if (type >= M_LAST) | |
291 | panic("_malloc TYPE"); | |
292 | ||
293 | /* | |
294 | * On zero request we do not return zero as that | |
295 | * could be mistaken for ENOMEM. | |
296 | */ | |
297 | if (size == 0) | |
298 | return (ZEROSIZETOKEN); | |
299 | ||
300 | if (flags & M_NOWAIT) { | |
301 | mem = (void *)kalloc_noblock(memsize); | |
302 | } else { | |
303 | mem = (void *)kalloc(memsize); | |
304 | } | |
305 | if (!mem) | |
306 | return (0); | |
307 | ||
308 | mem->hdr.mlen = memsize; | |
309 | ||
310 | return (mem->hdr.dat); | |
311 | } | |
312 | ||
313 | void _FREE( | |
314 | void *addr, | |
315 | int type) | |
316 | { | |
317 | struct _mhead *hdr; | |
318 | ||
319 | if (type >= M_LAST) | |
320 | panic("_free TYPE"); | |
321 | ||
322 | if (addr == (void *)ZEROSIZETOKEN) | |
323 | return; | |
324 | if (!addr) | |
325 | return; /* correct (convenient bsd kernel legacy) */ | |
326 | ||
327 | hdr = addr; hdr--; | |
328 | kfree((vm_offset_t)hdr, hdr->mlen); | |
329 | } | |
330 | ||
331 | void *_MALLOC_ZONE( | |
332 | size_t size, | |
333 | int type, | |
334 | int flags) | |
335 | { | |
336 | struct kmzones *kmz; | |
337 | void *elem; | |
338 | ||
339 | if (type >= M_LAST) | |
340 | panic("_malloc_zone TYPE"); | |
341 | ||
342 | kmz = &kmzones[type]; | |
343 | if (kmz->kz_zalloczone == KMZ_MALLOC) | |
344 | panic("_malloc_zone ZONE"); | |
345 | ||
346 | /* XXX */ | |
347 | if (kmz->kz_elemsize == -1) | |
348 | panic("_malloc_zone XXX"); | |
349 | /* XXX */ | |
350 | if (size == kmz->kz_elemsize) | |
351 | if (flags & M_NOWAIT) { | |
352 | elem = (void *)zalloc_noblock(kmz->kz_zalloczone); | |
353 | } else { | |
354 | elem = (void *)zalloc(kmz->kz_zalloczone); | |
355 | } | |
356 | else | |
357 | if (flags & M_NOWAIT) { | |
358 | elem = (void *)kalloc_noblock(size); | |
359 | } else { | |
360 | elem = (void *)kalloc(size); | |
361 | } | |
362 | ||
363 | return (elem); | |
364 | } | |
365 | ||
366 | void _FREE_ZONE( | |
367 | void *elem, | |
368 | size_t size, | |
369 | int type) | |
370 | { | |
371 | struct kmzones *kmz; | |
372 | ||
373 | if (type >= M_LAST) | |
374 | panic("FREE_SIZE"); | |
375 | ||
376 | kmz = &kmzones[type]; | |
377 | if (kmz->kz_zalloczone == KMZ_MALLOC) | |
378 | panic("free_zone ZONE"); | |
379 | ||
380 | /* XXX */ | |
381 | if (kmz->kz_elemsize == -1) | |
382 | panic("FREE_SIZE XXX"); | |
383 | /* XXX */ | |
384 | if (size == kmz->kz_elemsize) | |
385 | zfree(kmz->kz_zalloczone, (vm_offset_t)elem); | |
386 | else | |
387 | kfree((vm_offset_t)elem, size); | |
388 | } |