Module Name: src
Committed By: he
Date: Mon Apr 5 08:03:42 UTC 2010
Modified Files:
src/sys/kern: kern_malloc.c
src/sys/sys: mallocvar.h
src/usr.bin/vmstat: vmstat.c
Log Message:
Follow christos' suggestions, and make ks_active a u_short, and
also only use 16 u_shorts instead of 32 ints. Also add panic()
calls for under- and overflow of the ks_active members under
DIAGNOSTIC. The MAXBUCKET constant ended up in sys/mallocvar.h
and not sys/param.h, as the latter caused build problems.
Ride the kernel revision bump of my previous change.
To generate a diff of this commit:
cvs rdiff -u -r1.129 -r1.130 src/sys/kern/kern_malloc.c
cvs rdiff -u -r1.8 -r1.9 src/sys/sys/mallocvar.h
cvs rdiff -u -r1.167 -r1.168 src/usr.bin/vmstat/vmstat.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/sys/kern/kern_malloc.c
diff -u src/sys/kern/kern_malloc.c:1.129 src/sys/kern/kern_malloc.c:1.130
--- src/sys/kern/kern_malloc.c:1.129 Mon Apr 5 07:16:13 2010
+++ src/sys/kern/kern_malloc.c Mon Apr 5 08:03:42 2010
@@ -1,4 +1,4 @@
-/* $NetBSD: kern_malloc.c,v 1.129 2010/04/05 07:16:13 he Exp $ */
+/* $NetBSD: kern_malloc.c,v 1.130 2010/04/05 08:03:42 he Exp $ */
/*
* Copyright (c) 1987, 1991, 1993
@@ -66,7 +66,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_malloc.c,v 1.129 2010/04/05 07:16:13 he Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_malloc.c,v 1.130 2010/04/05 08:03:42 he Exp $");
#include <sys/param.h>
#include <sys/proc.h>
@@ -371,7 +371,11 @@
&malloc_lock);
}
ksp->ks_size |= 1 << indx;
- ksp->ks_active[indx]++;
+#ifdef DIAGNOSTIC
+ if (ksp->ks_active[indx - MINBUCKET] == USHRT_MAX)
+ panic("too many allocations in bucket");
+#endif
+ ksp->ks_active[indx - MINBUCKET]++;
#endif
#ifdef DIAGNOSTIC
copysize = 1 << indx < MAX_COPY ? 1 << indx : MAX_COPY;
@@ -605,7 +609,11 @@
#ifdef KMEMSTATS
size = kup->ku_pagecnt << PGSHIFT;
ksp->ks_memuse -= size;
- ksp->ks_active[kup->ku_indx]--;
+#ifdef DIAGNOSTIC
+ if (ksp->ks_active[kup->ku_indx - MINBUCKET] == 0)
+ panic("no active allocation(1), probably double free");
+#endif
+ ksp->ks_active[kup->ku_indx - MINBUCKET]--;
kup->ku_indx = 0;
kup->ku_pagecnt = 0;
if (ksp->ks_memuse + size >= ksp->ks_limit &&
@@ -662,7 +670,11 @@
}
kbp->kb_totalfree++;
ksp->ks_memuse -= size;
- ksp->ks_active[kup->ku_indx]--;
+#ifdef DIAGNOSTIC
+ if (ksp->ks_active[kup->ku_indx - MINBUCKET] == 0)
+ panic("no active allocation(2), probably double free");
+#endif
+ ksp->ks_active[kup->ku_indx - MINBUCKET]--;
if (ksp->ks_memuse + size >= ksp->ks_limit &&
ksp->ks_memuse < ksp->ks_limit)
wakeup((void *)ksp);
Index: src/sys/sys/mallocvar.h
diff -u src/sys/sys/mallocvar.h:1.8 src/sys/sys/mallocvar.h:1.9
--- src/sys/sys/mallocvar.h:1.8 Mon Apr 5 07:16:12 2010
+++ src/sys/sys/mallocvar.h Mon Apr 5 08:03:41 2010
@@ -1,4 +1,4 @@
-/* $NetBSD: mallocvar.h,v 1.8 2010/04/05 07:16:12 he Exp $ */
+/* $NetBSD: mallocvar.h,v 1.9 2010/04/05 08:03:41 he Exp $ */
/*
* Copyright (c) 1987, 1993
@@ -38,6 +38,7 @@
#define M_MAGIC 877983977
+#define MAXBUCKET 15
/*
* This structure describes a type of malloc'd memory and carries
* allocation statistics for that memory.
@@ -56,7 +57,7 @@
u_long ks_maxused; /* maximum number ever used */
u_long ks_limit; /* most that are allowed to exist */
u_long ks_size; /* sizes of this thing that are allocated */
- u_int ks_active[32]; /* number of active allocations per size */
+ u_short ks_active[MAXBUCKET+1]; /* number of active allocations per size */
};
#ifdef _KERNEL
Index: src/usr.bin/vmstat/vmstat.c
diff -u src/usr.bin/vmstat/vmstat.c:1.167 src/usr.bin/vmstat/vmstat.c:1.168
--- src/usr.bin/vmstat/vmstat.c:1.167 Mon Apr 5 07:16:13 2010
+++ src/usr.bin/vmstat/vmstat.c Mon Apr 5 08:03:42 2010
@@ -1,4 +1,4 @@
-/* $NetBSD: vmstat.c,v 1.167 2010/04/05 07:16:13 he Exp $ */
+/* $NetBSD: vmstat.c,v 1.168 2010/04/05 08:03:42 he Exp $ */
/*-
* Copyright (c) 1998, 2000, 2001, 2007 The NetBSD Foundation, Inc.
@@ -70,7 +70,7 @@
#if 0
static char sccsid[] = "@(#)vmstat.c 8.2 (Berkeley) 3/1/95";
#else
-__RCSID("$NetBSD: vmstat.c,v 1.167 2010/04/05 07:16:13 he Exp $");
+__RCSID("$NetBSD: vmstat.c,v 1.168 2010/04/05 08:03:42 he Exp $");
#endif
#endif /* not lint */
@@ -1183,7 +1183,7 @@
else
(void)printf(",%d", j);
first = 0;
- (void)printf(":%u", ks.ks_active[i]);
+ (void)printf(":%u", ks.ks_active[i - MINBUCKET]);
}
(void)printf("\n");
totuse += ks.ks_memuse;