1/* 2 * Copyright (c) 1987, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 17 unchanged lines hidden (view full) --- 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)kern_malloc.c 8.3 (Berkeley) 1/4/94 |
34 * $FreeBSD: head/sys/kern/kern_malloc.c 103531 2002-09-18 08:26:30Z jeff $ |
35 */ 36 37#include "opt_vm.h" 38 39#include <sys/param.h> 40#include <sys/systm.h> 41#include <sys/kernel.h> 42#include <sys/lock.h> 43#include <sys/malloc.h> 44#include <sys/mbuf.h> 45#include <sys/mutex.h> 46#include <sys/vmmeter.h> 47#include <sys/proc.h> 48#include <sys/sysctl.h> 49 50#include <vm/vm.h> |
51#include <vm/pmap.h> |
52#include <vm/vm_param.h> 53#include <vm/vm_kern.h> 54#include <vm/vm_extern.h> |
55#include <vm/vm_map.h> |
56#include <vm/vm_page.h> |
57#include <vm/uma.h> 58#include <vm/uma_int.h> 59#include <vm/uma_dbg.h> 60 61#if defined(INVARIANTS) && defined(__i386__) 62#include <machine/cpu.h> 63#endif 64 --- 51 unchanged lines hidden (view full) --- 116 {32768, "32768", NULL}, 117 {65536, "65536", NULL}, 118 {0, NULL}, 119}; 120 121u_int vm_kmem_size; 122 123/* |
124 * The malloc_mtx protects the kmemstatistics linked list. |
125 */ 126 127struct mtx malloc_mtx; 128 129#ifdef MALLOC_PROFILE 130uint64_t krequests[KMEM_ZSIZE + 1]; 131 132static int sysctl_kern_mprof(SYSCTL_HANDLER_ARGS); --- 68 unchanged lines hidden (view full) --- 201 * 202 * This routine may not block. 203 */ 204void 205free(addr, type) 206 void *addr; 207 struct malloc_type *type; 208{ |
209 register struct malloc_type *ksp = type; |
210 uma_slab_t slab; |
211 u_long size; |
212 213 /* free(NULL, ...) does nothing */ 214 if (addr == NULL) 215 return; 216 217 size = 0; 218 |
219 slab = vtoslab((vm_offset_t)addr & (~UMA_SLAB_MASK)); |
220 221 if (slab == NULL) 222 panic("free: address %p(%p) has not been allocated.\n", |
223 addr, (void *)((u_long)addr & (~UMA_SLAB_MASK))); |
224 |
225 |
226 if (!(slab->us_flags & UMA_SLAB_MALLOC)) { 227#ifdef INVARIANTS 228 struct malloc_type **mtp = addr; 229#endif 230 size = slab->us_zone->uz_size; 231#ifdef INVARIANTS 232 /* 233 * Cache a pointer to the malloc_type that most recently freed --- 33 unchanged lines hidden (view full) --- 267 uma_slab_t slab; 268 unsigned long alloc; 269 void *newaddr; 270 271 /* realloc(NULL, ...) is equivalent to malloc(...) */ 272 if (addr == NULL) 273 return (malloc(size, type, flags)); 274 |
275 slab = vtoslab((vm_offset_t)addr & ~(UMA_SLAB_MASK)); |
276 277 /* Sanity check */ 278 KASSERT(slab != NULL, 279 ("realloc: address %p out of range", (void *)addr)); 280 281 /* Get the size of the original block */ 282 if (slab->us_zone) 283 alloc = slab->us_zone->uz_size; --- 38 unchanged lines hidden (view full) --- 322/* ARGSUSED*/ 323static void 324kmeminit(dummy) 325 void *dummy; 326{ 327 u_int8_t indx; 328 u_long npg; 329 u_long mem_size; |
330 int i; 331 332 mtx_init(&malloc_mtx, "malloc", NULL, MTX_DEF); 333 334 /* 335 * Try to auto-tune the kernel memory size, so that it is 336 * more applicable for a wider range of machine sizes. 337 * On an X86, a VM_KMEM_SIZE_SCALE value of 4 is good, while --- 39 unchanged lines hidden (view full) --- 377 */ 378 npg = (nmbufs * MSIZE + nmbclusters * MCLBYTES + nmbcnt * 379 sizeof(u_int) + vm_kmem_size) / PAGE_SIZE; 380 381 kmem_map = kmem_suballoc(kernel_map, (vm_offset_t *)&kmembase, 382 (vm_offset_t *)&kmemlimit, (vm_size_t)(npg * PAGE_SIZE)); 383 kmem_map->system_map = 1; 384 |
385 uma_startup2(); |
386 |
387 for (i = 0, indx = 0; kmemzones[indx].kz_size != 0; indx++) { 388 int size = kmemzones[indx].kz_size; 389 char *name = kmemzones[indx].kz_name; 390 391 kmemzones[indx].kz_zone = uma_zcreate(name, size, 392#ifdef INVARIANTS 393 mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini, 394#else --- 200 unchanged lines hidden --- |