--- ray/src/common/malloc.c 1990/10/03 21:01:59 1.9 +++ ray/src/common/malloc.c 1991/04/02 09:36:21 1.12 @@ -1,4 +1,4 @@ -/* Copyright (c) 1990 Regents of the University of California */ +/* Copyright (c) 1991 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -23,7 +23,6 @@ static char SCCSid[] = "$SunId$ LBL"; #ifdef MSTATS #include - static unsigned b_nalloced = 0; static unsigned b_nfreed = 0; static unsigned b_nscrounged = 0; @@ -42,21 +41,30 @@ static unsigned m_nwasted = 0; #define MAXINCR (1<<16) /* largest sbrk(2) increment */ #ifdef NOVMEM -#define getpagesize() BYTES_WORD +#define getpagesize() 1024 #endif /* malloc free lists */ typedef union m_head { union m_head *next; - int bucket; + struct { + short magic; + short bucket; + } a; ALIGN dummy; } M_HEAD; +#define MAGIC 0x1a2 /* magic number for allocated memory */ + #define FIRSTBUCKET 3 #define NBUCKETS 30 static M_HEAD *free_list[NBUCKETS]; +static ALIGN dummy_mem; +#define DUMMYLOC ((char *)&dummy_mem) + + char * mscrounge(np) /* search free lists to satisfy request */ register unsigned *np; @@ -171,6 +179,9 @@ unsigned n; register M_HEAD *mp; register int bucket; register unsigned bsiz; + /* don't return NULL on 0 request */ + if (n == 0) + return(DUMMYLOC); /* find first bucket that fits */ for (bucket = FIRSTBUCKET, bsiz = 1<next; } - mp->bucket = bucket; /* tag block */ + mp->a.magic = MAGIC; /* tag block */ + mp->a.bucket = bucket; return((char *)(mp+1)); } @@ -205,8 +217,8 @@ unsigned n; char *p; register unsigned on; /* get old size */ - if (op != NULL) - on = 1 << ((M_HEAD *)op-1)->bucket; + if (op != NULL && op != DUMMYLOC && ((M_HEAD *)op-1)->a.magic == MAGIC) + on = 1 << ((M_HEAD *)op-1)->a.bucket; else on = 0; if (n <= on && (n > on>>1 || on == 1<bucket; + if (mp->a.magic != MAGIC) /* sanity check */ + return; + bucket = mp->a.bucket; mp->next = free_list[bucket]; free_list[bucket] = mp; #ifdef MSTATS