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