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