xref: /csrg-svn/sys/kern/kern_malloc.c (revision 50744)
1 /*
2  * Copyright (c) 1987, 1991 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)kern_malloc.c	7.26 (Berkeley) 08/01/91
8  */
9 
10 #include "param.h"
11 #include "proc.h"
12 #include "map.h"
13 #include "kernel.h"
14 #include "malloc.h"
15 #include "vm/vm.h"
16 #include "vm/vm_kern.h"
17 
18 struct kmembuckets bucket[MINBUCKET + 16];
19 struct kmemstats kmemstats[M_LAST];
20 struct kmemusage *kmemusage;
21 char *kmembase, *kmemlimit;
22 char *memname[] = INITKMEMNAMES;
23 
24 #ifdef DIAGNOSTIC
25 /*
26  * This structure serves two purposes.
27  * The first is to provide a set of masks to catch unaligned frees.
28  * The second is to provide known text to copy into free objects so
29  * that modifications after frees can be detected.
30  */
31 #define WEIRD_ADDR 0xdeadbeef
32 long addrmask[] = { WEIRD_ADDR,
33 	0x00000001, 0x00000003, 0x00000007, 0x0000000f,
34 	0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
35 	0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
36 	0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
37 };
38 #endif /* DIAGNOSTIC */
39 
40 /*
41  * Allocate a block of memory
42  */
43 void *
44 malloc(size, type, flags)
45 	unsigned long size;
46 	int type, flags;
47 {
48 	register struct kmembuckets *kbp;
49 	register struct kmemusage *kup;
50 	long indx, npg, alloc, allocsize;
51 	int s;
52 	caddr_t va, cp, savedlist;
53 #ifdef DIAGNOSTIC
54 	int i, copysize;
55 #endif
56 #ifdef KMEMSTATS
57 	register struct kmemstats *ksp = &kmemstats[type];
58 
59 	if (((unsigned long)type) > M_LAST)
60 		panic("malloc - bogus type");
61 #endif
62 
63 	indx = BUCKETINDX(size);
64 	kbp = &bucket[indx];
65 	s = splimp();
66 #ifdef KMEMSTATS
67 	while (ksp->ks_memuse >= ksp->ks_limit) {
68 		if (flags & M_NOWAIT) {
69 			splx(s);
70 			return ((void *) NULL);
71 		}
72 		if (ksp->ks_limblocks < 65535)
73 			ksp->ks_limblocks++;
74 		tsleep((caddr_t)ksp, PSWP+2, memname[type], 0);
75 	}
76 #endif
77 #ifdef DIAGNOSTIC
78 	copysize = 1 << indx < sizeof addrmask ? 1 << indx : sizeof addrmask;
79 #endif
80 	if (kbp->kb_next == NULL) {
81 		if (size > MAXALLOCSAVE)
82 			allocsize = roundup(size, CLBYTES);
83 		else
84 			allocsize = 1 << indx;
85 		npg = clrnd(btoc(allocsize));
86 		va = (caddr_t) kmem_malloc(kmem_map, (vm_size_t)ctob(npg),
87 					   !(flags & M_NOWAIT));
88 		if (va == NULL) {
89 			splx(s);
90 			return ((void *) NULL);
91 		}
92 #ifdef KMEMSTATS
93 		kbp->kb_total += kbp->kb_elmpercl;
94 #endif
95 		kup = btokup(va);
96 		kup->ku_indx = indx;
97 		if (allocsize > MAXALLOCSAVE) {
98 			if (npg > 65535)
99 				panic("malloc: allocation too large");
100 			kup->ku_pagecnt = npg;
101 #ifdef KMEMSTATS
102 			ksp->ks_memuse += allocsize;
103 #endif
104 			goto out;
105 		}
106 #ifdef KMEMSTATS
107 		kup->ku_freecnt = kbp->kb_elmpercl;
108 		kbp->kb_totalfree += kbp->kb_elmpercl;
109 #endif
110 		/*
111 		 * Just in case we blocked while allocating memory,
112 		 * and someone else also allocated memory for this
113 		 * bucket, don't assume the list is still empty.
114 		 */
115 		savedlist = kbp->kb_next;
116 		kbp->kb_next = va + (npg * NBPG) - allocsize;
117 		for (cp = kbp->kb_next; ; cp -= allocsize) {
118 #ifdef DIAGNOSTIC
119 			/*
120 			 * Copy in known text to detect modification
121 			 * after freeing.
122 			 */
123 			bcopy(addrmask, cp, copysize);
124 #endif /* DIAGNOSTIC */
125 			if (cp <= va)
126 				break;
127 			*(caddr_t *)cp = cp - allocsize;
128 		}
129 		*(caddr_t *)cp = savedlist;
130 	}
131 	va = kbp->kb_next;
132 	kbp->kb_next = *(caddr_t *)va;
133 #ifdef DIAGNOSTIC
134 	*(caddr_t *)va = (char *)WEIRD_ADDR;
135 	if (bcmp(addrmask, va, copysize)) {
136 		copysize >>= 2;
137 		for (i = 0; i < copysize && addrmask[i] == ((int *)va)[i]; i++)
138 			/* void */;
139 		printf("Word %d %s 0x%x of size %d modified (0x%x != 0x%x)\n",
140 		    i, "of object", va, size, ((int *)va)[i], addrmask[i]);
141 		panic("malloc: data modified on freelist");
142 	}
143 #endif /* DIAGNOSTIC */
144 #ifdef KMEMSTATS
145 	kup = btokup(va);
146 	if (kup->ku_indx != indx)
147 		panic("malloc: wrong bucket");
148 	if (kup->ku_freecnt == 0)
149 		panic("malloc: lost data");
150 	kup->ku_freecnt--;
151 	kbp->kb_totalfree--;
152 	ksp->ks_memuse += 1 << indx;
153 out:
154 	kbp->kb_calls++;
155 	ksp->ks_inuse++;
156 	ksp->ks_calls++;
157 	if (ksp->ks_memuse > ksp->ks_maxused)
158 		ksp->ks_maxused = ksp->ks_memuse;
159 #else
160 out:
161 #endif
162 	splx(s);
163 	return ((void *) va);
164 }
165 
166 /*
167  * Free a block of memory allocated by malloc.
168  */
169 void
170 free(addr, type)
171 	void *addr;
172 	int type;
173 {
174 	register struct kmembuckets *kbp;
175 	register struct kmemusage *kup;
176 	long size;
177 	int s;
178 #ifdef DIAGNOSTIC
179 	caddr_t cp;
180 	long alloc, copysize;
181 #endif
182 #ifdef KMEMSTATS
183 	register struct kmemstats *ksp = &kmemstats[type];
184 #endif
185 
186 	kup = btokup(addr);
187 	size = 1 << kup->ku_indx;
188 	kbp = &bucket[kup->ku_indx];
189 	s = splimp();
190 #ifdef DIAGNOSTIC
191 	/*
192 	 * Check for returns of data that do not point to the
193 	 * beginning of the allocation.
194 	 */
195 	if (size > NBPG * CLSIZE)
196 		alloc = addrmask[BUCKETINDX(NBPG * CLSIZE)];
197 	else
198 		alloc = addrmask[kup->ku_indx];
199 	if (((u_long)addr & alloc) != 0) {
200 		printf("free: unaligned addr 0x%x, size %d, type %d, mask %d\n",
201 			addr, size, type, alloc);
202 		panic("free: unaligned addr");
203 	}
204 #endif /* DIAGNOSTIC */
205 	if (size > MAXALLOCSAVE) {
206 		kmem_free(kmem_map, (vm_offset_t)addr, ctob(kup->ku_pagecnt));
207 #ifdef KMEMSTATS
208 		size = kup->ku_pagecnt << PGSHIFT;
209 		ksp->ks_memuse -= size;
210 		kup->ku_indx = 0;
211 		kup->ku_pagecnt = 0;
212 		if (ksp->ks_memuse + size >= ksp->ks_limit &&
213 		    ksp->ks_memuse < ksp->ks_limit)
214 			wakeup((caddr_t)ksp);
215 		ksp->ks_inuse--;
216 		kbp->kb_total -= 1;
217 #endif
218 		splx(s);
219 		return;
220 	}
221 #ifdef DIAGNOSTIC
222 	/*
223 	 * Check for multiple frees. Use a quick check to see if
224 	 * it looks free before laboriously searching the freelist.
225 	 */
226 	*(caddr_t *)addr = (char *)WEIRD_ADDR;
227 	copysize = size < sizeof addrmask ? size : sizeof addrmask;
228 	if (!bcmp(addrmask, addr, copysize)) {
229 		for (cp = kbp->kb_next; cp; cp = *(caddr_t *)cp) {
230 			if (addr == cp) {
231 				printf("multiply freed item 0x%x\n", addr);
232 				panic("free: duplicated free");
233 			}
234 		}
235 	}
236 	/*
237 	 * Copy in known text to detect modification after freeing
238 	 * and to make it look free.
239 	 */
240 	bcopy(addrmask, addr, copysize);
241 #endif /* DIAGNOSTIC */
242 #ifdef KMEMSTATS
243 	kup->ku_freecnt++;
244 	if (kup->ku_freecnt >= kbp->kb_elmpercl)
245 		if (kup->ku_freecnt > kbp->kb_elmpercl)
246 			panic("free: multiple frees");
247 		else if (kbp->kb_totalfree > kbp->kb_highwat)
248 			kbp->kb_couldfree++;
249 	kbp->kb_totalfree++;
250 	ksp->ks_memuse -= size;
251 	if (ksp->ks_memuse + size >= ksp->ks_limit &&
252 	    ksp->ks_memuse < ksp->ks_limit)
253 		wakeup((caddr_t)ksp);
254 	ksp->ks_inuse--;
255 #endif
256 	*(caddr_t *)addr = kbp->kb_next;
257 	kbp->kb_next = addr;
258 	splx(s);
259 }
260 
261 /*
262  * Initialize the kernel memory allocator
263  */
264 kmeminit()
265 {
266 	register long indx;
267 	int npg;
268 
269 #if	((MAXALLOCSAVE & (MAXALLOCSAVE - 1)) != 0)
270 		ERROR!_kmeminit:_MAXALLOCSAVE_not_power_of_2
271 #endif
272 #if	(MAXALLOCSAVE > MINALLOCSIZE * 32768)
273 		ERROR!_kmeminit:_MAXALLOCSAVE_too_big
274 #endif
275 #if	(MAXALLOCSAVE < CLBYTES)
276 		ERROR!_kmeminit:_MAXALLOCSAVE_too_small
277 #endif
278 	npg = VM_KMEM_SIZE/ NBPG;
279 	kmemusage = (struct kmemusage *) kmem_alloc(kernel_map,
280 		(vm_size_t)(npg * sizeof(struct kmemusage)));
281 	kmem_map = kmem_suballoc(kernel_map, (vm_offset_t)&kmembase,
282 		(vm_offset_t)&kmemlimit, (vm_size_t)(npg * NBPG), FALSE);
283 #ifdef KMEMSTATS
284 	for (indx = 0; indx < MINBUCKET + 16; indx++) {
285 		if (1 << indx >= CLBYTES)
286 			bucket[indx].kb_elmpercl = 1;
287 		else
288 			bucket[indx].kb_elmpercl = CLBYTES / (1 << indx);
289 		bucket[indx].kb_highwat = 5 * bucket[indx].kb_elmpercl;
290 	}
291 	for (indx = 0; indx < M_LAST; indx++)
292 		kmemstats[indx].ks_limit = npg * NBPG * 6 / 10;
293 #endif
294 }
295