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