]>
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 <hfs/hfs_cnode.h>
101 #include <miscfs/specfs/specdev.h>
103 #include <nfs/rpcv2.h>
104 #include <nfs/nfsproto.h>
105 #include <nfs/nfsnode.h>
106 #include <nfs/nfsmount.h>
108 #include <vfs/vfs_journal.h>
110 #include <mach/mach_types.h>
112 #include <kern/zalloc.h>
113 #include <kern/kalloc.h>
115 void kmeminit(void) __attribute__((section("__TEXT, initcode")));
117 /* Strings corresponding to types of memory.
118 * Must be in synch with the #defines is sys/malloc.h
119 * NOTE - the reason we pass null strings in some cases is to reduce of foot
120 * print as much as possible for systems where a tiny kernel is needed.
121 * todo - We should probably redsign this and use enums for our types and only
122 * include types needed for that configuration of the kernel. This can't be
123 * done without some kind of kpi since several types are hardwired and exported
124 * (for example see types M_HFSMNT, M_UDFMNT, M_TEMP, etc in sys/malloc.h)
126 const char *memname
[] = {
127 "free", /* 0 M_FREE */
128 "mbuf", /* 1 M_MBUF */
129 "devbuf", /* 2 M_DEVBUF */
130 "socket", /* 3 M_SOCKET */
132 "routetbl", /* 5 M_RTABLE */
133 "hosttbl", /* 6 M_HTABLE */
134 "fragtbl", /* 7 M_FTABLE */
135 "zombie", /* 8 M_ZOMBIE */
136 "ifaddr", /* 9 M_IFADDR */
137 "soopts", /* 10 M_SOOPTS */
138 "soname", /* 11 M_SONAME */
139 "namei", /* 12 M_NAMEI */
140 "gprof", /* 13 M_GPROF */
141 "ioctlops", /* 14 M_IOCTLOPS */
142 "mapmem", /* 15 M_MAPMEM */
143 "cred", /* 16 M_CRED */
144 "pgrp", /* 17 M_PGRP */
145 "session", /* 18 M_SESSION */
146 "iov32", /* 19 M_IOV32 */
147 "mount", /* 20 M_MOUNT */
148 "fhandle", /* 21 M_FHANDLE */
149 #if (NFSCLIENT || NFSSERVER)
150 "NFS req", /* 22 M_NFSREQ */
151 "NFS mount", /* 23 M_NFSMNT */
152 "NFS node", /* 24 M_NFSNODE */
154 "", /* 22 M_NFSREQ */
155 "", /* 23 M_NFSMNT */
156 "", /* 24 M_NFSNODE */
158 "vnodes", /* 25 M_VNODE */
159 "namecache", /* 26 M_CACHE */
161 "UFS quota", /* 27 M_DQUOT */
165 "", /* 28 M_UFSMNT */
166 #if (SYSV_SEM || SYSV_MSG || SYSV_SHM)
167 "shm", /* 29 M_SHM */
171 "plimit", /* 30 M_VMMAP */
172 "sigacts", /* 31 M_VMMAPENT */
173 "VM object", /* 32 M_VMOBJ */
174 "VM objhash", /* 33 M_VMOBJHASH */
175 "VM pmap", /* 34 M_VMPMAP */
176 "VM pvmap", /* 35 M_VMPVENT */
177 "VM pager", /* 36 M_VMPAGER */
178 "VM pgdata", /* 37 M_VMPGDATA */
179 "fileproc", /* 38 M_FILEPROC */
180 "file desc", /* 39 M_FILEDESC */
181 "lockf", /* 40 M_LOCKF */
182 "proc", /* 41 M_PROC */
183 "pstats", /* 42 M_SUBPROC */
184 "LFS segment", /* 43 M_SEGMENT */
185 "LFS node", /* 44 M_LFSNODE */
186 "", /* 45 M_FFSNODE */
187 "MFS node", /* 46 M_MFSNODE */
188 "NQNFS Lease", /* 47 M_NQLEASE */
189 "NQNFS Host", /* 48 M_NQMHOST */
190 "Export Host", /* 49 M_NETADDR */
191 #if (NFSCLIENT || NFSSERVER)
192 "NFS srvsock", /* 50 M_NFSSVC */
193 "NFS uid", /* 51 M_NFSUID */
194 "NFS daemon", /* 52 M_NFSD */
196 "", /* 50 M_NFSSVC */
197 "", /* 51 M_NFSUID */
200 "ip_moptions", /* 53 M_IPMOPTS */
201 "in_multi", /* 54 M_IPMADDR */
202 "ether_multi", /* 55 M_IFMADDR */
203 "mrt", /* 56 M_MRTABLE */
204 "", /* 57 unused entry */
205 "", /* 58 unused entry */
206 #if (NFSCLIENT || NFSSERVER)
207 "NFSV3 srvdesc",/* 59 M_NFSRVDESC */
208 "NFSV3 diroff", /* 60 M_NFSDIROFF */
209 "NFSV3 bigfh", /* 61 M_NFSBIGFH */
211 "", /* 59 M_NFSRVDESC */
212 "", /* 60 M_NFSDIROFF */
213 "", /* 61 M_NFSBIGFH */
215 "MSDOSFS mount",/* 62 M_MSDOSFSMNT */
216 "MSDOSFS fat", /* 63 M_MSDOSFSFAT */
217 "MSDOSFS node", /* 64 M_MSDOSFSNODE */
218 "ttys", /* 65 M_TTYS */
219 "exec", /* 66 M_EXEC */
220 "miscfs mount", /* 67 M_MISCFSMNT */
221 "miscfs node", /* 68 M_MISCFSNODE */
222 "adosfs mount", /* 69 M_ADOSFSMNT */
223 "adosfs node", /* 70 M_ADOSFSNODE */
224 "adosfs anode", /* 71 M_ANODE */
225 "buf hdrs", /* 72 M_BUFHDR */
226 "ofile tabl", /* 73 M_OFILETABL */
227 "mbuf clust", /* 74 M_MCLUST */
229 "HFS mount", /* 75 M_HFSMNT */
230 "HFS node", /* 76 M_HFSNODE */
231 "HFS fork", /* 77 M_HFSFORK */
233 "", /* 75 M_HFSMNT */
234 "", /* 76 M_HFSNODE */
235 "", /* 77 M_HFSFORK */
237 "ZFS mount", /* 78 M_ZFSFSMNT */
238 "ZFS node", /* 79 M_ZFSNODE */
239 "temp", /* 80 M_TEMP */
240 "key mgmt", /* 81 M_SECA */
241 "DEVFS", /* 82 M_DEVFS */
242 "IpFw/IpAcct", /* 83 M_IPFW */
243 "UDF node", /* 84 M_UDFNODE */
244 "UDF mount", /* 85 M_UDFMNT */
246 "IPv6 NDP", /* 86 M_IP6NDP */
247 "IPv6 options", /* 87 M_IP6OPT */
248 "IPv6 Misc", /* 88 M_IP6MISC */
250 "", /* 86 M_IP6NDP */
251 "", /* 87 M_IP6OPT */
252 "", /* 88 M_IP6MISC */
254 "TCP Segment Q",/* 89 M_TSEGQ */
255 "IGMP state", /* 90 M_IGMP */
257 "Journal", /* 91 M_JNL_JNL */
258 "Transaction", /* 92 M_JNL_TR */
260 "", /* 91 M_JNL_JNL */
261 "", /* 92 M_JNL_TR */
263 "specinfo", /* 93 M_SPECINFO */
264 "kqueue", /* 94 M_KQUEUE */
266 "HFS dirhint", /* 95 M_HFSDIRHINT */
268 "", /* 95 M_HFSDIRHINT */
270 "cluster_read", /* 96 M_CLRDAHEAD */
271 "cluster_write",/* 97 M_CLWRBEHIND */
272 "iov64", /* 98 M_IOV64 */
273 "fileglob", /* 99 M_FILEGLOB */
274 "kauth", /* 100 M_KAUTH */
275 "dummynet", /* 101 M_DUMMYNET */
277 "unsafe_fsnode", /* 102 M_UNSAFEFS */
279 "", /* 102 M_UNSAFEFS */
280 #endif /* __LP64__ */
281 "macpipelabel", /* 103 M_MACPIPELABEL */
282 "mactemp", /* 104 M_MACTEMP */
283 "sbuf", /* 105 M_SBUF */
284 "extattr", /* 106 M_EXTATTR */
285 "lctx", /* 107 M_LCTX */
287 "traffic_mgt", /* 108 M_TRAFFIC_MGT */
289 "", /* 108 M_TRAFFIC_MGT */
292 "decmpfs_cnode",/* 109 M_DECMPFS_CNODE */
294 "", /* 109 M_DECMPFS_CNODE */
295 #endif /* HFS_COMPRESSION */
298 /* for use with kmzones.kz_zalloczone */
299 #define KMZ_CREATEZONE ((void *)-2)
300 #define KMZ_LOOKUPZONE ((void *)-1)
301 #define KMZ_MALLOC ((void *)0)
302 #define KMZ_SHAREZONE ((void *)1)
307 boolean_t kz_noencrypt
;
308 } kmzones
[M_LAST
] = {
309 #define SOS(sname) sizeof (struct sname)
310 #define SOX(sname) -1
311 { -1, 0, FALSE
}, /* 0 M_FREE */
312 { MSIZE
, KMZ_CREATEZONE
, FALSE
}, /* 1 M_MBUF */
313 { 0, KMZ_MALLOC
, FALSE
}, /* 2 M_DEVBUF */
314 { SOS(socket
), KMZ_CREATEZONE
, TRUE
}, /* 3 M_SOCKET */
315 { SOS(inpcb
), KMZ_LOOKUPZONE
, TRUE
}, /* 4 M_PCB */
316 { M_MBUF
, KMZ_SHAREZONE
, FALSE
}, /* 5 M_RTABLE */
317 { M_MBUF
, KMZ_SHAREZONE
, FALSE
}, /* 6 M_HTABLE */
318 { M_MBUF
, KMZ_SHAREZONE
, FALSE
}, /* 7 M_FTABLE */
319 { SOS(rusage
), KMZ_CREATEZONE
, TRUE
}, /* 8 M_ZOMBIE */
320 { 0, KMZ_MALLOC
, FALSE
}, /* 9 M_IFADDR */
321 { M_MBUF
, KMZ_SHAREZONE
, FALSE
}, /* 10 M_SOOPTS */
322 { 0, KMZ_MALLOC
, FALSE
}, /* 11 M_SONAME */
323 { MAXPATHLEN
, KMZ_CREATEZONE
, FALSE
}, /* 12 M_NAMEI */
324 { 0, KMZ_MALLOC
, FALSE
}, /* 13 M_GPROF */
325 { 0, KMZ_MALLOC
, FALSE
}, /* 14 M_IOCTLOPS */
326 { 0, KMZ_MALLOC
, FALSE
}, /* 15 M_MAPMEM */
327 { SOS(ucred
), KMZ_CREATEZONE
, FALSE
}, /* 16 M_CRED */
328 { SOS(pgrp
), KMZ_CREATEZONE
, FALSE
}, /* 17 M_PGRP */
329 { SOS(session
), KMZ_CREATEZONE
, FALSE
}, /* 18 M_SESSION */
330 { SOS(user32_iovec
), KMZ_LOOKUPZONE
, FALSE
},/* 19 M_IOV32 */
331 { SOS(mount
), KMZ_CREATEZONE
, FALSE
}, /* 20 M_MOUNT */
332 { 0, KMZ_MALLOC
, FALSE
}, /* 21 M_FHANDLE */
333 #if (NFSCLIENT || NFSSERVER)
334 { SOS(nfsreq
), KMZ_CREATEZONE
, FALSE
}, /* 22 M_NFSREQ */
335 { SOS(nfsmount
), KMZ_CREATEZONE
, FALSE
},/* 23 M_NFSMNT */
336 { SOS(nfsnode
), KMZ_CREATEZONE
, FALSE
}, /* 24 M_NFSNODE */
338 { 0, KMZ_MALLOC
, FALSE
}, /* 22 M_NFSREQ */
339 { 0, KMZ_MALLOC
, FALSE
}, /* 23 M_NFSMNT */
340 { 0, KMZ_MALLOC
, FALSE
}, /* 24 M_NFSNODE */
342 { SOS(vnode
), KMZ_CREATEZONE
, TRUE
}, /* 25 M_VNODE */
343 { SOS(namecache
),KMZ_CREATEZONE
, FALSE
}, /* 26 M_CACHE */
345 { SOX(dquot
), KMZ_LOOKUPZONE
, FALSE
}, /* 27 M_DQUOT */
347 { 0, KMZ_MALLOC
, FALSE
}, /* 27 M_DQUOT */
349 { 0, KMZ_MALLOC
, FALSE
}, /* 28 M_UFSMNT */
350 { 0, KMZ_MALLOC
, FALSE
}, /* 29 M_CGSUM */
351 { SOS(plimit
), KMZ_CREATEZONE
, TRUE
}, /* 30 M_PLIMIT */
352 { SOS(sigacts
), KMZ_CREATEZONE
, TRUE
}, /* 31 M_SIGACTS */
353 { 0, KMZ_MALLOC
, FALSE
}, /* 32 M_VMOBJ */
354 { 0, KMZ_MALLOC
, FALSE
}, /* 33 M_VMOBJHASH */
355 { 0, KMZ_MALLOC
, FALSE
}, /* 34 M_VMPMAP */
356 { 0, KMZ_MALLOC
, FALSE
}, /* 35 M_VMPVENT */
357 { 0, KMZ_MALLOC
, FALSE
}, /* 36 M_VMPAGER */
358 { 0, KMZ_MALLOC
, FALSE
}, /* 37 M_VMPGDATA */
359 { SOS(fileproc
),KMZ_CREATEZONE
, TRUE
}, /* 38 M_FILEPROC */
360 { SOS(filedesc
),KMZ_CREATEZONE
, TRUE
}, /* 39 M_FILEDESC */
361 { SOX(lockf
), KMZ_CREATEZONE
, TRUE
}, /* 40 M_LOCKF */
362 { SOS(proc
), KMZ_CREATEZONE
, FALSE
}, /* 41 M_PROC */
363 { SOS(pstats
), KMZ_CREATEZONE
, TRUE
}, /* 42 M_PSTATS */
364 { 0, KMZ_MALLOC
, FALSE
}, /* 43 M_SEGMENT */
365 { M_FFSNODE
, KMZ_SHAREZONE
, FALSE
}, /* 44 M_LFSNODE */
366 { 0, KMZ_MALLOC
, FALSE
}, /* 45 M_FFSNODE */
367 { M_FFSNODE
, KMZ_SHAREZONE
, FALSE
}, /* 46 M_MFSNODE */
368 { 0, KMZ_MALLOC
, FALSE
}, /* 47 M_NQLEASE */
369 { 0, KMZ_MALLOC
, FALSE
}, /* 48 M_NQMHOST */
370 { 0, KMZ_MALLOC
, FALSE
}, /* 49 M_NETADDR */
371 #if (NFSCLIENT || NFSSERVER)
373 KMZ_CREATEZONE
, FALSE
}, /* 50 M_NFSSVC */
374 { 0, KMZ_MALLOC
, FALSE
}, /* 51 M_NFSUID */
376 KMZ_CREATEZONE
, FALSE
}, /* 52 M_NFSD */
378 { 0, KMZ_MALLOC
, FALSE
}, /* 50 M_NFSSVC */
379 { 0, KMZ_MALLOC
, FALSE
}, /* 51 M_NFSUID */
380 { 0, KMZ_MALLOC
, FALSE
}, /* 52 M_NFSD */
383 KMZ_LOOKUPZONE
, FALSE
}, /* 53 M_IPMOPTS */
384 { SOX(in_multi
),KMZ_LOOKUPZONE
, FALSE
}, /* 54 M_IPMADDR */
386 KMZ_LOOKUPZONE
, FALSE
}, /* 55 M_IFMADDR */
387 { SOX(mrt
), KMZ_CREATEZONE
, TRUE
}, /* 56 M_MRTABLE */
388 { 0, KMZ_MALLOC
, FALSE
}, /* 57 unused entry */
389 { 0, KMZ_MALLOC
, FALSE
}, /* 58 unused entry */
390 #if (NFSCLIENT || NFSSERVER)
391 { SOS(nfsrv_descript
),
392 KMZ_CREATEZONE
, FALSE
}, /* 59 M_NFSRVDESC */
393 { SOS(nfsdmap
), KMZ_CREATEZONE
, FALSE
}, /* 60 M_NFSDIROFF */
394 { SOS(fhandle
), KMZ_LOOKUPZONE
, FALSE
}, /* 61 M_NFSBIGFH */
396 { 0, KMZ_MALLOC
, FALSE
}, /* 59 M_NFSRVDESC */
397 { 0, KMZ_MALLOC
, FALSE
}, /* 60 M_NFSDIROFF */
398 { 0, KMZ_MALLOC
, FALSE
}, /* 61 M_NFSBIGFH */
400 { 0, KMZ_MALLOC
, FALSE
}, /* 62 M_MSDOSFSMNT */
401 { 0, KMZ_MALLOC
, FALSE
}, /* 63 M_MSDOSFSFAT */
402 { 0, KMZ_MALLOC
, FALSE
}, /* 64 M_MSDOSFSNODE */
403 { SOS(tty
), KMZ_CREATEZONE
, FALSE
}, /* 65 M_TTYS */
404 { 0, KMZ_MALLOC
, FALSE
}, /* 66 M_EXEC */
405 { 0, KMZ_MALLOC
, FALSE
}, /* 67 M_MISCFSMNT */
406 { 0, KMZ_MALLOC
, FALSE
}, /* 68 M_MISCFSNODE */
407 { 0, KMZ_MALLOC
, FALSE
}, /* 69 M_ADOSFSMNT */
408 { 0, KMZ_MALLOC
, FALSE
}, /* 70 M_ADOSFSNODE */
409 { 0, KMZ_MALLOC
, FALSE
}, /* 71 M_ANODE */
410 { SOX(buf
), KMZ_CREATEZONE
, TRUE
}, /* 72 M_BUFHDR */
411 { (NDFILE
* OFILESIZE
),
412 KMZ_CREATEZONE
, FALSE
}, /* 73 M_OFILETABL */
413 { MCLBYTES
, KMZ_CREATEZONE
, FALSE
}, /* 74 M_MCLUST */
415 { SOX(hfsmount
),KMZ_LOOKUPZONE
, FALSE
}, /* 75 M_HFSMNT */
416 { SOS(cnode
), KMZ_CREATEZONE
, TRUE
}, /* 76 M_HFSNODE */
417 { SOS(filefork
),KMZ_CREATEZONE
, TRUE
}, /* 77 M_HFSFORK */
419 { 0, KMZ_MALLOC
, FALSE
}, /* 75 M_HFSMNT */
420 { 0, KMZ_MALLOC
, FALSE
}, /* 76 M_HFSNODE */
421 { 0, KMZ_MALLOC
, FALSE
}, /* 77 M_HFSFORK */
423 { 0, KMZ_MALLOC
, FALSE
}, /* 78 M_ZFSMNT */
424 { 0, KMZ_MALLOC
, FALSE
}, /* 79 M_ZFSNODE */
425 { 0, KMZ_MALLOC
, FALSE
}, /* 80 M_TEMP */
426 { 0, KMZ_MALLOC
, FALSE
}, /* 81 M_SECA */
427 { 0, KMZ_MALLOC
, FALSE
}, /* 82 M_DEVFS */
428 { 0, KMZ_MALLOC
, FALSE
}, /* 83 M_IPFW */
429 { 0, KMZ_MALLOC
, FALSE
}, /* 84 M_UDFNODE */
430 { 0, KMZ_MALLOC
, FALSE
}, /* 85 M_UDFMOUNT */
431 { 0, KMZ_MALLOC
, FALSE
}, /* 86 M_IP6NDP */
432 { 0, KMZ_MALLOC
, FALSE
}, /* 87 M_IP6OPT */
433 { 0, KMZ_MALLOC
, FALSE
}, /* 88 M_IP6MISC */
434 { 0, KMZ_MALLOC
, FALSE
}, /* 89 M_TSEGQ */
435 { 0, KMZ_MALLOC
, FALSE
}, /* 90 M_IGMP */
437 { SOS(journal
), KMZ_CREATEZONE
, FALSE
}, /* 91 M_JNL_JNL */
438 { SOS(transaction
), KMZ_CREATEZONE
, FALSE
}, /* 92 M_JNL_TR */
440 { 0, KMZ_MALLOC
, FALSE
}, /* 91 M_JNL_JNL */
441 { 0, KMZ_MALLOC
, FALSE
}, /* 92 M_JNL_TR */
443 { SOS(specinfo
), KMZ_CREATEZONE
, TRUE
}, /* 93 M_SPECINFO */
444 { SOS(kqueue
), KMZ_CREATEZONE
, FALSE
}, /* 94 M_KQUEUE */
446 { SOS(directoryhint
), KMZ_CREATEZONE
, FALSE
}, /* 95 M_HFSDIRHINT */
448 { 0, KMZ_MALLOC
, FALSE
}, /* 95 M_HFSDIRHINT */
450 { SOS(cl_readahead
), KMZ_CREATEZONE
, TRUE
}, /* 96 M_CLRDAHEAD */
451 { SOS(cl_writebehind
),KMZ_CREATEZONE
, TRUE
}, /* 97 M_CLWRBEHIND */
452 { SOS(user64_iovec
), KMZ_LOOKUPZONE
, FALSE
},/* 98 M_IOV64 */
453 { SOS(fileglob
), KMZ_CREATEZONE
, TRUE
}, /* 99 M_FILEGLOB */
454 { 0, KMZ_MALLOC
, FALSE
}, /* 100 M_KAUTH */
455 { 0, KMZ_MALLOC
, FALSE
}, /* 101 M_DUMMYNET */
457 { SOS(unsafe_fsnode
),KMZ_CREATEZONE
, FALSE
}, /* 102 M_UNSAFEFS */
459 { 0, KMZ_MALLOC
, FALSE
}, /* 102 M_UNSAFEFS */
460 #endif /* __LP64__ */
461 { 0, KMZ_MALLOC
, FALSE
}, /* 103 M_MACPIPELABEL */
462 { 0, KMZ_MALLOC
, FALSE
}, /* 104 M_MACTEMP */
463 { 0, KMZ_MALLOC
, FALSE
}, /* 105 M_SBUF */
464 { 0, KMZ_MALLOC
, FALSE
}, /* 106 M_HFS_EXTATTR */
465 { 0, KMZ_MALLOC
, FALSE
}, /* 107 M_LCTX */
466 { 0, KMZ_MALLOC
, FALSE
}, /* 108 M_TRAFFIC_MGT */
468 { SOS(decmpfs_cnode
),KMZ_CREATEZONE
, FALSE
}, /* 109 M_DECMPFS_CNODE */
470 { 0, KMZ_MALLOC
, FALSE
}, /* 109 M_DECMPFS_CNODE */
471 #endif /* HFS_COMPRESSION */
476 extern zone_t
kalloc_zone(vm_size_t
); /* XXX */
479 * Initialize the kernel memory allocator
486 if ((sizeof(kmzones
)/sizeof(kmzones
[0])) != (sizeof(memname
)/sizeof(memname
[0]))) {
487 panic("kmeminit: kmzones has %lu elements but memname has %lu\n",
488 (sizeof(kmzones
)/sizeof(kmzones
[0])), (sizeof(memname
)/sizeof(memname
[0])));
492 while (kmz
< &kmzones
[M_LAST
]) {
494 if (kmz
->kz_elemsize
== (size_t)(-1))
498 if (kmz
->kz_zalloczone
== KMZ_CREATEZONE
) {
499 kmz
->kz_zalloczone
= zinit(kmz
->kz_elemsize
,
500 1024 * 1024, PAGE_SIZE
,
501 memname
[kmz
- kmzones
]);
502 if (kmz
->kz_noencrypt
== TRUE
)
503 zone_change(kmz
->kz_zalloczone
, Z_NOENCRYPT
, TRUE
);
505 else if (kmz
->kz_zalloczone
== KMZ_LOOKUPZONE
)
506 kmz
->kz_zalloczone
= kalloc_zone(kmz
->kz_elemsize
);
512 while (kmz
< &kmzones
[M_LAST
]) {
514 if (kmz
->kz_elemsize
== (size_t)(-1))
518 if (kmz
->kz_zalloczone
== KMZ_SHAREZONE
) {
520 kmzones
[kmz
->kz_elemsize
].kz_zalloczone
;
522 kmzones
[kmz
->kz_elemsize
].kz_elemsize
;
529 #define MDECL(reqlen) \
532 char _m[(reqlen) + sizeof (struct _mhead)]; \
547 size_t memsize
= sizeof (*mem
);
550 panic("_malloc TYPE");
555 if (flags
& M_NOWAIT
) {
556 mem
= (void *)kalloc_noblock(memsize
);
558 mem
= (void *)kalloc(memsize
);
563 * We get here when the caller told us to block waiting for memory, but
564 * kalloc said there's no memory left to get. Generally, this means there's a
565 * leak or the caller asked for an impossibly large amount of memory. Since there's
566 * nothing left to wait for and the caller isn't expecting a NULL return code, we
567 * just panic. This is less than ideal, but returning NULL doesn't help since the
568 * majority of callers don't check the return value and will just dereference the pointer and
569 * trap anyway. We may as well get a more descriptive message out while we can.
572 panic("_MALLOC: kalloc returned NULL (potential leak), size %llu", (uint64_t) size
);
578 mem
->hdr
.mlen
= memsize
;
581 bzero(mem
->hdr
.dat
, size
);
583 return (mem
->hdr
.dat
);
597 return; /* correct (convenient bsd kernel legacy) */
600 kfree(hdr
, hdr
->mlen
);
613 panic("_malloc_zone TYPE");
615 kmz
= &kmzones
[type
];
616 if (kmz
->kz_zalloczone
== KMZ_MALLOC
)
617 panic("_malloc_zone ZONE: type = %d", type
);
620 if (kmz
->kz_elemsize
== (size_t)(-1))
621 panic("_malloc_zone XXX");
623 if (size
== kmz
->kz_elemsize
)
624 if (flags
& M_NOWAIT
) {
625 elem
= (void *)zalloc_noblock(kmz
->kz_zalloczone
);
627 elem
= (void *)zalloc(kmz
->kz_zalloczone
);
630 if (flags
& M_NOWAIT
) {
631 elem
= (void *)kalloc_noblock(size
);
633 elem
= (void *)kalloc(size
);
650 kmz
= &kmzones
[type
];
651 if (kmz
->kz_zalloczone
== KMZ_MALLOC
)
652 panic("free_zone ZONE");
655 if (kmz
->kz_elemsize
== (size_t)(-1))
656 panic("FREE_SIZE XXX");
658 if (size
== kmz
->kz_elemsize
)
659 zfree(kmz
->kz_zalloczone
, elem
);