1994-05-24 10:09:53 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1987, 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @(#)kern_malloc.c 8.3 (Berkeley) 1/4/94
|
1996-01-29 11:12:37 +00:00
|
|
|
* $Id: kern_malloc.c,v 1.18 1996/01/29 09:58:34 davidg Exp $
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
1994-05-25 09:21:21 +00:00
|
|
|
#include <sys/systm.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
1996-01-29 09:58:34 +00:00
|
|
|
#include <sys/mbuf.h>
|
1995-12-07 12:48:31 +00:00
|
|
|
#include <sys/vmmeter.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
#include <vm/vm.h>
|
1995-12-07 12:48:31 +00:00
|
|
|
#include <vm/vm_param.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <vm/vm_kern.h>
|
1995-12-07 12:48:31 +00:00
|
|
|
#include <vm/vm_extern.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
|
1995-09-09 18:10:37 +00:00
|
|
|
static void kmeminit __P((void *));
|
1995-08-28 09:19:25 +00:00
|
|
|
SYSINIT(kmem, SI_SUB_KMEM, SI_ORDER_FIRST, kmeminit, NULL)
|
|
|
|
|
1995-12-14 08:32:45 +00:00
|
|
|
static struct kmembuckets bucket[MINBUCKET + 16];
|
1994-05-24 10:09:53 +00:00
|
|
|
struct kmemstats kmemstats[M_LAST];
|
|
|
|
struct kmemusage *kmemusage;
|
|
|
|
char *kmembase, *kmemlimit;
|
|
|
|
char *memname[] = INITKMEMNAMES;
|
|
|
|
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
/*
|
|
|
|
* This structure provides a set of masks to catch unaligned frees.
|
|
|
|
*/
|
1995-12-14 08:32:45 +00:00
|
|
|
static long addrmask[] = { 0,
|
1994-05-24 10:09:53 +00:00
|
|
|
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.
|
|
|
|
*/
|
1995-04-16 11:25:15 +00:00
|
|
|
#define WEIRD_ADDR 0xdeadc0de
|
|
|
|
#define MAX_COPY 64
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Normally the first word of the structure is used to hold the list
|
|
|
|
* pointer for free objects. However, when running with diagnostics,
|
|
|
|
* we use the third and fourth fields, so as to catch modifications
|
|
|
|
* in the most commonly trashed first two words.
|
|
|
|
*/
|
|
|
|
struct freelist {
|
|
|
|
long spare0;
|
|
|
|
short type;
|
|
|
|
long spare1;
|
|
|
|
caddr_t next;
|
|
|
|
};
|
|
|
|
#else /* !DIAGNOSTIC */
|
|
|
|
struct freelist {
|
|
|
|
caddr_t next;
|
|
|
|
};
|
|
|
|
#endif /* DIAGNOSTIC */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate a block of memory
|
|
|
|
*/
|
|
|
|
void *
|
|
|
|
malloc(size, type, flags)
|
|
|
|
unsigned long size;
|
|
|
|
int type, flags;
|
|
|
|
{
|
|
|
|
register struct kmembuckets *kbp;
|
|
|
|
register struct kmemusage *kup;
|
|
|
|
register struct freelist *freep;
|
|
|
|
long indx, npg, allocsize;
|
|
|
|
int s;
|
|
|
|
caddr_t va, cp, savedlist;
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
long *end, *lp;
|
|
|
|
int copysize;
|
|
|
|
char *savedtype;
|
|
|
|
#endif
|
|
|
|
#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];
|
1994-12-17 04:04:42 +00:00
|
|
|
s = splhigh();
|
1994-05-24 10:09:53 +00:00
|
|
|
#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);
|
|
|
|
}
|
|
|
|
ksp->ks_size |= 1 << indx;
|
|
|
|
#endif
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
copysize = 1 << indx < MAX_COPY ? 1 << indx : MAX_COPY;
|
|
|
|
#endif
|
|
|
|
if (kbp->kb_next == NULL) {
|
|
|
|
kbp->kb_last = NULL;
|
|
|
|
if (size > MAXALLOCSAVE)
|
|
|
|
allocsize = roundup(size, CLBYTES);
|
|
|
|
else
|
|
|
|
allocsize = 1 << indx;
|
|
|
|
npg = clrnd(btoc(allocsize));
|
1995-02-02 08:49:08 +00:00
|
|
|
va = (caddr_t) kmem_malloc(kmem_map, (vm_size_t)ctob(npg), flags);
|
1994-05-24 10:09:53 +00:00
|
|
|
if (va == NULL) {
|
|
|
|
splx(s);
|
|
|
|
return ((void *) NULL);
|
|
|
|
}
|
|
|
|
#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;
|
|
|
|
kbp->kb_next = cp = va + (npg * NBPG) - allocsize;
|
|
|
|
for (;;) {
|
|
|
|
freep = (struct freelist *)cp;
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
/*
|
|
|
|
* Copy in known text to detect modification
|
|
|
|
* after freeing.
|
|
|
|
*/
|
|
|
|
end = (long *)&cp[copysize];
|
|
|
|
for (lp = (long *)cp; lp < end; lp++)
|
|
|
|
*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;
|
|
|
|
}
|
|
|
|
va = kbp->kb_next;
|
|
|
|
kbp->kb_next = ((struct freelist *)va)->next;
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
freep = (struct freelist *)va;
|
|
|
|
savedtype = (unsigned)freep->type < M_LAST ?
|
|
|
|
memname[freep->type] : "???";
|
|
|
|
if (kbp->kb_next &&
|
|
|
|
!kernacc(kbp->kb_next, sizeof(struct freelist), 0)) {
|
1994-10-02 17:35:40 +00:00
|
|
|
printf("%s of object %p size %ld %s %s (invalid addr %p)\n",
|
1994-05-24 10:09:53 +00:00
|
|
|
"Data modified on freelist: word 2.5", va, size,
|
|
|
|
"previous type", savedtype, kbp->kb_next);
|
|
|
|
kbp->kb_next = NULL;
|
|
|
|
}
|
|
|
|
#if BYTE_ORDER == BIG_ENDIAN
|
|
|
|
freep->type = WEIRD_ADDR >> 16;
|
|
|
|
#endif
|
|
|
|
#if BYTE_ORDER == LITTLE_ENDIAN
|
|
|
|
freep->type = (short)WEIRD_ADDR;
|
|
|
|
#endif
|
|
|
|
if (((long)(&freep->next)) & 0x2)
|
|
|
|
freep->next = (caddr_t)((WEIRD_ADDR >> 16)|(WEIRD_ADDR << 16));
|
|
|
|
else
|
|
|
|
freep->next = (caddr_t)WEIRD_ADDR;
|
|
|
|
end = (long *)&va[copysize];
|
|
|
|
for (lp = (long *)va; lp < end; lp++) {
|
|
|
|
if (*lp == WEIRD_ADDR)
|
|
|
|
continue;
|
1994-10-02 17:35:40 +00:00
|
|
|
printf("%s %d of object %p size %ld %s %s (0x%lx != 0x%x)\n",
|
1994-05-24 10:09:53 +00:00
|
|
|
"Data modified on freelist: word", lp - (long *)va,
|
|
|
|
va, size, "previous type", savedtype, *lp, WEIRD_ADDR);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
freep->spare0 = 0;
|
|
|
|
#endif /* DIAGNOSTIC */
|
|
|
|
#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:
|
|
|
|
#endif
|
|
|
|
splx(s);
|
|
|
|
return ((void *) va);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free a block of memory allocated by malloc.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
free(addr, type)
|
|
|
|
void *addr;
|
|
|
|
int type;
|
|
|
|
{
|
|
|
|
register struct kmembuckets *kbp;
|
|
|
|
register struct kmemusage *kup;
|
|
|
|
register struct freelist *freep;
|
|
|
|
long size;
|
|
|
|
int s;
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
caddr_t cp;
|
|
|
|
long *end, *lp, alloc, copysize;
|
|
|
|
#endif
|
|
|
|
#ifdef KMEMSTATS
|
|
|
|
register struct kmemstats *ksp = &kmemstats[type];
|
|
|
|
#endif
|
|
|
|
|
1995-03-11 22:28:16 +00:00
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
if ((char *)addr < kmembase || (char *)addr >= kmemlimit) {
|
1995-03-19 14:29:26 +00:00
|
|
|
panic("free: address 0x%x out of range", addr);
|
1995-03-11 22:28:16 +00:00
|
|
|
}
|
|
|
|
if ((u_long)type > M_LAST) {
|
1995-03-19 14:29:26 +00:00
|
|
|
panic("free: type %d out of range", type);
|
1995-03-11 22:28:16 +00:00
|
|
|
}
|
|
|
|
#endif
|
1994-05-24 10:09:53 +00:00
|
|
|
kup = btokup(addr);
|
|
|
|
size = 1 << kup->ku_indx;
|
|
|
|
kbp = &bucket[kup->ku_indx];
|
1994-12-17 04:04:42 +00:00
|
|
|
s = splhigh();
|
1994-05-24 10:09:53 +00:00
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
/*
|
|
|
|
* Check for returns of data that do not point to the
|
|
|
|
* beginning of the allocation.
|
|
|
|
*/
|
|
|
|
if (size > NBPG * CLSIZE)
|
|
|
|
alloc = addrmask[BUCKETINDX(NBPG * CLSIZE)];
|
|
|
|
else
|
|
|
|
alloc = addrmask[kup->ku_indx];
|
|
|
|
if (((u_long)addr & alloc) != 0)
|
1995-03-19 14:29:26 +00:00
|
|
|
panic("free: unaligned addr 0x%x, size %d, type %s, mask %d",
|
1994-05-24 10:09:53 +00:00
|
|
|
addr, size, memname[type], alloc);
|
|
|
|
#endif /* DIAGNOSTIC */
|
|
|
|
if (size > MAXALLOCSAVE) {
|
|
|
|
kmem_free(kmem_map, (vm_offset_t)addr, ctob(kup->ku_pagecnt));
|
|
|
|
#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;
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
for (cp = kbp->kb_next; cp; cp = *(caddr_t *)cp) {
|
|
|
|
if (addr != cp)
|
|
|
|
continue;
|
1994-10-02 17:35:40 +00:00
|
|
|
printf("multiply freed item %p\n", addr);
|
1994-05-24 10:09:53 +00:00
|
|
|
panic("free: duplicated free");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
end = (long *)&((caddr_t)addr)[copysize];
|
|
|
|
for (lp = (long *)addr; lp < end; lp++)
|
|
|
|
*lp = WEIRD_ADDR;
|
|
|
|
freep->type = type;
|
|
|
|
#endif /* DIAGNOSTIC */
|
|
|
|
#ifdef KMEMSTATS
|
|
|
|
kup->ku_freecnt++;
|
|
|
|
if (kup->ku_freecnt >= kbp->kb_elmpercl)
|
|
|
|
if (kup->ku_freecnt > kbp->kb_elmpercl)
|
|
|
|
panic("free: multiple frees");
|
|
|
|
else if (kbp->kb_totalfree > kbp->kb_highwat)
|
|
|
|
kbp->kb_couldfree++;
|
|
|
|
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
|
|
|
|
if (kbp->kb_next == NULL)
|
|
|
|
kbp->kb_next = addr;
|
|
|
|
else
|
|
|
|
((struct freelist *)kbp->kb_last)->next = addr;
|
|
|
|
freep->next = NULL;
|
|
|
|
kbp->kb_last = addr;
|
|
|
|
splx(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the kernel memory allocator
|
|
|
|
*/
|
1995-08-28 09:19:25 +00:00
|
|
|
/* ARGSUSED*/
|
|
|
|
static void
|
1995-12-02 17:11:20 +00:00
|
|
|
kmeminit(dummy)
|
|
|
|
void *dummy;
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
|
|
|
register long indx;
|
|
|
|
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
|
1996-01-29 09:58:34 +00:00
|
|
|
npg = (nmbclusters * MCLBYTES + VM_KMEM_SIZE) / PAGE_SIZE;
|
1995-05-30 08:16:23 +00:00
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
kmemusage = (struct kmemusage *) kmem_alloc(kernel_map,
|
|
|
|
(vm_size_t)(npg * sizeof(struct kmemusage)));
|
|
|
|
kmem_map = kmem_suballoc(kernel_map, (vm_offset_t *)&kmembase,
|
1996-01-29 09:58:34 +00:00
|
|
|
(vm_offset_t *)&kmemlimit, (vm_size_t)(npg * PAGE_SIZE),
|
|
|
|
FALSE);
|
1994-05-24 10:09:53 +00: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;
|
|
|
|
}
|
1996-01-29 11:12:37 +00:00
|
|
|
/*
|
|
|
|
* Limit maximum memory for each type to 60% of malloc area size or
|
|
|
|
* 60% of physical memory, whichever is smaller.
|
|
|
|
*/
|
1996-01-29 09:58:34 +00:00
|
|
|
for (indx = 0; indx < M_LAST; indx++) {
|
1996-01-29 11:12:37 +00:00
|
|
|
kmemstats[indx].ks_limit = min(cnt.v_page_count * PAGE_SIZE,
|
|
|
|
(npg * PAGE_SIZE - nmbclusters * MCLBYTES)) * 6 / 10;
|
1996-01-29 09:58:34 +00:00
|
|
|
}
|
1994-05-24 10:09:53 +00:00
|
|
|
#endif
|
|
|
|
}
|