xref: /netbsd-src/sys/kern/kern_malloc.c (revision 05a9db8e4f5a54955cacec0e01c45d54b958765c)
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.7 1993/12/18 04:20:47 mycroft Exp $
35  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/proc.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 
43 #include <vm/vm.h>
44 #include <vm/vm_kern.h>
45 
46 struct kmembuckets bucket[MINBUCKET + 16];
47 struct kmemstats kmemstats[M_LAST + 1];
48 struct kmemusage *kmemusage;
49 char *kmembase, *kmemlimit;
50 char *memname[] = INITKMEMNAMES;
51 
52 /*
53  * Allocate a block of memory
54  */
55 void *
56 malloc(size, type, flags)
57 	unsigned long size;
58 	int type, flags;
59 {
60 	register struct kmembuckets *kbp;
61 	register struct kmemusage *kup;
62 	long indx, npg, allocsize;
63 	int s;
64 	caddr_t va, cp, savedlist;
65 #ifdef KMEMSTATS
66 	register struct kmemstats *ksp = &kmemstats[type];
67 
68 	if (((unsigned long)type) > M_LAST)
69 		panic("malloc - bogus type");
70 #endif
71 
72 	indx = BUCKETINDX(size);
73 	kbp = &bucket[indx];
74 	s = splimp();
75 #ifdef KMEMSTATS
76 	while (ksp->ks_memuse >= ksp->ks_limit) {
77 		if (flags & M_NOWAIT) {
78 			splx(s);
79 			return ((void *) NULL);
80 		}
81 		if (ksp->ks_limblocks < 65535)
82 			ksp->ks_limblocks++;
83 		tsleep((caddr_t)ksp, PSWP+2, memname[type], 0);
84 	}
85 #endif
86 	if (kbp->kb_next == NULL) {
87 		if (size > MAXALLOCSAVE)
88 			allocsize = roundup(size, CLBYTES);
89 		else
90 			allocsize = 1 << indx;
91 		npg = clrnd(btoc(allocsize));
92 		va = (caddr_t) kmem_malloc(kmem_map, (vm_size_t)ctob(npg),
93 					   !(flags & M_NOWAIT));
94 		if (va == NULL) {
95 			splx(s);
96 			return ((void *) NULL);
97 		}
98 #ifdef KMEMSTATS
99 		kbp->kb_total += kbp->kb_elmpercl;
100 #endif
101 		kup = btokup(va);
102 		kup->ku_indx = indx;
103 		if (allocsize > MAXALLOCSAVE) {
104 			if (npg > 65535)
105 				panic("malloc: allocation too large");
106 			kup->ku_pagecnt = npg;
107 #ifdef KMEMSTATS
108 			ksp->ks_memuse += allocsize;
109 #endif
110 			goto out;
111 		}
112 #ifdef KMEMSTATS
113 		kup->ku_freecnt = kbp->kb_elmpercl;
114 		kbp->kb_totalfree += kbp->kb_elmpercl;
115 #endif
116 		/*
117 		 * Just in case we blocked while allocating memory,
118 		 * and someone else also allocated memory for this
119 		 * bucket, don't assume the list is still empty.
120 		 */
121 		savedlist = kbp->kb_next;
122 		kbp->kb_next = va + (npg * NBPG) - allocsize;
123 		for (cp = kbp->kb_next; cp > va; cp -= allocsize)
124 			*(caddr_t *)cp = cp - allocsize;
125 		*(caddr_t *)cp = savedlist;
126 	}
127 	va = kbp->kb_next;
128 	kbp->kb_next = *(caddr_t *)va;
129 #ifdef KMEMSTATS
130 	kup = btokup(va);
131 	if (kup->ku_indx != indx)
132 		panic("malloc: wrong bucket");
133 	if (kup->ku_freecnt == 0)
134 		panic("malloc: lost data");
135 	kup->ku_freecnt--;
136 	kbp->kb_totalfree--;
137 	ksp->ks_memuse += 1 << indx;
138 out:
139 	kbp->kb_calls++;
140 	ksp->ks_inuse++;
141 	ksp->ks_calls++;
142 	if (ksp->ks_memuse > ksp->ks_maxused)
143 		ksp->ks_maxused = ksp->ks_memuse;
144 #else
145 out:
146 #endif
147 	splx(s);
148 	return ((void *) va);
149 }
150 
151 #ifdef DIAGNOSTIC
152 long addrmask[] = { 0x00000000,
153 	0x00000001, 0x00000003, 0x00000007, 0x0000000f,
154 	0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
155 	0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
156 	0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
157 };
158 #endif /* DIAGNOSTIC */
159 
160 /*
161  * Free a block of memory allocated by malloc.
162  */
163 void
164 free(addr, type)
165 	void *addr;
166 	int type;
167 {
168 	register struct kmembuckets *kbp;
169 	register struct kmemusage *kup;
170 #ifdef DIAGNOSTIC
171 	long alloc;
172 #endif
173 	long size;
174 	int s;
175 #ifdef KMEMSTATS
176 	register struct kmemstats *ksp = &kmemstats[type];
177 #endif
178 
179 	kup = btokup(addr);
180 	size = 1 << kup->ku_indx;
181 #ifdef DIAGNOSTIC
182 	if (size > NBPG * CLSIZE)
183 		alloc = addrmask[BUCKETINDX(NBPG * CLSIZE)];
184 	else
185 		alloc = addrmask[kup->ku_indx];
186 	if (((u_long)addr & alloc) != 0) {
187 		printf("free: unaligned addr 0x%x, size %d, type %d, mask %d\n",
188 			addr, size, type, alloc);
189 		panic("free: unaligned addr");
190 	}
191 #endif /* DIAGNOSTIC */
192 	kbp = &bucket[kup->ku_indx];
193 	s = splimp();
194 	if (size > MAXALLOCSAVE) {
195 		kmem_free(kmem_map, (vm_offset_t)addr, ctob(kup->ku_pagecnt));
196 #ifdef KMEMSTATS
197 		size = kup->ku_pagecnt << PGSHIFT;
198 		ksp->ks_memuse -= size;
199 		kup->ku_indx = 0;
200 		kup->ku_pagecnt = 0;
201 		if (ksp->ks_memuse + size >= ksp->ks_limit &&
202 		    ksp->ks_memuse < ksp->ks_limit)
203 			wakeup((caddr_t)ksp);
204 		ksp->ks_inuse--;
205 		kbp->kb_total -= 1;
206 #endif
207 		splx(s);
208 		return;
209 	}
210 #ifdef KMEMSTATS
211 	kup->ku_freecnt++;
212 	if (kup->ku_freecnt >= kbp->kb_elmpercl)
213 		if (kup->ku_freecnt > kbp->kb_elmpercl)
214 			panic("free: multiple frees");
215 		else if (kbp->kb_totalfree > kbp->kb_highwat)
216 			kbp->kb_couldfree++;
217 	kbp->kb_totalfree++;
218 	ksp->ks_memuse -= size;
219 	if (ksp->ks_memuse + size >= ksp->ks_limit &&
220 	    ksp->ks_memuse < ksp->ks_limit)
221 		wakeup((caddr_t)ksp);
222 	ksp->ks_inuse--;
223 #endif
224 	*(caddr_t *)addr = kbp->kb_next;
225 	kbp->kb_next = addr;
226 	splx(s);
227 }
228 
229 /*
230  * Initialize the kernel memory allocator
231  */
232 void
233 kmeminit()
234 {
235 	register long indx;
236 	int npg;
237 
238 #if	((MAXALLOCSAVE & (MAXALLOCSAVE - 1)) != 0)
239 		ERROR!_kmeminit:_MAXALLOCSAVE_not_power_of_2
240 #endif
241 #if	(MAXALLOCSAVE > MINALLOCSIZE * 32768)
242 		ERROR!_kmeminit:_MAXALLOCSAVE_too_big
243 #endif
244 #if	(MAXALLOCSAVE < CLBYTES)
245 		ERROR!_kmeminit:_MAXALLOCSAVE_too_small
246 #endif
247 	npg = VM_KMEM_SIZE/ NBPG;
248 	kmemusage = (struct kmemusage *) kmem_alloc(kernel_map,
249 		(vm_size_t)(npg * sizeof(struct kmemusage)));
250 	kmem_map = kmem_suballoc(kernel_map, (vm_offset_t *)&kmembase,
251 		(vm_offset_t *)&kmemlimit, (vm_size_t)(npg * NBPG), FALSE);
252 #ifdef KMEMSTATS
253 	for (indx = 0; indx < MINBUCKET + 16; indx++) {
254 		if (1 << indx >= CLBYTES)
255 			bucket[indx].kb_elmpercl = 1;
256 		else
257 			bucket[indx].kb_elmpercl = CLBYTES / (1 << indx);
258 		bucket[indx].kb_highwat = 5 * bucket[indx].kb_elmpercl;
259 	}
260 	for (indx = 0; indx <= M_LAST; indx++)
261 		kmemstats[indx].ks_limit = npg * NBPG * 6 / 10;
262 #endif
263 }
264