kern_malloc.c (b40ce4165d5eb3a5de1515245055350ae3dbab8e) | kern_malloc.c (44a8ff315e2a51614155fcbb86ab0577bb1f1152) |
---|---|
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 --- 43 unchanged lines hidden (view full) --- 52#include <vm/vm_extern.h> 53#include <vm/pmap.h> 54#include <vm/vm_map.h> 55 56#if defined(INVARIANTS) && defined(__i386__) 57#include <machine/cpu.h> 58#endif 59 | 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 --- 43 unchanged lines hidden (view full) --- 52#include <vm/vm_extern.h> 53#include <vm/pmap.h> 54#include <vm/vm_map.h> 55 56#if defined(INVARIANTS) && defined(__i386__) 57#include <machine/cpu.h> 58#endif 59 |
60/* 61 * When realloc() is called, if the new size is sufficiently smaller than 62 * the old size, realloc() will allocate a new, smaller block to avoid 63 * wasting memory. 'Sufficiently smaller' is defined as: newsize <= 64 * oldsize / 2^n, where REALLOC_FRACTION defines the value of 'n'. 65 */ 66#ifndef REALLOC_FRACTION 67#define REALLOC_FRACTION 1 /* new block if <= half the size */ 68#endif 69 |
|
60MALLOC_DEFINE(M_CACHE, "cache", "Various Dynamically allocated caches"); 61MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory"); 62MALLOC_DEFINE(M_TEMP, "temp", "misc temporary data buffers"); 63 64MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options"); 65MALLOC_DEFINE(M_IP6NDP, "ip6ndp", "IPv6 Neighbor Discovery"); 66 67static void kmeminit __P((void *)); --- 221 unchanged lines hidden (view full) --- 289 long size; 290 int s; 291#ifdef INVARIANTS 292 struct freelist *fp; 293 long *end, *lp, alloc, copysize; 294#endif 295 register struct malloc_type *ksp = type; 296 | 70MALLOC_DEFINE(M_CACHE, "cache", "Various Dynamically allocated caches"); 71MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory"); 72MALLOC_DEFINE(M_TEMP, "temp", "misc temporary data buffers"); 73 74MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options"); 75MALLOC_DEFINE(M_IP6NDP, "ip6ndp", "IPv6 Neighbor Discovery"); 76 77static void kmeminit __P((void *)); --- 221 unchanged lines hidden (view full) --- 299 long size; 300 int s; 301#ifdef INVARIANTS 302 struct freelist *fp; 303 long *end, *lp, alloc, copysize; 304#endif 305 register struct malloc_type *ksp = type; 306 |
307 /* free(NULL, ...) does nothing */ 308 if (addr == NULL) 309 return; 310 |
|
297 KASSERT(kmembase <= (char *)addr && (char *)addr < kmemlimit, 298 ("free: address %p out of range", (void *)addr)); 299 kup = btokup(addr); 300 size = 1 << kup->ku_indx; 301 kbp = &bucket[kup->ku_indx]; 302 s = splmem(); 303 mtx_lock(&malloc_mtx); 304#ifdef INVARIANTS --- 90 unchanged lines hidden (view full) --- 395 kbp->kb_next = addr; 396 } 397#endif 398 splx(s); 399 mtx_unlock(&malloc_mtx); 400} 401 402/* | 311 KASSERT(kmembase <= (char *)addr && (char *)addr < kmemlimit, 312 ("free: address %p out of range", (void *)addr)); 313 kup = btokup(addr); 314 size = 1 << kup->ku_indx; 315 kbp = &bucket[kup->ku_indx]; 316 s = splmem(); 317 mtx_lock(&malloc_mtx); 318#ifdef INVARIANTS --- 90 unchanged lines hidden (view full) --- 409 kbp->kb_next = addr; 410 } 411#endif 412 splx(s); 413 mtx_unlock(&malloc_mtx); 414} 415 416/* |
417 * realloc: change the size of a memory block 418 */ 419void * 420realloc(addr, size, type, flags) 421 void *addr; 422 unsigned long size; 423 struct malloc_type *type; 424 int flags; 425{ 426 struct kmemusage *kup; 427 unsigned long alloc; 428 void *newaddr; 429 430 /* realloc(NULL, ...) is equivalent to malloc(...) */ 431 if (addr == NULL) 432 return (malloc(size, type, flags)); 433 434 /* Sanity check */ 435 KASSERT(kmembase <= (char *)addr && (char *)addr < kmemlimit, 436 ("realloc: address %p out of range", (void *)addr)); 437 438 /* Get the size of the original block */ 439 kup = btokup(addr); 440 alloc = 1 << kup->ku_indx; 441 if (alloc > MAXALLOCSAVE) 442 alloc = kup->ku_pagecnt << PAGE_SHIFT; 443 444 /* Reuse the original block if appropriate */ 445 if (size <= alloc 446 && (size > (alloc >> REALLOC_FRACTION) || alloc == MINALLOCSIZE)) 447 return (addr); 448 449 /* Allocate a new, bigger (or smaller) block */ 450 if ((newaddr = malloc(size, type, flags)) == NULL) 451 return (NULL); 452 453 /* Copy over original contents */ 454 bcopy(addr, newaddr, min(size, alloc)); 455 free(addr, type); 456 return (newaddr); 457} 458 459/* 460 * reallocf: same as realloc() but free memory on failure. 461 */ 462void * 463reallocf(addr, size, type, flags) 464 void *addr; 465 unsigned long size; 466 struct malloc_type *type; 467 int flags; 468{ 469 void *mem; 470 471 if ((mem = realloc(addr, size, type, flags)) == NULL) 472 free(addr, type); 473 return (mem); 474} 475 476/* |
|
403 * Initialize the kernel memory allocator 404 */ 405/* ARGSUSED*/ 406static void 407kmeminit(dummy) 408 void *dummy; 409{ 410 register long indx; --- 155 unchanged lines hidden --- | 477 * Initialize the kernel memory allocator 478 */ 479/* ARGSUSED*/ 480static void 481kmeminit(dummy) 482 void *dummy; 483{ 484 register long indx; --- 155 unchanged lines hidden --- |