]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_malloc.c
2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1987, 1991, 1993
31 * The Regents of the University of California. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
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.
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
61 * @(#)kern_malloc.c 8.4 (Berkeley) 5/20/95
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,
70 #include <sys/param.h>
71 #include <sys/malloc.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
76 #include <net/route.h>
78 #include <netinet/in.h>
79 #include <netinet/in_systm.h>
80 #include <netinet/ip.h>
81 #include <netinet/in_pcb.h>
83 #include <sys/event.h>
84 #include <sys/eventvar.h>
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>
94 #include <sys/quota.h>
95 #include <sys/uio_internal.h>
96 #include <sys/resourcevar.h>
97 #include <sys/signalvar.h>
99 #include <ufs/ufs/inode.h>
101 #include <hfs/hfs_cnode.h>
102 #include <isofs/cd9660/cd9660_node.h>
104 #include <miscfs/specfs/specdev.h>
106 #include <nfs/rpcv2.h>
107 #include <nfs/nfsproto.h>
108 #include <nfs/nfsnode.h>
109 #include <nfs/nfsmount.h>
111 #include <vfs/vfs_journal.h>
113 #include <mach/mach_types.h>
115 #include <kern/zalloc.h>
116 #include <kern/kalloc.h>
118 void kmeminit(void) __attribute__((section("__TEXT, initcode")));
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)
129 const char *memname
[] = {
130 "free", /* 0 M_FREE */
131 "mbuf", /* 1 M_MBUF */
132 "devbuf", /* 2 M_DEVBUF */
133 "socket", /* 3 M_SOCKET */
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 */
157 "", /* 22 M_NFSREQ */
158 "", /* 23 M_NFSMNT */
159 "", /* 24 M_NFSNODE */
161 "vnodes", /* 25 M_VNODE */
162 "namecache", /* 26 M_CACHE */
164 "UFS quota", /* 27 M_DQUOT */
169 "UFS mount", /* 28 M_UFSMNT */
171 "", /* 28 M_UFSMNT */
173 #if (SYSV_SEM || SYSV_MSG || SYSV_SHM)
174 "shm", /* 29 M_SHM */
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 */
194 "FFS node", /* 45 M_FFSNODE */
196 "", /* 45 M_FFSNODE */
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 */
207 "", /* 50 M_NFSSVC */
208 "", /* 51 M_NFSUID */
211 "ip_moptions", /* 53 M_IPMOPTS */
212 "in_multi", /* 54 M_IPMADDR */
213 "ether_multi", /* 55 M_IFMADDR */
214 "mrt", /* 56 M_MRTABLE */
216 "ISOFS mount", /* 57 M_ISOFSMNT */
217 "ISOFS node", /* 58 M_ISOFSNODE */
219 "", /* 57 M_ISOFSMNT */
220 "", /* 58 M_ISOFSNODE */
222 #if (NFSCLIENT || NFSSERVER)
223 "NFSV3 srvdesc",/* 59 M_NFSRVDESC */
224 "NFSV3 diroff", /* 60 M_NFSDIROFF */
225 "NFSV3 bigfh", /* 61 M_NFSBIGFH */
227 "", /* 59 M_NFSRVDESC */
228 "", /* 60 M_NFSDIROFF */
229 "", /* 61 M_NFSBIGFH */
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 */
245 "HFS mount", /* 75 M_HFSMNT */
246 "HFS node", /* 76 M_HFSNODE */
247 "HFS fork", /* 77 M_HFSFORK */
249 "", /* 75 M_HFSMNT */
250 "", /* 76 M_HFSNODE */
251 "", /* 77 M_HFSFORK */
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 */
262 "IPv6 NDP", /* 86 M_IP6NDP */
263 "IPv6 options", /* 87 M_IP6OPT */
264 "IPv6 Misc", /* 88 M_IP6MISC */
266 "", /* 86 M_IP6NDP */
267 "", /* 87 M_IP6OPT */
268 "", /* 88 M_IP6MISC */
270 "TCP Segment Q",/* 89 M_TSEGQ */
271 "IGMP state", /* 90 M_IGMP */
273 "Journal", /* 91 M_JNL_JNL */
274 "Transaction", /* 92 M_JNL_TR */
276 "", /* 91 M_JNL_JNL */
277 "", /* 92 M_JNL_TR */
279 "specinfo", /* 93 M_SPECINFO */
280 "kqueue", /* 94 M_KQUEUE */
282 "HFS dirhint", /* 95 M_HFSDIRHINT */
284 "", /* 95 M_HFSDIRHINT */
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 */
299 "traffic_mgt", /* 108 M_TRAFFIC_MGT */
301 "", /* 108 M_TRAFFIC_MGT */
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)
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 */
344 { 0, KMZ_MALLOC
}, /* 22 M_NFSREQ */
345 { 0, KMZ_MALLOC
}, /* 23 M_NFSMNT */
346 { 0, KMZ_MALLOC
}, /* 24 M_NFSNODE */
348 { SOS(vnode
), KMZ_CREATEZONE
}, /* 25 M_VNODE */
349 { SOS(namecache
), KMZ_CREATEZONE
}, /* 26 M_CACHE */
351 { SOX(dquot
), KMZ_LOOKUPZONE
}, /* 27 M_DQUOT */
353 { 0, KMZ_MALLOC
}, /* 27 M_DQUOT */
356 { SOX(ufsmount
), KMZ_LOOKUPZONE
}, /* 28 M_UFSMNT */
358 { 0, KMZ_MALLOC
}, /* 28 M_UFSMNT */
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 */
377 { SOS(inode
), KMZ_CREATEZONE
}, /* 45 M_FFSNODE */
379 { 0, KMZ_MALLOC
}, /* 45 M_FFSNODE */
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)
387 KMZ_CREATEZONE
}, /* 50 M_NFSSVC */
388 { 0, KMZ_MALLOC
}, /* 51 M_NFSUID */
390 KMZ_CREATEZONE
}, /* 52 M_NFSD */
392 { 0, KMZ_MALLOC
}, /* 50 M_NFSSVC */
393 { 0, KMZ_MALLOC
}, /* 51 M_NFSUID */
394 { 0, KMZ_MALLOC
}, /* 52 M_NFSD */
397 KMZ_LOOKUPZONE
}, /* 53 M_IPMOPTS */
398 { SOX(in_multi
), KMZ_LOOKUPZONE
}, /* 54 M_IPMADDR */
400 KMZ_LOOKUPZONE
}, /* 55 M_IFMADDR */
401 { SOX(mrt
), KMZ_CREATEZONE
}, /* 56 M_MRTABLE */
403 { SOX(iso_mnt
), KMZ_LOOKUPZONE
}, /* 57 M_ISOFSMNT */
404 { SOS(iso_node
), KMZ_CREATEZONE
}, /* 58 M_ISOFSNODE */
406 { 0, KMZ_MALLOC
}, /* 57 M_ISOFSMNT */
407 { 0, KMZ_MALLOC
}, /* 58 M_ISOFSNODE */
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 */
415 { 0, KMZ_MALLOC
}, /* 59 M_NFSRVDESC */
416 { 0, KMZ_MALLOC
}, /* 60 M_NFSDIROFF */
417 { 0, KMZ_MALLOC
}, /* 61 M_NFSBIGFH */
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 */
434 { SOX(hfsmount
), KMZ_LOOKUPZONE
}, /* 75 M_HFSMNT */
435 { SOS(cnode
), KMZ_CREATEZONE
}, /* 76 M_HFSNODE */
436 { SOS(filefork
), KMZ_CREATEZONE
}, /* 77 M_HFSFORK */
438 { 0, KMZ_MALLOC
}, /* 75 M_HFSMNT */
439 { 0, KMZ_MALLOC
}, /* 76 M_HFSNODE */
440 { 0, KMZ_MALLOC
}, /* 77 M_HFSFORK */
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 */
456 { SOS(journal
), KMZ_CREATEZONE
}, /* 91 M_JNL_JNL */
457 { SOS(transaction
), KMZ_CREATEZONE
}, /* 92 M_JNL_TR */
459 { 0, KMZ_MALLOC
}, /* 91 M_JNL_JNL */
460 { 0, KMZ_MALLOC
}, /* 92 M_JNL_TR */
462 { SOS(specinfo
), KMZ_CREATEZONE
}, /* 93 M_SPECINFO */
463 { SOS(kqueue
), KMZ_CREATEZONE
}, /* 94 M_KQUEUE */
465 { SOS(directoryhint
), KMZ_CREATEZONE
}, /* 95 M_HFSDIRHINT */
467 { 0, KMZ_MALLOC
}, /* 95 M_HFSDIRHINT */
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 */
486 extern zone_t
kalloc_zone(vm_size_t
); /* XXX */
489 * Initialize the kernel memory allocator
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])));
502 while (kmz
< &kmzones
[M_LAST
]) {
504 if (kmz
->kz_elemsize
== (size_t)(-1))
508 if (kmz
->kz_zalloczone
== KMZ_CREATEZONE
) {
509 kmz
->kz_zalloczone
= zinit(kmz
->kz_elemsize
,
510 1024 * 1024, PAGE_SIZE
,
511 memname
[kmz
- kmzones
]);
513 else if (kmz
->kz_zalloczone
== KMZ_LOOKUPZONE
)
514 kmz
->kz_zalloczone
= kalloc_zone(kmz
->kz_elemsize
);
520 while (kmz
< &kmzones
[M_LAST
]) {
522 if (kmz
->kz_elemsize
== (size_t)(-1))
526 if (kmz
->kz_zalloczone
== KMZ_SHAREZONE
) {
528 kmzones
[kmz
->kz_elemsize
].kz_zalloczone
;
530 kmzones
[kmz
->kz_elemsize
].kz_elemsize
;
537 #define MDECL(reqlen) \
540 char _m[(reqlen) + sizeof (struct _mhead)]; \
555 size_t memsize
= sizeof (*mem
);
558 panic("_malloc TYPE");
563 if (flags
& M_NOWAIT
) {
564 mem
= (void *)kalloc_noblock(memsize
);
566 mem
= (void *)kalloc(memsize
);
571 mem
->hdr
.mlen
= memsize
;
574 bzero(mem
->hdr
.dat
, size
);
576 return (mem
->hdr
.dat
);
590 return; /* correct (convenient bsd kernel legacy) */
593 kfree(hdr
, hdr
->mlen
);
606 panic("_malloc_zone TYPE");
608 kmz
= &kmzones
[type
];
609 if (kmz
->kz_zalloczone
== KMZ_MALLOC
)
610 panic("_malloc_zone ZONE: type = %d", type
);
613 if (kmz
->kz_elemsize
== (size_t)(-1))
614 panic("_malloc_zone XXX");
616 if (size
== kmz
->kz_elemsize
)
617 if (flags
& M_NOWAIT
) {
618 elem
= (void *)zalloc_noblock(kmz
->kz_zalloczone
);
620 elem
= (void *)zalloc(kmz
->kz_zalloczone
);
623 if (flags
& M_NOWAIT
) {
624 elem
= (void *)kalloc_noblock(size
);
626 elem
= (void *)kalloc(size
);
643 kmz
= &kmzones
[type
];
644 if (kmz
->kz_zalloczone
== KMZ_MALLOC
)
645 panic("free_zone ZONE");
648 if (kmz
->kz_elemsize
== (size_t)(-1))
649 panic("FREE_SIZE XXX");
651 if (size
== kmz
->kz_elemsize
)
652 zfree(kmz
->kz_zalloczone
, elem
);