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