1 |
< |
/* Copyright (c) 1990 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
23 |
|
|
24 |
|
#ifdef MSTATS |
25 |
|
#include <stdio.h> |
26 |
– |
|
26 |
|
static unsigned b_nalloced = 0; |
27 |
|
static unsigned b_nfreed = 0; |
28 |
|
static unsigned b_nscrounged = 0; |
41 |
|
#define MAXINCR (1<<16) /* largest sbrk(2) increment */ |
42 |
|
|
43 |
|
#ifdef NOVMEM |
44 |
< |
#define getpagesize() BYTES_WORD |
44 |
> |
#define getpagesize() 1024 |
45 |
|
#endif |
46 |
|
/* malloc free lists */ |
47 |
|
typedef union m_head { |
48 |
|
union m_head *next; |
49 |
< |
int bucket; |
49 |
> |
struct { |
50 |
> |
short magic; |
51 |
> |
short bucket; |
52 |
> |
} a; |
53 |
|
ALIGN dummy; |
54 |
|
} M_HEAD; |
55 |
|
|
56 |
+ |
#define MAGIC 0x1a2 /* magic number for allocated memory */ |
57 |
+ |
|
58 |
|
#define FIRSTBUCKET 3 |
59 |
|
#define NBUCKETS 30 |
60 |
|
|
61 |
|
static M_HEAD *free_list[NBUCKETS]; |
62 |
|
|
63 |
+ |
static ALIGN dummy_mem; |
64 |
|
|
65 |
+ |
#define DUMMYLOC ((char *)&dummy_mem) |
66 |
+ |
|
67 |
+ |
|
68 |
|
char * |
69 |
|
mscrounge(np) /* search free lists to satisfy request */ |
70 |
|
register unsigned *np; |
179 |
|
register M_HEAD *mp; |
180 |
|
register int bucket; |
181 |
|
register unsigned bsiz; |
182 |
+ |
/* don't return NULL on 0 request */ |
183 |
+ |
if (n == 0) |
184 |
+ |
return(DUMMYLOC); |
185 |
|
/* find first bucket that fits */ |
186 |
|
for (bucket = FIRSTBUCKET, bsiz = 1<<FIRSTBUCKET; |
187 |
|
bucket < NBUCKETS; bucket++, bsiz <<= 1) |
203 |
|
mp = free_list[bucket]; |
204 |
|
free_list[bucket] = mp->next; |
205 |
|
} |
206 |
< |
mp->bucket = bucket; /* tag block */ |
206 |
> |
mp->a.magic = MAGIC; /* tag block */ |
207 |
> |
mp->a.bucket = bucket; |
208 |
|
return((char *)(mp+1)); |
209 |
|
} |
210 |
|
|
217 |
|
char *p; |
218 |
|
register unsigned on; |
219 |
|
/* get old size */ |
220 |
< |
if (op != NULL) |
221 |
< |
on = 1 << ((M_HEAD *)op-1)->bucket; |
220 |
> |
if (op != NULL && op != DUMMYLOC && ((M_HEAD *)op-1)->a.magic == MAGIC) |
221 |
> |
on = 1 << ((M_HEAD *)op-1)->a.bucket; |
222 |
|
else |
223 |
|
on = 0; |
224 |
|
if (n <= on && (n > on>>1 || on == 1<<FIRSTBUCKET)) |
241 |
|
register M_HEAD *mp; |
242 |
|
register int bucket; |
243 |
|
|
244 |
< |
if (p == NULL) |
244 |
> |
if (p == NULL || p == DUMMYLOC) |
245 |
|
return; |
246 |
|
mp = (M_HEAD *)p - 1; |
247 |
< |
bucket = mp->bucket; |
247 |
> |
if (mp->a.magic != MAGIC) /* sanity check */ |
248 |
> |
return; |
249 |
> |
bucket = mp->a.bucket; |
250 |
|
mp->next = free_list[bucket]; |
251 |
|
free_list[bucket] = mp; |
252 |
|
#ifdef MSTATS |