xref: /original-bsd/sys/kern/kern_malloc.c (revision f1324ba5)
1 /*
2  * Copyright (c) 1987 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  *	@(#)kern_malloc.c	7.10 (Berkeley) 06/29/88
18  */
19 
20 #include "param.h"
21 #include "vm.h"
22 #include "cmap.h"
23 #include "time.h"
24 #include "proc.h"
25 #include "map.h"
26 #include "kernel.h"
27 #include "malloc.h"
28 
29 #include "../machine/pte.h"
30 
31 struct kmembuckets bucket[MINBUCKET + 16];
32 struct kmemstats kmemstats[M_LAST];
33 struct kmemusage *kmemusage;
34 long wantkmemmap;
35 
36 /*
37  * Allocate a block of memory
38  */
39 qaddr_t
40 malloc(size, type, flags)
41 	unsigned long size;
42 	int type, flags;
43 {
44 	register struct kmembuckets *kbp;
45 	register struct kmemusage *kup;
46 	long indx, npg, alloc, allocsize;
47 	int s;
48 	caddr_t va, cp;
49 #ifdef KMEMSTATS
50 	register struct kmemstats *ksp = &kmemstats[type];
51 #endif
52 
53 	indx = BUCKETINDX(size);
54 	kbp = &bucket[indx];
55 	s = splimp();
56 again:
57 #ifdef KMEMSTATS
58 	while (ksp->ks_memuse >= ksp->ks_limit) {
59 		if (flags & M_NOWAIT) {
60 			splx(s);
61 			return (0);
62 		}
63 		if (ksp->ks_limblocks < 65535)
64 			ksp->ks_limblocks++;
65 		sleep((caddr_t)ksp, PSWP+2);
66 	}
67 #endif
68 	if (kbp->kb_next == NULL) {
69 		if (size > MAXALLOCSAVE)
70 			allocsize = roundup(size, CLBYTES);
71 		else
72 			allocsize = 1 << indx;
73 		npg = clrnd(btoc(allocsize));
74 		if ((flags & M_NOWAIT) && freemem < npg) {
75 			splx(s);
76 			return (0);
77 		}
78 		alloc = rmalloc(kmemmap, npg);
79 		if (alloc == 0) {
80 			if (flags & M_NOWAIT) {
81 				splx(s);
82 				return (0);
83 			}
84 #ifdef KMEMSTATS
85 			if (ksp->ks_mapblocks < 65535)
86 				ksp->ks_mapblocks++;
87 #endif
88 			wantkmemmap++;
89 			sleep((caddr_t)&wantkmemmap, PSWP+2);
90 			goto again;
91 		}
92 		alloc -= CLSIZE;		/* convert to base 0 */
93 		(void) vmemall(&kmempt[alloc], (int)npg, &proc[0], CSYS);
94 		va = (caddr_t) kmemxtob(alloc);
95 		vmaccess(&kmempt[alloc], va, (int)npg);
96 #ifdef KMEMSTATS
97 		kbp->kb_total += kbp->kb_elmpercl;
98 #endif
99 		kup = btokup(va);
100 		kup->ku_indx = indx;
101 		if (allocsize > MAXALLOCSAVE) {
102 			if (npg > 65535)
103 				panic("malloc: allocation too large");
104 			kup->ku_pagecnt = npg;
105 #ifdef KMEMSTATS
106 			ksp->ks_memuse += allocsize;
107 #endif
108 			goto out;
109 		}
110 #ifdef KMEMSTATS
111 		kup->ku_freecnt = kbp->kb_elmpercl;
112 		kbp->kb_totalfree += kbp->kb_elmpercl;
113 #endif
114 		kbp->kb_next = va + (npg * NBPG) - allocsize;
115 		for (cp = kbp->kb_next; cp > va; cp -= allocsize)
116 			*(caddr_t *)cp = cp - allocsize;
117 		*(caddr_t *)cp = NULL;
118 	}
119 	va = kbp->kb_next;
120 	kbp->kb_next = *(caddr_t *)va;
121 #ifdef KMEMSTATS
122 	kup = btokup(va);
123 	if (kup->ku_indx != indx)
124 		panic("malloc: wrong bucket");
125 	if (kup->ku_freecnt == 0)
126 		panic("malloc: lost data");
127 	kup->ku_freecnt--;
128 	kbp->kb_totalfree--;
129 	ksp->ks_memuse += 1 << indx;
130 out:
131 	kbp->kb_calls++;
132 	ksp->ks_inuse++;
133 	ksp->ks_calls++;
134 	if (ksp->ks_memuse > ksp->ks_maxused)
135 		ksp->ks_maxused = ksp->ks_memuse;
136 #else
137 out:
138 #endif
139 	splx(s);
140 	return ((qaddr_t)va);
141 }
142 
143 /*
144  * Free a block of memory allocated by malloc.
145  */
146 void
147 free(addr, type)
148 	caddr_t addr;
149 	int type;
150 {
151 	register struct kmembuckets *kbp;
152 	register struct kmemusage *kup;
153 	long alloc, size;
154 	int s;
155 #ifdef KMEMSTATS
156 	register struct kmemstats *ksp = &kmemstats[type];
157 #endif
158 
159 	kup = btokup(addr);
160 	kbp = &bucket[kup->ku_indx];
161 	s = splimp();
162 	size = 1 << kup->ku_indx;
163 	if (size > MAXALLOCSAVE) {
164 		alloc = btokmemx(addr);
165 		(void) memfree(&kmempt[alloc], (int)kup->ku_pagecnt, 0);
166 		rmfree(kmemmap, (long)kup->ku_pagecnt, alloc + CLSIZE);
167 		if (wantkmemmap) {
168 			wakeup((caddr_t)&wantkmemmap);
169 			wantkmemmap = 0;
170 		}
171 #ifdef KMEMSTATS
172 		size = kup->ku_pagecnt << PGSHIFT;
173 		ksp->ks_memuse -= size;
174 		kup->ku_indx = 0;
175 		kup->ku_pagecnt = 0;
176 		if (ksp->ks_memuse + size >= ksp->ks_limit &&
177 		    ksp->ks_memuse < ksp->ks_limit)
178 			wakeup((caddr_t)ksp);
179 		ksp->ks_inuse--;
180 		kbp->kb_total -= 1;
181 #endif
182 		splx(s);
183 		return;
184 	}
185 #ifdef KMEMSTATS
186 	kup->ku_freecnt++;
187 	if (kup->ku_freecnt >= kbp->kb_elmpercl)
188 		if (kup->ku_freecnt > kbp->kb_elmpercl)
189 			panic("free: multiple frees");
190 		else if (kbp->kb_totalfree > kbp->kb_highwat)
191 			kbp->kb_couldfree++;
192 	kbp->kb_totalfree++;
193 	ksp->ks_memuse -= size;
194 	if (ksp->ks_memuse + size >= ksp->ks_limit &&
195 	    ksp->ks_memuse < ksp->ks_limit)
196 		wakeup((caddr_t)ksp);
197 	ksp->ks_inuse--;
198 #endif
199 	*(caddr_t *)addr = kbp->kb_next;
200 	kbp->kb_next = addr;
201 	splx(s);
202 }
203 
204 /*
205  * Initialize the kernel memory allocator
206  */
207 kmeminit()
208 {
209 	register long indx;
210 	int npg;
211 
212 #if	((MAXALLOCSAVE & (MAXALLOCSAVE - 1)) != 0)
213 		ERROR!_kmeminit:_MAXALLOCSAVE_not_power_of_2
214 #endif
215 #if	(MAXALLOCSAVE > MINALLOCSIZE * 32768)
216 		ERROR!_kmeminit:_MAXALLOCSAVE_too_big
217 #endif
218 #if	(MAXALLOCSAVE < CLBYTES)
219 		ERROR!_kmeminit:_MAXALLOCSAVE_too_small
220 #endif
221 	npg = ekmempt - kmempt;
222 	rminit(kmemmap, (long)npg, (long)CLSIZE, "malloc map", npg);
223 #ifdef KMEMSTATS
224 	for (indx = 0; indx < MINBUCKET + 16; indx++) {
225 		if (1 << indx >= CLBYTES)
226 			bucket[indx].kb_elmpercl = 1;
227 		else
228 			bucket[indx].kb_elmpercl = CLBYTES / (1 << indx);
229 		bucket[indx].kb_highwat = 5 * bucket[indx].kb_elmpercl;
230 	}
231 	for (indx = 0; indx < M_LAST; indx++)
232 		kmemstats[indx].ks_limit = npg * CLBYTES * 8 / 10;
233 #endif
234 }
235