Source-Changes-HG archive

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

[src/trunk]: src/sys Make the memory allocation code MP-safe.



details:   https://anonhg.NetBSD.org/src/rev/81de9c57d4de
branches:  trunk
changeset: 543031:81de9c57d4de
user:      pk <pk%NetBSD.org@localhost>
date:      Fri Feb 14 21:51:36 2003 +0000

description:
Make the memory allocation code MP-safe.

diffstat:

 sys/kern/kern_malloc.c |  17 ++++++++++++++---
 sys/sys/malloc.h       |   9 ++++++++-
 2 files changed, 22 insertions(+), 4 deletions(-)

diffs (145 lines):

diff -r 4ef8d29b6af9 -r 81de9c57d4de sys/kern/kern_malloc.c
--- a/sys/kern/kern_malloc.c    Fri Feb 14 21:50:10 2003 +0000
+++ b/sys/kern/kern_malloc.c    Fri Feb 14 21:51:36 2003 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_malloc.c,v 1.77 2003/02/01 06:23:43 thorpej Exp $ */
+/*     $NetBSD: kern_malloc.c,v 1.78 2003/02/14 21:51:36 pk Exp $      */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_malloc.c,v 1.77 2003/02/01 06:23:43 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_malloc.c,v 1.78 2003/02/14 21:51:36 pk Exp $");
 
 #include "opt_lockdebug.h"
 
@@ -214,6 +214,8 @@
 MALLOC_DEFINE(M_MRTABLE, "mrt", "multicast routing tables");
 MALLOC_DEFINE(M_1394DATA, "1394data", "IEEE 1394 data buffers");
 
+struct simplelock malloc_slock = SIMPLELOCK_INITIALIZER;
+
 /*
  * Allocate a block of memory
  */
@@ -249,15 +251,18 @@
        indx = BUCKETINDX(size);
        kbp = &bucket[indx];
        s = splvm();
+       simple_lock(&malloc_slock);
 #ifdef KMEMSTATS
        while (ksp->ks_memuse >= ksp->ks_limit) {
                if (flags & M_NOWAIT) {
+                       simple_unlock(&malloc_slock);
                        splx(s);
                        return ((void *) NULL);
                }
                if (ksp->ks_limblocks < 65535)
                        ksp->ks_limblocks++;
-               tsleep((caddr_t)ksp, PSWP+2, ksp->ks_shortdesc, 0);
+               ltsleep((caddr_t)ksp, PSWP+2, ksp->ks_shortdesc, 0,
+                       &malloc_slock);
        }
        ksp->ks_size |= 1 << indx;
 #endif
@@ -271,6 +276,7 @@
                else
                        allocsize = 1 << indx;
                npg = btoc(allocsize);
+               simple_unlock(&malloc_slock);
                va = (caddr_t) uvm_km_kmemalloc(kmem_map, NULL,
                    (vsize_t)ctob(npg),
                    ((flags & M_NOWAIT) ? UVM_KMF_NOWAIT : 0) |
@@ -289,6 +295,7 @@
                        splx(s);
                        return (NULL);
                }
+               simple_lock(&malloc_slock);
 #ifdef KMEMSTATS
                kbp->kb_total += kbp->kb_elmpercl;
 #endif
@@ -414,6 +421,7 @@
 #ifdef MALLOCLOG
        domlog(va, size, type, 1, file, line);
 #endif
+       simple_unlock(&malloc_slock);
        splx(s);
        if ((flags & M_ZERO) != 0)
                memset(va, 0, size);
@@ -462,6 +470,7 @@
        size = 1 << kup->ku_indx;
        kbp = &bucket[kup->ku_indx];
        s = splvm();
+       simple_lock(&malloc_slock);
 #ifdef MALLOCLOG
        domlog(addr, 0, type, 2, file, line);
 #endif
@@ -491,6 +500,7 @@
                ksp->ks_inuse--;
                kbp->kb_total -= 1;
 #endif
+               simple_unlock(&malloc_slock);
                splx(s);
                return;
        }
@@ -551,6 +561,7 @@
                ((struct freelist *)kbp->kb_last)->next = addr;
        freep->next = NULL;
        kbp->kb_last = addr;
+       simple_unlock(&malloc_slock);
        splx(s);
 }
 
diff -r 4ef8d29b6af9 -r 81de9c57d4de sys/sys/malloc.h
--- a/sys/sys/malloc.h  Fri Feb 14 21:50:10 2003 +0000
+++ b/sys/sys/malloc.h  Fri Feb 14 21:51:36 2003 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: malloc.h,v 1.85 2003/02/02 02:22:14 christos Exp $     */
+/*     $NetBSD: malloc.h,v 1.86 2003/02/14 21:51:36 pk Exp $   */
 
 /*
  * Copyright (c) 1987, 1993
@@ -148,6 +148,8 @@
 #define        btokmemx(addr)  (((caddr_t)(addr) - kmembase) / NBPG)
 #define        btokup(addr)    (&kmemusage[((caddr_t)(addr) - kmembase) >> PGSHIFT])
 
+extern struct simplelock malloc_slock;
+
 /*
  * Macro versions for the usual cases of malloc/free
  */
@@ -162,12 +164,15 @@
 do {                                                                   \
        register struct kmembuckets *__kbp = &bucket[BUCKETINDX((size))]; \
        long __s = splvm();                                             \
+       simple_lock(&malloc_slock);                                     \
        if (__kbp->kb_next == NULL) {                                   \
+               simple_unlock(&malloc_slock);                           \
                (space) = (cast)malloc((u_long)(size), (type), (flags)); \
                splx(__s);                                              \
        } else {                                                        \
                (space) = (cast)__kbp->kb_next;                         \
                __kbp->kb_next = *(caddr_t *)(space);                   \
+               simple_unlock(&malloc_slock);                           \
                splx(__s);                                              \
                if ((flags) & M_ZERO)                                   \
                        memset((space), 0, (size));                     \
@@ -182,6 +187,7 @@
        if (1 << __kup->ku_indx > MAXALLOCSAVE) {                       \
                free((caddr_t)(addr), (type));                          \
        } else {                                                        \
+               simple_lock(&malloc_slock);                             \
                __kbp = &bucket[__kup->ku_indx];                        \
                if (__kbp->kb_next == NULL)                             \
                        __kbp->kb_next = (caddr_t)(addr);               \
@@ -189,6 +195,7 @@
                        *(caddr_t *)(__kbp->kb_last) = (caddr_t)(addr); \
                *(caddr_t *)(addr) = NULL;                              \
                __kbp->kb_last = (caddr_t)(addr);                       \
+               simple_unlock(&malloc_slock);                           \
        }                                                               \
        splx(__s);                                                      \
 } while(/* CONSTCOND */ 0)



Home | Main Index | Thread Index | Old Index