1999-03-24 08:50:49 +03:00
|
|
|
/* $NetBSD: kern_malloc.c,v 1.41 1999/03/24 05:51:23 mrg Exp $ */
|
1994-06-29 10:29:24 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1998-09-12 21:20:02 +04:00
|
|
|
* Copyright (c) 1996 Christopher G. Demetriou. All rights reserved.
|
1994-05-13 12:32:17 +04:00
|
|
|
* Copyright (c) 1987, 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1998-03-01 05:20:01 +03:00
|
|
|
* @(#)kern_malloc.c 8.4 (Berkeley) 5/20/95
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
|
|
|
|
1998-05-20 05:32:29 +04:00
|
|
|
#include "opt_lockdebug.h"
|
1998-02-10 17:08:44 +03:00
|
|
|
|
1993-12-18 06:59:02 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/proc.h>
|
1994-05-13 12:32:17 +04:00
|
|
|
#include <sys/map.h>
|
1993-12-18 06:59:02 +03:00
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
1996-02-04 05:15:01 +03:00
|
|
|
#include <sys/systm.h>
|
1993-12-18 06:59:02 +03:00
|
|
|
|
|
|
|
#include <vm/vm.h>
|
|
|
|
#include <vm/vm_kern.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1998-02-05 10:59:28 +03:00
|
|
|
#include <uvm/uvm_extern.h>
|
|
|
|
|
|
|
|
static struct vm_map kmem_map_store;
|
|
|
|
vm_map_t kmem_map = NULL;
|
|
|
|
|
1997-02-03 00:22:16 +03:00
|
|
|
#include "opt_kmemstats.h"
|
1998-01-22 01:24:32 +03:00
|
|
|
#include "opt_malloclog.h"
|
1997-02-03 00:22:16 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
struct kmembuckets bucket[MINBUCKET + 16];
|
1994-05-13 12:32:17 +04:00
|
|
|
struct kmemstats kmemstats[M_LAST];
|
1993-03-21 12:45:37 +03:00
|
|
|
struct kmemusage *kmemusage;
|
|
|
|
char *kmembase, *kmemlimit;
|
1997-10-09 17:02:18 +04:00
|
|
|
const char *memname[] = INITKMEMNAMES;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1998-01-22 01:24:32 +03:00
|
|
|
#ifdef MALLOCLOG
|
|
|
|
#ifndef MALLOCLOGSIZE
|
|
|
|
#define MALLOCLOGSIZE 100000
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct malloclog {
|
|
|
|
void *addr;
|
|
|
|
long size;
|
|
|
|
int type;
|
|
|
|
int action;
|
|
|
|
const char *file;
|
|
|
|
long line;
|
|
|
|
} malloclog[MALLOCLOGSIZE];
|
|
|
|
|
|
|
|
long malloclogptr;
|
|
|
|
|
|
|
|
static void domlog __P((void *a, long size, int type, int action,
|
|
|
|
const char *file, long line));
|
|
|
|
static void hitmlog __P((void *a));
|
|
|
|
|
|
|
|
static void
|
|
|
|
domlog(a, size, type, action, file, line)
|
|
|
|
void *a;
|
|
|
|
long size;
|
|
|
|
int type;
|
|
|
|
int action;
|
|
|
|
const char *file;
|
|
|
|
long line;
|
|
|
|
{
|
|
|
|
|
|
|
|
malloclog[malloclogptr].addr = a;
|
|
|
|
malloclog[malloclogptr].size = size;
|
|
|
|
malloclog[malloclogptr].type = type;
|
|
|
|
malloclog[malloclogptr].action = action;
|
|
|
|
malloclog[malloclogptr].file = file;
|
|
|
|
malloclog[malloclogptr].line = line;
|
|
|
|
malloclogptr++;
|
|
|
|
if (malloclogptr >= MALLOCLOGSIZE)
|
|
|
|
malloclogptr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
hitmlog(a)
|
|
|
|
void *a;
|
|
|
|
{
|
|
|
|
struct malloclog *lp;
|
|
|
|
long l;
|
|
|
|
|
|
|
|
#define PRT \
|
|
|
|
if (malloclog[l].addr == a && malloclog[l].action) { \
|
|
|
|
lp = &malloclog[l]; \
|
|
|
|
printf("malloc log entry %ld:\n", l); \
|
|
|
|
printf("\taddr = %p\n", lp->addr); \
|
|
|
|
printf("\tsize = %ld\n", lp->size); \
|
|
|
|
printf("\ttype = %s\n", memname[lp->type]); \
|
|
|
|
printf("\taction = %s\n", lp->action == 1 ? "alloc" : "free"); \
|
|
|
|
printf("\tfile = %s\n", lp->file); \
|
|
|
|
printf("\tline = %ld\n", lp->line); \
|
|
|
|
}
|
|
|
|
|
|
|
|
for (l = malloclogptr; l < MALLOCLOGSIZE; l++)
|
|
|
|
PRT
|
|
|
|
|
|
|
|
for (l = 0; l < malloclogptr; l++)
|
|
|
|
PRT
|
|
|
|
}
|
|
|
|
#endif /* MALLOCLOG */
|
|
|
|
|
1994-05-13 12:32:17 +04:00
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
/*
|
|
|
|
* This structure provides a set of masks to catch unaligned frees.
|
|
|
|
*/
|
|
|
|
long addrmask[] = { 0,
|
|
|
|
0x00000001, 0x00000003, 0x00000007, 0x0000000f,
|
|
|
|
0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
|
|
|
|
0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
|
|
|
|
0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The WEIRD_ADDR is used as known text to copy into free objects so
|
|
|
|
* that modifications after frees can be detected.
|
|
|
|
*/
|
1996-02-04 05:15:01 +03:00
|
|
|
#define WEIRD_ADDR ((unsigned) 0xdeadbeef)
|
1994-05-13 12:32:17 +04:00
|
|
|
#define MAX_COPY 32
|
|
|
|
|
|
|
|
/*
|
1995-05-02 02:39:11 +04:00
|
|
|
* Normally the freelist structure is used only to hold the list pointer
|
|
|
|
* for free objects. However, when running with diagnostics, the first
|
|
|
|
* 8 bytes of the structure is unused except for diagnostic information,
|
|
|
|
* and the free list pointer is at offst 8 in the structure. Since the
|
|
|
|
* first 8 bytes is the portion of the structure most often modified, this
|
|
|
|
* helps to detect memory reuse problems and avoid free list corruption.
|
1994-05-13 12:32:17 +04:00
|
|
|
*/
|
|
|
|
struct freelist {
|
1995-05-02 02:39:11 +04:00
|
|
|
int32_t spare0;
|
|
|
|
int16_t type;
|
|
|
|
int16_t spare1;
|
1994-05-13 12:32:17 +04:00
|
|
|
caddr_t next;
|
|
|
|
};
|
|
|
|
#else /* !DIAGNOSTIC */
|
|
|
|
struct freelist {
|
|
|
|
caddr_t next;
|
|
|
|
};
|
|
|
|
#endif /* DIAGNOSTIC */
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* Allocate a block of memory
|
|
|
|
*/
|
1998-01-22 01:24:32 +03:00
|
|
|
#ifdef MALLOCLOG
|
|
|
|
void *
|
|
|
|
_malloc(size, type, flags, file, line)
|
|
|
|
unsigned long size;
|
|
|
|
int type, flags;
|
|
|
|
const char *file;
|
|
|
|
long line;
|
|
|
|
#else
|
1993-03-21 12:45:37 +03:00
|
|
|
void *
|
|
|
|
malloc(size, type, flags)
|
|
|
|
unsigned long size;
|
|
|
|
int type, flags;
|
1998-01-22 01:24:32 +03:00
|
|
|
#endif /* MALLOCLOG */
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
register struct kmembuckets *kbp;
|
|
|
|
register struct kmemusage *kup;
|
1994-05-13 12:32:17 +04:00
|
|
|
register struct freelist *freep;
|
1993-06-27 10:01:27 +04:00
|
|
|
long indx, npg, allocsize;
|
1993-03-21 12:45:37 +03:00
|
|
|
int s;
|
|
|
|
caddr_t va, cp, savedlist;
|
1994-05-13 12:32:17 +04:00
|
|
|
#ifdef DIAGNOSTIC
|
1995-05-02 02:39:11 +04:00
|
|
|
int32_t *end, *lp;
|
1994-05-13 12:32:17 +04:00
|
|
|
int copysize;
|
1997-10-09 17:05:59 +04:00
|
|
|
const char *savedtype;
|
1994-05-13 12:32:17 +04:00
|
|
|
#endif
|
1998-03-01 05:20:01 +03:00
|
|
|
#ifdef LOCKDEBUG
|
|
|
|
extern int simplelockrecurse;
|
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifdef KMEMSTATS
|
|
|
|
register struct kmemstats *ksp = &kmemstats[type];
|
|
|
|
|
|
|
|
if (((unsigned long)type) > M_LAST)
|
|
|
|
panic("malloc - bogus type");
|
|
|
|
#endif
|
|
|
|
indx = BUCKETINDX(size);
|
|
|
|
kbp = &bucket[indx];
|
|
|
|
s = splimp();
|
|
|
|
#ifdef KMEMSTATS
|
|
|
|
while (ksp->ks_memuse >= ksp->ks_limit) {
|
|
|
|
if (flags & M_NOWAIT) {
|
|
|
|
splx(s);
|
|
|
|
return ((void *) NULL);
|
|
|
|
}
|
|
|
|
if (ksp->ks_limblocks < 65535)
|
|
|
|
ksp->ks_limblocks++;
|
|
|
|
tsleep((caddr_t)ksp, PSWP+2, memname[type], 0);
|
|
|
|
}
|
1994-05-13 12:32:17 +04:00
|
|
|
ksp->ks_size |= 1 << indx;
|
|
|
|
#endif
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
copysize = 1 << indx < MAX_COPY ? 1 << indx : MAX_COPY;
|
1998-03-01 05:20:01 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOCKDEBUG
|
|
|
|
if (flags & M_NOWAIT)
|
|
|
|
simplelockrecurse++;
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif
|
|
|
|
if (kbp->kb_next == NULL) {
|
1994-05-13 12:32:17 +04:00
|
|
|
kbp->kb_last = NULL;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (size > MAXALLOCSAVE)
|
|
|
|
allocsize = roundup(size, CLBYTES);
|
|
|
|
else
|
|
|
|
allocsize = 1 << indx;
|
|
|
|
npg = clrnd(btoc(allocsize));
|
1998-02-05 10:59:28 +03:00
|
|
|
va = (caddr_t) uvm_km_kmemalloc(kmem_map, uvmexp.kmem_object,
|
1998-08-13 06:10:37 +04:00
|
|
|
(vsize_t)ctob(npg),
|
1998-02-05 10:59:28 +03:00
|
|
|
(flags & M_NOWAIT) ? UVM_KMF_NOWAIT : 0);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (va == NULL) {
|
1996-06-13 20:53:34 +04:00
|
|
|
/*
|
|
|
|
* Kmem_malloc() can return NULL, even if it can
|
|
|
|
* wait, if there is no map space avaiable, because
|
|
|
|
* it can't fix that problem. Neither can we,
|
|
|
|
* right now. (We should release pages which
|
|
|
|
* are completely free and which are in buckets
|
|
|
|
* with too many free elements.)
|
|
|
|
*/
|
|
|
|
if ((flags & M_NOWAIT) == 0)
|
|
|
|
panic("malloc: out of space in kmem_map");
|
1998-03-01 05:20:01 +03:00
|
|
|
#ifdef LOCKDEBUG
|
|
|
|
simplelockrecurse--;
|
|
|
|
#endif
|
1993-07-15 17:33:23 +04:00
|
|
|
splx(s);
|
|
|
|
return ((void *) NULL);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
#ifdef KMEMSTATS
|
|
|
|
kbp->kb_total += kbp->kb_elmpercl;
|
|
|
|
#endif
|
|
|
|
kup = btokup(va);
|
|
|
|
kup->ku_indx = indx;
|
|
|
|
if (allocsize > MAXALLOCSAVE) {
|
|
|
|
if (npg > 65535)
|
|
|
|
panic("malloc: allocation too large");
|
|
|
|
kup->ku_pagecnt = npg;
|
|
|
|
#ifdef KMEMSTATS
|
|
|
|
ksp->ks_memuse += allocsize;
|
|
|
|
#endif
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
#ifdef KMEMSTATS
|
|
|
|
kup->ku_freecnt = kbp->kb_elmpercl;
|
|
|
|
kbp->kb_totalfree += kbp->kb_elmpercl;
|
|
|
|
#endif
|
|
|
|
/*
|
|
|
|
* Just in case we blocked while allocating memory,
|
|
|
|
* and someone else also allocated memory for this
|
|
|
|
* bucket, don't assume the list is still empty.
|
|
|
|
*/
|
|
|
|
savedlist = kbp->kb_next;
|
1994-05-13 12:32:17 +04:00
|
|
|
kbp->kb_next = cp = va + (npg * NBPG) - allocsize;
|
|
|
|
for (;;) {
|
|
|
|
freep = (struct freelist *)cp;
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
/*
|
|
|
|
* Copy in known text to detect modification
|
|
|
|
* after freeing.
|
|
|
|
*/
|
1995-05-02 02:39:11 +04:00
|
|
|
end = (int32_t *)&cp[copysize];
|
|
|
|
for (lp = (int32_t *)cp; lp < end; lp++)
|
1994-05-13 12:32:17 +04:00
|
|
|
*lp = WEIRD_ADDR;
|
|
|
|
freep->type = M_FREE;
|
|
|
|
#endif /* DIAGNOSTIC */
|
|
|
|
if (cp <= va)
|
|
|
|
break;
|
|
|
|
cp -= allocsize;
|
|
|
|
freep->next = cp;
|
|
|
|
}
|
|
|
|
freep->next = savedlist;
|
|
|
|
if (kbp->kb_last == NULL)
|
|
|
|
kbp->kb_last = (caddr_t)freep;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
va = kbp->kb_next;
|
1994-05-13 12:32:17 +04:00
|
|
|
kbp->kb_next = ((struct freelist *)va)->next;
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
freep = (struct freelist *)va;
|
|
|
|
savedtype = (unsigned)freep->type < M_LAST ?
|
|
|
|
memname[freep->type] : "???";
|
1998-02-07 05:40:36 +03:00
|
|
|
if (kbp->kb_next) {
|
|
|
|
int rv;
|
1998-08-13 06:10:37 +04:00
|
|
|
vaddr_t addr = (vaddr_t)kbp->kb_next;
|
1998-02-07 05:40:36 +03:00
|
|
|
|
|
|
|
vm_map_lock_read(kmem_map);
|
|
|
|
rv = uvm_map_checkprot(kmem_map, addr,
|
|
|
|
addr + sizeof(struct freelist),
|
|
|
|
VM_PROT_WRITE);
|
|
|
|
vm_map_unlock_read(kmem_map);
|
|
|
|
|
|
|
|
if (!rv)
|
1998-02-05 10:59:28 +03:00
|
|
|
{
|
1999-03-24 08:50:49 +03:00
|
|
|
printf(
|
1996-10-11 02:46:11 +04:00
|
|
|
"%s %ld of object %p size %ld %s %s (invalid addr %p)\n",
|
1999-03-24 08:50:49 +03:00
|
|
|
"Data modified on freelist: word",
|
|
|
|
(long)((int32_t *)&kbp->kb_next - (int32_t *)kbp),
|
|
|
|
va, size, "previous type", savedtype, kbp->kb_next);
|
1998-01-22 01:24:32 +03:00
|
|
|
#ifdef MALLOCLOG
|
1999-03-24 08:50:49 +03:00
|
|
|
hitmlog(va);
|
1998-01-22 01:24:32 +03:00
|
|
|
#endif
|
1999-03-24 08:50:49 +03:00
|
|
|
kbp->kb_next = NULL;
|
1998-02-07 05:40:36 +03:00
|
|
|
}
|
1994-05-13 12:32:17 +04:00
|
|
|
}
|
1995-05-02 02:39:11 +04:00
|
|
|
|
|
|
|
/* Fill the fields that we've used with WEIRD_ADDR */
|
1994-05-13 12:32:17 +04:00
|
|
|
#if BYTE_ORDER == BIG_ENDIAN
|
|
|
|
freep->type = WEIRD_ADDR >> 16;
|
|
|
|
#endif
|
|
|
|
#if BYTE_ORDER == LITTLE_ENDIAN
|
|
|
|
freep->type = (short)WEIRD_ADDR;
|
|
|
|
#endif
|
1995-05-02 02:39:11 +04:00
|
|
|
end = (int32_t *)&freep->next +
|
|
|
|
(sizeof(freep->next) / sizeof(int32_t));
|
|
|
|
for (lp = (int32_t *)&freep->next; lp < end; lp++)
|
|
|
|
*lp = WEIRD_ADDR;
|
|
|
|
|
|
|
|
/* and check that the data hasn't been modified. */
|
|
|
|
end = (int32_t *)&va[copysize];
|
|
|
|
for (lp = (int32_t *)va; lp < end; lp++) {
|
1994-05-13 12:32:17 +04:00
|
|
|
if (*lp == WEIRD_ADDR)
|
|
|
|
continue;
|
1996-10-13 06:32:29 +04:00
|
|
|
printf("%s %ld of object %p size %ld %s %s (0x%x != 0x%x)\n",
|
1996-10-11 02:46:11 +04:00
|
|
|
"Data modified on freelist: word",
|
|
|
|
(long)(lp - (int32_t *)va), va, size, "previous type",
|
|
|
|
savedtype, *lp, WEIRD_ADDR);
|
1998-01-22 01:24:32 +03:00
|
|
|
#ifdef MALLOCLOG
|
|
|
|
hitmlog(va);
|
|
|
|
#endif
|
1994-05-13 12:32:17 +04:00
|
|
|
break;
|
|
|
|
}
|
1995-05-02 02:39:11 +04:00
|
|
|
|
1994-05-13 12:32:17 +04:00
|
|
|
freep->spare0 = 0;
|
|
|
|
#endif /* DIAGNOSTIC */
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifdef KMEMSTATS
|
|
|
|
kup = btokup(va);
|
|
|
|
if (kup->ku_indx != indx)
|
|
|
|
panic("malloc: wrong bucket");
|
|
|
|
if (kup->ku_freecnt == 0)
|
|
|
|
panic("malloc: lost data");
|
|
|
|
kup->ku_freecnt--;
|
|
|
|
kbp->kb_totalfree--;
|
|
|
|
ksp->ks_memuse += 1 << indx;
|
|
|
|
out:
|
|
|
|
kbp->kb_calls++;
|
|
|
|
ksp->ks_inuse++;
|
|
|
|
ksp->ks_calls++;
|
|
|
|
if (ksp->ks_memuse > ksp->ks_maxused)
|
|
|
|
ksp->ks_maxused = ksp->ks_memuse;
|
|
|
|
#else
|
|
|
|
out:
|
1998-01-22 01:24:32 +03:00
|
|
|
#endif
|
|
|
|
#ifdef MALLOCLOG
|
|
|
|
domlog(va, size, type, 1, file, line);
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif
|
|
|
|
splx(s);
|
1998-03-01 05:20:01 +03:00
|
|
|
#ifdef LOCKDEBUG
|
|
|
|
if (flags & M_NOWAIT)
|
|
|
|
simplelockrecurse--;
|
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
return ((void *) va);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free a block of memory allocated by malloc.
|
|
|
|
*/
|
1998-01-22 01:24:32 +03:00
|
|
|
#ifdef MALLOCLOG
|
|
|
|
void
|
|
|
|
_free(addr, type, file, line)
|
|
|
|
void *addr;
|
|
|
|
int type;
|
|
|
|
const char *file;
|
|
|
|
long line;
|
|
|
|
#else
|
1993-03-21 12:45:37 +03:00
|
|
|
void
|
|
|
|
free(addr, type)
|
|
|
|
void *addr;
|
|
|
|
int type;
|
1998-01-22 01:24:32 +03:00
|
|
|
#endif /* MALLOCLOG */
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
register struct kmembuckets *kbp;
|
|
|
|
register struct kmemusage *kup;
|
1994-05-13 12:32:17 +04:00
|
|
|
register struct freelist *freep;
|
1993-06-27 10:01:27 +04:00
|
|
|
long size;
|
1993-03-21 12:45:37 +03:00
|
|
|
int s;
|
1994-05-13 12:32:17 +04:00
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
caddr_t cp;
|
1995-05-02 02:39:11 +04:00
|
|
|
int32_t *end, *lp;
|
|
|
|
long alloc, copysize;
|
1994-05-13 12:32:17 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifdef KMEMSTATS
|
|
|
|
register struct kmemstats *ksp = &kmemstats[type];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
kup = btokup(addr);
|
|
|
|
size = 1 << kup->ku_indx;
|
1994-05-13 12:32:17 +04:00
|
|
|
kbp = &bucket[kup->ku_indx];
|
|
|
|
s = splimp();
|
1998-01-22 01:24:32 +03:00
|
|
|
#ifdef MALLOCLOG
|
|
|
|
domlog(addr, 0, type, 2, file, line);
|
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifdef DIAGNOSTIC
|
1994-05-13 12:32:17 +04:00
|
|
|
/*
|
|
|
|
* Check for returns of data that do not point to the
|
|
|
|
* beginning of the allocation.
|
|
|
|
*/
|
1993-03-21 12:45:37 +03:00
|
|
|
if (size > NBPG * CLSIZE)
|
|
|
|
alloc = addrmask[BUCKETINDX(NBPG * CLSIZE)];
|
|
|
|
else
|
|
|
|
alloc = addrmask[kup->ku_indx];
|
1994-05-13 12:32:17 +04:00
|
|
|
if (((u_long)addr & alloc) != 0)
|
1996-03-17 02:17:04 +03:00
|
|
|
panic("free: unaligned addr %p, size %ld, type %s, mask %ld\n",
|
1994-05-13 12:32:17 +04:00
|
|
|
addr, size, memname[type], alloc);
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* DIAGNOSTIC */
|
|
|
|
if (size > MAXALLOCSAVE) {
|
1998-08-13 06:10:37 +04:00
|
|
|
uvm_km_free(kmem_map, (vaddr_t)addr, ctob(kup->ku_pagecnt));
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifdef KMEMSTATS
|
|
|
|
size = kup->ku_pagecnt << PGSHIFT;
|
|
|
|
ksp->ks_memuse -= size;
|
|
|
|
kup->ku_indx = 0;
|
|
|
|
kup->ku_pagecnt = 0;
|
|
|
|
if (ksp->ks_memuse + size >= ksp->ks_limit &&
|
|
|
|
ksp->ks_memuse < ksp->ks_limit)
|
|
|
|
wakeup((caddr_t)ksp);
|
|
|
|
ksp->ks_inuse--;
|
|
|
|
kbp->kb_total -= 1;
|
|
|
|
#endif
|
|
|
|
splx(s);
|
|
|
|
return;
|
|
|
|
}
|
1994-05-13 12:32:17 +04:00
|
|
|
freep = (struct freelist *)addr;
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
/*
|
|
|
|
* Check for multiple frees. Use a quick check to see if
|
|
|
|
* it looks free before laboriously searching the freelist.
|
|
|
|
*/
|
|
|
|
if (freep->spare0 == WEIRD_ADDR) {
|
1996-06-06 23:13:32 +04:00
|
|
|
for (cp = kbp->kb_next; cp;
|
|
|
|
cp = ((struct freelist *)cp)->next) {
|
1994-05-13 12:32:17 +04:00
|
|
|
if (addr != cp)
|
|
|
|
continue;
|
1996-10-13 06:32:29 +04:00
|
|
|
printf("multiply freed item %p\n", addr);
|
1998-01-22 01:24:32 +03:00
|
|
|
#ifdef MALLOCLOG
|
|
|
|
hitmlog(addr);
|
|
|
|
#endif
|
1994-05-13 12:32:17 +04:00
|
|
|
panic("free: duplicated free");
|
|
|
|
}
|
|
|
|
}
|
1998-11-04 09:19:55 +03:00
|
|
|
#ifdef LOCKDEBUG
|
|
|
|
/*
|
|
|
|
* Check if we're freeing a locked simple lock.
|
|
|
|
*/
|
1999-01-22 10:55:49 +03:00
|
|
|
simple_lock_freecheck(addr, (char *)addr + size);
|
1998-11-04 09:19:55 +03:00
|
|
|
#endif
|
1994-05-13 12:32:17 +04:00
|
|
|
/*
|
|
|
|
* Copy in known text to detect modification after freeing
|
|
|
|
* and to make it look free. Also, save the type being freed
|
|
|
|
* so we can list likely culprit if modification is detected
|
|
|
|
* when the object is reallocated.
|
|
|
|
*/
|
|
|
|
copysize = size < MAX_COPY ? size : MAX_COPY;
|
1995-05-02 02:39:11 +04:00
|
|
|
end = (int32_t *)&((caddr_t)addr)[copysize];
|
|
|
|
for (lp = (int32_t *)addr; lp < end; lp++)
|
1994-05-13 12:32:17 +04:00
|
|
|
*lp = WEIRD_ADDR;
|
|
|
|
freep->type = type;
|
|
|
|
#endif /* DIAGNOSTIC */
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifdef KMEMSTATS
|
|
|
|
kup->ku_freecnt++;
|
1998-08-18 10:11:35 +04:00
|
|
|
if (kup->ku_freecnt >= kbp->kb_elmpercl) {
|
1993-03-21 12:45:37 +03:00
|
|
|
if (kup->ku_freecnt > kbp->kb_elmpercl)
|
|
|
|
panic("free: multiple frees");
|
|
|
|
else if (kbp->kb_totalfree > kbp->kb_highwat)
|
|
|
|
kbp->kb_couldfree++;
|
1998-08-18 10:11:35 +04:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
kbp->kb_totalfree++;
|
|
|
|
ksp->ks_memuse -= size;
|
|
|
|
if (ksp->ks_memuse + size >= ksp->ks_limit &&
|
|
|
|
ksp->ks_memuse < ksp->ks_limit)
|
|
|
|
wakeup((caddr_t)ksp);
|
|
|
|
ksp->ks_inuse--;
|
|
|
|
#endif
|
1994-05-13 12:32:17 +04:00
|
|
|
if (kbp->kb_next == NULL)
|
|
|
|
kbp->kb_next = addr;
|
|
|
|
else
|
|
|
|
((struct freelist *)kbp->kb_last)->next = addr;
|
|
|
|
freep->next = NULL;
|
|
|
|
kbp->kb_last = addr;
|
1993-03-21 12:45:37 +03:00
|
|
|
splx(s);
|
|
|
|
}
|
|
|
|
|
1996-08-28 00:01:42 +04:00
|
|
|
/*
|
|
|
|
* Change the size of a block of memory.
|
|
|
|
*/
|
|
|
|
void *
|
|
|
|
realloc(curaddr, newsize, type, flags)
|
|
|
|
void *curaddr;
|
|
|
|
unsigned long newsize;
|
|
|
|
int type, flags;
|
|
|
|
{
|
|
|
|
register struct kmemusage *kup;
|
|
|
|
long cursize;
|
|
|
|
void *newaddr;
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
long alloc;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Realloc() with a NULL pointer is the same as malloc().
|
|
|
|
*/
|
|
|
|
if (curaddr == NULL)
|
|
|
|
return (malloc(newsize, type, flags));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Realloc() with zero size is the same as free().
|
|
|
|
*/
|
|
|
|
if (newsize == 0) {
|
|
|
|
free(curaddr, type);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find out how large the old allocation was (and do some
|
|
|
|
* sanity checking).
|
|
|
|
*/
|
|
|
|
kup = btokup(curaddr);
|
|
|
|
cursize = 1 << kup->ku_indx;
|
|
|
|
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
/*
|
|
|
|
* Check for returns of data that do not point to the
|
|
|
|
* beginning of the allocation.
|
|
|
|
*/
|
|
|
|
if (cursize > NBPG * CLSIZE)
|
|
|
|
alloc = addrmask[BUCKETINDX(NBPG * CLSIZE)];
|
|
|
|
else
|
|
|
|
alloc = addrmask[kup->ku_indx];
|
|
|
|
if (((u_long)curaddr & alloc) != 0)
|
|
|
|
panic("realloc: unaligned addr %p, size %ld, type %s, mask %ld\n",
|
|
|
|
curaddr, cursize, memname[type], alloc);
|
|
|
|
#endif /* DIAGNOSTIC */
|
|
|
|
|
|
|
|
if (cursize > MAXALLOCSAVE)
|
|
|
|
cursize = ctob(kup->ku_pagecnt);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we already actually have as much as they want, we're done.
|
|
|
|
*/
|
|
|
|
if (newsize <= cursize)
|
|
|
|
return (curaddr);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Can't satisfy the allocation with the existing block.
|
|
|
|
* Allocate a new one and copy the data.
|
|
|
|
*/
|
|
|
|
newaddr = malloc(newsize, type, flags);
|
|
|
|
if (newaddr == NULL) {
|
|
|
|
/*
|
|
|
|
* Malloc() failed, because flags included M_NOWAIT.
|
|
|
|
* Return NULL to indicate that failure. The old
|
|
|
|
* pointer is still valid.
|
|
|
|
*/
|
|
|
|
return NULL;
|
|
|
|
}
|
Abolition of bcopy, ovbcopy, bcmp, and bzero, phase one.
bcopy(x, y, z) -> memcpy(y, x, z)
ovbcopy(x, y, z) -> memmove(y, x, z)
bcmp(x, y, z) -> memcmp(x, y, z)
bzero(x, y) -> memset(x, 0, y)
1998-08-04 08:03:10 +04:00
|
|
|
memcpy(newaddr, curaddr, cursize);
|
1996-08-28 00:01:42 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We were successful: free the old allocation and return
|
|
|
|
* the new one.
|
|
|
|
*/
|
|
|
|
free(curaddr, type);
|
|
|
|
return (newaddr);
|
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* Initialize the kernel memory allocator
|
|
|
|
*/
|
1996-02-04 05:15:01 +03:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
kmeminit()
|
|
|
|
{
|
1997-01-30 09:50:46 +03:00
|
|
|
#ifdef KMEMSTATS
|
1993-03-21 12:45:37 +03:00
|
|
|
register long indx;
|
1997-01-30 09:50:46 +03:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
int npg;
|
|
|
|
|
|
|
|
#if ((MAXALLOCSAVE & (MAXALLOCSAVE - 1)) != 0)
|
|
|
|
ERROR!_kmeminit:_MAXALLOCSAVE_not_power_of_2
|
|
|
|
#endif
|
|
|
|
#if (MAXALLOCSAVE > MINALLOCSIZE * 32768)
|
|
|
|
ERROR!_kmeminit:_MAXALLOCSAVE_too_big
|
|
|
|
#endif
|
|
|
|
#if (MAXALLOCSAVE < CLBYTES)
|
|
|
|
ERROR!_kmeminit:_MAXALLOCSAVE_too_small
|
|
|
|
#endif
|
1995-05-02 02:39:11 +04:00
|
|
|
|
|
|
|
if (sizeof(struct freelist) > (1 << MINBUCKET))
|
|
|
|
panic("minbucket too small/struct freelist too big");
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
npg = VM_KMEM_SIZE/ NBPG;
|
1998-02-05 10:59:28 +03:00
|
|
|
kmemusage = (struct kmemusage *) uvm_km_zalloc(kernel_map,
|
1998-08-13 06:10:37 +04:00
|
|
|
(vsize_t)(npg * sizeof(struct kmemusage)));
|
|
|
|
kmem_map = uvm_km_suballoc(kernel_map, (vaddr_t *)&kmembase,
|
|
|
|
(vaddr_t *)&kmemlimit, (vsize_t)(npg * NBPG),
|
1998-02-08 09:15:53 +03:00
|
|
|
FALSE, FALSE, &kmem_map_store);
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifdef KMEMSTATS
|
|
|
|
for (indx = 0; indx < MINBUCKET + 16; indx++) {
|
|
|
|
if (1 << indx >= CLBYTES)
|
|
|
|
bucket[indx].kb_elmpercl = 1;
|
|
|
|
else
|
|
|
|
bucket[indx].kb_elmpercl = CLBYTES / (1 << indx);
|
|
|
|
bucket[indx].kb_highwat = 5 * bucket[indx].kb_elmpercl;
|
|
|
|
}
|
1994-05-13 12:32:17 +04:00
|
|
|
for (indx = 0; indx < M_LAST; indx++)
|
1993-03-21 12:45:37 +03:00
|
|
|
kmemstats[indx].ks_limit = npg * NBPG * 6 / 10;
|
|
|
|
#endif
|
|
|
|
}
|
1998-12-02 23:35:28 +03:00
|
|
|
|
|
|
|
#ifdef DDB
|
|
|
|
#include <ddb/db_output.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dump kmem statistics from ddb.
|
|
|
|
*
|
|
|
|
* usage: call dump_kmemstats
|
|
|
|
*/
|
|
|
|
void dump_kmemstats __P((void));
|
|
|
|
|
|
|
|
void
|
|
|
|
dump_kmemstats()
|
|
|
|
{
|
|
|
|
#ifdef KMEMSTATS
|
|
|
|
const char *name;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < M_LAST; i++) {
|
|
|
|
name = memname[i] ? memname[i] : "";
|
|
|
|
|
|
|
|
db_printf("%2d %s%.*s %ld\n", i, name,
|
|
|
|
(int)(20 - strlen(name)), " ",
|
|
|
|
kmemstats[i].ks_memuse);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
db_printf("Kmem stats are not being collected.\n");
|
|
|
|
#endif /* KMEMSTATS */
|
|
|
|
}
|
|
|
|
#endif /* DDB */
|