Home | History | Annotate | Line # | Download | only in kern
kern_malloc.c revision 1.32
      1  1.32      fvdl /*	$NetBSD: kern_malloc.c,v 1.32 1998/03/01 02:22:29 fvdl Exp $	*/
      2   1.9       cgd 
      3   1.1       cgd /*
      4  1.20       cgd  * Copyright 1996 Christopher G. Demetriou.  All rights reserved.
      5   1.8       cgd  * Copyright (c) 1987, 1991, 1993
      6   1.8       cgd  *	The Regents of the University of California.  All rights reserved.
      7   1.1       cgd  *
      8   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      9   1.1       cgd  * modification, are permitted provided that the following conditions
     10   1.1       cgd  * are met:
     11   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     12   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     13   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     15   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     16   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     17   1.1       cgd  *    must display the following acknowledgement:
     18   1.1       cgd  *	This product includes software developed by the University of
     19   1.1       cgd  *	California, Berkeley and its contributors.
     20   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     21   1.1       cgd  *    may be used to endorse or promote products derived from this software
     22   1.1       cgd  *    without specific prior written permission.
     23   1.1       cgd  *
     24   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34   1.1       cgd  * SUCH DAMAGE.
     35   1.1       cgd  *
     36  1.32      fvdl  *	@(#)kern_malloc.c	8.4 (Berkeley) 5/20/95
     37   1.1       cgd  */
     38  1.31       mrg 
     39  1.31       mrg #include "opt_uvm.h"
     40   1.1       cgd 
     41   1.7   mycroft #include <sys/param.h>
     42   1.7   mycroft #include <sys/proc.h>
     43   1.8       cgd #include <sys/map.h>
     44   1.7   mycroft #include <sys/kernel.h>
     45   1.7   mycroft #include <sys/malloc.h>
     46  1.12  christos #include <sys/systm.h>
     47   1.7   mycroft 
     48   1.7   mycroft #include <vm/vm.h>
     49   1.7   mycroft #include <vm/vm_kern.h>
     50  1.24   thorpej 
     51  1.28       mrg #if defined(UVM)
     52  1.28       mrg #include <uvm/uvm_extern.h>
     53  1.28       mrg 
     54  1.28       mrg static struct vm_map kmem_map_store;
     55  1.28       mrg vm_map_t kmem_map = NULL;
     56  1.28       mrg #endif
     57  1.28       mrg 
     58  1.24   thorpej #include "opt_kmemstats.h"
     59  1.27   thorpej #include "opt_malloclog.h"
     60  1.12  christos 
     61   1.1       cgd struct kmembuckets bucket[MINBUCKET + 16];
     62   1.8       cgd struct kmemstats kmemstats[M_LAST];
     63   1.1       cgd struct kmemusage *kmemusage;
     64   1.1       cgd char *kmembase, *kmemlimit;
     65  1.25   mycroft const char *memname[] = INITKMEMNAMES;
     66   1.1       cgd 
     67  1.27   thorpej #ifdef MALLOCLOG
     68  1.27   thorpej #ifndef MALLOCLOGSIZE
     69  1.27   thorpej #define	MALLOCLOGSIZE	100000
     70  1.27   thorpej #endif
     71  1.27   thorpej 
     72  1.27   thorpej struct malloclog {
     73  1.27   thorpej 	void *addr;
     74  1.27   thorpej 	long size;
     75  1.27   thorpej 	int type;
     76  1.27   thorpej 	int action;
     77  1.27   thorpej 	const char *file;
     78  1.27   thorpej 	long line;
     79  1.27   thorpej } malloclog[MALLOCLOGSIZE];
     80  1.27   thorpej 
     81  1.27   thorpej long	malloclogptr;
     82  1.27   thorpej 
     83  1.27   thorpej static void domlog __P((void *a, long size, int type, int action,
     84  1.27   thorpej 	const char *file, long line));
     85  1.27   thorpej static void hitmlog __P((void *a));
     86  1.27   thorpej 
     87  1.27   thorpej static void
     88  1.27   thorpej domlog(a, size, type, action, file, line)
     89  1.27   thorpej 	void *a;
     90  1.27   thorpej 	long size;
     91  1.27   thorpej 	int type;
     92  1.27   thorpej 	int action;
     93  1.27   thorpej 	const char *file;
     94  1.27   thorpej 	long line;
     95  1.27   thorpej {
     96  1.27   thorpej 
     97  1.27   thorpej 	malloclog[malloclogptr].addr = a;
     98  1.27   thorpej 	malloclog[malloclogptr].size = size;
     99  1.27   thorpej 	malloclog[malloclogptr].type = type;
    100  1.27   thorpej 	malloclog[malloclogptr].action = action;
    101  1.27   thorpej 	malloclog[malloclogptr].file = file;
    102  1.27   thorpej 	malloclog[malloclogptr].line = line;
    103  1.27   thorpej 	malloclogptr++;
    104  1.27   thorpej 	if (malloclogptr >= MALLOCLOGSIZE)
    105  1.27   thorpej 		malloclogptr = 0;
    106  1.27   thorpej }
    107  1.27   thorpej 
    108  1.27   thorpej static void
    109  1.27   thorpej hitmlog(a)
    110  1.27   thorpej 	void *a;
    111  1.27   thorpej {
    112  1.27   thorpej 	struct malloclog *lp;
    113  1.27   thorpej 	long l;
    114  1.27   thorpej 
    115  1.27   thorpej #define	PRT \
    116  1.27   thorpej 	if (malloclog[l].addr == a && malloclog[l].action) { \
    117  1.27   thorpej 		lp = &malloclog[l]; \
    118  1.27   thorpej 		printf("malloc log entry %ld:\n", l); \
    119  1.27   thorpej 		printf("\taddr = %p\n", lp->addr); \
    120  1.27   thorpej 		printf("\tsize = %ld\n", lp->size); \
    121  1.27   thorpej 		printf("\ttype = %s\n", memname[lp->type]); \
    122  1.27   thorpej 		printf("\taction = %s\n", lp->action == 1 ? "alloc" : "free"); \
    123  1.27   thorpej 		printf("\tfile = %s\n", lp->file); \
    124  1.27   thorpej 		printf("\tline = %ld\n", lp->line); \
    125  1.27   thorpej 	}
    126  1.27   thorpej 
    127  1.27   thorpej 	for (l = malloclogptr; l < MALLOCLOGSIZE; l++)
    128  1.27   thorpej 		PRT
    129  1.27   thorpej 
    130  1.27   thorpej 	for (l = 0; l < malloclogptr; l++)
    131  1.27   thorpej 		PRT
    132  1.27   thorpej }
    133  1.27   thorpej #endif /* MALLOCLOG */
    134  1.27   thorpej 
    135   1.8       cgd #ifdef DIAGNOSTIC
    136   1.8       cgd /*
    137   1.8       cgd  * This structure provides a set of masks to catch unaligned frees.
    138   1.8       cgd  */
    139   1.8       cgd long addrmask[] = { 0,
    140   1.8       cgd 	0x00000001, 0x00000003, 0x00000007, 0x0000000f,
    141   1.8       cgd 	0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
    142   1.8       cgd 	0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
    143   1.8       cgd 	0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
    144   1.8       cgd };
    145   1.8       cgd 
    146   1.8       cgd /*
    147   1.8       cgd  * The WEIRD_ADDR is used as known text to copy into free objects so
    148   1.8       cgd  * that modifications after frees can be detected.
    149   1.8       cgd  */
    150  1.12  christos #define WEIRD_ADDR	((unsigned) 0xdeadbeef)
    151   1.8       cgd #define MAX_COPY	32
    152   1.8       cgd 
    153   1.8       cgd /*
    154  1.11       cgd  * Normally the freelist structure is used only to hold the list pointer
    155  1.11       cgd  * for free objects.  However, when running with diagnostics, the first
    156  1.11       cgd  * 8 bytes of the structure is unused except for diagnostic information,
    157  1.11       cgd  * and the free list pointer is at offst 8 in the structure.  Since the
    158  1.11       cgd  * first 8 bytes is the portion of the structure most often modified, this
    159  1.11       cgd  * helps to detect memory reuse problems and avoid free list corruption.
    160   1.8       cgd  */
    161   1.8       cgd struct freelist {
    162  1.11       cgd 	int32_t	spare0;
    163  1.11       cgd 	int16_t	type;
    164  1.11       cgd 	int16_t	spare1;
    165   1.8       cgd 	caddr_t	next;
    166   1.8       cgd };
    167   1.8       cgd #else /* !DIAGNOSTIC */
    168   1.8       cgd struct freelist {
    169   1.8       cgd 	caddr_t	next;
    170   1.8       cgd };
    171   1.8       cgd #endif /* DIAGNOSTIC */
    172   1.8       cgd 
    173   1.1       cgd /*
    174   1.1       cgd  * Allocate a block of memory
    175   1.1       cgd  */
    176  1.27   thorpej #ifdef MALLOCLOG
    177  1.27   thorpej void *
    178  1.27   thorpej _malloc(size, type, flags, file, line)
    179  1.27   thorpej 	unsigned long size;
    180  1.27   thorpej 	int type, flags;
    181  1.27   thorpej 	const char *file;
    182  1.27   thorpej 	long line;
    183  1.27   thorpej #else
    184   1.1       cgd void *
    185   1.1       cgd malloc(size, type, flags)
    186   1.1       cgd 	unsigned long size;
    187   1.1       cgd 	int type, flags;
    188  1.27   thorpej #endif /* MALLOCLOG */
    189   1.1       cgd {
    190   1.1       cgd 	register struct kmembuckets *kbp;
    191   1.1       cgd 	register struct kmemusage *kup;
    192   1.8       cgd 	register struct freelist *freep;
    193   1.5    andrew 	long indx, npg, allocsize;
    194   1.1       cgd 	int s;
    195   1.1       cgd 	caddr_t va, cp, savedlist;
    196   1.8       cgd #ifdef DIAGNOSTIC
    197  1.11       cgd 	int32_t *end, *lp;
    198   1.8       cgd 	int copysize;
    199  1.26   mycroft 	const char *savedtype;
    200   1.8       cgd #endif
    201  1.32      fvdl #ifdef LOCKDEBUG
    202  1.32      fvdl 	extern int simplelockrecurse;
    203  1.32      fvdl #endif
    204   1.1       cgd #ifdef KMEMSTATS
    205   1.1       cgd 	register struct kmemstats *ksp = &kmemstats[type];
    206   1.1       cgd 
    207   1.1       cgd 	if (((unsigned long)type) > M_LAST)
    208   1.1       cgd 		panic("malloc - bogus type");
    209   1.1       cgd #endif
    210   1.1       cgd 	indx = BUCKETINDX(size);
    211   1.1       cgd 	kbp = &bucket[indx];
    212   1.1       cgd 	s = splimp();
    213   1.1       cgd #ifdef KMEMSTATS
    214   1.1       cgd 	while (ksp->ks_memuse >= ksp->ks_limit) {
    215   1.1       cgd 		if (flags & M_NOWAIT) {
    216   1.1       cgd 			splx(s);
    217   1.1       cgd 			return ((void *) NULL);
    218   1.1       cgd 		}
    219   1.1       cgd 		if (ksp->ks_limblocks < 65535)
    220   1.1       cgd 			ksp->ks_limblocks++;
    221   1.1       cgd 		tsleep((caddr_t)ksp, PSWP+2, memname[type], 0);
    222   1.1       cgd 	}
    223   1.8       cgd 	ksp->ks_size |= 1 << indx;
    224   1.8       cgd #endif
    225   1.8       cgd #ifdef DIAGNOSTIC
    226   1.8       cgd 	copysize = 1 << indx < MAX_COPY ? 1 << indx : MAX_COPY;
    227   1.1       cgd #endif
    228  1.32      fvdl #ifdef LOCKDEBUG
    229  1.32      fvdl 	if (flags & M_NOWAIT)
    230  1.32      fvdl 		simplelockrecurse++;
    231  1.32      fvdl #endif
    232   1.1       cgd 	if (kbp->kb_next == NULL) {
    233   1.8       cgd 		kbp->kb_last = NULL;
    234   1.1       cgd 		if (size > MAXALLOCSAVE)
    235   1.1       cgd 			allocsize = roundup(size, CLBYTES);
    236   1.1       cgd 		else
    237   1.1       cgd 			allocsize = 1 << indx;
    238   1.1       cgd 		npg = clrnd(btoc(allocsize));
    239  1.28       mrg #if defined(UVM)
    240  1.28       mrg 		va = (caddr_t) uvm_km_kmemalloc(kmem_map, uvmexp.kmem_object,
    241  1.28       mrg 				(vm_size_t)ctob(npg),
    242  1.28       mrg 				(flags & M_NOWAIT) ? UVM_KMF_NOWAIT : 0);
    243  1.28       mrg #else
    244   1.1       cgd 		va = (caddr_t) kmem_malloc(kmem_map, (vm_size_t)ctob(npg),
    245   1.1       cgd 					   !(flags & M_NOWAIT));
    246  1.28       mrg #endif
    247   1.1       cgd 		if (va == NULL) {
    248  1.17       cgd 			/*
    249  1.17       cgd 			 * Kmem_malloc() can return NULL, even if it can
    250  1.17       cgd 			 * wait, if there is no map space avaiable, because
    251  1.17       cgd 			 * it can't fix that problem.  Neither can we,
    252  1.17       cgd 			 * right now.  (We should release pages which
    253  1.17       cgd 			 * are completely free and which are in buckets
    254  1.17       cgd 			 * with too many free elements.)
    255  1.17       cgd 			 */
    256  1.17       cgd 			if ((flags & M_NOWAIT) == 0)
    257  1.17       cgd 				panic("malloc: out of space in kmem_map");
    258  1.32      fvdl #ifdef LOCKDEBUG
    259  1.32      fvdl 			simplelockrecurse--;
    260  1.32      fvdl #endif
    261   1.6       cgd 			splx(s);
    262   1.6       cgd 			return ((void *) NULL);
    263   1.1       cgd 		}
    264   1.1       cgd #ifdef KMEMSTATS
    265   1.1       cgd 		kbp->kb_total += kbp->kb_elmpercl;
    266   1.1       cgd #endif
    267   1.1       cgd 		kup = btokup(va);
    268   1.1       cgd 		kup->ku_indx = indx;
    269   1.1       cgd 		if (allocsize > MAXALLOCSAVE) {
    270   1.1       cgd 			if (npg > 65535)
    271   1.1       cgd 				panic("malloc: allocation too large");
    272   1.1       cgd 			kup->ku_pagecnt = npg;
    273   1.1       cgd #ifdef KMEMSTATS
    274   1.1       cgd 			ksp->ks_memuse += allocsize;
    275   1.1       cgd #endif
    276   1.1       cgd 			goto out;
    277   1.1       cgd 		}
    278   1.1       cgd #ifdef KMEMSTATS
    279   1.1       cgd 		kup->ku_freecnt = kbp->kb_elmpercl;
    280   1.1       cgd 		kbp->kb_totalfree += kbp->kb_elmpercl;
    281   1.1       cgd #endif
    282   1.1       cgd 		/*
    283   1.1       cgd 		 * Just in case we blocked while allocating memory,
    284   1.1       cgd 		 * and someone else also allocated memory for this
    285   1.1       cgd 		 * bucket, don't assume the list is still empty.
    286   1.1       cgd 		 */
    287   1.1       cgd 		savedlist = kbp->kb_next;
    288   1.8       cgd 		kbp->kb_next = cp = va + (npg * NBPG) - allocsize;
    289   1.8       cgd 		for (;;) {
    290   1.8       cgd 			freep = (struct freelist *)cp;
    291   1.8       cgd #ifdef DIAGNOSTIC
    292   1.8       cgd 			/*
    293   1.8       cgd 			 * Copy in known text to detect modification
    294   1.8       cgd 			 * after freeing.
    295   1.8       cgd 			 */
    296  1.11       cgd 			end = (int32_t *)&cp[copysize];
    297  1.11       cgd 			for (lp = (int32_t *)cp; lp < end; lp++)
    298   1.8       cgd 				*lp = WEIRD_ADDR;
    299   1.8       cgd 			freep->type = M_FREE;
    300   1.8       cgd #endif /* DIAGNOSTIC */
    301   1.8       cgd 			if (cp <= va)
    302   1.8       cgd 				break;
    303   1.8       cgd 			cp -= allocsize;
    304   1.8       cgd 			freep->next = cp;
    305   1.8       cgd 		}
    306   1.8       cgd 		freep->next = savedlist;
    307   1.8       cgd 		if (kbp->kb_last == NULL)
    308   1.8       cgd 			kbp->kb_last = (caddr_t)freep;
    309   1.1       cgd 	}
    310   1.1       cgd 	va = kbp->kb_next;
    311   1.8       cgd 	kbp->kb_next = ((struct freelist *)va)->next;
    312   1.8       cgd #ifdef DIAGNOSTIC
    313   1.8       cgd 	freep = (struct freelist *)va;
    314   1.8       cgd 	savedtype = (unsigned)freep->type < M_LAST ?
    315   1.8       cgd 		memname[freep->type] : "???";
    316  1.28       mrg #if defined(UVM)
    317  1.29       chs 	if (kbp->kb_next) {
    318  1.29       chs 		int rv;
    319  1.29       chs 		vm_offset_t addr = (vm_offset_t)kbp->kb_next;
    320  1.29       chs 
    321  1.29       chs 		vm_map_lock_read(kmem_map);
    322  1.29       chs 		rv = uvm_map_checkprot(kmem_map, addr,
    323  1.29       chs 				       addr + sizeof(struct freelist),
    324  1.29       chs 				       VM_PROT_WRITE);
    325  1.29       chs 		vm_map_unlock_read(kmem_map);
    326  1.29       chs 
    327  1.29       chs 		if (!rv)
    328  1.28       mrg #else
    329  1.29       chs 	if (kbp->kb_next &&
    330  1.28       mrg 	    !kernacc(kbp->kb_next, sizeof(struct freelist), 0))
    331  1.28       mrg #endif
    332  1.28       mrg 								{
    333  1.22  christos 		printf(
    334  1.21  christos 		    "%s %ld of object %p size %ld %s %s (invalid addr %p)\n",
    335  1.21  christos 		    "Data modified on freelist: word",
    336  1.21  christos 		    (long)((int32_t *)&kbp->kb_next - (int32_t *)kbp),
    337  1.21  christos 		    va, size, "previous type", savedtype, kbp->kb_next);
    338  1.27   thorpej #ifdef MALLOCLOG
    339  1.27   thorpej 		hitmlog(va);
    340  1.27   thorpej #endif
    341   1.8       cgd 		kbp->kb_next = NULL;
    342  1.29       chs #if defined(UVM)
    343  1.29       chs 		}
    344  1.29       chs #endif
    345   1.8       cgd 	}
    346  1.11       cgd 
    347  1.11       cgd 	/* Fill the fields that we've used with WEIRD_ADDR */
    348   1.8       cgd #if BYTE_ORDER == BIG_ENDIAN
    349   1.8       cgd 	freep->type = WEIRD_ADDR >> 16;
    350   1.8       cgd #endif
    351   1.8       cgd #if BYTE_ORDER == LITTLE_ENDIAN
    352   1.8       cgd 	freep->type = (short)WEIRD_ADDR;
    353   1.8       cgd #endif
    354  1.11       cgd 	end = (int32_t *)&freep->next +
    355  1.11       cgd 	    (sizeof(freep->next) / sizeof(int32_t));
    356  1.11       cgd 	for (lp = (int32_t *)&freep->next; lp < end; lp++)
    357  1.11       cgd 		*lp = WEIRD_ADDR;
    358  1.11       cgd 
    359  1.11       cgd 	/* and check that the data hasn't been modified. */
    360  1.11       cgd 	end = (int32_t *)&va[copysize];
    361  1.11       cgd 	for (lp = (int32_t *)va; lp < end; lp++) {
    362   1.8       cgd 		if (*lp == WEIRD_ADDR)
    363   1.8       cgd 			continue;
    364  1.22  christos 		printf("%s %ld of object %p size %ld %s %s (0x%x != 0x%x)\n",
    365  1.21  christos 		    "Data modified on freelist: word",
    366  1.21  christos 		    (long)(lp - (int32_t *)va), va, size, "previous type",
    367  1.21  christos 		    savedtype, *lp, WEIRD_ADDR);
    368  1.27   thorpej #ifdef MALLOCLOG
    369  1.27   thorpej 		hitmlog(va);
    370  1.27   thorpej #endif
    371   1.8       cgd 		break;
    372   1.8       cgd 	}
    373  1.11       cgd 
    374   1.8       cgd 	freep->spare0 = 0;
    375   1.8       cgd #endif /* DIAGNOSTIC */
    376   1.1       cgd #ifdef KMEMSTATS
    377   1.1       cgd 	kup = btokup(va);
    378   1.1       cgd 	if (kup->ku_indx != indx)
    379   1.1       cgd 		panic("malloc: wrong bucket");
    380   1.1       cgd 	if (kup->ku_freecnt == 0)
    381   1.1       cgd 		panic("malloc: lost data");
    382   1.1       cgd 	kup->ku_freecnt--;
    383   1.1       cgd 	kbp->kb_totalfree--;
    384   1.1       cgd 	ksp->ks_memuse += 1 << indx;
    385   1.1       cgd out:
    386   1.1       cgd 	kbp->kb_calls++;
    387   1.1       cgd 	ksp->ks_inuse++;
    388   1.1       cgd 	ksp->ks_calls++;
    389   1.1       cgd 	if (ksp->ks_memuse > ksp->ks_maxused)
    390   1.1       cgd 		ksp->ks_maxused = ksp->ks_memuse;
    391   1.1       cgd #else
    392   1.1       cgd out:
    393   1.1       cgd #endif
    394  1.27   thorpej #ifdef MALLOCLOG
    395  1.27   thorpej 	domlog(va, size, type, 1, file, line);
    396  1.27   thorpej #endif
    397   1.1       cgd 	splx(s);
    398  1.32      fvdl #ifdef LOCKDEBUG
    399  1.32      fvdl 	if (flags & M_NOWAIT)
    400  1.32      fvdl 		simplelockrecurse--;
    401  1.32      fvdl #endif
    402   1.1       cgd 	return ((void *) va);
    403   1.1       cgd }
    404   1.1       cgd 
    405   1.1       cgd /*
    406   1.1       cgd  * Free a block of memory allocated by malloc.
    407   1.1       cgd  */
    408  1.27   thorpej #ifdef MALLOCLOG
    409  1.27   thorpej void
    410  1.27   thorpej _free(addr, type, file, line)
    411  1.27   thorpej 	void *addr;
    412  1.27   thorpej 	int type;
    413  1.27   thorpej 	const char *file;
    414  1.27   thorpej 	long line;
    415  1.27   thorpej #else
    416   1.1       cgd void
    417   1.1       cgd free(addr, type)
    418   1.1       cgd 	void *addr;
    419   1.1       cgd 	int type;
    420  1.27   thorpej #endif /* MALLOCLOG */
    421   1.1       cgd {
    422   1.1       cgd 	register struct kmembuckets *kbp;
    423   1.1       cgd 	register struct kmemusage *kup;
    424   1.8       cgd 	register struct freelist *freep;
    425   1.8       cgd 	long size;
    426   1.8       cgd 	int s;
    427   1.5    andrew #ifdef DIAGNOSTIC
    428   1.8       cgd 	caddr_t cp;
    429  1.11       cgd 	int32_t *end, *lp;
    430  1.11       cgd 	long alloc, copysize;
    431   1.5    andrew #endif
    432   1.1       cgd #ifdef KMEMSTATS
    433   1.1       cgd 	register struct kmemstats *ksp = &kmemstats[type];
    434   1.1       cgd #endif
    435   1.1       cgd 
    436   1.1       cgd 	kup = btokup(addr);
    437   1.1       cgd 	size = 1 << kup->ku_indx;
    438   1.8       cgd 	kbp = &bucket[kup->ku_indx];
    439   1.8       cgd 	s = splimp();
    440  1.27   thorpej #ifdef MALLOCLOG
    441  1.27   thorpej 	domlog(addr, 0, type, 2, file, line);
    442  1.27   thorpej #endif
    443   1.1       cgd #ifdef DIAGNOSTIC
    444   1.8       cgd 	/*
    445   1.8       cgd 	 * Check for returns of data that do not point to the
    446   1.8       cgd 	 * beginning of the allocation.
    447   1.8       cgd 	 */
    448   1.1       cgd 	if (size > NBPG * CLSIZE)
    449   1.1       cgd 		alloc = addrmask[BUCKETINDX(NBPG * CLSIZE)];
    450   1.1       cgd 	else
    451   1.1       cgd 		alloc = addrmask[kup->ku_indx];
    452   1.8       cgd 	if (((u_long)addr & alloc) != 0)
    453  1.15  christos 		panic("free: unaligned addr %p, size %ld, type %s, mask %ld\n",
    454   1.8       cgd 			addr, size, memname[type], alloc);
    455   1.1       cgd #endif /* DIAGNOSTIC */
    456   1.1       cgd 	if (size > MAXALLOCSAVE) {
    457  1.28       mrg #if defined(UVM)
    458  1.28       mrg 		uvm_km_free(kmem_map, (vm_offset_t)addr, ctob(kup->ku_pagecnt));
    459  1.28       mrg #else
    460   1.1       cgd 		kmem_free(kmem_map, (vm_offset_t)addr, ctob(kup->ku_pagecnt));
    461  1.28       mrg #endif
    462   1.1       cgd #ifdef KMEMSTATS
    463   1.1       cgd 		size = kup->ku_pagecnt << PGSHIFT;
    464   1.1       cgd 		ksp->ks_memuse -= size;
    465   1.1       cgd 		kup->ku_indx = 0;
    466   1.1       cgd 		kup->ku_pagecnt = 0;
    467   1.1       cgd 		if (ksp->ks_memuse + size >= ksp->ks_limit &&
    468   1.1       cgd 		    ksp->ks_memuse < ksp->ks_limit)
    469   1.1       cgd 			wakeup((caddr_t)ksp);
    470   1.1       cgd 		ksp->ks_inuse--;
    471   1.1       cgd 		kbp->kb_total -= 1;
    472   1.1       cgd #endif
    473   1.1       cgd 		splx(s);
    474   1.1       cgd 		return;
    475   1.1       cgd 	}
    476   1.8       cgd 	freep = (struct freelist *)addr;
    477   1.8       cgd #ifdef DIAGNOSTIC
    478   1.8       cgd 	/*
    479   1.8       cgd 	 * Check for multiple frees. Use a quick check to see if
    480   1.8       cgd 	 * it looks free before laboriously searching the freelist.
    481   1.8       cgd 	 */
    482   1.8       cgd 	if (freep->spare0 == WEIRD_ADDR) {
    483  1.16       cgd 		for (cp = kbp->kb_next; cp;
    484  1.16       cgd 		    cp = ((struct freelist *)cp)->next) {
    485   1.8       cgd 			if (addr != cp)
    486   1.8       cgd 				continue;
    487  1.22  christos 			printf("multiply freed item %p\n", addr);
    488  1.27   thorpej #ifdef MALLOCLOG
    489  1.27   thorpej 			hitmlog(addr);
    490  1.27   thorpej #endif
    491   1.8       cgd 			panic("free: duplicated free");
    492   1.8       cgd 		}
    493   1.8       cgd 	}
    494   1.8       cgd 	/*
    495   1.8       cgd 	 * Copy in known text to detect modification after freeing
    496   1.8       cgd 	 * and to make it look free. Also, save the type being freed
    497   1.8       cgd 	 * so we can list likely culprit if modification is detected
    498   1.8       cgd 	 * when the object is reallocated.
    499   1.8       cgd 	 */
    500   1.8       cgd 	copysize = size < MAX_COPY ? size : MAX_COPY;
    501  1.11       cgd 	end = (int32_t *)&((caddr_t)addr)[copysize];
    502  1.11       cgd 	for (lp = (int32_t *)addr; lp < end; lp++)
    503   1.8       cgd 		*lp = WEIRD_ADDR;
    504   1.8       cgd 	freep->type = type;
    505   1.8       cgd #endif /* DIAGNOSTIC */
    506   1.1       cgd #ifdef KMEMSTATS
    507   1.1       cgd 	kup->ku_freecnt++;
    508   1.1       cgd 	if (kup->ku_freecnt >= kbp->kb_elmpercl)
    509   1.1       cgd 		if (kup->ku_freecnt > kbp->kb_elmpercl)
    510   1.1       cgd 			panic("free: multiple frees");
    511   1.1       cgd 		else if (kbp->kb_totalfree > kbp->kb_highwat)
    512   1.1       cgd 			kbp->kb_couldfree++;
    513   1.1       cgd 	kbp->kb_totalfree++;
    514   1.1       cgd 	ksp->ks_memuse -= size;
    515   1.1       cgd 	if (ksp->ks_memuse + size >= ksp->ks_limit &&
    516   1.1       cgd 	    ksp->ks_memuse < ksp->ks_limit)
    517   1.1       cgd 		wakeup((caddr_t)ksp);
    518   1.1       cgd 	ksp->ks_inuse--;
    519   1.1       cgd #endif
    520   1.8       cgd 	if (kbp->kb_next == NULL)
    521   1.8       cgd 		kbp->kb_next = addr;
    522   1.8       cgd 	else
    523   1.8       cgd 		((struct freelist *)kbp->kb_last)->next = addr;
    524   1.8       cgd 	freep->next = NULL;
    525   1.8       cgd 	kbp->kb_last = addr;
    526   1.1       cgd 	splx(s);
    527  1.20       cgd }
    528  1.20       cgd 
    529  1.20       cgd /*
    530  1.20       cgd  * Change the size of a block of memory.
    531  1.20       cgd  */
    532  1.20       cgd void *
    533  1.20       cgd realloc(curaddr, newsize, type, flags)
    534  1.20       cgd 	void *curaddr;
    535  1.20       cgd 	unsigned long newsize;
    536  1.20       cgd 	int type, flags;
    537  1.20       cgd {
    538  1.20       cgd 	register struct kmemusage *kup;
    539  1.20       cgd 	long cursize;
    540  1.20       cgd 	void *newaddr;
    541  1.20       cgd #ifdef DIAGNOSTIC
    542  1.20       cgd 	long alloc;
    543  1.20       cgd #endif
    544  1.20       cgd 
    545  1.20       cgd 	/*
    546  1.20       cgd 	 * Realloc() with a NULL pointer is the same as malloc().
    547  1.20       cgd 	 */
    548  1.20       cgd 	if (curaddr == NULL)
    549  1.20       cgd 		return (malloc(newsize, type, flags));
    550  1.20       cgd 
    551  1.20       cgd 	/*
    552  1.20       cgd 	 * Realloc() with zero size is the same as free().
    553  1.20       cgd 	 */
    554  1.20       cgd 	if (newsize == 0) {
    555  1.20       cgd 		free(curaddr, type);
    556  1.20       cgd 		return (NULL);
    557  1.20       cgd 	}
    558  1.20       cgd 
    559  1.20       cgd 	/*
    560  1.20       cgd 	 * Find out how large the old allocation was (and do some
    561  1.20       cgd 	 * sanity checking).
    562  1.20       cgd 	 */
    563  1.20       cgd 	kup = btokup(curaddr);
    564  1.20       cgd 	cursize = 1 << kup->ku_indx;
    565  1.20       cgd 
    566  1.20       cgd #ifdef DIAGNOSTIC
    567  1.20       cgd 	/*
    568  1.20       cgd 	 * Check for returns of data that do not point to the
    569  1.20       cgd 	 * beginning of the allocation.
    570  1.20       cgd 	 */
    571  1.20       cgd 	if (cursize > NBPG * CLSIZE)
    572  1.20       cgd 		alloc = addrmask[BUCKETINDX(NBPG * CLSIZE)];
    573  1.20       cgd 	else
    574  1.20       cgd 		alloc = addrmask[kup->ku_indx];
    575  1.20       cgd 	if (((u_long)curaddr & alloc) != 0)
    576  1.20       cgd 		panic("realloc: unaligned addr %p, size %ld, type %s, mask %ld\n",
    577  1.20       cgd 			curaddr, cursize, memname[type], alloc);
    578  1.20       cgd #endif /* DIAGNOSTIC */
    579  1.20       cgd 
    580  1.20       cgd 	if (cursize > MAXALLOCSAVE)
    581  1.20       cgd 		cursize = ctob(kup->ku_pagecnt);
    582  1.20       cgd 
    583  1.20       cgd 	/*
    584  1.20       cgd 	 * If we already actually have as much as they want, we're done.
    585  1.20       cgd 	 */
    586  1.20       cgd 	if (newsize <= cursize)
    587  1.20       cgd 		return (curaddr);
    588  1.20       cgd 
    589  1.20       cgd 	/*
    590  1.20       cgd 	 * Can't satisfy the allocation with the existing block.
    591  1.20       cgd 	 * Allocate a new one and copy the data.
    592  1.20       cgd 	 */
    593  1.20       cgd 	newaddr = malloc(newsize, type, flags);
    594  1.20       cgd 	if (newaddr == NULL) {
    595  1.20       cgd 		/*
    596  1.20       cgd 		 * Malloc() failed, because flags included M_NOWAIT.
    597  1.20       cgd 		 * Return NULL to indicate that failure.  The old
    598  1.20       cgd 		 * pointer is still valid.
    599  1.20       cgd 		 */
    600  1.20       cgd 		return NULL;
    601  1.20       cgd 	}
    602  1.20       cgd 	bcopy(curaddr, newaddr, cursize);
    603  1.20       cgd 
    604  1.20       cgd 	/*
    605  1.20       cgd 	 * We were successful: free the old allocation and return
    606  1.20       cgd 	 * the new one.
    607  1.20       cgd 	 */
    608  1.20       cgd 	free(curaddr, type);
    609  1.20       cgd 	return (newaddr);
    610   1.1       cgd }
    611   1.1       cgd 
    612   1.1       cgd /*
    613   1.1       cgd  * Initialize the kernel memory allocator
    614   1.1       cgd  */
    615  1.12  christos void
    616   1.1       cgd kmeminit()
    617   1.1       cgd {
    618  1.23       tls #ifdef KMEMSTATS
    619   1.1       cgd 	register long indx;
    620  1.23       tls #endif
    621   1.1       cgd 	int npg;
    622   1.1       cgd 
    623   1.1       cgd #if	((MAXALLOCSAVE & (MAXALLOCSAVE - 1)) != 0)
    624   1.1       cgd 		ERROR!_kmeminit:_MAXALLOCSAVE_not_power_of_2
    625   1.1       cgd #endif
    626   1.1       cgd #if	(MAXALLOCSAVE > MINALLOCSIZE * 32768)
    627   1.1       cgd 		ERROR!_kmeminit:_MAXALLOCSAVE_too_big
    628   1.1       cgd #endif
    629   1.1       cgd #if	(MAXALLOCSAVE < CLBYTES)
    630   1.1       cgd 		ERROR!_kmeminit:_MAXALLOCSAVE_too_small
    631   1.1       cgd #endif
    632  1.11       cgd 
    633  1.11       cgd 	if (sizeof(struct freelist) > (1 << MINBUCKET))
    634  1.11       cgd 		panic("minbucket too small/struct freelist too big");
    635  1.11       cgd 
    636   1.1       cgd 	npg = VM_KMEM_SIZE/ NBPG;
    637  1.28       mrg #if defined(UVM)
    638  1.28       mrg 	kmemusage = (struct kmemusage *) uvm_km_zalloc(kernel_map,
    639  1.28       mrg 		(vm_size_t)(npg * sizeof(struct kmemusage)));
    640  1.28       mrg 	kmem_map = uvm_km_suballoc(kernel_map, (vm_offset_t *)&kmembase,
    641  1.28       mrg 		(vm_offset_t *)&kmemlimit, (vm_size_t)(npg * NBPG),
    642  1.30   thorpej 			FALSE, FALSE, &kmem_map_store);
    643  1.28       mrg #else
    644   1.1       cgd 	kmemusage = (struct kmemusage *) kmem_alloc(kernel_map,
    645   1.1       cgd 		(vm_size_t)(npg * sizeof(struct kmemusage)));
    646   1.1       cgd 	kmem_map = kmem_suballoc(kernel_map, (vm_offset_t *)&kmembase,
    647   1.1       cgd 		(vm_offset_t *)&kmemlimit, (vm_size_t)(npg * NBPG), FALSE);
    648  1.28       mrg #endif
    649   1.1       cgd #ifdef KMEMSTATS
    650   1.1       cgd 	for (indx = 0; indx < MINBUCKET + 16; indx++) {
    651   1.1       cgd 		if (1 << indx >= CLBYTES)
    652   1.1       cgd 			bucket[indx].kb_elmpercl = 1;
    653   1.1       cgd 		else
    654   1.1       cgd 			bucket[indx].kb_elmpercl = CLBYTES / (1 << indx);
    655   1.1       cgd 		bucket[indx].kb_highwat = 5 * bucket[indx].kb_elmpercl;
    656   1.1       cgd 	}
    657   1.8       cgd 	for (indx = 0; indx < M_LAST; indx++)
    658   1.1       cgd 		kmemstats[indx].ks_limit = npg * NBPG * 6 / 10;
    659   1.1       cgd #endif
    660   1.1       cgd }
    661