--- ray/src/common/bmalloc.c 1990/09/25 19:37:15 1.2 +++ ray/src/common/bmalloc.c 1991/05/06 16:44:10 1.3 @@ -1,13 +1,15 @@ -/* 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"; #endif /* - * Basic memory allocation w/o overhead. - * Calls malloc(3) for big requests. - * + * Bmalloc provides basic memory allocation without overhead (no free lists). + * Use only to take the load off of malloc for all those + * piddling little requests that you never expect to free. + * Bmalloc defers to malloc for big requests. + * Bfree should hand memory to bmalloc, but it usually fails here. */ #define NULL 0 @@ -15,42 +17,60 @@ static char SCCSid[] = "$SunId$ LBL"; #ifndef MBLKSIZ #define MBLKSIZ 16376 /* size of memory allocation block */ #endif -#define WASTEFRAC 4 /* don't waste more than this */ +#define WASTEFRAC 12 /* don't waste more than a fraction */ #ifndef ALIGN #define ALIGN int /* type for alignment */ #endif #define BYTES_WORD sizeof(ALIGN) +extern char *malloc(); +static char *bposition = NULL; +static unsigned nremain = 0; + + char * bmalloc(n) /* allocate a block of n bytes, no refunds */ register unsigned n; { - extern char *malloc(); - static char *bpos = NULL; - static unsigned nrem = 0; - - if (n > nrem && (n > MBLKSIZ || nrem > MBLKSIZ/WASTEFRAC)) + if (n > nremain && (n > MBLKSIZ || nremain > MBLKSIZ/WASTEFRAC)) return(malloc(n)); /* too big */ n = (n+(BYTES_WORD-1))&~(BYTES_WORD-1); /* word align */ - if (n > nrem) { - if ((bpos = malloc((unsigned)MBLKSIZ)) == NULL) { - nrem = 0; + if (n > nremain) { + if ((bposition = malloc((unsigned)MBLKSIZ)) == NULL) { + nremain = 0; return(NULL); } - nrem = MBLKSIZ; + nremain = MBLKSIZ; } - bpos += n; - nrem -= n; - return(bpos - n); + bposition += n; + nremain -= n; + return(bposition - n); } bfree(p, n) /* free random memory */ -char *p; -unsigned n; +register char *p; +register unsigned n; { - /* not implemented */ + register unsigned bsiz; + /* check alignment */ + bsiz = BYTES_WORD - ((unsigned)p&(BYTES_WORD-1)); + if (bsiz < BYTES_WORD) { + p += bsiz; + n -= bsiz; + } + if (p + n == bposition) { /* just allocated? */ + bposition = p; + nremain += n; + return; + } + if (n > nremain) { /* better than what we've got? */ + bposition = p; + nremain = n; + return; + } + /* just throw it away, then */ }