Minor fixes
[unix-history] / usr / src / bin / csh / alloc.c
index b967345..de6ec89 100644 (file)
@@ -6,7 +6,7 @@
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)alloc.c    5.7 (Berkeley) %G%";
+static char sccsid[] = "@(#)alloc.c    5.11 (Berkeley) %G%";
 #endif /* not lint */
 
 /*
 #endif /* not lint */
 
 /*
@@ -21,7 +21,17 @@ static char sccsid[] = "@(#)alloc.c  5.7 (Berkeley) %G%";
  * but bombs when it runs out.
  */
 
  * but bombs when it runs out.
  */
 
+#include <sys/types.h>
+#include <unistd.h>
+#include <string.h>
+#if __STDC__
+# include <stdarg.h>
+#else
+# include <varargs.h>
+#endif
+
 #include "csh.h"
 #include "csh.h"
+#include "extern.h"
 
 char   *memtop = NULL;         /* PWP: top of current memory */
 char   *membot = NULL;         /* PWP: bottom of allocatable memory */
 
 char   *memtop = NULL;         /* PWP: top of current memory */
 char   *membot = NULL;         /* PWP: bottom of allocatable memory */
@@ -36,11 +46,6 @@ char   *membot = NULL;               /* PWP: bottom of allocatable memory */
 #define        NULL 0
 #endif
 
 #define        NULL 0
 #endif
 
-typedef unsigned char U_char;  /* we don't really have signed chars */
-typedef unsigned int U_int;
-typedef unsigned short U_short;
-static int findbucket();
-static void morecore();
 
 /*
  * The overhead on a block is at least 4 bytes.  When free, this space
 
 /*
  * The overhead on a block is at least 4 bytes.  When free, this space
@@ -52,26 +57,18 @@ static void morecore();
  * plus the range checking words, and the header word MINUS ONE.
  */
 
  * plus the range checking words, and the header word MINUS ONE.
  */
 
-#ifdef SUNOS4
-/*
- * SunOS localtime() overwrites the 9th byte on an 8 byte malloc()....
- * So we align to 16 bytes...
- */
-#define ROUNDUP        15
-#else
 #define ROUNDUP        7
 #define ROUNDUP        7
