Deleted Added
full compact
kern_malloc.c (97655) kern_malloc.c (103531)
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
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 97655 2002-05-31 09:41:09Z robert $
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>
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>
51#include <vm/vm_param.h>
52#include <vm/vm_kern.h>
53#include <vm/vm_extern.h>
52#include <vm/vm_param.h>
53#include <vm/vm_kern.h>
54#include <vm/vm_extern.h>
54#include <vm/pmap.h>
55#include <vm/vm_map.h>
55#include <vm/vm_map.h>
56#include <vm/vm_page.h>
56#include <vm/uma.h>
57#include <vm/uma_int.h>
58#include <vm/uma_dbg.h>
59
60#if defined(INVARIANTS) && defined(__i386__)
61#include <machine/cpu.h>
62#endif
63

--- 51 unchanged lines hidden (view full) ---

115 {32768, "32768", NULL},
116 {65536, "65536", NULL},
117 {0, NULL},
118};
119
120u_int vm_kmem_size;
121
122/*
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/*
123 * The malloc_mtx protects the kmemstatistics linked list as well as the
124 * mallochash.
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{
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;
209 uma_slab_t slab;
210 uma_slab_t slab;
210 void *mem;
211 u_long size;
211 u_long size;
212 register struct malloc_type *ksp = type;
213
214 /* free(NULL, ...) does nothing */
215 if (addr == NULL)
216 return;
217
218 size = 0;
219
212
213 /* free(NULL, ...) does nothing */
214 if (addr == NULL)
215 return;
216
217 size = 0;
218
220 mem = (void *)((u_long)addr & (~UMA_SLAB_MASK));
221 mtx_lock(&malloc_mtx);
222 slab = hash_sfind(mallochash, mem);
223 mtx_unlock(&malloc_mtx);
219 slab = vtoslab((vm_offset_t)addr & (~UMA_SLAB_MASK));
224
225 if (slab == NULL)
226 panic("free: address %p(%p) has not been allocated.\n",
220
221 if (slab == NULL)
222 panic("free: address %p(%p) has not been allocated.\n",
227 addr, mem);
223 addr, (void *)((u_long)addr & (~UMA_SLAB_MASK)));
228
224
225
229 if (!(slab->us_flags & UMA_SLAB_MALLOC)) {
230#ifdef INVARIANTS
231 struct malloc_type **mtp = addr;
232#endif
233 size = slab->us_zone->uz_size;
234#ifdef INVARIANTS
235 /*
236 * Cache a pointer to the malloc_type that most recently freed

--- 33 unchanged lines hidden (view full) ---

270 uma_slab_t slab;
271 unsigned long alloc;
272 void *newaddr;
273
274 /* realloc(NULL, ...) is equivalent to malloc(...) */
275 if (addr == NULL)
276 return (malloc(size, type, flags));
277
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
278 mtx_lock(&malloc_mtx);
279 slab = hash_sfind(mallochash,
280 (void *)((u_long)addr & ~(UMA_SLAB_MASK)));
281 mtx_unlock(&malloc_mtx);
275 slab = vtoslab((vm_offset_t)addr & ~(UMA_SLAB_MASK));
282
283 /* Sanity check */
284 KASSERT(slab != NULL,
285 ("realloc: address %p out of range", (void *)addr));
286
287 /* Get the size of the original block */
288 if (slab->us_zone)
289 alloc = slab->us_zone->uz_size;

--- 38 unchanged lines hidden (view full) ---

328/* ARGSUSED*/
329static void
330kmeminit(dummy)
331 void *dummy;
332{
333 u_int8_t indx;
334 u_long npg;
335 u_long mem_size;
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;
336 void *hashmem;
337 u_long hashsize;
338 int highbit;
339 int bits;
340 int i;
341
342 mtx_init(&malloc_mtx, "malloc", NULL, MTX_DEF);
343
344 /*
345 * Try to auto-tune the kernel memory size, so that it is
346 * more applicable for a wider range of machine sizes.
347 * On an X86, a VM_KMEM_SIZE_SCALE value of 4 is good, while

--- 39 unchanged lines hidden (view full) ---

387 */
388 npg = (nmbufs * MSIZE + nmbclusters * MCLBYTES + nmbcnt *
389 sizeof(u_int) + vm_kmem_size) / PAGE_SIZE;
390
391 kmem_map = kmem_suballoc(kernel_map, (vm_offset_t *)&kmembase,
392 (vm_offset_t *)&kmemlimit, (vm_size_t)(npg * PAGE_SIZE));
393 kmem_map->system_map = 1;
394
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
395 hashsize = npg * sizeof(void *);
385 uma_startup2();
396
386
397 highbit = 0;
398 bits = 0;
399 /* The hash size must be a power of two */
400 for (i = 0; i < 8 * sizeof(hashsize); i++)
401 if (hashsize & (1 << i)) {
402 highbit = i;
403 bits++;
404 }
405 if (bits > 1)
406 hashsize = 1 << (highbit);
407
408 hashmem = (void *)kmem_alloc(kernel_map, (vm_size_t)hashsize);
409 uma_startup2(hashmem, hashsize / sizeof(void *));
410
411 for (i = 0, indx = 0; kmemzones[indx].kz_size != 0; indx++) {
412 int size = kmemzones[indx].kz_size;
413 char *name = kmemzones[indx].kz_name;
414
415 kmemzones[indx].kz_zone = uma_zcreate(name, size,
416#ifdef INVARIANTS
417 mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini,
418#else

--- 200 unchanged lines hidden ---
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 ---