add definition for ability to produce a backtrace
[unix-history] / usr / src / sys / kern / kern_malloc.c
index de11753..76d5e1a 100644 (file)
@@ -1,19 +1,20 @@
 /*
 /*
- * Copyright (c) 1987, 1991 The Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1987, 1991, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  * %sccs.include.redist.c%
  *
  *
  * %sccs.include.redist.c%
  *
- *     @(#)kern_malloc.c       7.27 (Berkeley) %G%
+ *     @(#)kern_malloc.c       8.3 (Berkeley) %G%
  */
 
  */
 
-#include "param.h"
-#include "proc.h"
-#include "map.h"
-#include "kernel.h"
-#include "malloc.h"
-#include "vm/vm.h"
-#include "vm/vm_kern.h"
+#include <sys/param.h>
+#include <sys/proc.h>
+#include <sys/map.h>
+#include <sys/kernel.h>
+#include <sys/malloc.h>
+
+#include <vm/vm.h>
+#include <vm/vm_kern.h>
 
 struct kmembuckets bucket[MINBUCKET + 16];
 struct kmemstats kmemstats[M_LAST];
 
 struct kmembuckets bucket[MINBUCKET + 16];
 struct kmemstats kmemstats[M_LAST];
@@ -28,19 +29,22 @@ long malloc_reentered;
 
 #ifdef DIAGNOSTIC
 /*
 
 #ifdef DIAGNOSTIC
 /*
- * This structure serves two purposes.
- * The first is to provide a set of masks to catch unaligned frees.
- * The second is to provide known text to copy into free objects so
- * that modifications after frees can be detected.
+ * This structure provides a set of masks to catch unaligned frees.
  */
  */
-#define WEIRD_ADDR 0xdeadbeef
-long addrmask[] = { WEIRD_ADDR,
+long addrmask[] = { 0,
        0x00000001, 0x00000003, 0x00000007, 0x0000000f,
        0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
        0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
        0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
 };
 
        0x00000001, 0x00000003, 0x00000007, 0x0000000f,
        0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
        0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
        0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
 };
 
+/*
+ * The WEIRD_ADDR is used as known text to copy into free objects so
+ * that modifications after frees can be detected.
+ */
+#define WEIRD_ADDR     0xdeadbeef
+#define MAX_COPY       32
+
 /*
  * Normally the first word of the structure is used to hold the list
  * pointer for free objects. However, when running with diagnostics,
 /*
  * Normally the first word of the structure is used to hold the list
  * pointer for free objects. However, when running with diagnostics,
@@ -49,9 +53,8 @@ long addrmask[] = { WEIRD_ADDR,
  */
 struct freelist {
        long    spare0;
  */
 struct freelist {
        long    spare0;
-       long    spare1;
        short   type;
        short   type;
-       short   spare2;
+       long    spare1;
        caddr_t next;
 };
 #else /* !DIAGNOSTIC */
        caddr_t next;
 };
 #else /* !DIAGNOSTIC */
@@ -60,6 +63,13 @@ struct freelist {
 };
 #endif /* DIAGNOSTIC */
 
 };
 #endif /* DIAGNOSTIC */
 
+struct uselist {
+       struct  uselist *next;
+       caddr_t mem;
+       long    size;
+       long    type;
+} *listhd;
+
 /*
  * Allocate a block of memory
  */
 /*
  * Allocate a block of memory
  */
@@ -71,7 +81,7 @@ malloc(size, type, flags)
        register struct kmembuckets *kbp;
        register struct kmemusage *kup;
        register struct freelist *freep;
        register struct kmembuckets *kbp;
        register struct kmemusage *kup;
        register struct freelist *freep;
-       long indx, npg, alloc, allocsize;
+       long indx, npg, allocsize;
        int s;
        caddr_t va, cp, rp;
 #ifdef KMEMSTATS
        int s;
        caddr_t va, cp, rp;
 #ifdef KMEMSTATS
@@ -82,7 +92,6 @@ malloc(size, type, flags)
                panic("malloc - bogus type");
        if (type == M_NAMEI)
                curproc->p_spare[0]++;
                panic("malloc - bogus type");
        if (type == M_NAMEI)
                curproc->p_spare[0]++;
-
        indx = BUCKETINDX(size);
        kbp = &bucket[indx];
        s = splimp();
        indx = BUCKETINDX(size);
        kbp = &bucket[indx];
        s = splimp();
