Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src Follow christos' suggestions, and make ks_active a u_short, and



details:   https://anonhg.NetBSD.org/src/rev/616d053b0b51
branches:  trunk
changeset: 753636:616d053b0b51
user:      he <he%NetBSD.org@localhost>
date:      Mon Apr 05 08:03:41 2010 +0000

description:
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.

diffstat:

 sys/kern/kern_malloc.c  |  22 +++++++++++++++++-----
 sys/sys/mallocvar.h     |   5 +++--
 usr.bin/vmstat/vmstat.c |   6 +++---
 3 files changed, 23 insertions(+), 10 deletions(-)

diffs (110 lines):

diff -r a5d7f0a887c8 -r 616d053b0b51 sys/kern/kern_malloc.c
--- a/sys/kern/kern_malloc.c    Mon Apr 05 07:53:47 2010 +0000
+++ b/sys/kern/kern_malloc.c    Mon Apr 05 08:03:41 2010 +0000
@@ -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);
diff -r a5d7f0a887c8 -r 616d053b0b51 sys/sys/mallocvar.h
--- a/sys/sys/mallocvar.h       Mon Apr 05 07:53:47 2010 +0000
+++ b/sys/sys/mallocvar.h       Mon Apr 05 08:03:41 2010 +0000
@@ -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
diff -r a5d7f0a887c8 -r 616d053b0b51 usr.bin/vmstat/vmstat.c
--- a/usr.bin/vmstat/vmstat.c   Mon Apr 05 07:53:47 2010 +0000
+++ b/usr.bin/vmstat/vmstat.c   Mon Apr 05 08:03:41 2010 +0000
@@ -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;



Home | Main Index | Thread Index | Old Index