]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_malloc.c
xnu-1228.15.4.tar.gz
[apple/xnu.git] / bsd / kern / kern_malloc.c
1 /*
2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
29 /*
30 * Copyright (c) 1987, 1991, 1993
31 * The Regents of the University of California. All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)kern_malloc.c 8.4 (Berkeley) 5/20/95
62 */
63 /*
64 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
65 * support for mandatory and extensible security protections. This notice
66 * is included in support of clause 2.2 (b) of the Apple Public License,
67 * Version 2.0.
68 */
69
70 #include <sys/param.h>
71 #include <sys/malloc.h>
72
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
75
76 #include <net/route.h>
77
78 #include <netinet/in.h>
79 #include <netinet/in_systm.h>
80 #include <netinet/ip.h>
81 #include <netinet/in_pcb.h>
82
83 #include <sys/event.h>
84 #include <sys/eventvar.h>
85
86 #include <sys/proc_internal.h>
87 #include <sys/mount_internal.h>
88 #include <sys/vnode_internal.h>
89 #include <sys/ubc_internal.h>
90 #include <sys/namei.h>
91 #include <sys/file_internal.h>
92 #include <sys/filedesc.h>
93 #include <sys/tty.h>
94 #include <sys/quota.h>
95 #include <sys/uio_internal.h>
96 #include <sys/resourcevar.h>
97 #include <sys/signalvar.h>
98
99 #include <ufs/ufs/inode.h>
100
101 #include <hfs/hfs_cnode.h>
102 #include <isofs/cd9660/cd9660_node.h>
103
104 #include <miscfs/specfs/specdev.h>
105
106 #include <nfs/rpcv2.h>
107 #include <nfs/nfsproto.h>
108 #include <nfs/nfsnode.h>
109 #include <nfs/nfsmount.h>
110
111 #include <vfs/vfs_journal.h>
112
113 #include <mach/mach_types.h>
114
115 #include <kern/zalloc.h>
116 #include <kern/kalloc.h>
117
118 void kmeminit(void) __attribute__((section("__TEXT, initcode")));
119
120 /* Strings corresponding to types of memory.
121 * Must be in synch with the #defines is sys/malloc.h
122 * NOTE - the reason we pass null strings in some cases is to reduce of foot
123 * print as much as possible for systems where a tiny kernel is needed.
124 * todo - We should probably redsign this and use enums for our types and only
125 * include types needed for that configuration of the kernel. This can't be
126 * done without some kind of kpi since several types are hardwired and exported
127 * (for example see types M_HFSMNT, M_UDFMNT, M_TEMP, etc in sys/malloc.h)
128 */
129 const char *memname[] = {
130 "free", /* 0 M_FREE */
131 "mbuf", /* 1 M_MBUF */
132 "devbuf", /* 2 M_DEVBUF */
133 "socket", /* 3 M_SOCKET */
134 "pcb", /* 4 M_PCB */
135 "routetbl", /* 5 M_RTABLE */
136 "hosttbl", /* 6 M_HTABLE */
137 "fragtbl", /* 7 M_FTABLE */
138 "zombie", /* 8 M_ZOMBIE */
139 "ifaddr", /* 9 M_IFADDR */
140 "soopts", /* 10 M_SOOPTS */
141 "soname", /* 11 M_SONAME */
142 "namei", /* 12 M_NAMEI */
143 "gprof", /* 13 M_GPROF */
144 "ioctlops", /* 14 M_IOCTLOPS */
145 "mapmem", /* 15 M_MAPMEM */
146 "cred", /* 16 M_CRED */
147 "pgrp", /* 17 M_PGRP */
148 "session", /* 18 M_SESSION */
149 "iov32", /* 19 M_IOV32 */
150 "mount", /* 20 M_MOUNT */
151 "fhandle", /* 21 M_FHANDLE */
152 #if (NFSCLIENT || NFSSERVER)
153 "NFS req", /* 22 M_NFSREQ */
154 "NFS mount", /* 23 M_NFSMNT */
155 "NFS node", /* 24 M_NFSNODE */
156 #else
157 "", /* 22 M_NFSREQ */
158 "", /* 23 M_NFSMNT */
159 "", /* 24 M_NFSNODE */
160 #endif
161 "vnodes", /* 25 M_VNODE */
162 "namecache", /* 26 M_CACHE */
163 #if QUOTA
164 "UFS quota", /* 27 M_DQUOT */
165 #else
166 "", /* 27 M_DQUOT */
167 #endif
168 #if FFS
169 "UFS mount", /* 28 M_UFSMNT */
170 #else
171 "", /* 28 M_UFSMNT */
172 #endif
173 #if (SYSV_SEM || SYSV_MSG || SYSV_SHM)
174 "shm", /* 29 M_SHM */
175 #else
176 "", /* 29 M_SHM */
177 #endif
178 "plimit", /* 30 M_VMMAP */
179 "sigacts", /* 31 M_VMMAPENT */
180 "VM object", /* 32 M_VMOBJ */
181 "VM objhash", /* 33 M_VMOBJHASH */
182 "VM pmap", /* 34 M_VMPMAP */
183 "VM pvmap", /* 35 M_VMPVENT */
184 "VM pager", /* 36 M_VMPAGER */
185 "VM pgdata", /* 37 M_VMPGDATA */
186 "fileproc", /* 38 M_FILEPROC */
187 "file desc", /* 39 M_FILEDESC */
188 "lockf", /* 40 M_LOCKF */
189 "proc", /* 41 M_PROC */
190 "pstats", /* 42 M_SUBPROC */
191 "LFS segment", /* 43 M_SEGMENT */
192 "LFS node", /* 44 M_LFSNODE */
193 #if FFS
194 "FFS node", /* 45 M_FFSNODE */
195 #else
196 "", /* 45 M_FFSNODE */
197 #endif
198 "MFS node", /* 46 M_MFSNODE */
199 "NQNFS Lease", /* 47 M_NQLEASE */
200 "NQNFS Host", /* 48 M_NQMHOST */
201 "Export Host", /* 49 M_NETADDR */
202 #if (NFSCLIENT || NFSSERVER)
203 "NFS srvsock", /* 50 M_NFSSVC */
204 "NFS uid", /* 51 M_NFSUID */
205 "NFS daemon", /* 52 M_NFSD */
206 #else
207 "", /* 50 M_NFSSVC */
208 "", /* 51 M_NFSUID */
209 "", /* 52 M_NFSD */
210 #endif
211 "ip_moptions", /* 53 M_IPMOPTS */
212 "in_multi", /* 54 M_IPMADDR */
213 "ether_multi", /* 55 M_IFMADDR */
214 "mrt", /* 56 M_MRTABLE */
215 #if CD9660
216 "ISOFS mount", /* 57 M_ISOFSMNT */
217 "ISOFS node", /* 58 M_ISOFSNODE */
218 #else
219 "", /* 57 M_ISOFSMNT */
220 "", /* 58 M_ISOFSNODE */
221 #endif
222 #if (NFSCLIENT || NFSSERVER)
223 "NFSV3 srvdesc",/* 59 M_NFSRVDESC */
224 "NFSV3 diroff", /* 60 M_NFSDIROFF */
225 "NFSV3 bigfh", /* 61 M_NFSBIGFH */
226 #else
227 "", /* 59 M_NFSRVDESC */
228 "", /* 60 M_NFSDIROFF */
229 "", /* 61 M_NFSBIGFH */
230 #endif
231 "MSDOSFS mount",/* 62 M_MSDOSFSMNT */
232 "MSDOSFS fat", /* 63 M_MSDOSFSFAT */
233 "MSDOSFS node", /* 64 M_MSDOSFSNODE */
234 "ttys", /* 65 M_TTYS */
235 "exec", /* 66 M_EXEC */
236 "miscfs mount", /* 67 M_MISCFSMNT */
237 "miscfs node", /* 68 M_MISCFSNODE */
238 "adosfs mount", /* 69 M_ADOSFSMNT */
239 "adosfs node", /* 70 M_ADOSFSNODE */
240 "adosfs anode", /* 71 M_ANODE */
241 "buf hdrs", /* 72 M_BUFHDR */
242 "ofile tabl", /* 73 M_OFILETABL */
243 "mbuf clust", /* 74 M_MCLUST */
244 #if HFS
245 "HFS mount", /* 75 M_HFSMNT */
246 "HFS node", /* 76 M_HFSNODE */
247 "HFS fork", /* 77 M_HFSFORK */
248 #else
249 "", /* 75 M_HFSMNT */
250 "", /* 76 M_HFSNODE */
251 "", /* 77 M_HFSFORK */
252 #endif
253 "ZFS mount", /* 78 M_ZFSFSMNT */
254 "ZFS node", /* 79 M_ZFSNODE */
255 "temp", /* 80 M_TEMP */
256 "key mgmt", /* 81 M_SECA */
257 "DEVFS", /* 82 M_DEVFS */
258 "IpFw/IpAcct", /* 83 M_IPFW */
259 "UDF node", /* 84 M_UDFNODE */
260 "UDF mount", /* 85 M_UDFMNT */
261 #if INET6
262 "IPv6 NDP", /* 86 M_IP6NDP */
263 "IPv6 options", /* 87 M_IP6OPT */
264 "IPv6 Misc", /* 88 M_IP6MISC */
265 #else
266 "", /* 86 M_IP6NDP */
267 "", /* 87 M_IP6OPT */
268 "", /* 88 M_IP6MISC */
269 #endif
270 "TCP Segment Q",/* 89 M_TSEGQ */
271 "IGMP state", /* 90 M_IGMP */
272 #if JOURNALING
273 "Journal", /* 91 M_JNL_JNL */
274 "Transaction", /* 92 M_JNL_TR */
275 #else
276 "", /* 91 M_JNL_JNL */
277 "", /* 92 M_JNL_TR */
278 #endif
279 "specinfo", /* 93 M_SPECINFO */
280 "kqueue", /* 94 M_KQUEUE */
281 #if HFS
282 "HFS dirhint", /* 95 M_HFSDIRHINT */
283 #else
284 "", /* 95 M_HFSDIRHINT */
285 #endif
286 "cluster_read", /* 96 M_CLRDAHEAD */
287 "cluster_write",/* 97 M_CLWRBEHIND */
288 "iov64", /* 98 M_IOV64 */
289 "fileglob", /* 99 M_FILEGLOB */
290 "kauth", /* 100 M_KAUTH */
291 "dummynet", /* 101 M_DUMMYNET */
292 "unsafe_fsnode", /* 102 M_UNSAFEFS */
293 "macpipelabel", /* 103 M_MACPIPELABEL */
294 "mactemp", /* 104 M_MACTEMP */
295 "sbuf", /* 105 M_SBUF */
296 "extattr", /* 106 M_EXTATTR */
297 "lctx", /* 107 M_LCTX */
298 #if TRAFFIC_MGT
299 "traffic_mgt", /* 108 M_TRAFFIC_MGT */
300 #else
301 "", /* 108 M_TRAFFIC_MGT */
302 #endif
303 };
304
305 /* for use with kmzones.kz_zalloczone */
306 #define KMZ_CREATEZONE ((void *)-2)
307 #define KMZ_LOOKUPZONE ((void *)-1)
308 #define KMZ_MALLOC ((void *)0)
309 #define KMZ_SHAREZONE ((void *)1)
310
311 struct kmzones {
312 size_t kz_elemsize;
313 void *kz_zalloczone;
314 } kmzones[M_LAST] = {
315 #define SOS(sname) sizeof (struct sname)
316 #define SOX(sname) -1
317 { -1, 0 }, /* 0 M_FREE */
318 { MSIZE, KMZ_CREATEZONE }, /* 1 M_MBUF */
319 { 0, KMZ_MALLOC }, /* 2 M_DEVBUF */
320 { SOS(socket), KMZ_CREATEZONE }, /* 3 M_SOCKET */
321 { SOS(inpcb), KMZ_LOOKUPZONE }, /* 4 M_PCB */
322 { M_MBUF, KMZ_SHAREZONE }, /* 5 M_RTABLE */
323 { M_MBUF, KMZ_SHAREZONE }, /* 6 M_HTABLE */
324 { M_MBUF, KMZ_SHAREZONE }, /* 7 M_FTABLE */
325 { SOS(rusage), KMZ_CREATEZONE }, /* 8 M_ZOMBIE */
326 { 0, KMZ_MALLOC }, /* 9 M_IFADDR */
327 { M_MBUF, KMZ_SHAREZONE }, /* 10 M_SOOPTS */
328 { 0, KMZ_MALLOC }, /* 11 M_SONAME */
329 { MAXPATHLEN, KMZ_CREATEZONE }, /* 12 M_NAMEI */
330 { 0, KMZ_MALLOC }, /* 13 M_GPROF */
331 { 0, KMZ_MALLOC }, /* 14 M_IOCTLOPS */
332 { 0, KMZ_MALLOC }, /* 15 M_MAPMEM */
333 { SOS(ucred), KMZ_CREATEZONE }, /* 16 M_CRED */
334 { SOS(pgrp), KMZ_CREATEZONE }, /* 17 M_PGRP */
335 { SOS(session), KMZ_CREATEZONE }, /* 18 M_SESSION */
336 { SOS(iovec_32), KMZ_LOOKUPZONE }, /* 19 M_IOV32 */
337 { SOS(mount), KMZ_CREATEZONE }, /* 20 M_MOUNT */
338 { 0, KMZ_MALLOC }, /* 21 M_FHANDLE */
339 #if (NFSCLIENT || NFSSERVER)
340 { SOS(nfsreq), KMZ_CREATEZONE }, /* 22 M_NFSREQ */
341 { SOS(nfsmount), KMZ_CREATEZONE }, /* 23 M_NFSMNT */
342 { SOS(nfsnode), KMZ_CREATEZONE }, /* 24 M_NFSNODE */
343 #else
344 { 0, KMZ_MALLOC }, /* 22 M_NFSREQ */
345 { 0, KMZ_MALLOC }, /* 23 M_NFSMNT */
346 { 0, KMZ_MALLOC }, /* 24 M_NFSNODE */
347 #endif
348 { SOS(vnode), KMZ_CREATEZONE }, /* 25 M_VNODE */
349 { SOS(namecache), KMZ_CREATEZONE }, /* 26 M_CACHE */
350 #if QUOTA
351 { SOX(dquot), KMZ_LOOKUPZONE }, /* 27 M_DQUOT */
352 #else
353 { 0, KMZ_MALLOC }, /* 27 M_DQUOT */
354 #endif
355 #if FFS
356 { SOX(ufsmount), KMZ_LOOKUPZONE }, /* 28 M_UFSMNT */
357 #else
358 { 0, KMZ_MALLOC }, /* 28 M_UFSMNT */
359 #endif
360 { 0, KMZ_MALLOC }, /* 29 M_CGSUM */
361 { SOS(plimit), KMZ_CREATEZONE }, /* 30 M_PLIMIT */
362 { SOS(sigacts), KMZ_CREATEZONE }, /* 31 M_SIGACTS */
363 { 0, KMZ_MALLOC }, /* 32 M_VMOBJ */
364 { 0, KMZ_MALLOC }, /* 33 M_VMOBJHASH */
365 { 0, KMZ_MALLOC }, /* 34 M_VMPMAP */
366 { 0, KMZ_MALLOC }, /* 35 M_VMPVENT */
367 { 0, KMZ_MALLOC }, /* 36 M_VMPAGER */
368 { 0, KMZ_MALLOC }, /* 37 M_VMPGDATA */
369 { SOS(fileproc), KMZ_CREATEZONE }, /* 38 M_FILEPROC */
370 { SOS(filedesc), KMZ_CREATEZONE }, /* 39 M_FILEDESC */
371 { SOX(lockf), KMZ_CREATEZONE }, /* 40 M_LOCKF */
372 { SOS(proc), KMZ_CREATEZONE }, /* 41 M_PROC */
373 { SOS(pstats), KMZ_CREATEZONE }, /* 42 M_PSTATS */
374 { 0, KMZ_MALLOC }, /* 43 M_SEGMENT */
375 { M_FFSNODE, KMZ_SHAREZONE }, /* 44 M_LFSNODE */
376 #if FFS
377 { SOS(inode), KMZ_CREATEZONE }, /* 45 M_FFSNODE */
378 #else
379 { 0, KMZ_MALLOC }, /* 45 M_FFSNODE */
380 #endif
381 { M_FFSNODE, KMZ_SHAREZONE }, /* 46 M_MFSNODE */
382 { 0, KMZ_MALLOC }, /* 47 M_NQLEASE */
383 { 0, KMZ_MALLOC }, /* 48 M_NQMHOST */
384 { 0, KMZ_MALLOC }, /* 49 M_NETADDR */
385 #if (NFSCLIENT || NFSSERVER)
386 { SOX(nfsrv_sock),
387 KMZ_CREATEZONE }, /* 50 M_NFSSVC */
388 { 0, KMZ_MALLOC }, /* 51 M_NFSUID */
389 { SOX(nfsrvcache),
390 KMZ_CREATEZONE }, /* 52 M_NFSD */
391 #else
392 { 0, KMZ_MALLOC }, /* 50 M_NFSSVC */
393 { 0, KMZ_MALLOC }, /* 51 M_NFSUID */
394 { 0, KMZ_MALLOC }, /* 52 M_NFSD */
395 #endif
396 { SOX(ip_moptions),
397 KMZ_LOOKUPZONE }, /* 53 M_IPMOPTS */
398 { SOX(in_multi), KMZ_LOOKUPZONE }, /* 54 M_IPMADDR */
399 { SOX(ether_multi),
400 KMZ_LOOKUPZONE }, /* 55 M_IFMADDR */
401 { SOX(mrt), KMZ_CREATEZONE }, /* 56 M_MRTABLE */
402 #if CD9660
403 { SOX(iso_mnt), KMZ_LOOKUPZONE }, /* 57 M_ISOFSMNT */
404 { SOS(iso_node), KMZ_CREATEZONE }, /* 58 M_ISOFSNODE */
405 #else
406 { 0, KMZ_MALLOC }, /* 57 M_ISOFSMNT */
407 { 0, KMZ_MALLOC }, /* 58 M_ISOFSNODE */
408 #endif
409 #if (NFSCLIENT || NFSSERVER)
410 { SOS(nfsrv_descript),
411 KMZ_CREATEZONE }, /* 59 M_NFSRVDESC */
412 { SOS(nfsdmap), KMZ_CREATEZONE }, /* 60 M_NFSDIROFF */
413 { SOS(fhandle), KMZ_LOOKUPZONE }, /* 61 M_NFSBIGFH */
414 #else
415 { 0, KMZ_MALLOC }, /* 59 M_NFSRVDESC */
416 { 0, KMZ_MALLOC }, /* 60 M_NFSDIROFF */
417 { 0, KMZ_MALLOC }, /* 61 M_NFSBIGFH */
418 #endif
419 { 0, KMZ_MALLOC }, /* 62 M_MSDOSFSMNT */
420 { 0, KMZ_MALLOC }, /* 63 M_MSDOSFSFAT */
421 { 0, KMZ_MALLOC }, /* 64 M_MSDOSFSNODE */
422 { SOS(tty), KMZ_CREATEZONE }, /* 65 M_TTYS */
423 { 0, KMZ_MALLOC }, /* 66 M_EXEC */
424 { 0, KMZ_MALLOC }, /* 67 M_MISCFSMNT */
425 { 0, KMZ_MALLOC }, /* 68 M_MISCFSNODE */
426 { 0, KMZ_MALLOC }, /* 69 M_ADOSFSMNT */
427 { 0, KMZ_MALLOC }, /* 70 M_ADOSFSNODE */
428 { 0, KMZ_MALLOC }, /* 71 M_ANODE */
429 { SOX(buf), KMZ_CREATEZONE }, /* 72 M_BUFHDR */
430 { (NDFILE * OFILESIZE),
431 KMZ_CREATEZONE }, /* 73 M_OFILETABL */
432 { MCLBYTES, KMZ_CREATEZONE }, /* 74 M_MCLUST */
433 #if HFS
434 { SOX(hfsmount), KMZ_LOOKUPZONE }, /* 75 M_HFSMNT */
435 { SOS(cnode), KMZ_CREATEZONE }, /* 76 M_HFSNODE */
436 { SOS(filefork), KMZ_CREATEZONE }, /* 77 M_HFSFORK */
437 #else
438 { 0, KMZ_MALLOC }, /* 75 M_HFSMNT */
439 { 0, KMZ_MALLOC }, /* 76 M_HFSNODE */
440 { 0, KMZ_MALLOC }, /* 77 M_HFSFORK */
441 #endif
442 { 0, KMZ_MALLOC }, /* 78 M_ZFSMNT */
443 { 0, KMZ_MALLOC }, /* 79 M_ZFSNODE */
444 { 0, KMZ_MALLOC }, /* 80 M_TEMP */
445 { 0, KMZ_MALLOC }, /* 81 M_SECA */
446 { 0, KMZ_MALLOC }, /* 82 M_DEVFS */
447 { 0, KMZ_MALLOC }, /* 83 M_IPFW */
448 { 0, KMZ_MALLOC }, /* 84 M_UDFNODE */
449 { 0, KMZ_MALLOC }, /* 85 M_UDFMOUNT */
450 { 0, KMZ_MALLOC }, /* 86 M_IP6NDP */
451 { 0, KMZ_MALLOC }, /* 87 M_IP6OPT */
452 { 0, KMZ_MALLOC }, /* 88 M_IP6MISC */
453 { 0, KMZ_MALLOC }, /* 89 M_TSEGQ */
454 { 0, KMZ_MALLOC }, /* 90 M_IGMP */
455 #if JOURNALING
456 { SOS(journal), KMZ_CREATEZONE }, /* 91 M_JNL_JNL */
457 { SOS(transaction), KMZ_CREATEZONE }, /* 92 M_JNL_TR */
458 #else
459 { 0, KMZ_MALLOC }, /* 91 M_JNL_JNL */
460 { 0, KMZ_MALLOC }, /* 92 M_JNL_TR */
461 #endif
462 { SOS(specinfo), KMZ_CREATEZONE }, /* 93 M_SPECINFO */
463 { SOS(kqueue), KMZ_CREATEZONE }, /* 94 M_KQUEUE */
464 #if HFS
465 { SOS(directoryhint), KMZ_CREATEZONE }, /* 95 M_HFSDIRHINT */
466 #else
467 { 0, KMZ_MALLOC }, /* 95 M_HFSDIRHINT */
468 #endif
469 { SOS(cl_readahead), KMZ_CREATEZONE }, /* 96 M_CLRDAHEAD */
470 { SOS(cl_writebehind),KMZ_CREATEZONE }, /* 97 M_CLWRBEHIND */
471 { SOS(iovec_64), KMZ_LOOKUPZONE }, /* 98 M_IOV64 */
472 { SOS(fileglob), KMZ_CREATEZONE }, /* 99 M_FILEGLOB */
473 { 0, KMZ_MALLOC }, /* 100 M_KAUTH */
474 { 0, KMZ_MALLOC }, /* 101 M_DUMMYNET */
475 { SOS(unsafe_fsnode),KMZ_CREATEZONE }, /* 102 M_UNSAFEFS */
476 { 0, KMZ_MALLOC }, /* 103 M_MACPIPELABEL */
477 { 0, KMZ_MALLOC }, /* 104 M_MACTEMP */
478 { 0, KMZ_MALLOC }, /* 105 M_SBUF */
479 { 0, KMZ_MALLOC }, /* 106 M_HFS_EXTATTR */
480 { 0, KMZ_MALLOC }, /* 107 M_LCTX */
481 { 0, KMZ_MALLOC }, /* 108 M_TRAFFIC_MGT */
482 #undef SOS
483 #undef SOX
484 };
485
486 extern zone_t kalloc_zone(vm_size_t); /* XXX */
487
488 /*
489 * Initialize the kernel memory allocator
490 */
491 void
492 kmeminit(void)
493 {
494 struct kmzones *kmz;
495
496 if ((sizeof(kmzones)/sizeof(kmzones[0])) != (sizeof(memname)/sizeof(memname[0]))) {
497 panic("kmeminit: kmzones has %lu elements but memname has %lu\n",
498 (sizeof(kmzones)/sizeof(kmzones[0])), (sizeof(memname)/sizeof(memname[0])));
499 }
500
501 kmz = kmzones;
502 while (kmz < &kmzones[M_LAST]) {
503 /* XXX */
504 if (kmz->kz_elemsize == (size_t)(-1))
505 ;
506 else
507 /* XXX */
508 if (kmz->kz_zalloczone == KMZ_CREATEZONE) {
509 kmz->kz_zalloczone = zinit(kmz->kz_elemsize,
510 1024 * 1024, PAGE_SIZE,
511 memname[kmz - kmzones]);
512 }
513 else if (kmz->kz_zalloczone == KMZ_LOOKUPZONE)
514 kmz->kz_zalloczone = kalloc_zone(kmz->kz_elemsize);
515
516 kmz++;
517 }
518
519 kmz = kmzones;
520 while (kmz < &kmzones[M_LAST]) {
521 /* XXX */
522 if (kmz->kz_elemsize == (size_t)(-1))
523 ;
524 else
525 /* XXX */
526 if (kmz->kz_zalloczone == KMZ_SHAREZONE) {
527 kmz->kz_zalloczone =
528 kmzones[kmz->kz_elemsize].kz_zalloczone;
529 kmz->kz_elemsize =
530 kmzones[kmz->kz_elemsize].kz_elemsize;
531 }
532
533 kmz++;
534 }
535 }
536
537 #define MDECL(reqlen) \
538 union { \
539 struct _mhead hdr; \
540 char _m[(reqlen) + sizeof (struct _mhead)]; \
541 }
542
543 struct _mhead {
544 size_t mlen;
545 char dat[0];
546 };
547
548 void *
549 _MALLOC(
550 size_t size,
551 int type,
552 int flags)
553 {
554 MDECL(size) *mem;
555 size_t memsize = sizeof (*mem);
556
557 if (type >= M_LAST)
558 panic("_malloc TYPE");
559
560 if (size == 0)
561 return (NULL);
562
563 if (flags & M_NOWAIT) {
564 mem = (void *)kalloc_noblock(memsize);
565 } else {
566 mem = (void *)kalloc(memsize);
567 }
568 if (!mem)
569 return (0);
570
571 mem->hdr.mlen = memsize;
572
573 if (flags & M_ZERO)
574 bzero(mem->hdr.dat, size);
575
576 return (mem->hdr.dat);
577 }
578
579 void
580 _FREE(
581 void *addr,
582 int type)
583 {
584 struct _mhead *hdr;
585
586 if (type >= M_LAST)
587 panic("_free TYPE");
588
589 if (!addr)
590 return; /* correct (convenient bsd kernel legacy) */
591
592 hdr = addr; hdr--;
593 kfree(hdr, hdr->mlen);
594 }
595
596 void *
597 _MALLOC_ZONE(
598 size_t size,
599 int type,
600 int flags)
601 {
602 struct kmzones *kmz;
603 void *elem;
604
605 if (type >= M_LAST)
606 panic("_malloc_zone TYPE");
607
608 kmz = &kmzones[type];
609 if (kmz->kz_zalloczone == KMZ_MALLOC)
610 panic("_malloc_zone ZONE: type = %d", type);
611
612 /* XXX */
613 if (kmz->kz_elemsize == (size_t)(-1))
614 panic("_malloc_zone XXX");
615 /* XXX */
616 if (size == kmz->kz_elemsize)
617 if (flags & M_NOWAIT) {
618 elem = (void *)zalloc_noblock(kmz->kz_zalloczone);
619 } else {
620 elem = (void *)zalloc(kmz->kz_zalloczone);
621 }
622 else
623 if (flags & M_NOWAIT) {
624 elem = (void *)kalloc_noblock(size);
625 } else {
626 elem = (void *)kalloc(size);
627 }
628
629 return (elem);
630 }
631
632 void
633 _FREE_ZONE(
634 void *elem,
635 size_t size,
636 int type)
637 {
638 struct kmzones *kmz;
639
640 if (type >= M_LAST)
641 panic("FREE_SIZE");
642
643 kmz = &kmzones[type];
644 if (kmz->kz_zalloczone == KMZ_MALLOC)
645 panic("free_zone ZONE");
646
647 /* XXX */
648 if (kmz->kz_elemsize == (size_t)(-1))
649 panic("FREE_SIZE XXX");
650 /* XXX */
651 if (size == kmz->kz_elemsize)
652 zfree(kmz->kz_zalloczone, elem);
653 else
654 kfree(elem, size);
655 }