This commit was generated by cvs2svn to track changes on a CVS vendor
[unix-history] / sys / kern / kern_malloc.c
CommitLineData
15637ed4
RG
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 *
600f7f07 33 * from: @(#)kern_malloc.c 7.25 (Berkeley) 5/8/91
1d7f5b47 34 * $Id: kern_malloc.c,v 1.5 1993/11/25 01:33:02 wollman Exp $
15637ed4
RG
35 */
36
37#include "param.h"
4c45483e 38#include "systm.h"
15637ed4
RG
39#include "proc.h"
40#include "kernel.h"
41#include "malloc.h"
42#include "vm/vm.h"
43#include "vm/vm_kern.h"
44
45struct kmembuckets bucket[MINBUCKET + 16];
46struct kmemstats kmemstats[M_LAST];
47struct kmemusage *kmemusage;
48char *kmembase, *kmemlimit;
49char *memname[] = INITKMEMNAMES;
50
51/*
52 * Allocate a block of memory
53 */
54void *
55malloc(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, alloc, allocsize;
62 int s;
63 caddr_t va, cp, savedlist;
64#ifdef KMEMSTATS
65 register struct kmemstats *ksp = &kmemstats[type];
66
c138d68d 67 if (((unsigned long)type) >= M_LAST)
15637ed4
RG
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;
137out:
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
144out:
145#endif
146 splx(s);
147 return ((void *) va);
148}
149
150#ifdef DIAGNOSTIC
151long 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 */
162void
163free(addr, type)
164 void *addr;
165 int type;
166{
167 register struct kmembuckets *kbp;
168 register struct kmemusage *kup;
169 long alloc, size;
170 int s;
171#ifdef KMEMSTATS
172 register struct kmemstats *ksp = &kmemstats[type];
173#endif
174
175 kup = btokup(addr);
176 size = 1 << kup->ku_indx;
177#ifdef DIAGNOSTIC
178 if (size > NBPG * CLSIZE)
179 alloc = addrmask[BUCKETINDX(NBPG * CLSIZE)];
180 else
181 alloc = addrmask[kup->ku_indx];
182 if (((u_long)addr & alloc) != 0) {
183 printf("free: unaligned addr 0x%x, size %d, type %d, mask %d\n",
184 addr, size, type, alloc);
185 panic("free: unaligned addr");
186 }
187#endif /* DIAGNOSTIC */
188 kbp = &bucket[kup->ku_indx];
189 s = splimp();
190 if (size > MAXALLOCSAVE) {
191 kmem_free(kmem_map, (vm_offset_t)addr, ctob(kup->ku_pagecnt));
192#ifdef KMEMSTATS
193 size = kup->ku_pagecnt << PGSHIFT;
194 ksp->ks_memuse -= size;
195 kup->ku_indx = 0;
196 kup->ku_pagecnt = 0;
197 if (ksp->ks_memuse + size >= ksp->ks_limit &&
198 ksp->ks_memuse < ksp->ks_limit)
199 wakeup((caddr_t)ksp);
200 ksp->ks_inuse--;
201 kbp->kb_total -= 1;
202#endif
203 splx(s);
204 return;
205 }
206#ifdef KMEMSTATS
207 kup->ku_freecnt++;
208 if (kup->ku_freecnt >= kbp->kb_elmpercl)
209 if (kup->ku_freecnt > kbp->kb_elmpercl)
210 panic("free: multiple frees");
211 else if (kbp->kb_totalfree > kbp->kb_highwat)
212 kbp->kb_couldfree++;
213 kbp->kb_totalfree++;
214 ksp->ks_memuse -= size;
215 if (ksp->ks_memuse + size >= ksp->ks_limit &&
216 ksp->ks_memuse < ksp->ks_limit)
217 wakeup((caddr_t)ksp);
218 ksp->ks_inuse--;
219#endif
220 *(caddr_t *)addr = kbp->kb_next;
221 kbp->kb_next = addr;
222 splx(s);
223}
224
225/*
226 * Initialize the kernel memory allocator
227 */
4c45483e 228void
15637ed4
RG
229kmeminit()
230{
231 register long indx;
232 int npg;
233
15637ed4 234#if (MAXALLOCSAVE > MINALLOCSIZE * 32768)
4c45483e 235# error "kmeminit: MAXALLOCSAVE too big"
15637ed4 236#endif
d8bca54c 237#if (MAXALLOCSAVE < CLBYTES-1)
4c45483e 238# error "kmeminit: MAXALLOCSAVE too small"
15637ed4 239#endif
1d7f5b47 240 npg = (VM_KMEM_SIZE + VM_MBUF_SIZE) / NBPG;
15637ed4
RG
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}