]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/kern_malloc.c
xnu-344.34.tar.gz
[apple/xnu.git] / bsd / kern / kern_malloc.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
de355530
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 *
de355530
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,
de355530
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
71#include <sys/proc.h>
72#include <sys/mount.h>
73#include <sys/vnode.h>
74#include <sys/namei.h>
75#include <sys/file.h>
76#include <sys/filedesc.h>
77#include <sys/tty.h>
9bccf70c 78#include <sys/quota.h>
1c79356b 79
1c79356b
A
80#include <ufs/ufs/inode.h>
81
9bccf70c 82#include <hfs/hfs_cnode.h>
1c79356b
A
83#include <isofs/cd9660/cd9660_node.h>
84
85#include <miscfs/volfs/volfs.h>
86
87#include <nfs/rpcv2.h>
88#include <nfs/nfsproto.h>
89#include <nfs/nfsnode.h>
90#include <nfs/nfsmount.h>
91#include <nfs/nqnfs.h>
92
d7e50217
A
93#include <vfs/vfs_journal.h>
94
1c79356b
A
95#include <mach/mach_types.h>
96
97#include <kern/zalloc.h>
98#include <kern/kalloc.h>
99
100struct kmemstats kmemstats[M_LAST];
101char *memname[] = INITKMEMNAMES;
102
103struct kmzones {
104 size_t kz_elemsize;
105 void *kz_zalloczone;
106#define KMZ_CREATEZONE ((void *)-2)
107#define KMZ_LOOKUPZONE ((void *)-1)
108#define KMZ_MALLOC ((void *)0)
109#define KMZ_SHAREZONE ((void *)1)
110} kmzones[M_LAST] = {
111#define SOS(sname) sizeof (struct sname)
112#define SOX(sname) -1
113 -1, 0, /* 0 M_FREE */
114 MSIZE, KMZ_CREATEZONE, /* 1 M_MBUF */
115 0, KMZ_MALLOC, /* 2 M_DEVBUF */
116 SOS(socket), KMZ_CREATEZONE, /* 3 M_SOCKET */
117 SOS(inpcb), KMZ_LOOKUPZONE, /* 4 M_PCB */
118 M_MBUF, KMZ_SHAREZONE, /* 5 M_RTABLE */
119 M_MBUF, KMZ_SHAREZONE, /* 6 M_HTABLE */
120 M_MBUF, KMZ_SHAREZONE, /* 7 M_FTABLE */
121 SOS(rusage), KMZ_CREATEZONE, /* 8 M_ZOMBIE */
122 0, KMZ_MALLOC, /* 9 M_IFADDR */
123 M_MBUF, KMZ_SHAREZONE, /* 10 M_SOOPTS */
124 0, KMZ_MALLOC, /* 11 M_SONAME */
125 MAXPATHLEN, KMZ_CREATEZONE, /* 12 M_NAMEI */
126 0, KMZ_MALLOC, /* 13 M_GPROF */
127 0, KMZ_MALLOC, /* 14 M_IOCTLOPS */
128 0, KMZ_MALLOC, /* 15 M_MAPMEM */
129 SOS(ucred), KMZ_CREATEZONE, /* 16 M_CRED */
130 SOS(pgrp), KMZ_CREATEZONE, /* 17 M_PGRP */
131 SOS(session), KMZ_CREATEZONE, /* 18 M_SESSION */
132 SOS(iovec), KMZ_LOOKUPZONE, /* 19 M_IOV */
133 SOS(mount), KMZ_CREATEZONE, /* 20 M_MOUNT */
134 0, KMZ_MALLOC, /* 21 M_FHANDLE */
135 SOS(nfsreq), KMZ_CREATEZONE, /* 22 M_NFSREQ */
136 SOS(nfsmount), KMZ_CREATEZONE, /* 23 M_NFSMNT */
137 SOS(nfsnode), KMZ_CREATEZONE, /* 24 M_NFSNODE */
138 SOS(vnode), KMZ_CREATEZONE, /* 25 M_VNODE */
139 SOS(namecache), KMZ_CREATEZONE, /* 26 M_CACHE */
140 SOX(dquot), KMZ_LOOKUPZONE, /* 27 M_DQUOT */
141 SOX(ufsmount), KMZ_LOOKUPZONE, /* 28 M_UFSMNT */
142 0, KMZ_MALLOC, /* 29 M_CGSUM */
143 0, KMZ_MALLOC, /* 30 M_VMMAP */
144 0, KMZ_MALLOC, /* 31 M_VMMAPENT */
145 0, KMZ_MALLOC, /* 32 M_VMOBJ */
146 0, KMZ_MALLOC, /* 33 M_VMOBJHASH */
147 0, KMZ_MALLOC, /* 34 M_VMPMAP */
148 0, KMZ_MALLOC, /* 35 M_VMPVENT */
149 0, KMZ_MALLOC, /* 36 M_VMPAGER */
150 0, KMZ_MALLOC, /* 37 M_VMPGDATA */
151 SOS(file), KMZ_CREATEZONE, /* 38 M_FILE */
152 SOS(filedesc), KMZ_CREATEZONE, /* 39 M_FILEDESC */
153 SOX(lockf), KMZ_CREATEZONE, /* 40 M_LOCKF */
154 SOS(proc), KMZ_CREATEZONE, /* 41 M_PROC */
155 SOS(pcred), KMZ_CREATEZONE, /* 42 M_SUBPROC */
156 0, KMZ_MALLOC, /* 43 M_SEGMENT */
157 M_FFSNODE, KMZ_SHAREZONE, /* 44 M_LFSNODE */
158 SOS(inode), KMZ_CREATEZONE, /* 45 M_FFSNODE */
159 M_FFSNODE, KMZ_SHAREZONE, /* 46 M_MFSNODE */
160 SOS(nqlease), KMZ_CREATEZONE, /* 47 M_NQLEASE */
161 SOS(nqm), KMZ_CREATEZONE, /* 48 M_NQMHOST */
162 0, KMZ_MALLOC, /* 49 M_NETADDR */
163 SOX(nfssvc_sock),
164 KMZ_CREATEZONE, /* 50 M_NFSSVC */
165 SOS(nfsuid), KMZ_CREATEZONE, /* 51 M_NFSUID */
166 SOX(nfsrvcache),
167 KMZ_CREATEZONE, /* 52 M_NFSD */
168 SOX(ip_moptions),
169 KMZ_LOOKUPZONE, /* 53 M_IPMOPTS */
170 SOX(in_multi), KMZ_LOOKUPZONE, /* 54 M_IPMADDR */
171 SOX(ether_multi),
172 KMZ_LOOKUPZONE, /* 55 M_IFMADDR */
173 SOX(mrt), KMZ_CREATEZONE, /* 56 M_MRTABLE */
174 SOX(iso_mnt), KMZ_LOOKUPZONE, /* 57 M_ISOFSMNT */
175 SOS(iso_node), KMZ_CREATEZONE, /* 58 M_ISOFSNODE */
176 SOS(nfsrv_descript),
177 KMZ_CREATEZONE, /* 59 M_NFSRVDESC */
178 SOS(nfsdmap), KMZ_CREATEZONE, /* 60 M_NFSDIROFF */
179 SOS(fhandle), KMZ_LOOKUPZONE, /* 61 M_NFSBIGFH */
180 0, KMZ_MALLOC, /* 62 M_MSDOSFSMNT */
181 0, KMZ_MALLOC, /* 63 M_MSDOSFSFAT */
182 0, KMZ_MALLOC, /* 64 M_MSDOSFSNODE */
183 SOS(tty), KMZ_CREATEZONE, /* 65 M_TTYS */
184 0, KMZ_MALLOC, /* 66 M_EXEC */
185 0, KMZ_MALLOC, /* 67 M_MISCFSMNT */
186 0, KMZ_MALLOC, /* 68 M_MISCFSNODE */
187 0, KMZ_MALLOC, /* 69 M_ADOSFSMNT */
188 0, KMZ_MALLOC, /* 70 M_ADOSFSNODE */
189 0, KMZ_MALLOC, /* 71 M_ANODE */
190 SOX(buf), KMZ_CREATEZONE, /* 72 M_BUFHDR */
191 (NDFILE * OFILESIZE),
192 KMZ_CREATEZONE, /* 73 M_OFILETABL */
193 MCLBYTES, KMZ_CREATEZONE, /* 74 M_MCLUST */
194 SOX(hfsmount), KMZ_LOOKUPZONE, /* 75 M_HFSMNT */
9bccf70c
A
195 SOS(cnode), KMZ_CREATEZONE, /* 76 M_HFSNODE */
196 SOS(filefork), KMZ_CREATEZONE, /* 77 M_HFSFORK */
1c79356b
A
197 SOX(volfs_mntdata), KMZ_LOOKUPZONE, /* 78 M_VOLFSMNT */
198 SOS(volfs_vndata), KMZ_CREATEZONE, /* 79 M_VOLFSNODE */
199 0, KMZ_MALLOC, /* 80 M_TEMP */
200 0, KMZ_MALLOC, /* 81 M_SECA */
201 0, KMZ_MALLOC, /* 82 M_DEVFS */
202 0, KMZ_MALLOC, /* 83 M_IPFW */
203 0, KMZ_MALLOC, /* 84 M_UDFNODE */
204 0, KMZ_MALLOC, /* 85 M_UDFMOUNT */
205 0, KMZ_MALLOC, /* 86 M_IP6NDP */
206 0, KMZ_MALLOC, /* 87 M_IP6OPT */
9bccf70c
A
207 0, KMZ_MALLOC, /* 88 M_IP6MISC */
208 0, KMZ_MALLOC, /* 89 M_TSEGQ */
209 0, KMZ_MALLOC, /* 90 M_IGMP */
d7e50217
A
210 SOS(journal), KMZ_CREATEZONE, /* 91 M_JNL_JNL */
211 SOS(transaction), KMZ_CREATEZONE, /* 92 M_JNL_TR */
1c79356b
A
212#undef SOS
213#undef SOX
214};
215
216
217/*
218 * Initialize the kernel memory allocator
219 */
220void
221kmeminit(void)
222{
223 struct kmzones *kmz;
224
d7e50217
A
225 if ((sizeof(kmzones)/sizeof(kmzones[0])) != (sizeof(memname)/sizeof(memname[0]))) {
226 panic("kmeminit: kmzones has %d elements but memname has %d\n",
227 (sizeof(kmzones)/sizeof(kmzones[0])), (sizeof(memname)/sizeof(memname[0])));
228 }
229
1c79356b
A
230 kmz = kmzones;
231 while (kmz < &kmzones[M_LAST]) {
232/* XXX */
233 if (kmz->kz_elemsize == -1)
234 ;
235 else
236/* XXX */
237 if (kmz->kz_zalloczone == KMZ_CREATEZONE) {
238 kmz->kz_zalloczone = zinit(kmz->kz_elemsize,
239 1024 * 1024, PAGE_SIZE,
240 memname[kmz - kmzones]);
241 }
242 else if (kmz->kz_zalloczone == KMZ_LOOKUPZONE)
243 kmz->kz_zalloczone = kalloc_zone(kmz->kz_elemsize);
244
245 kmz++;
246 }
247
248 kmz = kmzones;
249 while (kmz < &kmzones[M_LAST]) {
250/* XXX */
251 if (kmz->kz_elemsize == -1)
252 ;
253 else
254/* XXX */
255 if (kmz->kz_zalloczone == KMZ_SHAREZONE) {
256 kmz->kz_zalloczone =
257 kmzones[kmz->kz_elemsize].kz_zalloczone;
258 kmz->kz_elemsize =
259 kmzones[kmz->kz_elemsize].kz_elemsize;
260 }
261
262 kmz++;
263 }
264}
265
266#define MDECL(reqlen) \
267union { \
268 struct _mhead hdr; \
269 char _m[(reqlen) + sizeof (struct _mhead)]; \
270}
271
272struct _mhead {
273 size_t mlen;
274 char dat[0];
275};
276
9bccf70c
A
277#define ZEROSIZETOKEN 0xFADEDFAD
278
1c79356b
A
279void *_MALLOC(
280 size_t size,
281 int type,
282 int flags)
283{
284 MDECL(size) *mem;
285 size_t memsize = sizeof (*mem);
286
287 if (type >= M_LAST)
288 panic("_malloc TYPE");
289
9bccf70c
A
290 /*
291 * On zero request we do not return zero as that
292 * could be mistaken for ENOMEM.
293 */
1c79356b 294 if (size == 0)
9bccf70c 295 return (ZEROSIZETOKEN);
1c79356b
A
296
297 if (flags & M_NOWAIT) {
298 mem = (void *)kalloc_noblock(memsize);
299 } else {
300 mem = (void *)kalloc(memsize);
301 }
302 if (!mem)
303 return (0);
304
305 mem->hdr.mlen = memsize;
306
307 return (mem->hdr.dat);
308}
309
310void _FREE(
311 void *addr,
312 int type)
313{
314 struct _mhead *hdr;
315
316 if (type >= M_LAST)
317 panic("_free TYPE");
318
9bccf70c 319 if (addr == (void *)ZEROSIZETOKEN)
1c79356b 320 return;
9bccf70c
A
321 if (!addr)
322 return; /* correct (convenient bsd kernel legacy) */
1c79356b
A
323
324 hdr = addr; hdr--;
325 kfree((vm_offset_t)hdr, hdr->mlen);
326}
327
328void *_MALLOC_ZONE(
329 size_t size,
330 int type,
331 int flags)
332{
333 struct kmzones *kmz;
334 void *elem;
335
336 if (type >= M_LAST)
337 panic("_malloc_zone TYPE");
338
339 kmz = &kmzones[type];
340 if (kmz->kz_zalloczone == KMZ_MALLOC)
341 panic("_malloc_zone ZONE");
342
343/* XXX */
344 if (kmz->kz_elemsize == -1)
345 panic("_malloc_zone XXX");
346/* XXX */
347 if (size == kmz->kz_elemsize)
348 if (flags & M_NOWAIT) {
349 elem = (void *)zalloc_noblock(kmz->kz_zalloczone);
350 } else {
351 elem = (void *)zalloc(kmz->kz_zalloczone);
352 }
353 else
354 if (flags & M_NOWAIT) {
355 elem = (void *)kalloc_noblock(size);
356 } else {
357 elem = (void *)kalloc(size);
358 }
359
360 return (elem);
361}
362
363void _FREE_ZONE(
364 void *elem,
365 size_t size,
366 int type)
367{
368 struct kmzones *kmz;
369
370 if (type >= M_LAST)
371 panic("FREE_SIZE");
372
373 kmz = &kmzones[type];
374 if (kmz->kz_zalloczone == KMZ_MALLOC)
375 panic("free_zone ZONE");
376
377/* XXX */
378 if (kmz->kz_elemsize == -1)
379 panic("FREE_SIZE XXX");
380/* XXX */
381 if (size == kmz->kz_elemsize)
382 zfree(kmz->kz_zalloczone, (vm_offset_t)elem);
383 else
384 kfree((vm_offset_t)elem, size);
385}