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