xref: /original-bsd/sys/kern/kern_malloc.c (revision 333da485)
1 /*
2  * Copyright (c) 1987, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)kern_malloc.c	8.3 (Berkeley) 01/04/94
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, 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 (kbp->kb_next &&
170 	    !kernacc(kbp->kb_next, sizeof(struct freelist), 0)) {
171 		printf("%s of object 0x%x size %d %s %s (invalid addr 0x%x)\n",
172 			"Data modified on freelist: word 2.5", va, size,
173 			"previous type", savedtype, kbp->kb_next);
174 		kbp->kb_next = NULL;
175 	}
176 #if BYTE_ORDER == BIG_ENDIAN
177 	freep->type = WEIRD_ADDR >> 16;
178 #endif
179 #if BYTE_ORDER == LITTLE_ENDIAN
180 	freep->type = (short)WEIRD_ADDR;
181 #endif
182 	if (((long)(&freep->next)) & 0x2)
183 		freep->next = (caddr_t)((WEIRD_ADDR >> 16)|(WEIRD_ADDR << 16));
184 	else
185 		freep->next = (caddr_t)WEIRD_ADDR;
186 	end = (long *)&va[copysize];
187 	for (lp = (long *)va; lp < end; lp++) {
188 		if (*lp == WEIRD_ADDR)
189 			continue;
190 		printf("%s %d of object 0x%x size %d %s %s (0x%x != 0x%x)\n",
191 			"Data modified on freelist: word", lp - (long *)va,
192 			va, size, "previous type", savedtype, *lp, WEIRD_ADDR);
193 		break;
194 	}
195 	freep->spare0 = 0;
196 #endif /* DIAGNOSTIC */
197 #ifdef KMEMSTATS
198 	kup = btokup(va);
199 	if (kup->ku_indx != indx)
200 		panic("malloc: wrong bucket");
201 	if (kup->ku_freecnt == 0)
202 		panic("malloc: lost data");
203 	kup->ku_freecnt--;
204 	kbp->kb_totalfree--;
205 	ksp->ks_memuse += 1 << indx;
206 out:
207 	kbp->kb_calls++;
208 	ksp->ks_inuse++;
209 	ksp->ks_calls++;
210 	if (ksp->ks_memuse > ksp->ks_maxused)
211 		ksp->ks_maxused = ksp->ks_memuse;
212 #else
213 out:
214 #endif
215 	splx(s);
216 	return ((void *) va);
217 }
218 
219 /*
220  * Free a block of memory allocated by malloc.
221  */
222 void
223 free(addr, type)
224 	void *addr;
225 	int type;
226 {
227 	register struct kmembuckets *kbp;
228 	register struct kmemusage *kup;
229 	register struct freelist *freep;
230 	long size;
231 	int s;
232 #ifdef DIAGNOSTIC
233 	caddr_t cp;
234 	long *end, *lp, alloc, copysize;
235 #endif
236 #ifdef KMEMSTATS
237 	register struct kmemstats *ksp = &kmemstats[type];
238 #endif
239 
240 	kup = btokup(addr);
241 	size = 1 << kup->ku_indx;
242 	kbp = &bucket[kup->ku_indx];
243 	s = splimp();
244 #ifdef DIAGNOSTIC
245 	/*
246 	 * Check for returns of data that do not point to the
247 	 * beginning of the allocation.
248 	 */
249 	if (size > NBPG * CLSIZE)
250 		alloc = addrmask[BUCKETINDX(NBPG * CLSIZE)];
251 	else
252 		alloc = addrmask[kup->ku_indx];
253 	if (((u_long)addr & alloc) != 0)
254 		panic("free: unaligned addr 0x%x, size %d, type %s, mask %d\n",
255 			addr, size, memname[type], alloc);
256 #endif /* DIAGNOSTIC */
257 	if (size > MAXALLOCSAVE) {
258 		kmem_free(kmem_map, (vm_offset_t)addr, ctob(kup->ku_pagecnt));
259 #ifdef KMEMSTATS
260 		size = kup->ku_pagecnt << PGSHIFT;
261 		ksp->ks_memuse -= size;
262 		kup->ku_indx = 0;
263 		kup->ku_pagecnt = 0;
264 		if (ksp->ks_memuse + size >= ksp->ks_limit &&
265 		    ksp->ks_memuse < ksp->ks_limit)
266 			wakeup((caddr_t)ksp);
267 		ksp->ks_inuse--;
268 		kbp->kb_total -= 1;
269 #endif
270 		splx(s);
271 		return;
272 	}
273 	freep = (struct freelist *)addr;
274 #ifdef DIAGNOSTIC
275 	/*
276 	 * Check for multiple frees. Use a quick check to see if
277 	 * it looks free before laboriously searching the freelist.
278 	 */
279 	if (freep->spare0 == WEIRD_ADDR) {
280 		for (cp = kbp->kb_next; cp; cp = *(caddr_t *)cp) {
281 			if (addr != cp)
282 				continue;
283 			printf("multiply freed item 0x%x\n", addr);
284 			panic("free: duplicated free");
285 		}
286 	}
287 	/*
288 	 * Copy in known text to detect modification after freeing
289 	 * and to make it look free. Also, save the type being freed
290 	 * so we can list likely culprit if modification is detected
291 	 * when the object is reallocated.
292 	 */
293 	copysize = size < MAX_COPY ? size : MAX_COPY;
294 	end = (long *)&((caddr_t)addr)[copysize];
295 	for (lp = (long *)addr; lp < end; lp++)
296 		*lp = WEIRD_ADDR;
297 	freep->type = type;
298 #endif /* DIAGNOSTIC */
299 #ifdef KMEMSTATS
300 	kup->ku_freecnt++;
301 	if (kup->ku_freecnt >= kbp->kb_elmpercl)
302 		if (kup->ku_freecnt > kbp->kb_elmpercl)
303 			panic("free: multiple frees");
304 		else if (kbp->kb_totalfree > kbp->kb_highwat)
305 			kbp->kb_couldfree++;
306 	kbp->kb_totalfree++;
307 	ksp->ks_memuse -= size;
308 	if (ksp->ks_memuse + size >= ksp->ks_limit &&
309 	    ksp->ks_memuse < ksp->ks_limit)
310 		wakeup((caddr_t)ksp);
311 	ksp->ks_inuse--;
312 #endif
313 	if (kbp->kb_next == NULL)
314 		kbp->kb_next = addr;
315 	else
316 		((struct freelist *)kbp->kb_last)->next = addr;
317 	freep->next = NULL;
318 	kbp->kb_last = addr;
319 	splx(s);
320 }
321 
322 /*
323  * Initialize the kernel memory allocator
324  */
325 kmeminit()
326 {
327 	register long indx;
328 	int npg;
329 
330 #if	((MAXALLOCSAVE & (MAXALLOCSAVE - 1)) != 0)
331 		ERROR!_kmeminit:_MAXALLOCSAVE_not_power_of_2
332 #endif
333 #if	(MAXALLOCSAVE > MINALLOCSIZE * 32768)
334 		ERROR!_kmeminit:_MAXALLOCSAVE_too_big
335 #endif
336 #if	(MAXALLOCSAVE < CLBYTES)
337 		ERROR!_kmeminit:_MAXALLOCSAVE_too_small
338 #endif
339 	npg = VM_KMEM_SIZE/ NBPG;
340 	kmemusage = (struct kmemusage *) kmem_alloc(kernel_map,
341 		(vm_size_t)(npg * sizeof(struct kmemusage)));
342 	kmem_map = kmem_suballoc(kernel_map, (vm_offset_t *)&kmembase,
343 		(vm_offset_t *)&kmemlimit, (vm_size_t)(npg * NBPG), FALSE);
344 #ifdef KMEMSTATS
345 	for (indx = 0; indx < MINBUCKET + 16; indx++) {
346 		if (1 << indx >= CLBYTES)
347 			bucket[indx].kb_elmpercl = 1;
348 		else
349 			bucket[indx].kb_elmpercl = CLBYTES / (1 << indx);
350 		bucket[indx].kb_highwat = 5 * bucket[indx].kb_elmpercl;
351 	}
352 	for (indx = 0; indx < M_LAST; indx++)
353 		kmemstats[indx].ks_limit = npg * NBPG * 6 / 10;
354 #endif
355 }
356