*/
#ifndef lint
-static char sccsid[] = "@(#)alloc.c 5.7 (Berkeley) %G%";
+static char sccsid[] = "@(#)alloc.c 5.11 (Berkeley) %G%";
#endif /* not lint */
/*
* 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 "extern.h"
char *memtop = NULL; /* PWP: top of current memory */
char *membot = NULL; /* PWP: bottom of allocatable memory */
#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
* 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
-#endif
#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
- 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
#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
#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.
*/
-static U_int nmalloc[NBUCKETS];
+static u_int nmalloc[NBUCKETS];
#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) { \
- 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
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 */
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
*/
static void
morecore(bucket)
- register bucket;
+ register int bucket;
{
register union overhead *op;
register int rnu; /* 2^rnu bytes will be requested */
/* 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;
* 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--;
}
/*
op->ov_next = (union overhead *) (((caddr_t) op) + siz);
op = (union overhead *) (((caddr_t) op) + siz);
}
+ op->ov_next = NULL;
}
#endif
-#ifdef sun
-int
-#else
void
-#endif
free(cp)
ptr_t cp;
{
#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);
size_t nbytes;
{
#ifndef lint
- register U_int onb;
+ register u_int onb;
union overhead *op;
char *res;
register int i;
* 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;
- 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++);
- xprintf(" %4d", j);
+ (void) fprintf(cshout, " %4d", j);
totfree += j * (1 << (i + 3));
}
- xprintf("\nused:\t");
+ (void) fprintf(cshout, "\nused:\t");
for (i = 0; i < NBUCKETS; i++) {
- xprintf(" %4d", nmalloc[i]);
+ (void) fprintf(cshout, "%4d", nmalloc[i]);
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);
- 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
- 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 */
}