--- ray/src/common/malloc.c 1992/09/04 09:57:43 2.6 +++ ray/src/common/malloc.c 2003/04/23 00:52:33 2.14 @@ -1,9 +1,6 @@ -/* Copyright (c) 1992 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: malloc.c,v 2.14 2003/04/23 00:52:33 greg Exp $"; #endif - /* * Fast malloc for memory hogs and VM environments. * Performs a minimum of searching through free lists. @@ -22,9 +19,14 @@ static char SCCSid[] = "$SunId$ LBL"; * Greg Ward Lawrence Berkeley Laboratory */ +#include "copyright.h" + #include -extern int errno; +#ifndef BSD +#define bcopy(s,d,n) (void)memcpy(d,s,n) +#define bzero(d,n) (void)memset(d,0,n) +#endif #ifdef MSTATS #include @@ -40,10 +42,10 @@ static unsigned m_nwasted = 0; #define NULL 0 #endif -#ifndef ALIGN -#define ALIGN int /* align type */ +#ifndef ALIGNT +#define ALIGNT int /* align type */ #endif -#define BYTES_WORD sizeof(ALIGN) +#define BYTES_WORD sizeof(ALIGNT) #ifndef MAXINCR #define MAXINCR (1<<16) /* largest sbrk(2) increment */ @@ -55,7 +57,7 @@ typedef union m_head { short magic; short bucket; } a; - ALIGN dummy; + ALIGNT dummy; } M_HEAD; #define MAGIC 0x1a2 /* magic number for allocated memory */ @@ -65,7 +67,7 @@ typedef union m_head { static M_HEAD *free_list[NBUCKETS]; -static ALIGN dummy_mem; +static ALIGNT dummy_mem; static char *memlim[2]; @@ -156,7 +158,8 @@ unsigned *np; for ( ; ; ) { /* compact free lists */ - compactfree(); + while (compactfree()) + ; /* find largest block */ tab = mtab(&cptab); tablen = mtablen(&cptab); big = tab; @@ -169,7 +172,7 @@ unsigned *np; big->siz = 0; /* remove from table */ return(big->ptr); /* return it */ } - if (mtablen(big) < tablen+1) { + if (mtablen(big) <= tablen) { *np = 0; /* cannot grow table */ return(NULL); /* report failure */ } @@ -179,17 +182,10 @@ unsigned *np; cptab.ptr = big->ptr; cptab.siz = big->siz; big->siz = 0; /* clear and copy */ -#ifdef BSD bcopy((char *)tab, (char *)(mtab(&cptab)+1), tablen*sizeof(struct mblk)); bzero((char *)(mtab(&cptab)+tablen+1), (mtablen(&cptab)-tablen-1)*sizeof(struct mblk)); -#else - (void)memcpy((char *)(mtab(&cptab)+1), (char *)tab, - tablen*sizeof(struct mblk)); - memset((char *)(mtab(&cptab)+tablen+1), 0, - (mtablen(&cptab)-tablen-1)*sizeof(struct mblk)); -#endif } /* next round */ } #endif /* MCOMP */ @@ -254,6 +250,7 @@ register unsigned n; n = (n+(BYTES_WORD-1))&~(BYTES_WORD-1); /* word align rqst. */ if (n > nrem) { /* need more core */ + tryagain: if (n > amnt) { /* big chunk */ thisamnt = (n+(pagesz-1))&~(pagesz-1); if (thisamnt <= MAXINCR) /* increase amnt */ @@ -262,10 +259,17 @@ register unsigned n; thisamnt = amnt; p = sbrk(thisamnt); if ((int)p == -1) { /* uh-oh, ENOMEM */ - thisamnt = n; /* search free lists */ - p = mscrounge(&thisamnt); - if (p == NULL) /* we're really out */ + errno = 0; /* call cavalry */ + if (thisamnt >= n+pagesz) { + amnt = pagesz; /* minimize request */ + goto tryagain; + } + thisamnt = n; + p = mscrounge(&thisamnt); /* search free lists */ + if (p == NULL) { /* we're really out */ + errno = ENOMEM; return(NULL); + } } #ifdef MSTATS else b_nsbrked += thisamnt; @@ -298,7 +302,7 @@ register unsigned n; register int bucket; register unsigned bsiz; - if (n < 1<on ? on : n); -#else - (void)memcpy(p, op, n>on ? on : n); -#endif free(op); } return(p);