| 1 |
– |
/* Copyright (c) 1990 Regents of the University of California */ |
| 2 |
– |
|
| 1 |
|
#ifndef lint |
| 2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
| 2 |
> |
static const char RCSid[] = "$Id$"; |
| 3 |
|
#endif |
| 6 |
– |
|
| 4 |
|
/* |
| 5 |
< |
* Simple memory allocation without overhead |
| 5 |
> |
* Bmalloc provides basic memory allocation without overhead (no free lists). |
| 6 |
> |
* Use only to take the load off of malloc for all those |
| 7 |
> |
* piddling little requests that you never expect to free. |
| 8 |
> |
* Bmalloc defers to malloc for big requests. |
| 9 |
> |
* Bfree should hand memory to bmalloc, but it usually fails here. |
| 10 |
|
* |
| 11 |
+ |
* External symbols declared in standard.h |
| 12 |
|
*/ |
| 13 |
|
|
| 14 |
+ |
#include "copyright.h" |
| 15 |
+ |
|
| 16 |
+ |
#include <stdlib.h> |
| 17 |
+ |
|
| 18 |
+ |
#include "rtmisc.h" |
| 19 |
+ |
|
| 20 |
|
#ifndef MBLKSIZ |
| 21 |
|
#define MBLKSIZ 16376 /* size of memory allocation block */ |
| 22 |
|
#endif |
| 23 |
< |
#ifndef ALIGN |
| 24 |
< |
#define ALIGN int /* type for alignment */ |
| 23 |
> |
#define WASTEFRAC 12 /* don't waste more than a fraction */ |
| 24 |
> |
#ifndef ALIGNT |
| 25 |
> |
#define ALIGNT double /* type for alignment */ |
| 26 |
|
#endif |
| 27 |
< |
#define BYTES_WORD sizeof(ALIGN) |
| 27 |
> |
#define BYTES_WORD sizeof(ALIGNT) |
| 28 |
|
|
| 29 |
+ |
static char *bposition = NULL; |
| 30 |
+ |
static unsigned int nremain = 0; |
| 31 |
|
|
| 32 |
+ |
|
| 33 |
|
char * |
| 34 |
< |
bmalloc(n) /* allocate a block of n bytes, no refunds */ |
| 35 |
< |
register unsigned n; |
| 34 |
> |
bmalloc( /* allocate a block of n bytes */ |
| 35 |
> |
register unsigned int n |
| 36 |
> |
) |
| 37 |
|
{ |
| 38 |
< |
static char *bpos = NULL; |
| 39 |
< |
static unsigned nrem = 0; |
| 38 |
> |
if (n > nremain && (n > MBLKSIZ || nremain > MBLKSIZ/WASTEFRAC)) |
| 39 |
> |
return(malloc(n)); /* too big */ |
| 40 |
|
|
| 28 |
– |
if (n > MBLKSIZ/2) /* too big for me */ |
| 29 |
– |
return(malloc(n)); |
| 30 |
– |
|
| 41 |
|
n = (n+(BYTES_WORD-1))&~(BYTES_WORD-1); /* word align */ |
| 42 |
|
|
| 43 |
< |
if (n > nrem) { |
| 44 |
< |
if ((bpos = malloc((unsigned)MBLKSIZ)) == NULL) { |
| 45 |
< |
nrem = 0; |
| 36 |
< |
return(NULL); |
| 37 |
< |
} |
| 38 |
< |
nrem = MBLKSIZ; |
| 43 |
> |
if (n > nremain && (bposition = (char *)malloc(nremain = MBLKSIZ)) == NULL) { |
| 44 |
> |
nremain = 0; |
| 45 |
> |
return(NULL); |
| 46 |
|
} |
| 47 |
< |
bpos += n; |
| 48 |
< |
nrem -= n; |
| 49 |
< |
return(bpos - n); |
| 47 |
> |
bposition += n; |
| 48 |
> |
nremain -= n; |
| 49 |
> |
return(bposition - n); |
| 50 |
|
} |
| 51 |
|
|
| 52 |
|
|
| 53 |
< |
bfree(p, n) /* free random memory */ |
| 54 |
< |
char *p; |
| 55 |
< |
unsigned n; |
| 53 |
> |
void |
| 54 |
> |
bfree( /* free random memory */ |
| 55 |
> |
register char *p, |
| 56 |
> |
register unsigned int n |
| 57 |
> |
) |
| 58 |
|
{ |
| 59 |
< |
/* not implemented */ |
| 59 |
> |
register unsigned int bsiz; |
| 60 |
> |
/* check alignment */ |
| 61 |
> |
bsiz = BYTES_WORD - ((unsigned int)p&(BYTES_WORD-1)); |
| 62 |
> |
if (bsiz < BYTES_WORD) { |
| 63 |
> |
p += bsiz; |
| 64 |
> |
n -= bsiz; |
| 65 |
> |
} |
| 66 |
> |
if (p + n == bposition) { /* just allocated? */ |
| 67 |
> |
bposition = p; |
| 68 |
> |
nremain += n; |
| 69 |
> |
return; |
| 70 |
> |
} |
| 71 |
> |
if (n > nremain) { /* better than what we've got? */ |
| 72 |
> |
bposition = p; |
| 73 |
> |
nremain = n; |
| 74 |
> |
return; |
| 75 |
> |
} |
| 76 |
> |
/* just throw it away, then */ |
| 77 |
|
} |