new naming convention
[unix-history] / usr / src / sys / kern / kern_malloc.c
index f13544e..1a9bed6 100644 (file)
@@ -3,13 +3,18 @@
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms are permitted
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms are permitted
- * provided that this notice is preserved and that due credit is given
- * to the University of California at Berkeley. The name of the University
- * may not be used to endorse or promote products derived from this
- * software without specific prior written permission. This software
- * is provided ``as is'' without express or implied warranty.
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  *
  *
- *     @(#)kern_malloc.c       7.6 (Berkeley) %G%
+ *     @(#)kern_malloc.c       7.12 (Berkeley) %G%
  */
 
 #include "param.h"
  */
 
 #include "param.h"
@@ -21,7 +26,7 @@
 #include "kernel.h"
 #include "malloc.h"
 
 #include "kernel.h"
 #include "malloc.h"
 
-#include "../machine/pte.h"
+#include "machine/pte.h"
 
 struct kmembuckets bucket[MINBUCKET + 16];
 struct kmemstats kmemstats[M_LAST];
 
 struct kmembuckets bucket[MINBUCKET + 16];
 struct kmemstats kmemstats[M_LAST];
@@ -31,16 +36,22 @@ long wantkmemmap;
 /*
  * Allocate a block of memory
  */
 /*
  * Allocate a block of memory
  */
-qaddr_t malloc(size, type, flags)
+qaddr_t
+malloc(size, type, flags)
        unsigned long size;
        unsigned long size;
-       long type, flags;
+       int type, flags;
 {
        register struct kmembuckets *kbp;
        register struct kmemusage *kup;
 {
        register struct kmembuckets *kbp;
        register struct kmemusage *kup;
-       long indx, npg, alloc, allocsize, s;
+       long indx, npg, alloc, allocsize;
+       int s;
        caddr_t va, cp;
 #ifdef KMEMSTATS
        register struct kmemstats *ksp = &kmemstats[type];
        caddr_t va, cp;
 #ifdef KMEMSTATS
        register struct kmemstats *ksp = &kmemstats[type];
+#if defined(ISO) || defined(TPIP)
+       if (((unsigned int)type) > M_LAST)
+               panic("malloc - bogus type");
+#endif
 #endif
 
        indx = BUCKETINDX(size);
 #endif
 
        indx = BUCKETINDX(size);
@@ -48,7 +59,7 @@ qaddr_t malloc(size, type, flags)
        s = splimp();
 again:
 #ifdef KMEMSTATS
        s = splimp();
 again:
 #ifdef KMEMSTATS
-       while (ksp->ks_inuse >= ksp->ks_limit) {
+       while (ksp->ks_memuse >= ksp->ks_limit) {
                if (flags & M_NOWAIT) {
                        splx(s);
                        return (0);
                if (flags & M_NOWAIT) {
                        splx(s);
                        return (0);
@@ -83,9 +94,9 @@ again:
                        goto again;
                }
                alloc -= CLSIZE;                /* convert to base 0 */
                        goto again;
                }
                alloc -= CLSIZE;                /* convert to base 0 */
-               (void) vmemall(&kmempt[alloc], npg, &proc[0], CSYS);
+               (void) vmemall(&kmempt[alloc], (int)npg, &proc[0], CSYS);
                va = (caddr_t) kmemxtob(alloc);
                va = (caddr_t) kmemxtob(alloc);
-               vmaccess(&kmempt[alloc], va, npg);
+               vmaccess(&kmempt[alloc], va, (int)npg);
 #ifdef KMEMSTATS
                kbp->kb_total += kbp->kb_elmpercl;
 #endif
 #ifdef KMEMSTATS
                kbp->kb_total += kbp->kb_elmpercl;
 #endif
@@ -124,8 +135,8 @@ out:
        kbp->kb_calls++;
        ksp->ks_inuse++;
        ksp->ks_calls++;
        kbp->kb_calls++;
        ksp->ks_inuse++;
        ksp->ks_calls++;
-       if (ksp->ks_inuse > ksp->ks_maxused)
-               ksp->ks_maxused = ksp->ks_inuse;
+       if (ksp->ks_memuse > ksp->ks_maxused)
+               ksp->ks_maxused = ksp->ks_memuse;
 #else
 out:
 #endif
 #else
 out:
 #endif
@@ -136,39 +147,45 @@ out:
 /*
  * Free a block of memory allocated by malloc.
  */
 /*
  * Free a block of memory allocated by malloc.
  */
-void free(addr, type)
+void
+free(addr, type)
        caddr_t addr;
        caddr_t addr;
-       long type;
+       int type;
 {
        register struct kmembuckets *kbp;
        register struct kmemusage *kup;
 {
        register struct kmembuckets *kbp;
        register struct kmemusage *kup;
-       long alloc, s;
+       long alloc, size;
+       int s;
 #ifdef KMEMSTATS
        register struct kmemstats *ksp = &kmemstats[type];
 #endif
 
        kup = btokup(addr);
 #ifdef KMEMSTATS
        register struct kmemstats *ksp = &kmemstats[type];
 #endif
 
        kup = btokup(addr);
+       kbp = &bucket[kup->ku_indx];
        s = splimp();
        s = splimp();
-       if (1 << kup->ku_indx > MAXALLOCSAVE) {
+       size = 1 << kup->ku_indx;
+       if (size > MAXALLOCSAVE) {
                alloc = btokmemx(addr);
                alloc = btokmemx(addr);
-               (void) memfree(&kmempt[alloc], kup->ku_pagecnt, 0);
+               (void) memfree(&kmempt[alloc], (int)kup->ku_pagecnt, 0);
                rmfree(kmemmap, (long)kup->ku_pagecnt, alloc + CLSIZE);
                if (wantkmemmap) {
                        wakeup((caddr_t)&wantkmemmap);
                        wantkmemmap = 0;
                }
 #ifdef KMEMSTATS
                rmfree(kmemmap, (long)kup->ku_pagecnt, alloc + CLSIZE);
                if (wantkmemmap) {
                        wakeup((caddr_t)&wantkmemmap);
                        wantkmemmap = 0;
                }
 #ifdef KMEMSTATS
-               ksp->ks_memuse -= kup->ku_pagecnt << PGSHIFT;
+               size = kup->ku_pagecnt << PGSHIFT;
+               ksp->ks_memuse -= size;
                kup->ku_indx = 0;
                kup->ku_pagecnt = 0;
                kup->ku_indx = 0;
                kup->ku_pagecnt = 0;
-               if (ksp->ks_inuse == ksp->ks_limit)
+               if (ksp->ks_memuse + size >= ksp->ks_limit &&
+                   ksp->ks_memuse < ksp->ks_limit)
                        wakeup((caddr_t)ksp);
                ksp->ks_inuse--;
                        wakeup((caddr_t)ksp);
                ksp->ks_inuse--;
+               kbp->kb_total -= 1;
 #endif
                splx(s);
                return;
        }
 #endif
                splx(s);
                return;
        }
-       kbp = &bucket[kup->ku_indx];
 #ifdef KMEMSTATS
        kup->ku_freecnt++;
        if (kup->ku_freecnt >= kbp->kb_elmpercl)
 #ifdef KMEMSTATS
        kup->ku_freecnt++;
        if (kup->ku_freecnt >= kbp->kb_elmpercl)
@@ -177,10 +194,11 @@ void free(addr, type)
                else if (kbp->kb_totalfree > kbp->kb_highwat)
                        kbp->kb_couldfree++;
        kbp->kb_totalfree++;
                else if (kbp->kb_totalfree > kbp->kb_highwat)
                        kbp->kb_couldfree++;
        kbp->kb_totalfree++;
-       if (ksp->ks_inuse == ksp->ks_limit)
+       ksp->ks_memuse -= size;
+       if (ksp->ks_memuse + size >= ksp->ks_limit &&
+           ksp->ks_memuse < ksp->ks_limit)
                wakeup((caddr_t)ksp);
        ksp->ks_inuse--;
                wakeup((caddr_t)ksp);
        ksp->ks_inuse--;
-       ksp->ks_memuse -= 1 << kup->ku_indx;
 #endif
        *(caddr_t *)addr = kbp->kb_next;
        kbp->kb_next = addr;
 #endif
        *(caddr_t *)addr = kbp->kb_next;
        kbp->kb_next = addr;
@@ -193,15 +211,19 @@ void free(addr, type)
 kmeminit()
 {
        register long indx;
 kmeminit()
 {
        register long indx;
+       int npg;
 
 
-       if (!powerof2(MAXALLOCSAVE))
-               panic("kmeminit: MAXALLOCSAVE not power of 2");
-       if (MAXALLOCSAVE > MINALLOCSIZE * 32768)
-               panic("kmeminit: MAXALLOCSAVE too big");
-       if (MAXALLOCSAVE < CLBYTES)
-               panic("kmeminit: MAXALLOCSAVE too small");
-       rminit(kmemmap, ekmempt - kmempt, (long)CLSIZE,
-               "malloc map", ekmempt - kmempt);
+#if    ((MAXALLOCSAVE & (MAXALLOCSAVE - 1)) != 0)
+               ERROR!_kmeminit:_MAXALLOCSAVE_not_power_of_2
+#endif
+#if    (MAXALLOCSAVE > MINALLOCSIZE * 32768)
+               ERROR!_kmeminit:_MAXALLOCSAVE_too_big
+#endif
+#if    (MAXALLOCSAVE < CLBYTES)
+               ERROR!_kmeminit:_MAXALLOCSAVE_too_small
+#endif
+       npg = ekmempt - kmempt;
+       rminit(kmemmap, (long)npg, (long)CLSIZE, "malloc map", npg);
 #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)
@@ -211,7 +233,6 @@ kmeminit()
                bucket[indx].kb_highwat = 5 * bucket[indx].kb_elmpercl;
        }
        for (indx = 0; indx < M_LAST; indx++)
                bucket[indx].kb_highwat = 5 * bucket[indx].kb_elmpercl;
        }
        for (indx = 0; indx < M_LAST; indx++)
-               kmemstats[indx].ks_limit =
-                       (ekmempt - kmempt) * CLBYTES * 9 / 10;
+               kmemstats[indx].ks_limit = npg * CLBYTES * 8 / 10;
 #endif
 }
 #endif
 }