@@ -100,11 +109,13 @@ malloc(size, type, flags)
                tsleep((caddr_t)ksp, PSWP+2, memname[type], 0);
                IN;
        }
                tsleep((caddr_t)ksp, PSWP+2, memname[type], 0);
                IN;
        }
+       ksp->ks_size |= 1 << indx;
 #endif
 #ifdef DIAGNOSTIC
 #endif
 #ifdef DIAGNOSTIC
-       copysize = 1 << indx < sizeof addrmask ? 1 << indx : sizeof addrmask;
+       copysize = 1 << indx < MAX_COPY ? 1 << indx : MAX_COPY;
 #endif
        if (kbp->kb_next == NULL) {
 #endif
        if (kbp->kb_next == NULL) {
+               kbp->kb_last = NULL;
                if (size > MAXALLOCSAVE)
                        allocsize = roundup(size, CLBYTES);
                else
                if (size > MAXALLOCSAVE)
                        allocsize = roundup(size, CLBYTES);
                else
@@ -142,7 +153,6 @@ malloc(size, type, flags)
                 */
                savedlist = kbp->kb_next;
                rp = kbp->kb_next; /* returned while blocked in vmemall */
                 */
                savedlist = kbp->kb_next;
                rp = kbp->kb_next; /* returned while blocked in vmemall */
-               kbp->kb_next = va + (npg * NBPG) - allocsize;
                for (cp = kbp->kb_next; cp >= va; cp -= allocsize) {
                        ((caddr_t *)cp)[2] = (cp > va ? cp - allocsize : rp);
                        if (indx == 7) {
                for (cp = kbp->kb_next; cp >= va; cp -= allocsize) {
                        ((caddr_t *)cp)[2] = (cp > va ? cp - allocsize : rp);
                        if (indx == 7) {
@@ -176,6 +186,14 @@ out:
 #else
 out:
 #endif
 #else
 out:
 #endif
+       if (size > 64 && size <= 128) {
+               mlp = (struct uselist *)malloc(sizeof(*mlp), M_TEMP, M_WAITOK);
+               mlp->type = type;
+               mlp->size = size;
+               mlp->mem = va;
+               mlp->next = listhd;
+               listhd = mlp;
+       }
        OUT;
        splx(s);
        return ((void *) va);
        OUT;
        splx(s);
        return ((void *) va);
@@ -205,7 +223,7 @@ free(addr, type)
        int s;
 #ifdef DIAGNOSTIC
        caddr_t cp;
        int s;
 #ifdef DIAGNOSTIC
        caddr_t cp;
-       long alloc, copysize;
+       long *end, *lp, alloc, copysize;
 #endif
 #ifdef KMEMSTATS
        register struct kmemstats *ksp = &kmemstats[type];
 #endif
 #ifdef KMEMSTATS
        register struct kmemstats *ksp = &kmemstats[type];
@@ -227,6 +245,25 @@ free(addr, type)
        size = 1 << kup->ku_indx;
        kbp = &bucket[kup->ku_indx];
        s = splimp();
        size = 1 << kup->ku_indx;
        kbp = &bucket[kup->ku_indx];
        s = splimp();
+       if (size == 128) {
+               struct uselist *mlp, *pmlp;
+
+               mlp = listhd;
+               if (mlp->mem == addr)
+                       listhd = mlp->next;
+               else for (pmlp = mlp, mlp = mlp->next ; mlp; mlp = mlp->next) {
+                       if (mlp->mem == addr) {
+                               pmlp->next = mlp->next;
+                               break;
+                       }
+                       pmlp = mlp;
+               }
+               if (mlp == NULL)
+                       printf("free: lost type %s size %d\n", memname[type],
+                           size);
+               else
+                       free(mlp, M_TEMP);
+       }
 #ifdef DIAGNOSTIC
        /*
         * Check for returns of data that do not point to the
 #ifdef DIAGNOSTIC
        /*
         * Check for returns of data that do not point to the
@@ -238,11 +275,9 @@ free(addr, type)
                alloc = addrmask[BUCKETINDX(NBPG * CLSIZE)];
        else
                alloc = addrmask[kup->ku_indx];
                alloc = addrmask[BUCKETINDX(NBPG * CLSIZE)];
        else
                alloc = addrmask[kup->ku_indx];
-       if (((u_long)addr & alloc) != 0) {
-               printf("free: unaligned addr 0x%x, size %d, type %d, mask %d\n",
-                       addr, size, type, alloc);
-               panic("free: unaligned addr");
-       }
+       if (((u_long)addr & alloc) != 0)
+               panic("free: unaligned addr 0x%x, size %d, type %s, mask %d\n",
+                       addr, size, memname[type], alloc);
 #endif /* DIAGNOSTIC */
        IN;
        if (size > MAXALLOCSAVE) {
 #endif /* DIAGNOSTIC */
        IN;
        if (size > MAXALLOCSAVE) {
@@ -267,18 +302,12 @@ free(addr, type)
         * Check for multiple frees. Use a quick check to see if
         * it looks free before laboriously searching the freelist.
         */
         * Check for multiple frees. Use a quick check to see if
         * it looks free before laboriously searching the freelist.
         */
-       copysize = size < sizeof addrmask ? size : sizeof addrmask;
        if (freep->spare0 == WEIRD_ADDR) {
        if (freep->spare0 == WEIRD_ADDR) {
-               freep->type = ((struct freelist *)addrmask)->type;
-               freep->next = ((struct freelist *)addrmask)->next;
-               if (!bcmp(addrmask, addr, copysize)) {
-                       for (cp = kbp->kb_next; cp; cp = *(caddr_t *)cp) {
-                               if (addr == cp) {
-                                       printf("multiply freed item 0x%x\n",
-                                           addr);
-                                       panic("free: duplicated free");
-                               }
-                       }
+               for (cp = kbp->kb_next; cp; cp = *(caddr_t *)cp) {
+                       if (addr != cp)
+                               continue;
+                       printf("multiply freed item 0x%x\n", addr);
+                       panic("free: duplicated free");
                }
        }
        /*
                }
        }
        /*
@@ -287,7 +316,10 @@ free(addr, type)
         * so we can list likely culprit if modification is detected
         * when the object is reallocated.
         */
         * so we can list likely culprit if modification is detected
         * when the object is reallocated.
         */
-       bcopy(addrmask, addr, copysize);
+       copysize = size < MAX_COPY ? size : MAX_COPY;
+       end = (long *)&((caddr_t)addr)[copysize];
+       for (lp = (long *)addr; lp < end; lp++)
+               *lp = WEIRD_ADDR;
        freep->type = type;
 #endif /* DIAGNOSTIC */
        if (size == 128) {
        freep->type = type;
 #endif /* DIAGNOSTIC */
        if (size == 128) {
@@ -308,8 +340,12 @@ free(addr, type)
                wakeup((caddr_t)ksp);
        ksp->ks_inuse--;
 #endif
                wakeup((caddr_t)ksp);
        ksp->ks_inuse--;
 #endif
-       ((caddr_t *)addr)[2] = kbp->kb_next;
-       kbp->kb_next = addr;
+       if (kbp->kb_next == NULL)
+               kbp->kb_next = addr;
+       else
+               ((struct freelist *)kbp->kb_last)->next = addr;
+       freep->next = NULL;
+       kbp->kb_last = addr;
        OUT;
        splx(s);
 }
        OUT;
        splx(s);
 }
@@ -334,8 +370,8 @@ kmeminit()
        npg = VM_KMEM_SIZE/ NBPG;
        kmemusage = (struct kmemusage *) kmem_alloc(kernel_map,
                (vm_size_t)(npg * sizeof(struct kmemusage)));
        npg = VM_KMEM_SIZE/ NBPG;
        kmemusage = (struct kmemusage *) kmem_alloc(kernel_map,
                (vm_size_t)(npg * sizeof(struct kmemusage)));
-       kmem_map = kmem_suballoc(kernel_map, (vm_offset_t)&kmembase,
-               (vm_offset_t)&kmemlimit, (vm_size_t)(npg * NBPG), FALSE);
+       kmem_map = kmem_suballoc(kernel_map, (vm_offset_t *)&kmembase,
+               (vm_offset_t *)&kmemlimit, (vm_size_t)(npg * NBPG), FALSE);
 #ifdef KMEMSTATS
        for (indx = 0; indx < MINBUCKET + 16; indx++) {
                if (1 << indx >= CLBYTES)
 #ifdef KMEMSTATS
        for (indx = 0; indx < MINBUCKET + 16; indx++) {
                if (1 << indx >= CLBYTES)