Home | History | Annotate | Line # | Download | only in kern
kern_malloc.c revision 1.21
      1  1.21  christos /*	$NetBSD: kern_malloc.c,v 1.21 1996/10/10 22:46:15 christos 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.9       cgd  *	@(#)kern_malloc.c	8.3 (Berkeley) 1/4/94
     37   1.1       cgd  */
     38   1.1       cgd 
     39   1.7   mycroft #include <sys/param.h>
     40   1.7   mycroft #include <sys/proc.h>
     41   1.8       cgd #include <sys/map.h>
     42   1.7   mycroft #include <sys/kernel.h>
     43   1.7   mycroft #include <sys/malloc.h>
     44  1.12  christos #include <sys/systm.h>
     45   1.7   mycroft 
     46   1.7   mycroft #include <vm/vm.h>
     47   1.7   mycroft #include <vm/vm_kern.h>
     48  1.12  christos 
     49   1.1       cgd struct kmembuckets bucket[MINBUCKET + 16];
     50   1.8       cgd struct kmemstats kmemstats[M_LAST];
     51   1.1       cgd struct kmemusage *kmemusage;
     52   1.1       cgd char *kmembase, *kmemlimit;
     53   1.1       cgd char *memname[] = INITKMEMNAMES;
     54   1.1       cgd 
     55   1.8       cgd #ifdef DIAGNOSTIC
     56   1.8       cgd /*
     57   1.8       cgd  * This structure provides a set of masks to catch unaligned frees.
     58   1.8       cgd  */
     59   1.8       cgd long addrmask[] = { 0,
     60   1.8       cgd 	0x00000001, 0x00000003, 0x00000007, 0x0000000f,
     61   1.8       cgd 	0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
     62   1.8       cgd 	0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
     63   1.8       cgd 	0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
     64   1.8       cgd };
     65   1.8       cgd 
     66   1.8       cgd /*
     67   1.8       cgd  * The WEIRD_ADDR is used as known text to copy into free objects so
     68   1.8       cgd  * that modifications after frees can be detected.
     69   1.8       cgd  */
     70  1.12  christos #define WEIRD_ADDR	((unsigned) 0xdeadbeef)
     71   1.8       cgd #define MAX_COPY	32
     72   1.8       cgd 
     73   1.8       cgd /*
     74  1.11       cgd  * Normally the freelist structure is used only to hold the list pointer
     75  1.11       cgd  * for free objects.  However, when running with diagnostics, the first
     76  1.11       cgd  * 8 bytes of the structure is unused except for diagnostic information,
     77  1.11       cgd  * and the free list pointer is at offst 8 in the structure.  Since the
     78  1.11       cgd  * first 8 bytes is the portion of the structure most often modified, this
     79  1.11       cgd  * helps to detect memory reuse problems and avoid free list corruption.
     80   1.8       cgd  */
     81   1.8       cgd struct freelist {
     82  1.11       cgd 	int32_t	spare0;
     83  1.11       cgd 	int16_t	type;
     84  1.11       cgd 	int16_t	spare1;
     85   1.8       cgd 	caddr_t	next;
     86   1.8       cgd };
     87   1.8       cgd #else /* !DIAGNOSTIC */
     88   1.8       cgd struct freelist {
     89   1.8       cgd 	caddr_t	next;
     90   1.8       cgd };
     91   1.8       cgd #endif /* DIAGNOSTIC */
     92   1.8       cgd 
     93   1.1       cgd /*
     94   1.1       cgd  * Allocate a block of memory
     95   1.1       cgd  */
     96   1.1       cgd void *
     97   1.1       cgd malloc(size, type, flags)
     98   1.1       cgd 	unsigned long size;
     99   1.1       cgd 	int type, flags;
    100   1.1       cgd {
    101   1.1       cgd 	register struct kmembuckets *kbp;
    102   1.1       cgd 	register struct kmemusage *kup;
    103   1.8       cgd 	register struct freelist *freep;
    104   1.5    andrew 	long indx, npg, allocsize;
    105   1.1       cgd 	int s;
    106   1.1       cgd 	caddr_t va, cp, savedlist;
    107   1.8       cgd #ifdef DIAGNOSTIC
    108  1.11       cgd 	int32_t *end, *lp;
    109   1.8       cgd 	int copysize;
    110   1.8       cgd 	char *savedtype;
    111   1.8       cgd #endif
    112   1.1       cgd #ifdef KMEMSTATS
    113   1.1       cgd 	register struct kmemstats *ksp = &kmemstats[type];
    114   1.1       cgd 
    115   1.1       cgd 	if (((unsigned long)type) > M_LAST)
    116   1.1       cgd 		panic("malloc - bogus type");
    117   1.1       cgd #endif
    118   1.1       cgd 	indx = BUCKETINDX(size);
    119   1.1       cgd 	kbp = &bucket[indx];
    120   1.1       cgd 	s = splimp();
    121   1.1       cgd #ifdef KMEMSTATS
    122   1.1       cgd 	while (ksp->ks_memuse >= ksp->ks_limit) {
    123   1.1       cgd 		if (flags & M_NOWAIT) {
    124   1.1       cgd 			splx(s);
    125   1.1       cgd 			return ((void *) NULL);
    126   1.1       cgd 		}
    127   1.1       cgd 		if (ksp->ks_limblocks < 65535)
    128   1.1       cgd 			ksp->ks_limblocks++;
    129   1.1       cgd 		tsleep((caddr_t)ksp, PSWP+2, memname[type], 0);
    130   1.1       cgd 	}
    131   1.8       cgd 	ksp->ks_size |= 1 << indx;
    132   1.8       cgd #endif
    133   1.8       cgd #ifdef DIAGNOSTIC
    134   1.8       cgd 	copysize = 1 << indx < MAX_COPY ? 1 << indx : MAX_COPY;
    135   1.1       cgd #endif
    136   1.1       cgd 	if (kbp->kb_next == NULL) {
    137   1.8       cgd 		kbp->kb_last = NULL;
    138   1.1       cgd 		if (size > MAXALLOCSAVE)
    139   1.1       cgd 			allocsize = roundup(size, CLBYTES);
    140   1.1       cgd 		else
    141   1.1       cgd 			allocsize = 1 << indx;
    142   1.1       cgd 		npg = clrnd(btoc(allocsize));
    143   1.1       cgd 		va = (caddr_t) kmem_malloc(kmem_map, (vm_size_t)ctob(npg),
    144   1.1       cgd 					   !(flags & M_NOWAIT));
    145   1.1       cgd 		if (va == NULL) {
    146  1.17       cgd 			/*
    147  1.17       cgd 			 * Kmem_malloc() can return NULL, even if it can
    148  1.17       cgd 			 * wait, if there is no map space avaiable, because
    149  1.17       cgd 			 * it can't fix that problem.  Neither can we,
    150  1.17       cgd 			 * right now.  (We should release pages which
    151  1.17       cgd 			 * are completely free and which are in buckets
    152  1.17       cgd 			 * with too many free elements.)
    153  1.17       cgd 			 */
    154  1.17       cgd 			if ((flags & M_NOWAIT) == 0)
    155  1.17       cgd 				panic("malloc: out of space in kmem_map");
    156   1.6       cgd 			splx(s);
    157   1.6       cgd 			return ((void *) NULL);
    158   1.1       cgd 		}
    159   1.1       cgd #ifdef KMEMSTATS
    160   1.1       cgd 		kbp->kb_total += kbp->kb_elmpercl;
    161   1.1       cgd #endif
    162   1.1       cgd 		kup = btokup(va);
    163   1.1       cgd 		kup->ku_indx = indx;
    164   1.1       cgd 		if (allocsize > MAXALLOCSAVE) {
    165   1.1       cgd 			if (npg > 65535)
    166   1.1       cgd 				panic("malloc: allocation too large");
    167   1.1       cgd 			kup->ku_pagecnt = npg;
    168   1.1       cgd #ifdef KMEMSTATS
    169   1.1       cgd 			ksp->ks_memuse += allocsize;
    170   1.1       cgd #endif
    171   1.1       cgd 			goto out;
    172   1.1       cgd 		}
    173   1.1       cgd #ifdef KMEMSTATS
    174   1.1       cgd 		kup->ku_freecnt = kbp->kb_elmpercl;
    175   1.1       cgd 		kbp->kb_totalfree += kbp->kb_elmpercl;
    176   1.1       cgd #endif
    177   1.1       cgd 		/*
    178   1.1       cgd 		 * Just in case we blocked while allocating memory,
    179   1.1       cgd 		 * and someone else also allocated memory for this
    180   1.1       cgd 		 * bucket, don't assume the list is still empty.
    181   1.1       cgd 		 */
    182   1.1       cgd 		savedlist = kbp->kb_next;
    183   1.8       cgd 		kbp->kb_next = cp = va + (npg * NBPG) - allocsize;
    184   1.8       cgd 		for (;;) {
    185   1.8       cgd 			freep = (struct freelist *)cp;
    186   1.8       cgd #ifdef DIAGNOSTIC
    187   1.8       cgd 			/*
    188   1.8       cgd 			 * Copy in known text to detect modification
    189   1.8       cgd 			 * after freeing.
    190   1.8       cgd 			 */
    191  1.11       cgd 			end = (int32_t *)&cp[copysize];
    192  1.11       cgd 			for (lp = (int32_t *)cp; lp < end; lp++)
    193   1.8       cgd 				*lp = WEIRD_ADDR;
    194   1.8       cgd 			freep->type = M_FREE;
    195   1.8       cgd #endif /* DIAGNOSTIC */
    196   1.8       cgd 			if (cp <= va)
    197   1.8       cgd 				break;
    198   1.8       cgd 			cp -= allocsize;
    199   1.8       cgd 			freep->next = cp;
    200   1.8       cgd 		}
    201   1.8       cgd 		freep->next = savedlist;
    202   1.8       cgd 		if (kbp->kb_last == NULL)
    203   1.8       cgd 			kbp->kb_last = (caddr_t)freep;
    204   1.1       cgd 	}
    205   1.1       cgd 	va = kbp->kb_next;
    206   1.8       cgd 	kbp->kb_next = ((struct freelist *)va)->next;
    207   1.8       cgd #ifdef DIAGNOSTIC
    208   1.8       cgd 	freep = (struct freelist *)va;
    209   1.8       cgd 	savedtype = (unsigned)freep->type < M_LAST ?
    210   1.8       cgd 		memname[freep->type] : "???";
    211   1.8       cgd 	if (kbp->kb_next &&
    212   1.8       cgd 	    !kernacc(kbp->kb_next, sizeof(struct freelist), 0)) {
    213  1.21  christos 		kprintf(
    214  1.21  christos 		    "%s %ld of object %p size %ld %s %s (invalid addr %p)\n",
    215  1.21  christos 		    "Data modified on freelist: word",
    216  1.21  christos 		    (long)((int32_t *)&kbp->kb_next - (int32_t *)kbp),
    217  1.21  christos 		    va, size, "previous type", savedtype, kbp->kb_next);
    218   1.8       cgd 		kbp->kb_next = NULL;
    219   1.8       cgd 	}
    220  1.11       cgd 
    221  1.11       cgd 	/* Fill the fields that we've used with WEIRD_ADDR */
    222   1.8       cgd #if BYTE_ORDER == BIG_ENDIAN
    223   1.8       cgd 	freep->type = WEIRD_ADDR >> 16;
    224   1.8       cgd #endif
    225   1.8       cgd #if BYTE_ORDER == LITTLE_ENDIAN
    226   1.8       cgd 	freep->type = (short)WEIRD_ADDR;
    227   1.8       cgd #endif
    228  1.11       cgd 	end = (int32_t *)&freep->next +
    229  1.11       cgd 	    (sizeof(freep->next) / sizeof(int32_t));
    230  1.11       cgd 	for (lp = (int32_t *)&freep->next; lp < end; lp++)
    231  1.11       cgd 		*lp = WEIRD_ADDR;
    232  1.11       cgd 
    233  1.11       cgd 	/* and check that the data hasn't been modified. */
    234  1.11       cgd 	end = (int32_t *)&va[copysize];
    235  1.11       cgd 	for (lp = (int32_t *)va; lp < end; lp++) {
    236   1.8       cgd 		if (*lp == WEIRD_ADDR)
    237   1.8       cgd 			continue;
    238  1.21  christos 		kprintf("%s %ld of object %p size %ld %s %s (0x%x != 0x%x)\n",
    239  1.21  christos 		    "Data modified on freelist: word",
    240  1.21  christos 		    (long)(lp - (int32_t *)va), va, size, "previous type",
    241  1.21  christos 		    savedtype, *lp, WEIRD_ADDR);
    242   1.8       cgd 		break;
    243   1.8       cgd 	}
    244  1.11       cgd 
    245   1.8       cgd 	freep->spare0 = 0;
    246   1.8       cgd #endif /* DIAGNOSTIC */
    247   1.1       cgd #ifdef KMEMSTATS
    248   1.1       cgd 	kup = btokup(va);
    249   1.1       cgd 	if (kup->ku_indx != indx)
    250   1.1       cgd 		panic("malloc: wrong bucket");
    251   1.1       cgd 	if (kup->ku_freecnt == 0)
    252   1.1       cgd 		panic("malloc: lost data");
    253   1.1       cgd 	kup->ku_freecnt--;
    254   1.1       cgd 	kbp->kb_totalfree--;
    255   1.1       cgd 	ksp->ks_memuse += 1 << indx;
    256   1.1       cgd out:
    257   1.1       cgd 	kbp->kb_calls++;
    258   1.1       cgd 	ksp->ks_inuse++;
    259   1.1       cgd 	ksp->ks_calls++;
    260   1.1       cgd 	if (ksp->ks_memuse > ksp->ks_maxused)
    261   1.1       cgd 		ksp->ks_maxused = ksp->ks_memuse;
    262   1.1       cgd #else
    263   1.1       cgd out:
    264   1.1       cgd #endif
    265   1.1       cgd 	splx(s);
    266   1.1       cgd 	return ((void *) va);
    267   1.1       cgd }
    268   1.1       cgd 
    269   1.1       cgd /*
    270   1.1       cgd  * Free a block of memory allocated by malloc.
    271   1.1       cgd  */
    272   1.1       cgd void
    273   1.1       cgd free(addr, type)
    274   1.1       cgd 	void *addr;
    275   1.1       cgd 	int type;
    276   1.1       cgd {
    277   1.1       cgd 	register struct kmembuckets *kbp;
    278   1.1       cgd 	register struct kmemusage *kup;
    279   1.8       cgd 	register struct freelist *freep;
    280   1.8       cgd 	long size;
    281   1.8       cgd 	int s;
    282   1.5    andrew #ifdef DIAGNOSTIC
    283   1.8       cgd 	caddr_t cp;
    284  1.11       cgd 	int32_t *end, *lp;
    285  1.11       cgd 	long alloc, copysize;
    286   1.5    andrew #endif
    287   1.1       cgd #ifdef KMEMSTATS
    288   1.1       cgd 	register struct kmemstats *ksp = &kmemstats[type];
    289   1.1       cgd #endif
    290   1.1       cgd 
    291   1.1       cgd 	kup = btokup(addr);
    292   1.1       cgd 	size = 1 << kup->ku_indx;
    293   1.8       cgd 	kbp = &bucket[kup->ku_indx];
    294   1.8       cgd 	s = splimp();
    295   1.1       cgd #ifdef DIAGNOSTIC
    296   1.8       cgd 	/*
    297   1.8       cgd 	 * Check for returns of data that do not point to the
    298   1.8       cgd 	 * beginning of the allocation.
    299   1.8       cgd 	 */
    300   1.1       cgd 	if (size > NBPG * CLSIZE)
    301   1.1       cgd 		alloc = addrmask[BUCKETINDX(NBPG * CLSIZE)];
    302   1.1       cgd 	else
    303   1.1       cgd 		alloc = addrmask[kup->ku_indx];
    304   1.8       cgd 	if (((u_long)addr & alloc) != 0)
    305  1.15  christos 		panic("free: unaligned addr %p, size %ld, type %s, mask %ld\n",
    306   1.8       cgd 			addr, size, memname[type], alloc);
    307   1.1       cgd #endif /* DIAGNOSTIC */
    308   1.1       cgd 	if (size > MAXALLOCSAVE) {
    309   1.1       cgd 		kmem_free(kmem_map, (vm_offset_t)addr, ctob(kup->ku_pagecnt));
    310   1.1       cgd #ifdef KMEMSTATS
    311   1.1       cgd 		size = kup->ku_pagecnt << PGSHIFT;
    312   1.1       cgd 		ksp->ks_memuse -= size;
    313   1.1       cgd 		kup->ku_indx = 0;
    314   1.1       cgd 		kup->ku_pagecnt = 0;
    315   1.1       cgd 		if (ksp->ks_memuse + size >= ksp->ks_limit &&
    316   1.1       cgd 		    ksp->ks_memuse < ksp->ks_limit)
    317   1.1       cgd 			wakeup((caddr_t)ksp);
    318   1.1       cgd 		ksp->ks_inuse--;
    319   1.1       cgd 		kbp->kb_total -= 1;
    320   1.1       cgd #endif
    321   1.1       cgd 		splx(s);
    322   1.1       cgd 		return;
    323   1.1       cgd 	}
    324   1.8       cgd 	freep = (struct freelist *)addr;
    325   1.8       cgd #ifdef DIAGNOSTIC
    326   1.8       cgd 	/*
    327   1.8       cgd 	 * Check for multiple frees. Use a quick check to see if
    328   1.8       cgd 	 * it looks free before laboriously searching the freelist.
    329   1.8       cgd 	 */
    330   1.8       cgd 	if (freep->spare0 == WEIRD_ADDR) {
    331  1.16       cgd 		for (cp = kbp->kb_next; cp;
    332  1.16       cgd 		    cp = ((struct freelist *)cp)->next) {
    333   1.8       cgd 			if (addr != cp)
    334   1.8       cgd 				continue;
    335  1.21  christos 			kprintf("multiply freed item %p\n", addr);
    336   1.8       cgd 			panic("free: duplicated free");
    337   1.8       cgd 		}
    338   1.8       cgd 	}
    339   1.8       cgd 	/*
    340   1.8       cgd 	 * Copy in known text to detect modification after freeing
    341   1.8       cgd 	 * and to make it look free. Also, save the type being freed
    342   1.8       cgd 	 * so we can list likely culprit if modification is detected
    343   1.8       cgd 	 * when the object is reallocated.
    344   1.8       cgd 	 */
    345   1.8       cgd 	copysize = size < MAX_COPY ? size : MAX_COPY;
    346  1.11       cgd 	end = (int32_t *)&((caddr_t)addr)[copysize];
    347  1.11       cgd 	for (lp = (int32_t *)addr; lp < end; lp++)
    348   1.8       cgd 		*lp = WEIRD_ADDR;
    349   1.8       cgd 	freep->type = type;
    350   1.8       cgd #endif /* DIAGNOSTIC */
    351   1.1       cgd #ifdef KMEMSTATS
    352   1.1       cgd 	kup->ku_freecnt++;
    353   1.1       cgd 	if (kup->ku_freecnt >= kbp->kb_elmpercl)
    354   1.1       cgd 		if (kup->ku_freecnt > kbp->kb_elmpercl)
    355   1.1       cgd 			panic("free: multiple frees");
    356   1.1       cgd 		else if (kbp->kb_totalfree > kbp->kb_highwat)
    357   1.1       cgd 			kbp->kb_couldfree++;
    358   1.1       cgd 	kbp->kb_totalfree++;
    359   1.1       cgd 	ksp->ks_memuse -= size;
    360   1.1       cgd 	if (ksp->ks_memuse + size >= ksp->ks_limit &&
    361   1.1       cgd 	    ksp->ks_memuse < ksp->ks_limit)
    362   1.1       cgd 		wakeup((caddr_t)ksp);
    363   1.1       cgd 	ksp->ks_inuse--;
    364   1.1       cgd #endif
    365   1.8       cgd 	if (kbp->kb_next == NULL)
    366   1.8       cgd 		kbp->kb_next = addr;
    367   1.8       cgd 	else
    368   1.8       cgd 		((struct freelist *)kbp->kb_last)->next = addr;
    369   1.8       cgd 	freep->next = NULL;
    370   1.8       cgd 	kbp->kb_last = addr;
    371   1.1       cgd 	splx(s);
    372  1.20       cgd }
    373  1.20       cgd 
    374  1.20       cgd /*
    375  1.20       cgd  * Change the size of a block of memory.
    376  1.20       cgd  */
    377  1.20       cgd void *
    378  1.20       cgd realloc(curaddr, newsize, type, flags)
    379  1.20       cgd 	void *curaddr;
    380  1.20       cgd 	unsigned long newsize;
    381  1.20       cgd 	int type, flags;
    382  1.20       cgd {
    383  1.20       cgd 	register struct kmemusage *kup;
    384  1.20       cgd 	long cursize;
    385  1.20       cgd 	void *newaddr;
    386  1.20       cgd #ifdef DIAGNOSTIC
    387  1.20       cgd 	long alloc;
    388  1.20       cgd #endif
    389  1.20       cgd 
    390  1.20       cgd 	/*
    391  1.20       cgd 	 * Realloc() with a NULL pointer is the same as malloc().
    392  1.20       cgd 	 */
    393  1.20       cgd 	if (curaddr == NULL)
    394  1.20       cgd 		return (malloc(newsize, type, flags));
    395  1.20       cgd 
    396  1.20       cgd 	/*
    397  1.20       cgd 	 * Realloc() with zero size is the same as free().
    398  1.20       cgd 	 */
    399  1.20       cgd 	if (newsize == 0) {
    400  1.20       cgd 		free(curaddr, type);
    401  1.20       cgd 		return (NULL);
    402  1.20       cgd 	}
    403  1.20       cgd 
    404  1.20       cgd 	/*
    405  1.20       cgd 	 * Find out how large the old allocation was (and do some
    406  1.20       cgd 	 * sanity checking).
    407  1.20       cgd 	 */
    408  1.20       cgd 	kup = btokup(curaddr);
    409  1.20       cgd 	cursize = 1 << kup->ku_indx;
    410  1.20       cgd 
    411  1.20       cgd #ifdef DIAGNOSTIC
    412  1.20       cgd 	/*
    413  1.20       cgd 	 * Check for returns of data that do not point to the
    414  1.20       cgd 	 * beginning of the allocation.
    415  1.20       cgd 	 */
    416  1.20       cgd 	if (cursize > NBPG * CLSIZE)
    417  1.20       cgd 		alloc = addrmask[BUCKETINDX(NBPG * CLSIZE)];
    418  1.20       cgd 	else
    419  1.20       cgd 		alloc = addrmask[kup->ku_indx];
    420  1.20       cgd 	if (((u_long)curaddr & alloc) != 0)
    421  1.20       cgd 		panic("realloc: unaligned addr %p, size %ld, type %s, mask %ld\n",
    422  1.20       cgd 			curaddr, cursize, memname[type], alloc);
    423  1.20       cgd #endif /* DIAGNOSTIC */
    424  1.20       cgd 
    425  1.20       cgd 	if (cursize > MAXALLOCSAVE)
    426  1.20       cgd 		cursize = ctob(kup->ku_pagecnt);
    427  1.20       cgd 
    428  1.20       cgd 	/*
    429  1.20       cgd 	 * If we already actually have as much as they want, we're done.
    430  1.20       cgd 	 */
    431  1.20       cgd 	if (newsize <= cursize)
    432  1.20       cgd 		return (curaddr);
    433  1.20       cgd 
    434  1.20       cgd 	/*
    435  1.20       cgd 	 * Can't satisfy the allocation with the existing block.
    436  1.20       cgd 	 * Allocate a new one and copy the data.
    437  1.20       cgd 	 */
    438  1.20       cgd 	newaddr = malloc(newsize, type, flags);
    439  1.20       cgd 	if (newaddr == NULL) {
    440  1.20       cgd 		/*
    441  1.20       cgd 		 * Malloc() failed, because flags included M_NOWAIT.
    442  1.20       cgd 		 * Return NULL to indicate that failure.  The old
    443  1.20       cgd 		 * pointer is still valid.
    444  1.20       cgd 		 */
    445  1.20       cgd 		return NULL;
    446  1.20       cgd 	}
    447  1.20       cgd 	bcopy(curaddr, newaddr, cursize);
    448  1.20       cgd 
    449  1.20       cgd 	/*
    450  1.20       cgd 	 * We were successful: free the old allocation and return
    451  1.20       cgd 	 * the new one.
    452  1.20       cgd 	 */
    453  1.20       cgd 	free(curaddr, type);
    454  1.20       cgd 	return (newaddr);
    455   1.1       cgd }
    456   1.1       cgd 
    457   1.1       cgd /*
    458   1.1       cgd  * Initialize the kernel memory allocator
    459   1.1       cgd  */
    460  1.12  christos void
    461   1.1       cgd kmeminit()
    462   1.1       cgd {
    463   1.1       cgd 	register long indx;
    464   1.1       cgd 	int npg;
    465   1.1       cgd 
    466   1.1       cgd #if	((MAXALLOCSAVE & (MAXALLOCSAVE - 1)) != 0)
    467   1.1       cgd 		ERROR!_kmeminit:_MAXALLOCSAVE_not_power_of_2
    468   1.1       cgd #endif
    469   1.1       cgd #if	(MAXALLOCSAVE > MINALLOCSIZE * 32768)
    470   1.1       cgd 		ERROR!_kmeminit:_MAXALLOCSAVE_too_big
    471   1.1       cgd #endif
    472   1.1       cgd #if	(MAXALLOCSAVE < CLBYTES)
    473   1.1       cgd 		ERROR!_kmeminit:_MAXALLOCSAVE_too_small
    474   1.1       cgd #endif
    475  1.11       cgd 
    476  1.11       cgd 	if (sizeof(struct freelist) > (1 << MINBUCKET))
    477  1.11       cgd 		panic("minbucket too small/struct freelist too big");
    478  1.11       cgd 
    479   1.1       cgd 	npg = VM_KMEM_SIZE/ NBPG;
    480   1.1       cgd 	kmemusage = (struct kmemusage *) kmem_alloc(kernel_map,
    481   1.1       cgd 		(vm_size_t)(npg * sizeof(struct kmemusage)));
    482   1.1       cgd 	kmem_map = kmem_suballoc(kernel_map, (vm_offset_t *)&kmembase,
    483   1.1       cgd 		(vm_offset_t *)&kmemlimit, (vm_size_t)(npg * NBPG), FALSE);
    484   1.1       cgd #ifdef KMEMSTATS
    485   1.1       cgd 	for (indx = 0; indx < MINBUCKET + 16; indx++) {
    486   1.1       cgd 		if (1 << indx >= CLBYTES)
    487   1.1       cgd 			bucket[indx].kb_elmpercl = 1;
    488   1.1       cgd 		else
    489   1.1       cgd 			bucket[indx].kb_elmpercl = CLBYTES / (1 << indx);
    490   1.1       cgd 		bucket[indx].kb_highwat = 5 * bucket[indx].kb_elmpercl;
    491   1.1       cgd 	}
    492   1.8       cgd 	for (indx = 0; indx < M_LAST; indx++)
    493   1.1       cgd 		kmemstats[indx].ks_limit = npg * NBPG * 6 / 10;
    494   1.1       cgd #endif
    495   1.1       cgd }
    496