Commit | Line | Data |
---|---|---|
d4202556 KM |
1 | /* |
2 | * Copyright (c) 1987 Regents of the University of California. | |
2420c94a | 3 | * All rights reserved. |
d4202556 | 4 | * |
2420c94a | 5 | * Redistribution and use in source and binary forms are permitted |
616d42db KB |
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. | |
2420c94a | 16 | * |
616d42db | 17 | * @(#)kern_malloc.c 7.10 (Berkeley) %G% |
d4202556 KM |
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; | |
fd78e9f6 | 34 | long wantkmemmap; |
d4202556 KM |
35 | |
36 | /* | |
37 | * Allocate a block of memory | |
38 | */ | |
738ba0d6 KM |
39 | qaddr_t |
40 | malloc(size, type, flags) | |
d4202556 | 41 | unsigned long size; |
47516941 | 42 | int type, flags; |
d4202556 KM |
43 | { |
44 | register struct kmembuckets *kbp; | |
45 | register struct kmemusage *kup; | |
47516941 MK |
46 | long indx, npg, alloc, allocsize; |
47 | int s; | |
d4202556 KM |
48 | caddr_t va, cp; |
49 | #ifdef KMEMSTATS | |
fd78e9f6 | 50 | register struct kmemstats *ksp = &kmemstats[type]; |
d4202556 | 51 | #endif |
fd78e9f6 | 52 | |
d4202556 KM |
53 | indx = BUCKETINDX(size); |
54 | kbp = &bucket[indx]; | |
55 | s = splimp(); | |
fd78e9f6 KM |
56 | again: |
57 | #ifdef KMEMSTATS | |
0a4aff4d | 58 | while (ksp->ks_memuse >= ksp->ks_limit) { |
fd78e9f6 KM |
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 | |
d4202556 KM |
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) { | |
fd78e9f6 KM |
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; | |
d4202556 | 91 | } |
7656fce5 | 92 | alloc -= CLSIZE; /* convert to base 0 */ |
47516941 | 93 | (void) vmemall(&kmempt[alloc], (int)npg, &proc[0], CSYS); |
d4202556 | 94 | va = (caddr_t) kmemxtob(alloc); |
47516941 | 95 | vmaccess(&kmempt[alloc], va, (int)npg); |
d4202556 KM |
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; | |
fd78e9f6 KM |
105 | #ifdef KMEMSTATS |
106 | ksp->ks_memuse += allocsize; | |
107 | #endif | |
d4202556 KM |
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; | |
fd78e9f6 | 115 | for (cp = kbp->kb_next; cp > va; cp -= allocsize) |
d4202556 KM |
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--; | |
fd78e9f6 | 129 | ksp->ks_memuse += 1 << indx; |
d4202556 KM |
130 | out: |
131 | kbp->kb_calls++; | |
132 | ksp->ks_inuse++; | |
133 | ksp->ks_calls++; | |
0a4aff4d KM |
134 | if (ksp->ks_memuse > ksp->ks_maxused) |
135 | ksp->ks_maxused = ksp->ks_memuse; | |
d4202556 KM |
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 | */ | |
738ba0d6 KM |
146 | void |
147 | free(addr, type) | |
d4202556 | 148 | caddr_t addr; |
47516941 | 149 | int type; |
d4202556 KM |
150 | { |
151 | register struct kmembuckets *kbp; | |
152 | register struct kmemusage *kup; | |
47516941 MK |
153 | long alloc, size; |
154 | int s; | |
fd78e9f6 KM |
155 | #ifdef KMEMSTATS |
156 | register struct kmemstats *ksp = &kmemstats[type]; | |
157 | #endif | |
d4202556 KM |
158 | |
159 | kup = btokup(addr); | |
738ba0d6 | 160 | kbp = &bucket[kup->ku_indx]; |
d4202556 | 161 | s = splimp(); |
0a4aff4d KM |
162 | size = 1 << kup->ku_indx; |
163 | if (size > MAXALLOCSAVE) { | |
d4202556 | 164 | alloc = btokmemx(addr); |
47516941 | 165 | (void) memfree(&kmempt[alloc], (int)kup->ku_pagecnt, 0); |
fd78e9f6 KM |
166 | rmfree(kmemmap, (long)kup->ku_pagecnt, alloc + CLSIZE); |
167 | if (wantkmemmap) { | |
168 | wakeup((caddr_t)&wantkmemmap); | |
169 | wantkmemmap = 0; | |
170 | } | |
d4202556 | 171 | #ifdef KMEMSTATS |
0a4aff4d KM |
172 | size = kup->ku_pagecnt << PGSHIFT; |
173 | ksp->ks_memuse -= size; | |
d4202556 KM |
174 | kup->ku_indx = 0; |
175 | kup->ku_pagecnt = 0; | |
0a4aff4d KM |
176 | if (ksp->ks_memuse + size >= ksp->ks_limit && |
177 | ksp->ks_memuse < ksp->ks_limit) | |
fd78e9f6 KM |
178 | wakeup((caddr_t)ksp); |
179 | ksp->ks_inuse--; | |
738ba0d6 | 180 | kbp->kb_total -= 1; |
d4202556 KM |
181 | #endif |
182 | splx(s); | |
183 | return; | |
184 | } | |
d4202556 KM |
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++; | |
0a4aff4d KM |
193 | ksp->ks_memuse -= size; |
194 | if (ksp->ks_memuse + size >= ksp->ks_limit && | |
195 | ksp->ks_memuse < ksp->ks_limit) | |
fd78e9f6 KM |
196 | wakeup((caddr_t)ksp); |
197 | ksp->ks_inuse--; | |
d4202556 KM |
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; | |
738ba0d6 | 210 | int npg; |
d4202556 | 211 | |
47516941 MK |
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 | |
738ba0d6 | 221 | npg = ekmempt - kmempt; |
47516941 | 222 | rminit(kmemmap, (long)npg, (long)CLSIZE, "malloc map", npg); |
d4202556 KM |
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++) | |
738ba0d6 | 232 | kmemstats[indx].ks_limit = npg * CLBYTES * 8 / 10; |
d4202556 KM |
233 | #endif |
234 | } |