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