2 * Copyright (c) 2000-2004 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@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
60 * Author: Avadis Tevanian, Jr.
63 * General kernel memory allocator. This allocator is designed
64 * to be used by the kernel to manage dynamic memory fast.
67 #include <zone_debug.h>
69 #include <mach/boolean.h>
70 #include <mach/machine/vm_types.h>
71 #include <mach/vm_param.h>
72 #include <kern/misc_protos.h>
73 #include <kern/zalloc.h>
74 #include <kern/kalloc.h>
75 #include <kern/lock.h>
76 #include <vm/vm_kern.h>
77 #include <vm/vm_object.h>
78 #include <vm/vm_map.h>
79 #include <libkern/OSMalloc.h>
82 zone_t
kalloc_zone(vm_size_t
);
86 vm_size_t kalloc_map_size
= 16 * 1024 * 1024;
88 vm_size_t kalloc_max_prerounded
;
89 vm_size_t kalloc_kernmap_size
; /* size of kallocs that can come from kernel map */
91 unsigned int kalloc_large_inuse
;
92 vm_size_t kalloc_large_total
;
93 vm_size_t kalloc_large_max
;
94 vm_size_t kalloc_largest_allocated
= 0;
97 * All allocations of size less than kalloc_max are rounded to the
98 * next highest power of 2. This allocator is built on top of
99 * the zone allocator. A zone is created for each potential size
100 * that we are willing to get in small blocks.
102 * We assume that kalloc_max is not greater than 64K;
103 * thus 16 is a safe array size for k_zone and k_zone_name.
105 * Note that kalloc_max is somewhat confusingly named.
106 * It represents the first power of two for which no zone exists.
107 * kalloc_max_prerounded is the smallest allocation size, before
108 * rounding, for which no zone exists.
109 * Also if the allocation size is more than kalloc_kernmap_size
110 * then allocate from kernel map rather than kalloc_map.
113 int first_k_zone
= -1;
114 struct zone
*k_zone
[16];
115 static const char *k_zone_name
[16] = {
116 "kalloc.1", "kalloc.2",
117 "kalloc.4", "kalloc.8",
118 "kalloc.16", "kalloc.32",
119 "kalloc.64", "kalloc.128",
120 "kalloc.256", "kalloc.512",
121 "kalloc.1024", "kalloc.2048",
122 "kalloc.4096", "kalloc.8192",
123 "kalloc.16384", "kalloc.32768"
127 * Max number of elements per zone. zinit rounds things up correctly
128 * Doing things this way permits each zone to have a different maximum size
129 * based on need, rather than just guessing; it also
130 * means its patchable in case you're wrong!
132 unsigned long k_zone_max
[16] = {
143 1024, /* 1024 Byte */
144 1024, /* 2048 Byte */
145 1024, /* 4096 Byte */
146 4096, /* 8192 Byte */
151 /* forward declarations */
152 void * kalloc_canblock(
157 /* OSMalloc local data declarations */
159 queue_head_t OSMalloc_tag_list
;
161 decl_simple_lock_data(static,OSMalloc_tag_lock
)
163 /* OSMalloc forward declarations */
164 void OSMalloc_init(void);
165 void OSMalloc_Tagref(OSMallocTag tag
);
166 void OSMalloc_Tagrele(OSMallocTag tag
);
169 * Initialize the memory allocator. This should be called only
170 * once on a system wide basis (i.e. first processor to get here
171 * does the initialization).
173 * This initializes all of the zones.
180 kern_return_t retval
;
185 retval
= kmem_suballoc(kernel_map
, &min
, kalloc_map_size
,
186 FALSE
, VM_FLAGS_ANYWHERE
, &kalloc_map
);
188 if (retval
!= KERN_SUCCESS
)
189 panic("kalloc_init: kmem_suballoc failed");
192 * Ensure that zones up to size 8192 bytes exist.
193 * This is desirable because messages are allocated
194 * with kalloc, and messages up through size 8192 are common.
197 if (PAGE_SIZE
< 16*1024)
198 kalloc_max
= 16*1024;
200 kalloc_max
= PAGE_SIZE
;
201 kalloc_max_prerounded
= kalloc_max
/ 2 + 1;
202 /* size it to be more than 16 times kalloc_max (256k) for allocations from kernel map */
203 kalloc_kernmap_size
= (kalloc_max
* 16) + 1;
206 * Allocate a zone for each size we are going to handle.
207 * We specify non-paged memory.
209 for (i
= 0, size
= 1; size
< kalloc_max
; i
++, size
<<= 1) {
210 if (size
< KALLOC_MINSIZE
) {
214 if (size
== KALLOC_MINSIZE
) {
217 k_zone
[i
] = zinit(size
, k_zone_max
[i
] * size
, size
,
229 register vm_size_t allocsize
;
230 vm_map_t alloc_map
= VM_MAP_NULL
;
233 * If size is too large for a zone, then use kmem_alloc.
234 * (We use kmem_alloc instead of kmem_alloc_wired so that
235 * krealloc can use kmem_realloc.)
238 if (size
>= kalloc_max_prerounded
) {
241 /* kmem_alloc could block so we return if noblock */
246 if (size
>= kalloc_kernmap_size
) {
247 alloc_map
= kernel_map
;
249 if (size
> kalloc_largest_allocated
)
250 kalloc_largest_allocated
= size
;
252 alloc_map
= kalloc_map
;
254 if (kmem_alloc(alloc_map
, (vm_offset_t
*)&addr
, size
) != KERN_SUCCESS
)
258 kalloc_large_inuse
++;
259 kalloc_large_total
+= size
;
261 if (kalloc_large_total
> kalloc_large_max
)
262 kalloc_large_max
= kalloc_large_total
;
267 /* compute the size of the block that we will actually allocate */
269 allocsize
= KALLOC_MINSIZE
;
270 zindex
= first_k_zone
;
271 while (allocsize
< size
) {
276 /* allocate from the appropriate zone */
277 assert(allocsize
< kalloc_max
);
278 return(zalloc_canblock(k_zone
[zindex
], canblock
));
285 return( kalloc_canblock(size
, TRUE
) );
292 return( kalloc_canblock(size
, FALSE
) );
304 register vm_size_t allocsize
;
306 vm_map_t alloc_map
= VM_MAP_NULL
;
308 /* can only be used for increasing allocation size */
310 assert(new_size
> old_size
);
312 /* if old_size is zero, then we are simply allocating */
316 naddr
= kalloc(new_size
);
322 /* if old block was kmem_alloc'd, then use kmem_realloc if necessary */
324 if (old_size
>= kalloc_max_prerounded
) {
325 if (old_size
>= kalloc_kernmap_size
)
326 alloc_map
= kernel_map
;
328 alloc_map
= kalloc_map
;
330 old_size
= round_page(old_size
);
331 new_size
= round_page(new_size
);
332 if (new_size
> old_size
) {
334 if (KERN_SUCCESS
!= kmem_realloc(alloc_map
,
335 (vm_offset_t
)*addrp
, old_size
,
336 (vm_offset_t
*)&naddr
, new_size
)) {
337 panic("krealloc: kmem_realloc");
342 *addrp
= (void *) naddr
;
344 /* kmem_realloc() doesn't free old page range. */
345 kmem_free(alloc_map
, (vm_offset_t
)*addrp
, old_size
);
347 kalloc_large_total
+= (new_size
- old_size
);
349 if (kalloc_large_total
> kalloc_large_max
)
350 kalloc_large_max
= kalloc_large_total
;
356 /* compute the size of the block that we actually allocated */
358 allocsize
= KALLOC_MINSIZE
;
359 zindex
= first_k_zone
;
360 while (allocsize
< old_size
) {
365 /* if new size fits in old block, then return */
367 if (new_size
<= allocsize
) {
371 /* if new size does not fit in zone, kmem_alloc it, else zalloc it */
374 if (new_size
>= kalloc_max_prerounded
) {
375 if (new_size
>= kalloc_kernmap_size
)
376 alloc_map
= kernel_map
;
378 alloc_map
= kalloc_map
;
379 if (KERN_SUCCESS
!= kmem_alloc(alloc_map
,
380 (vm_offset_t
*)&naddr
, new_size
)) {
381 panic("krealloc: kmem_alloc");
386 kalloc_large_inuse
++;
387 kalloc_large_total
+= new_size
;
389 if (kalloc_large_total
> kalloc_large_max
)
390 kalloc_large_max
= kalloc_large_total
;
392 register int new_zindex
;
395 new_zindex
= zindex
+ 1;
396 while (allocsize
< new_size
) {
400 naddr
= zalloc(k_zone
[new_zindex
]);
404 /* copy existing data */
406 bcopy((const char *)*addrp
, (char *)naddr
, old_size
);
408 /* free old block, and return */
410 zfree(k_zone
[zindex
], *addrp
);
412 /* set up new address */
414 *addrp
= (void *) naddr
;
423 register vm_size_t allocsize
;
425 /* size must not be too large for a zone */
427 if (size
>= kalloc_max_prerounded
) {
428 /* This will never work, so we might as well panic */
432 /* compute the size of the block that we will actually allocate */
434 allocsize
= KALLOC_MINSIZE
;
435 zindex
= first_k_zone
;
436 while (allocsize
< size
) {
441 /* allocate from the appropriate zone */
443 assert(allocsize
< kalloc_max
);
444 return(zget(k_zone
[zindex
]));
453 register vm_size_t freesize
;
454 vm_map_t alloc_map
= VM_MAP_NULL
;
456 /* if size was too large for a zone, then use kmem_free */
458 if (size
>= kalloc_max_prerounded
) {
459 if (size
>= kalloc_kernmap_size
) {
460 alloc_map
= kernel_map
;
462 if (size
> kalloc_largest_allocated
)
464 * work around double FREEs of small MALLOCs
465 * this used to end up being a nop
466 * since the pointer being freed from an
467 * alloc backed by the zalloc world could
468 * never show up in the kalloc_map... however,
469 * the kernel_map is a different issue... since it
470 * was released back into the zalloc pool, a pointer
471 * would have gotten written over the 'size' that
472 * the MALLOC was retaining in the first 4 bytes of
473 * the underlying allocation... that pointer ends up
474 * looking like a really big size on the 2nd FREE and
475 * pushes the kfree into the kernel_map... we
476 * end up removing a ton of virutal space before we panic
477 * this check causes us to ignore the kfree for a size
478 * that must be 'bogus'... note that it might not be due
479 * to the above scenario, but it would still be wrong and
480 * cause serious damage.
484 alloc_map
= kalloc_map
;
485 kmem_free(alloc_map
, (vm_offset_t
)data
, size
);
487 kalloc_large_total
-= size
;
488 kalloc_large_inuse
--;
493 /* compute the size of the block that we actually allocated from */
495 freesize
= KALLOC_MINSIZE
;
496 zindex
= first_k_zone
;
497 while (freesize
< size
) {
502 /* free to the appropriate zone */
504 assert(freesize
< kalloc_max
);
505 zfree(k_zone
[zindex
], data
);
513 register int zindex
= 0;
514 register vm_size_t allocsize
;
516 /* compute the size of the block that we will actually allocate */
519 if (size
<= kalloc_max
) {
520 allocsize
= KALLOC_MINSIZE
;
521 zindex
= first_k_zone
;
522 while (allocsize
< size
) {
526 return (k_zone
[zindex
]);
534 kalloc_fake_zone_info(int *count
, vm_size_t
*cur_size
, vm_size_t
*max_size
, vm_size_t
*elem_size
,
535 vm_size_t
*alloc_size
, int *collectable
, int *exhaustable
)
537 *count
= kalloc_large_inuse
;
538 *cur_size
= kalloc_large_total
;
539 *max_size
= kalloc_large_max
;
540 *elem_size
= kalloc_large_total
/ kalloc_large_inuse
;
541 *alloc_size
= kalloc_large_total
/ kalloc_large_inuse
;
551 queue_init(&OSMalloc_tag_list
);
552 simple_lock_init(&OSMalloc_tag_lock
, 0);
562 OSMTag
= (OSMallocTag
)kalloc(sizeof(*OSMTag
));
564 bzero((void *)OSMTag
, sizeof(*OSMTag
));
566 if (flags
& OSMT_PAGEABLE
)
567 OSMTag
->OSMT_attr
= OSMT_ATTR_PAGEABLE
;
569 OSMTag
->OSMT_refcnt
= 1;
571 strncpy(OSMTag
->OSMT_name
, str
, OSMT_MAX_NAME
);
573 simple_lock(&OSMalloc_tag_lock
);
574 enqueue_tail(&OSMalloc_tag_list
, (queue_entry_t
)OSMTag
);
575 simple_unlock(&OSMalloc_tag_lock
);
576 OSMTag
->OSMT_state
= OSMT_VALID
;
584 if (!((tag
->OSMT_state
& OSMT_VALID_MASK
) == OSMT_VALID
))
585 panic("OSMalloc_Tagref(): bad state 0x%08X\n",tag
->OSMT_state
);
587 (void)hw_atomic_add((uint32_t *)(&tag
->OSMT_refcnt
), 1);
594 if (!((tag
->OSMT_state
& OSMT_VALID_MASK
) == OSMT_VALID
))
595 panic("OSMalloc_Tagref(): bad state 0x%08X\n",tag
->OSMT_state
);
597 if (hw_atomic_sub((uint32_t *)(&tag
->OSMT_refcnt
), 1) == 0) {
598 if (hw_compare_and_store(OSMT_VALID
|OSMT_RELEASED
, OSMT_VALID
|OSMT_RELEASED
, &tag
->OSMT_state
)) {
599 simple_lock(&OSMalloc_tag_lock
);
600 (void)remque((queue_entry_t
)tag
);
601 simple_unlock(&OSMalloc_tag_lock
);
602 kfree((void*)tag
, sizeof(*tag
));
604 panic("OSMalloc_Tagrele(): refcnt 0\n");
612 if (!hw_compare_and_store(OSMT_VALID
, OSMT_VALID
|OSMT_RELEASED
, &tag
->OSMT_state
))
613 panic("OSMalloc_Tagfree(): bad state 0x%08X\n", tag
->OSMT_state
);
615 if (hw_atomic_sub((uint32_t *)(&tag
->OSMT_refcnt
), 1) == 0) {
616 simple_lock(&OSMalloc_tag_lock
);
617 (void)remque((queue_entry_t
)tag
);
618 simple_unlock(&OSMalloc_tag_lock
);
619 kfree((void*)tag
, sizeof(*tag
));
631 OSMalloc_Tagref(tag
);
632 if ((tag
->OSMT_attr
& OSMT_PAGEABLE
)
633 && (size
& ~PAGE_MASK
)) {
635 if ((kr
= kmem_alloc_pageable(kernel_map
, (vm_offset_t
*)&addr
, size
)) != KERN_SUCCESS
)
636 panic("OSMalloc(): kmem_alloc_pageable() failed 0x%08X\n", kr
);
638 addr
= kalloc((vm_size_t
)size
);
650 if (tag
->OSMT_attr
& OSMT_PAGEABLE
)
653 OSMalloc_Tagref(tag
);
654 /* XXX: use non-blocking kalloc for now */
655 addr
= kalloc_noblock((vm_size_t
)size
);
657 OSMalloc_Tagrele(tag
);
669 if (tag
->OSMT_attr
& OSMT_PAGEABLE
)
672 OSMalloc_Tagref(tag
);
673 addr
= kalloc_noblock((vm_size_t
)size
);
675 OSMalloc_Tagrele(tag
);
686 if ((tag
->OSMT_attr
& OSMT_PAGEABLE
)
687 && (size
& ~PAGE_MASK
)) {
688 kmem_free(kernel_map
, (vm_offset_t
)addr
, size
);
690 kfree((void*)addr
, size
);
692 OSMalloc_Tagrele(tag
);