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