Home | History | Annotate | Line # | Download | only in kern
kern_malloc.c revision 1.6.4.1
      1 /*
      2  * Copyright (c) 1987, 1991 The Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  *
     33  *	from: @(#)kern_malloc.c	7.25 (Berkeley) 5/8/91
     34  *	$Id: kern_malloc.c,v 1.6.4.1 1993/09/24 08:51:08 mycroft Exp $
     35  */
     36 
     37 #include "param.h"
     38 #include "systm.h"
     39 #include "proc.h"
     40 #include "kernel.h"
     41 #include "malloc.h"
     42 
     43 #include "machine/cpu.h"
     44 
     45 #include "vm/vm.h"
     46 #include "vm/vm_kern.h"
     47 
     48 struct kmembuckets bucket[MINBUCKET + 16];
     49 struct kmemstats kmemstats[M_LAST + 1];
     50 struct kmemusage *kmemusage;
     51 char *kmembase, *kmemlimit;
     52 char *memname[] = INITKMEMNAMES;
     53 
     54 /*
     55  * Allocate a block of memory
     56  */
     57 void *
     58 malloc(size, type, flags)
     59 	unsigned long size;
     60 	int type, flags;
     61 {
     62 	register struct kmembuckets *kbp;
     63 	register struct kmemusage *kup;
     64 	long indx, npg, allocsize;
     65 	int s;
     66 	caddr_t va, cp, savedlist;
     67 #ifdef KMEMSTATS
     68 	register struct kmemstats *ksp = &kmemstats[type];
     69 
     70 	if (((unsigned long)type) > M_LAST)
     71 		panic("malloc - bogus type");
     72 #endif
     73 
     74 	indx = BUCKETINDX(size);
     75 	kbp = &bucket[indx];
     76 	s = splimp();
     77 #ifdef KMEMSTATS
     78 	while (ksp->ks_memuse >= ksp->ks_limit) {
     79 		if (flags & M_NOWAIT) {
     80 			splx(s);
     81 			return ((void *) NULL);
     82 		}
     83 		if (ksp->ks_limblocks < 65535)
     84 			ksp->ks_limblocks++;
     85 		tsleep((caddr_t)ksp, PSWP+2, memname[type], 0);
     86 	}
     87 #endif
     88 	if (kbp->kb_next == NULL) {
     89 		if (size > MAXALLOCSAVE)
     90 			allocsize = roundup(size, CLBYTES);
     91 		else
     92 			allocsize = 1 << indx;
     93 		npg = clrnd(btoc(allocsize));
     94 		va = (caddr_t) kmem_malloc(kmem_map, (vm_size_t)ctob(npg),
     95 					   !(flags & M_NOWAIT));
     96 		if (va == NULL) {
     97 			splx(s);
     98 			return ((void *) NULL);
     99 		}
    100 #ifdef KMEMSTATS
    101 		kbp->kb_total += kbp->kb_elmpercl;
    102 #endif
    103 		kup = btokup(va);
    104 		kup->ku_indx = indx;
    105 		if (allocsize > MAXALLOCSAVE) {
    106 			if (npg > 65535)
    107 				panic("malloc: allocation too large");
    108 			kup->ku_pagecnt = npg;
    109 #ifdef KMEMSTATS
    110 			ksp->ks_memuse += allocsize;
    111 #endif
    112 			goto out;
    113 		}
    114 #ifdef KMEMSTATS
    115 		kup->ku_freecnt = kbp->kb_elmpercl;
    116 		kbp->kb_totalfree += kbp->kb_elmpercl;
    117 #endif
    118 		/*
    119 		 * Just in case we blocked while allocating memory,
    120 		 * and someone else also allocated memory for this
    121 		 * bucket, don't assume the list is still empty.
    122 		 */
    123 		savedlist = kbp->kb_next;
    124 		kbp->kb_next = va + (npg * NBPG) - allocsize;
    125 		for (cp = kbp->kb_next; cp > va; cp -= allocsize)
    126 			*(caddr_t *)cp = cp - allocsize;
    127 		*(caddr_t *)cp = savedlist;
    128 	}
    129 	va = kbp->kb_next;
    130 	kbp->kb_next = *(caddr_t *)va;
    131 #ifdef KMEMSTATS
    132 	kup = btokup(va);
    133 	if (kup->ku_indx != indx)
    134 		panic("malloc: wrong bucket");
    135 	if (kup->ku_freecnt == 0)
    136 		panic("malloc: lost data");
    137 	kup->ku_freecnt--;
    138 	kbp->kb_totalfree--;
    139 	ksp->ks_memuse += 1 << indx;
    140 out:
    141 	kbp->kb_calls++;
    142 	ksp->ks_inuse++;
    143 	ksp->ks_calls++;
    144 	if (ksp->ks_memuse > ksp->ks_maxused)
    145 		ksp->ks_maxused = ksp->ks_memuse;
    146 #else
    147 out:
    148 #endif
    149 	splx(s);
    150 	return ((void *) va);
    151 }
    152 
    153 #ifdef DIAGNOSTIC
    154 long addrmask[] = { 0x00000000,
    155 	0x00000001, 0x00000003, 0x00000007, 0x0000000f,
    156 	0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
    157 	0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
    158 	0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
    159 };
    160 #endif /* DIAGNOSTIC */
    161 
    162 /*
    163  * Free a block of memory allocated by malloc.
    164  */
    165 void
    166 free(addr, type)
    167 	void *addr;
    168 	int type;
    169 {
    170 	register struct kmembuckets *kbp;
    171 	register struct kmemusage *kup;
    172 #ifdef DIAGNOSTIC
    173 	long alloc;
    174 #endif
    175 	long size;
    176 	int s;
    177 #ifdef KMEMSTATS
    178 	register struct kmemstats *ksp = &kmemstats[type];
    179 #endif
    180 
    181 	kup = btokup(addr);
    182 	size = 1 << kup->ku_indx;
    183 #ifdef DIAGNOSTIC
    184 	if (size > NBPG * CLSIZE)
    185 		alloc = addrmask[BUCKETINDX(NBPG * CLSIZE)];
    186 	else
    187 		alloc = addrmask[kup->ku_indx];
    188 	if (((u_long)addr & alloc) != 0) {
    189 		printf("free: unaligned addr 0x%x, size %d, type %d, mask %d\n",
    190 			addr, size, type, alloc);
    191 		panic("free: unaligned addr");
    192 	}
    193 #endif /* DIAGNOSTIC */
    194 	kbp = &bucket[kup->ku_indx];
    195 	s = splimp();
    196 	if (size > MAXALLOCSAVE) {
    197 		kmem_free(kmem_map, (vm_offset_t)addr, ctob(kup->ku_pagecnt));
    198 #ifdef KMEMSTATS
    199 		size = kup->ku_pagecnt << PGSHIFT;
    200 		ksp->ks_memuse -= size;
    201 		kup->ku_indx = 0;
    202 		kup->ku_pagecnt = 0;
    203 		if (ksp->ks_memuse + size >= ksp->ks_limit &&
    204 		    ksp->ks_memuse < ksp->ks_limit)
    205 			wakeup((caddr_t)ksp);
    206 		ksp->ks_inuse--;
    207 		kbp->kb_total -= 1;
    208 #endif
    209 		splx(s);
    210 		return;
    211 	}
    212 #ifdef KMEMSTATS
    213 	kup->ku_freecnt++;
    214 	if (kup->ku_freecnt >= kbp->kb_elmpercl)
    215 		if (kup->ku_freecnt > kbp->kb_elmpercl)
    216 			panic("free: multiple frees");
    217 		else if (kbp->kb_totalfree > kbp->kb_highwat)
    218 			kbp->kb_couldfree++;
    219 	kbp->kb_totalfree++;
    220 	ksp->ks_memuse -= size;
    221 	if (ksp->ks_memuse + size >= ksp->ks_limit &&
    222 	    ksp->ks_memuse < ksp->ks_limit)
    223 		wakeup((caddr_t)ksp);
    224 	ksp->ks_inuse--;
    225 #endif
    226 	*(caddr_t *)addr = kbp->kb_next;
    227 	kbp->kb_next = addr;
    228 	splx(s);
    229 }
    230 
    231 /*
    232  * Initialize the kernel memory allocator
    233  */
    234 void
    235 kmeminit()
    236 {
    237 	register long indx;
    238 	int npg;
    239 
    240 #if	((MAXALLOCSAVE & (MAXALLOCSAVE - 1)) != 0)
    241 		ERROR!_kmeminit:_MAXALLOCSAVE_not_power_of_2
    242 #endif
    243 #if	(MAXALLOCSAVE > MINALLOCSIZE * 32768)
    244 		ERROR!_kmeminit:_MAXALLOCSAVE_too_big
    245 #endif
    246 #if	(MAXALLOCSAVE < CLBYTES)
    247 		ERROR!_kmeminit:_MAXALLOCSAVE_too_small
    248 #endif
    249 	npg = VM_KMEM_SIZE/ NBPG;
    250 	kmemusage = (struct kmemusage *) kmem_alloc(kernel_map,
    251 		(vm_size_t)(npg * sizeof(struct kmemusage)));
    252 	kmem_map = kmem_suballoc(kernel_map, (vm_offset_t *)&kmembase,
    253 		(vm_offset_t *)&kmemlimit, (vm_size_t)(npg * NBPG), FALSE);
    254 #ifdef KMEMSTATS
    255 	for (indx = 0; indx < MINBUCKET + 16; indx++) {
    256 		if (1 << indx >= CLBYTES)
    257 			bucket[indx].kb_elmpercl = 1;
    258 		else
    259 			bucket[indx].kb_elmpercl = CLBYTES / (1 << indx);
    260 		bucket[indx].kb_highwat = 5 * bucket[indx].kb_elmpercl;
    261 	}
    262 	for (indx = 0; indx <= M_LAST; indx++)
    263 		kmemstats[indx].ks_limit = npg * NBPG * 6 / 10;
    264 #endif
    265 }
    266