-#endif
 
 #define ALIGN(a) (((a) + ROUNDUP) & ~ROUNDUP)
 
 union overhead {
     union overhead *ov_next;   /* when free */
     struct {
 
 #define ALIGN(a) (((a) + ROUNDUP) & ~ROUNDUP)
 
 union overhead {
     union overhead *ov_next;   /* when free */
     struct {
-       U_char  ovu_magic;      /* magic number */
-       U_char  ovu_index;      /* bucket # */
+       u_char  ovu_magic;      /* magic number */
+       u_char  ovu_index;      /* bucket # */
 #ifdef RCHECK
 #ifdef RCHECK
-       U_short ovu_size;       /* actual block size */
-       U_int   ovu_rmagic;     /* range magic number */
+       u_short ovu_size;       /* actual block size */
+       u_int   ovu_rmagic;     /* range magic number */
 #endif
     }       ovu;
 #define        ov_magic        ovu.ovu_magic
 #endif
     }       ovu;
 #define        ov_magic        ovu.ovu_magic
@@ -83,7 +80,7 @@ union overhead {
 #define        MAGIC           0xfd    /* magic # on accounting info */
 #define RMAGIC         0x55555555      /* magic # on range info */
 #ifdef RCHECK
 #define        MAGIC           0xfd    /* magic # on accounting info */
 #define RMAGIC         0x55555555      /* magic # on range info */
 #ifdef RCHECK
-#define        RSLOP           sizeof (U_int)
+#define        RSLOP           sizeof (u_int)
 #else
 #define        RSLOP           0
 #endif
 #else
 #define        RSLOP           0
 #endif
@@ -96,31 +93,29 @@ union overhead {
 #define        NBUCKETS 30
 static union overhead *nextf[NBUCKETS];
 
 #define        NBUCKETS 30
 static union overhead *nextf[NBUCKETS];
 
-#ifdef notdef
-extern char *sbrk();
-
-#endif
+static int     findbucket __P((union overhead *, int));
+static void    morecore __P((int));
 
 /*
  * nmalloc[i] is the difference between the number of mallocs and frees
  * for a given block size.
  */
 
 /*
  * nmalloc[i] is the difference between the number of mallocs and frees
  * for a given block size.
  */
-static U_int nmalloc[NBUCKETS];
+static u_int nmalloc[NBUCKETS];
 
 
 #ifdef DEBUG
 #define CHECK(a, str, p) \
     if (a) { \
 
 
 #ifdef DEBUG
 #define CHECK(a, str, p) \
     if (a) { \
-       xprintf(str, p);        \
-       xprintf("memtop = %lx membot = %lx.\n", memtop, membot);        \
+       (void) fprintfcsherr, (str, p); \
+       (void) fprintf(csherr, "memtop = %lx membot = %lx.\n", memtop, membot);\
        abort(); \
     }  \
     else
 #else
 #define CHECK(a, str, p) \
     if (a) { \
        abort(); \
     }  \
     else
 #else
 #define CHECK(a, str, p) \
     if (a) { \
-       xprintf(str, p);        \
-       xprintf("memtop = %lx membot = %lx.\n", memtop, membot);        \
+       (void) fprintf(csherr, str, p); \
+       (void) fprintf(csherr, "memtop = %lx membot = %lx.\n", memtop, membot);\
        return; \
     }  \
     else
        return; \
     }  \
     else
@@ -158,7 +153,7 @@ malloc(nbytes)
        stderror(ERR_NOMEM);
 #else
        showall();
        stderror(ERR_NOMEM);
 #else
        showall();
-       xprintf("nbytes=%d: Out of memory\n", nbytes);
+       (void) fprintf(csherr, "nbytes=%d: Out of memory\n", nbytes);
        abort();
 #endif
        /* fool lint */
        abort();
 #endif
        /* fool lint */
@@ -176,7 +171,7 @@ malloc(nbytes)
     if (nbytes <= 0x10000)
        p->ov_size = nbytes - 1;
     p->ov_rmagic = RMAGIC;
     if (nbytes <= 0x10000)
        p->ov_size = nbytes - 1;
     p->ov_rmagic = RMAGIC;
-    *((U_int *) (((caddr_t) p) + nbytes - RSLOP)) = RMAGIC;
+    *((u_int *) (((caddr_t) p) + nbytes - RSLOP)) = RMAGIC;
 #endif
     return ((ptr_t) (((caddr_t) p) + ALIGN(sizeof(union overhead))));
 #else
 #endif
     return ((ptr_t) (((caddr_t) p) + ALIGN(sizeof(union overhead))));
 #else
@@ -193,7 +188,7 @@ malloc(nbytes)
  */
 static void
 morecore(bucket)
  */
 static void
 morecore(bucket)
-    register bucket;
+    register int bucket;
 {
     register union overhead *op;
     register int rnu;          /* 2^rnu bytes will be requested */
 {
     register union overhead *op;
     register int rnu;          /* 2^rnu bytes will be requested */
@@ -218,8 +213,6 @@ morecore(bucket)
     /* take 2k unless the block is bigger than that */
     rnu = (bucket <= 8) ? 11 : bucket + 3;
     nblks = 1 << (rnu - (bucket + 3)); /* how many blocks to get */
     /* take 2k unless the block is bigger than that */
     rnu = (bucket <= 8) ? 11 : bucket + 3;
     nblks = 1 << (rnu - (bucket + 3)); /* how many blocks to get */
-    if (rnu < bucket)
-       rnu = bucket;
     memtop = (char *) sbrk(1 << rnu);  /* PWP */
     op = (union overhead *) memtop;
     memtop += 1 << rnu;
     memtop = (char *) sbrk(1 << rnu);  /* PWP */
     op = (union overhead *) memtop;
     memtop += 1 << rnu;
@@ -230,8 +223,8 @@ morecore(bucket)
      * Round up to minimum allocation size boundary and deduct from block count
      * to reflect.
      */
      * Round up to minimum allocation size boundary and deduct from block count
      * to reflect.
      */
-    if (((U_int) op) & ROUNDUP) {
-       op = (union overhead *) (((U_int) op + (ROUNDUP + 1)) & ~ROUNDUP);
+    if (((u_int) op) & ROUNDUP) {
+       op = (union overhead *) (((u_int) op + (ROUNDUP + 1)) & ~ROUNDUP);
        nblks--;
     }
     /*
        nblks--;
     }
     /*
@@ -243,15 +236,12 @@ morecore(bucket)
        op->ov_next = (union overhead *) (((caddr_t) op) + siz);
        op = (union overhead *) (((caddr_t) op) + siz);
     }
        op->ov_next = (union overhead *) (((caddr_t) op) + siz);
        op = (union overhead *) (((caddr_t) op) + siz);
     }
+    op->ov_next = NULL;
 }
 
 #endif
 
 }
 
 #endif
 
-#ifdef sun
-int
-#else
 void
 void
-#endif
 free(cp)
     ptr_t   cp;
 {
 free(cp)
     ptr_t   cp;
 {
@@ -269,7 +259,7 @@ free(cp)
 
 #ifdef RCHECK
     if (op->ov_index <= 13)
 
 #ifdef RCHECK
     if (op->ov_index <= 13)
-       CHECK(*(U_int *) ((caddr_t) op + op->ov_size + 1 - RSLOP) != RMAGIC,
+       CHECK(*(u_int *) ((caddr_t) op + op->ov_size + 1 - RSLOP) != RMAGIC,
              "free(%lx) bad range check.", cp);
 #endif
     CHECK(op->ov_index >= NBUCKETS, "free(%lx) bad block index.", cp);
              "free(%lx) bad range check.", cp);
 #endif
     CHECK(op->ov_index >= NBUCKETS, "free(%lx) bad block index.", cp);
@@ -330,7 +320,7 @@ realloc(cp, nbytes)
     size_t  nbytes;
 {
 #ifndef lint
     size_t  nbytes;
 {
 #ifndef lint
-    register U_int onb;
+    register u_int onb;
     union overhead *op;
     char   *res;
     register int i;
     union overhead *op;
     char   *res;
     register int i;
@@ -492,30 +482,34 @@ Free(p)
  * frees for each size category.
  */
 void
  * frees for each size category.
  */
 void
-showall()
+/*ARGSUSED*/
+showall(v, t)
+    Char **v;
+    struct command *t;
 {
 #ifndef SYSMALLOC
     register int i, j;
     register union overhead *p;
     int     totfree = 0, totused = 0;
 
 {
 #ifndef SYSMALLOC
     register int i, j;
     register union overhead *p;
     int     totfree = 0, totused = 0;
 
-    xprintf("csh current memory allocation:\nfree:\t");
+    (void) fprintf(cshout, "csh current memory allocation:\nfree:\t");
     for (i = 0; i < NBUCKETS; i++) {
        for (j = 0, p = nextf[i]; p; p = p->ov_next, j++);
     for (i = 0; i < NBUCKETS; i++) {
        for (j = 0, p = nextf[i]; p; p = p->ov_next, j++);
-       xprintf(" %4d", j);
+       (void) fprintf(cshout, " %4d", j);
        totfree += j * (1 << (i + 3));
     }
        totfree += j * (1 << (i + 3));
     }
-    xprintf("\nused:\t");
+    (void) fprintf(cshout, "\nused:\t");
     for (i = 0; i < NBUCKETS; i++) {
     for (i = 0; i < NBUCKETS; i++) {
-       xprintf(" %4d", nmalloc[i]);
+       (void) fprintf(cshout, "%4d", nmalloc[i]);
        totused += nmalloc[i] * (1 << (i + 3));
     }
        totused += nmalloc[i] * (1 << (i + 3));
     }
-    xprintf("\n\tTotal in use: %d, total free: %d\n",
+    (void) fprintf(cshout, "\n\tTotal in use: %d, total free: %d\n",
            totused, totfree);
            totused, totfree);
-    xprintf("\tAllocated memory from 0x%lx to 0x%lx.  Real top at 0x%lx\n",
+    (void) fprintf(cshout, 
+           "\tAllocated memory from 0x%lx to 0x%lx.  Real top at 0x%lx\n",
            membot, memtop, (char *) sbrk(0));
 #else
            membot, memtop, (char *) sbrk(0));
 #else
-    xprintf("Allocated memory from 0x%lx to 0x%lx (%ld).\n",
+    (void) fprintf(cshout, "Allocated memory from 0x%lx to 0x%lx (%ld).\n",
            membot, memtop = (char *) sbrk(0), memtop - membot);
 #endif                         /* SYSMALLOC */
 }
            membot, memtop = (char *) sbrk(0), memtop - membot);
 #endif                         /* SYSMALLOC */
 }