xref: /openbsd-src/sys/kern/kern_malloc.c (revision cf2525843d483a385de106a1361b2b9c18d96583)
1 /*	$OpenBSD: kern_malloc.c,v 1.63 2006/09/30 14:31:28 mickey Exp $	*/
2 /*	$NetBSD: kern_malloc.c,v 1.15.4.2 1996/06/13 17:10:56 cgd Exp $	*/
3 
4 /*
5  * Copyright (c) 1987, 1991, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)kern_malloc.c	8.3 (Berkeley) 1/4/94
33  */
34 
35 #include <sys/param.h>
36 #include <sys/proc.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/systm.h>
40 #include <sys/sysctl.h>
41 
42 #include <uvm/uvm_extern.h>
43 
44 static struct vm_map_intrsafe kmem_map_store;
45 struct vm_map *kmem_map = NULL;
46 
47 #ifdef NKMEMCLUSTERS
48 #error NKMEMCLUSTERS is obsolete; remove it from your kernel config file and use NKMEMPAGES instead or let the kernel auto-size
49 #endif
50 
51 /*
52  * Default number of pages in kmem_map.  We attempt to calculate this
53  * at run-time, but allow it to be either patched or set in the kernel
54  * config file.
55  */
56 #ifndef NKMEMPAGES
57 #define	NKMEMPAGES	0
58 #endif
59 u_int	nkmempages = NKMEMPAGES;
60 
61 /*
62  * Defaults for lower- and upper-bounds for the kmem_map page count.
63  * Can be overridden by kernel config options.
64  */
65 #ifndef	NKMEMPAGES_MIN
66 #define	NKMEMPAGES_MIN	NKMEMPAGES_MIN_DEFAULT
67 #endif
68 u_int	nkmempages_min = 0;
69 
70 #ifndef NKMEMPAGES_MAX
71 #define	NKMEMPAGES_MAX	NKMEMPAGES_MAX_DEFAULT
72 #endif
73 u_int	nkmempages_max = 0;
74 
75 struct kmembuckets bucket[MINBUCKET + 16];
76 struct kmemstats kmemstats[M_LAST];
77 struct kmemusage *kmemusage;
78 char *kmembase, *kmemlimit;
79 char buckstring[16 * sizeof("123456,")];
80 int buckstring_init = 0;
81 #if defined(KMEMSTATS) || defined(DIAGNOSTIC) || defined(FFS_SOFTUPDATES)
82 char *memname[] = INITKMEMNAMES;
83 char *memall = NULL;
84 extern struct lock sysctl_kmemlock;
85 #endif
86 
87 #ifdef DIAGNOSTIC
88 /*
89  * This structure provides a set of masks to catch unaligned frees.
90  */
91 const long addrmask[] = { 0,
92 	0x00000001, 0x00000003, 0x00000007, 0x0000000f,
93 	0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
94 	0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
95 	0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
96 };
97 
98 /*
99  * The WEIRD_ADDR is used as known text to copy into free objects so
100  * that modifications after frees can be detected.
101  */
102 #define WEIRD_ADDR	((unsigned) 0xdeadbeef)
103 #define MAX_COPY	32
104 
105 /*
106  * Normally the freelist structure is used only to hold the list pointer
107  * for free objects.  However, when running with diagnostics, the first
108  * 8 bytes of the structure is unused except for diagnostic information,
109  * and the free list pointer is at offset 8 in the structure.  Since the
110  * first 8 bytes is the portion of the structure most often modified, this
111  * helps to detect memory reuse problems and avoid free list corruption.
112  */
113 struct freelist {
114 	int32_t	spare0;
115 	int16_t	type;
116 	int16_t	spare1;
117 	caddr_t	next;
118 };
119 #else /* !DIAGNOSTIC */
120 struct freelist {
121 	caddr_t	next;
122 };
123 #endif /* DIAGNOSTIC */
124 
125 /*
126  * Allocate a block of memory
127  */
128 void *
129 malloc(unsigned long size, int type, int flags)
130 {
131 	struct kmembuckets *kbp;
132 	struct kmemusage *kup;
133 	struct freelist *freep;
134 	long indx, npg, allocsize;
135 	int s;
136 	caddr_t va, cp, savedlist;
137 #ifdef DIAGNOSTIC
138 	int32_t *end, *lp;
139 	int copysize;
140 	char *savedtype;
141 #endif
142 #ifdef KMEMSTATS
143 	struct kmemstats *ksp = &kmemstats[type];
144 
145 	if (((unsigned long)type) >= M_LAST)
146 		panic("malloc - bogus type");
147 #endif
148 
149 #ifdef MALLOC_DEBUG
150 	if (debug_malloc(size, type, flags, (void **)&va))
151 		return ((void *) va);
152 #endif
153 
154 	if (size > 65535 * PAGE_SIZE)
155 		panic("malloc: allocation too large");
156 	indx = BUCKETINDX(size);
157 	kbp = &bucket[indx];
158 	s = splvm();
159 #ifdef KMEMSTATS
160 	while (ksp->ks_memuse >= ksp->ks_limit) {
161 		if (flags & M_NOWAIT) {
162 			splx(s);
163 			return ((void *) NULL);
164 		}
165 		if (ksp->ks_limblocks < 65535)
166 			ksp->ks_limblocks++;
167 		tsleep(ksp, PSWP+2, memname[type], 0);
168 	}
169 	ksp->ks_size |= 1 << indx;
170 #endif
171 #ifdef DIAGNOSTIC
172 	copysize = 1 << indx < MAX_COPY ? 1 << indx : MAX_COPY;
173 #endif
174 	if (kbp->kb_next == NULL) {
175 		kbp->kb_last = NULL;
176 		if (size > MAXALLOCSAVE)
177 			allocsize = round_page(size);
178 		else
179 			allocsize = 1 << indx;
180 		npg = btoc(allocsize);
181 		va = (caddr_t) uvm_km_kmemalloc(kmem_map, uvmexp.kmem_object,
182 		    (vsize_t)ctob(npg),
183 		    ((flags & M_NOWAIT) ? UVM_KMF_NOWAIT : 0) |
184 		    ((flags & M_CANFAIL) ? UVM_KMF_CANFAIL : 0));
185 		if (va == NULL) {
186 			/*
187 			 * Kmem_malloc() can return NULL, even if it can
188 			 * wait, if there is no map space available, because
189 			 * it can't fix that problem.  Neither can we,
190 			 * right now.  (We should release pages which
191 			 * are completely free and which are in buckets
192 			 * with too many free elements.)
193 			 */
194 			if ((flags & (M_NOWAIT|M_CANFAIL)) == 0)
195 				panic("malloc: out of space in kmem_map");
196 			splx(s);
197 			return (NULL);
198 		}
199 #ifdef KMEMSTATS
200 		kbp->kb_total += kbp->kb_elmpercl;
201 #endif
202 		kup = btokup(va);
203 		kup->ku_indx = indx;
204 		if (allocsize > MAXALLOCSAVE) {
205 			kup->ku_pagecnt = npg;
206 #ifdef KMEMSTATS
207 			ksp->ks_memuse += allocsize;
208 #endif
209 			goto out;
210 		}
211 #ifdef KMEMSTATS
212 		kup->ku_freecnt = kbp->kb_elmpercl;
213 		kbp->kb_totalfree += kbp->kb_elmpercl;
214 #endif
215 		/*
216 		 * Just in case we blocked while allocating memory,
217 		 * and someone else also allocated memory for this
218 		 * bucket, don't assume the list is still empty.
219 		 */
220 		savedlist = kbp->kb_next;
221 		kbp->kb_next = cp = va + (npg * PAGE_SIZE) - allocsize;
222 		for (;;) {
223 			freep = (struct freelist *)cp;
224 #ifdef DIAGNOSTIC
225 			/*
226 			 * Copy in known text to detect modification
227 			 * after freeing.
228 			 */
229 			end = (int32_t *)&cp[copysize];
230 			for (lp = (int32_t *)cp; lp < end; lp++)
231 				*lp = WEIRD_ADDR;
232 			freep->type = M_FREE;
233 #endif /* DIAGNOSTIC */
234 			if (cp <= va)
235 				break;
236 			cp -= allocsize;
237 			freep->next = cp;
238 		}
239 		freep->next = savedlist;
240 		if (kbp->kb_last == NULL)
241 			kbp->kb_last = (caddr_t)freep;
242 	}
243 	va = kbp->kb_next;
244 	kbp->kb_next = ((struct freelist *)va)->next;
245 #ifdef DIAGNOSTIC
246 	freep = (struct freelist *)va;
247 	savedtype = (unsigned)freep->type < M_LAST ?
248 		memname[freep->type] : "???";
249 	if (kbp->kb_next) {
250 		int rv;
251 		vaddr_t addr = (vaddr_t)kbp->kb_next;
252 
253 		vm_map_lock(kmem_map);
254 		rv = uvm_map_checkprot(kmem_map, addr,
255 		    addr + sizeof(struct freelist), VM_PROT_WRITE);
256 		vm_map_unlock(kmem_map);
257 
258 		if (!rv)  {
259 		printf("%s %d of object %p size 0x%lx %s %s (invalid addr %p)\n",
260 			"Data modified on freelist: word",
261 			(int32_t *)&kbp->kb_next - (int32_t *)kbp, va, size,
262 			"previous type", savedtype, kbp->kb_next);
263 		kbp->kb_next = NULL;
264 		}
265 	}
266 
267 	/* Fill the fields that we've used with WEIRD_ADDR */
268 #if BYTE_ORDER == BIG_ENDIAN
269 	freep->type = WEIRD_ADDR >> 16;
270 #endif
271 #if BYTE_ORDER == LITTLE_ENDIAN
272 	freep->type = (short)WEIRD_ADDR;
273 #endif
274 	end = (int32_t *)&freep->next +
275 	    (sizeof(freep->next) / sizeof(int32_t));
276 	for (lp = (int32_t *)&freep->next; lp < end; lp++)
277 		*lp = WEIRD_ADDR;
278 
279 	/* and check that the data hasn't been modified. */
280 	end = (int32_t *)&va[copysize];
281 	for (lp = (int32_t *)va; lp < end; lp++) {
282 		if (*lp == WEIRD_ADDR)
283 			continue;
284 		printf("%s %d of object %p size 0x%lx %s %s (0x%x != 0x%x)\n",
285 			"Data modified on freelist: word", lp - (int32_t *)va,
286 			va, size, "previous type", savedtype, *lp, WEIRD_ADDR);
287 		break;
288 	}
289 
290 	freep->spare0 = 0;
291 #endif /* DIAGNOSTIC */
292 #ifdef KMEMSTATS
293 	kup = btokup(va);
294 	if (kup->ku_indx != indx)
295 		panic("malloc: wrong bucket");
296 	if (kup->ku_freecnt == 0)
297 		panic("malloc: lost data");
298 	kup->ku_freecnt--;
299 	kbp->kb_totalfree--;
300 	ksp->ks_memuse += 1 << indx;
301 out:
302 	kbp->kb_calls++;
303 	ksp->ks_inuse++;
304 	ksp->ks_calls++;
305 	if (ksp->ks_memuse > ksp->ks_maxused)
306 		ksp->ks_maxused = ksp->ks_memuse;
307 #else
308 out:
309 #endif
310 	splx(s);
311 	return ((void *) va);
312 }
313 
314 /*
315  * Free a block of memory allocated by malloc.
316  */
317 void
318 free(void *addr, int type)
319 {
320 	struct kmembuckets *kbp;
321 	struct kmemusage *kup;
322 	struct freelist *freep;
323 	long size;
324 	int s;
325 #ifdef DIAGNOSTIC
326 	caddr_t cp;
327 	int32_t *end, *lp;
328 	long alloc, copysize;
329 #endif
330 #ifdef KMEMSTATS
331 	struct kmemstats *ksp = &kmemstats[type];
332 #endif
333 
334 #ifdef MALLOC_DEBUG
335 	if (debug_free(addr, type))
336 		return;
337 #endif
338 
339 #ifdef DIAGNOSTIC
340 	if (addr < (void *)kmembase || addr >= (void *)kmemlimit)
341 		panic("free: non-malloced addr %p type %s", addr,
342 		    memname[type]);
343 #endif
344 
345 	kup = btokup(addr);
346 	size = 1 << kup->ku_indx;
347 	kbp = &bucket[kup->ku_indx];
348 	s = splvm();
349 #ifdef DIAGNOSTIC
350 	/*
351 	 * Check for returns of data that do not point to the
352 	 * beginning of the allocation.
353 	 */
354 	if (size > PAGE_SIZE)
355 		alloc = addrmask[BUCKETINDX(PAGE_SIZE)];
356 	else
357 		alloc = addrmask[kup->ku_indx];
358 	if (((u_long)addr & alloc) != 0)
359 		panic("free: unaligned addr %p, size %ld, type %s, mask %ld",
360 			addr, size, memname[type], alloc);
361 #endif /* DIAGNOSTIC */
362 	if (size > MAXALLOCSAVE) {
363 		uvm_km_free(kmem_map, (vaddr_t)addr, ctob(kup->ku_pagecnt));
364 #ifdef KMEMSTATS
365 		size = kup->ku_pagecnt << PGSHIFT;
366 		ksp->ks_memuse -= size;
367 		kup->ku_indx = 0;
368 		kup->ku_pagecnt = 0;
369 		if (ksp->ks_memuse + size >= ksp->ks_limit &&
370 		    ksp->ks_memuse < ksp->ks_limit)
371 			wakeup(ksp);
372 		ksp->ks_inuse--;
373 		kbp->kb_total -= 1;
374 #endif
375 		splx(s);
376 		return;
377 	}
378 	freep = (struct freelist *)addr;
379 #ifdef DIAGNOSTIC
380 	/*
381 	 * Check for multiple frees. Use a quick check to see if
382 	 * it looks free before laboriously searching the freelist.
383 	 */
384 	if (freep->spare0 == WEIRD_ADDR) {
385 		for (cp = kbp->kb_next; cp;
386 		    cp = ((struct freelist *)cp)->next) {
387 			if (addr != cp)
388 				continue;
389 			printf("multiply freed item %p\n", addr);
390 			panic("free: duplicated free");
391 		}
392 	}
393 	/*
394 	 * Copy in known text to detect modification after freeing
395 	 * and to make it look free. Also, save the type being freed
396 	 * so we can list likely culprit if modification is detected
397 	 * when the object is reallocated.
398 	 */
399 	copysize = size < MAX_COPY ? size : MAX_COPY;
400 	end = (int32_t *)&((caddr_t)addr)[copysize];
401 	for (lp = (int32_t *)addr; lp < end; lp++)
402 		*lp = WEIRD_ADDR;
403 	freep->type = type;
404 #endif /* DIAGNOSTIC */
405 #ifdef KMEMSTATS
406 	kup->ku_freecnt++;
407 	if (kup->ku_freecnt >= kbp->kb_elmpercl) {
408 		if (kup->ku_freecnt > kbp->kb_elmpercl)
409 			panic("free: multiple frees");
410 		else if (kbp->kb_totalfree > kbp->kb_highwat)
411 			kbp->kb_couldfree++;
412 	}
413 	kbp->kb_totalfree++;
414 	ksp->ks_memuse -= size;
415 	if (ksp->ks_memuse + size >= ksp->ks_limit &&
416 	    ksp->ks_memuse < ksp->ks_limit)
417 		wakeup(ksp);
418 	ksp->ks_inuse--;
419 #endif
420 	if (kbp->kb_next == NULL)
421 		kbp->kb_next = addr;
422 	else
423 		((struct freelist *)kbp->kb_last)->next = addr;
424 	freep->next = NULL;
425 	kbp->kb_last = addr;
426 	splx(s);
427 }
428 
429 /*
430  * Compute the number of pages that kmem_map will map, that is,
431  * the size of the kernel malloc arena.
432  */
433 void
434 kmeminit_nkmempages(void)
435 {
436 	u_int npages;
437 
438 	if (nkmempages != 0) {
439 		/*
440 		 * It's already been set (by us being here before, or
441 		 * by patching or kernel config options), bail out now.
442 		 */
443 		return;
444 	}
445 
446 	/*
447 	 * We can't initialize these variables at compilation time, since
448 	 * the page size may not be known (on sparc GENERIC kernels, for
449 	 * example). But we still want the MD code to be able to provide
450 	 * better values.
451 	 */
452 	if (nkmempages_min == 0)
453 		nkmempages_min = NKMEMPAGES_MIN;
454 	if (nkmempages_max == 0)
455 		nkmempages_max = NKMEMPAGES_MAX;
456 
457 	/*
458 	 * We use the following (simple) formula:
459 	 *
460 	 *	- Starting point is physical memory / 4.
461 	 *
462 	 *	- Clamp it down to nkmempages_max.
463 	 *
464 	 *	- Round it up to nkmempages_min.
465 	 */
466 	npages = physmem / 4;
467 
468 	if (npages > nkmempages_max)
469 		npages = nkmempages_max;
470 
471 	if (npages < nkmempages_min)
472 		npages = nkmempages_min;
473 
474 	nkmempages = npages;
475 }
476 
477 /*
478  * Initialize the kernel memory allocator
479  */
480 void
481 kmeminit(void)
482 {
483 	vaddr_t base, limit;
484 #ifdef KMEMSTATS
485 	long indx;
486 #endif
487 
488 #ifdef DIAGNOSTIC
489 	if (sizeof(struct freelist) > (1 << MINBUCKET))
490 		panic("kmeminit: minbucket too small/struct freelist too big");
491 #endif
492 
493 	/*
494 	 * Compute the number of kmem_map pages, if we have not
495 	 * done so already.
496 	 */
497 	kmeminit_nkmempages();
498 	base = vm_map_min(kernel_map);
499 	kmem_map = uvm_km_suballoc(kernel_map, &base, &limit,
500 	    (vsize_t)(nkmempages * PAGE_SIZE), VM_MAP_INTRSAFE, FALSE,
501 	    &kmem_map_store.vmi_map);
502 	kmembase = (char *)base;
503 	kmemlimit = (char *)limit;
504 	kmemusage = (struct kmemusage *) uvm_km_zalloc(kernel_map,
505 		(vsize_t)(nkmempages * sizeof(struct kmemusage)));
506 #ifdef KMEMSTATS
507 	for (indx = 0; indx < MINBUCKET + 16; indx++) {
508 		if (1 << indx >= PAGE_SIZE)
509 			bucket[indx].kb_elmpercl = 1;
510 		else
511 			bucket[indx].kb_elmpercl = PAGE_SIZE / (1 << indx);
512 		bucket[indx].kb_highwat = 5 * bucket[indx].kb_elmpercl;
513 	}
514 	for (indx = 0; indx < M_LAST; indx++)
515 		kmemstats[indx].ks_limit = nkmempages * PAGE_SIZE * 6 / 10;
516 #endif
517 #ifdef MALLOC_DEBUG
518 	debug_malloc_init();
519 #endif
520 }
521 
522 /*
523  * Return kernel malloc statistics information.
524  */
525 int
526 sysctl_malloc(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
527     size_t newlen, struct proc *p)
528 {
529 	struct kmembuckets kb;
530 	int i, siz;
531 
532 	if (namelen != 2 && name[0] != KERN_MALLOC_BUCKETS &&
533 	    name[0] != KERN_MALLOC_KMEMNAMES)
534 		return (ENOTDIR);		/* overloaded */
535 
536 	switch (name[0]) {
537 	case KERN_MALLOC_BUCKETS:
538 		/* Initialize the first time */
539 		if (buckstring_init == 0) {
540 			buckstring_init = 1;
541 			bzero(buckstring, sizeof(buckstring));
542 			for (siz = 0, i = MINBUCKET; i < MINBUCKET + 16; i++) {
543 				snprintf(buckstring + siz,
544 				    sizeof buckstring - siz,
545 				    "%d,", (u_int)(1<<i));
546 				siz += strlen(buckstring + siz);
547 			}
548 			/* Remove trailing comma */
549 			if (siz)
550 				buckstring[siz - 1] = '\0';
551 		}
552 		return (sysctl_rdstring(oldp, oldlenp, newp, buckstring));
553 
554 	case KERN_MALLOC_BUCKET:
555 		bcopy(&bucket[BUCKETINDX(name[1])], &kb, sizeof(kb));
556 		kb.kb_next = kb.kb_last = 0;
557 		return (sysctl_rdstruct(oldp, oldlenp, newp, &kb, sizeof(kb)));
558 	case KERN_MALLOC_KMEMSTATS:
559 #ifdef KMEMSTATS
560 		if ((name[1] < 0) || (name[1] >= M_LAST))
561 			return (EINVAL);
562 		return (sysctl_rdstruct(oldp, oldlenp, newp,
563 		    &kmemstats[name[1]], sizeof(struct kmemstats)));
564 #else
565 		return (EOPNOTSUPP);
566 #endif
567 	case KERN_MALLOC_KMEMNAMES:
568 #if defined(KMEMSTATS) || defined(DIAGNOSTIC) || defined(FFS_SOFTUPDATES)
569 		if (memall == NULL) {
570 			int totlen;
571 
572 			i = lockmgr(&sysctl_kmemlock, LK_EXCLUSIVE, NULL);
573 			if (i)
574 				return (i);
575 
576 			/* Figure out how large a buffer we need */
577 			for (totlen = 0, i = 0; i < M_LAST; i++) {
578 				if (memname[i])
579 					totlen += strlen(memname[i]);
580 				totlen++;
581 			}
582 			memall = malloc(totlen + M_LAST, M_SYSCTL, M_WAITOK);
583 			bzero(memall, totlen + M_LAST);
584 			for (siz = 0, i = 0; i < M_LAST; i++) {
585 				snprintf(memall + siz,
586 				    totlen + M_LAST - siz,
587 				    "%s,", memname[i] ? memname[i] : "");
588 				siz += strlen(memall + siz);
589 			}
590 			/* Remove trailing comma */
591 			if (siz)
592 				memall[siz - 1] = '\0';
593 
594 			/* Now, convert all spaces to underscores */
595 			for (i = 0; i < totlen; i++)
596 				if (memall[i] == ' ')
597 					memall[i] = '_';
598 			lockmgr(&sysctl_kmemlock, LK_RELEASE, NULL);
599 		}
600 		return (sysctl_rdstring(oldp, oldlenp, newp, memall));
601 #else
602 		return (EOPNOTSUPP);
603 #endif
604 	default:
605 		return (EOPNOTSUPP);
606 	}
607 	/* NOTREACHED */
608 }
609 
610 /*
611  * Round up a size to how much malloc would actually allocate.
612  */
613 size_t
614 malloc_roundup(size_t sz)
615 {
616 	if (sz > MAXALLOCSAVE)
617 		return round_page(sz);
618 
619 	return (1 << BUCKETINDX(sz));
620 }
621 
622 #if defined(DDB)
623 #include <machine/db_machdep.h>
624 #include <ddb/db_interface.h>
625 #include <ddb/db_output.h>
626 
627 void
628 malloc_printit(int (*pr)(const char *, ...))
629 {
630 #ifdef KMEMSTATS
631 	struct kmemstats *km;
632 	int i;
633 
634 	(*pr)("%15s %5s  %6s  %7s  %6s %9s %8s %8s\n",
635 	    "Type", "InUse", "MemUse", "HighUse", "Limit", "Requests",
636 	    "Type Lim", "Kern Lim");
637 	for (i = 0, km = kmemstats; i < M_LAST; i++, km++) {
638 		if (!km->ks_calls || !memname[i])
639 			continue;
640 
641 		(*pr)("%15s %5ld %6ldK %7ldK %6ldK %9ld %8d %8d\n",
642 		    memname[i], km->ks_inuse, km->ks_memuse / 1024,
643 		    km->ks_maxused / 1024, km->ks_limit / 1024,
644 		    km->ks_calls, km->ks_limblocks, km->ks_mapblocks);
645 	}
646 #else
647 	(*pr)("No KMEMSTATS compiled in\n");
648 #endif
649 }
650 #endif /* DDB */
651