1 |
– |
/* Copyright (c) 1991 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 |
|
* 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 |
< |
#define NULL 0 |
14 |
> |
#include "copyright.h" |
15 |
|
|
16 |
< |
#ifndef MBLKSIZ |
17 |
< |
#define MBLKSIZ 16376 /* size of memory allocation block */ |
16 |
> |
#include <stdlib.h> |
17 |
> |
|
18 |
> |
#include "rtmisc.h" |
19 |
> |
|
20 |
> |
#ifndef ALIGNT |
21 |
> |
#define ALIGNT double /* type for alignment */ |
22 |
|
#endif |
23 |
< |
#define WASTEFRAC 12 /* don't waste more than a fraction */ |
24 |
< |
#ifndef ALIGN |
25 |
< |
#define ALIGN int /* type for alignment */ |
23 |
> |
#define BYTES_WORD sizeof(ALIGNT) |
24 |
> |
|
25 |
> |
#ifndef MBLKSIZ /* size of memory allocation block */ |
26 |
> |
#define MBLKSIZ ((1<<15)-BYTES_WORD) |
27 |
|
#endif |
28 |
< |
#define BYTES_WORD sizeof(ALIGN) |
28 |
> |
#define WASTEFRAC 12 /* don't waste more than a fraction */ |
29 |
|
|
26 |
– |
extern char *malloc(); |
27 |
– |
|
30 |
|
static char *bposition = NULL; |
31 |
< |
static unsigned nremain = 0; |
31 |
> |
static size_t nremain = 0; |
32 |
|
|
33 |
< |
|
34 |
< |
char * |
35 |
< |
bmalloc(n) /* allocate a block of n bytes, no refunds */ |
36 |
< |
register unsigned n; |
33 |
> |
void * |
34 |
> |
bmalloc( /* quickly allocate a block of n bytes */ |
35 |
> |
size_t n |
36 |
> |
) |
37 |
|
{ |
38 |
< |
if (n > nremain && (n > MBLKSIZ || nremain > MBLKSIZ/WASTEFRAC)) |
38 |
> |
if ((n > nremain) & ((n > MBLKSIZ) | (nremain > MBLKSIZ/WASTEFRAC))) |
39 |
|
return(malloc(n)); /* too big */ |
40 |
|
|
41 |
< |
n = (n+(BYTES_WORD-1))&~(BYTES_WORD-1); /* word align */ |
41 |
> |
n = (n+(BYTES_WORD-1)) & ~(BYTES_WORD-1); /* word align */ |
42 |
|
|
43 |
< |
if (n > nremain) { |
44 |
< |
if ((bposition = malloc((unsigned)MBLKSIZ)) == NULL) { |
45 |
< |
nremain = 0; |
44 |
< |
return(NULL); |
45 |
< |
} |
46 |
< |
nremain = MBLKSIZ; |
43 |
> |
if (n > nremain && (bposition = malloc(nremain = MBLKSIZ)) == NULL) { |
44 |
> |
nremain = 0; |
45 |
> |
return(NULL); |
46 |
|
} |
47 |
|
bposition += n; |
48 |
|
nremain -= n; |
49 |
|
return(bposition - n); |
50 |
|
} |
51 |
|
|
52 |
< |
|
53 |
< |
bfree(p, n) /* free random memory */ |
54 |
< |
register char *p; |
55 |
< |
register unsigned n; |
52 |
> |
void |
53 |
> |
bfree( /* free some memory; anything in process space */ |
54 |
> |
void *pp, |
55 |
> |
size_t n |
56 |
> |
) |
57 |
|
{ |
58 |
< |
register unsigned bsiz; |
58 |
> |
char *p = pp; |
59 |
> |
size_t bsiz; |
60 |
|
/* check alignment */ |
61 |
< |
bsiz = BYTES_WORD - ((unsigned)p&(BYTES_WORD-1)); |
61 |
> |
bsiz = BYTES_WORD - ((size_t)p&(BYTES_WORD-1)); |
62 |
|
if (bsiz < BYTES_WORD) { |
63 |
|
p += bsiz; |
64 |
|
n -= bsiz; |