24 |
|
|
25 |
|
#include <errno.h> |
26 |
|
|
27 |
+ |
extern int errno; |
28 |
+ |
|
29 |
|
#ifdef MSTATS |
30 |
|
#include <stdio.h> |
31 |
< |
static unsigned b_nsbrked = 0; |
31 |
> |
static unsigned b_nsbrked = 0; |
32 |
|
static unsigned b_nalloced = 0; |
33 |
|
static unsigned b_nfreed = 0; |
34 |
|
static unsigned b_nscrounged = 0; |
67 |
|
|
68 |
|
static ALIGN dummy_mem; |
69 |
|
|
70 |
+ |
static char *memlim[2]; |
71 |
+ |
|
72 |
|
#define DUMMYLOC ((char *)&dummy_mem) |
73 |
|
|
74 |
+ |
#define BADPTR(p) ((p) < memlim[0] | (p) >= memlim[1]) |
75 |
+ |
|
76 |
|
#ifdef MCOMP /* memory compaction routines */ |
77 |
|
static char seedtab[1024]; /* seed for compaction table */ |
78 |
|
|
247 |
|
#endif |
248 |
|
bpos += nrem & (BYTES_WORD-1); /* align pointer */ |
249 |
|
nrem &= ~(BYTES_WORD-1); |
250 |
+ |
memlim[0] = bpos; |
251 |
+ |
memlim[1] = bpos + nrem; |
252 |
|
} |
253 |
|
|
254 |
|
n = (n+(BYTES_WORD-1))&~(BYTES_WORD-1); /* word align rqst. */ |
276 |
|
nrem = thisamnt; |
277 |
|
} else /* otherwise tack on */ |
278 |
|
nrem += thisamnt; |
279 |
+ |
if (bpos < memlim[0]) |
280 |
+ |
memlim[0] = bpos; |
281 |
+ |
if (bpos + nrem > memlim[1]) |
282 |
+ |
memlim[1] = bpos + nrem; |
283 |
|
} |
284 |
|
p = bpos; |
285 |
|
bpos += n; /* advance */ |
309 |
|
p += bsiz; |
310 |
|
n -= bsiz; |
311 |
|
} |
312 |
+ |
if (p < memlim[0]) |
313 |
+ |
memlim[0] = p; |
314 |
+ |
if (p + n > memlim[1]) |
315 |
+ |
memlim[1] = p + n; |
316 |
|
/* fill big buckets first */ |
317 |
|
for (bucket = NBUCKETS-1, bsiz = 1<<(NBUCKETS-1); |
318 |
|
bucket >= FIRSTBUCKET; bucket--, bsiz >>= 1) |
329 |
|
malloc(n) /* allocate n bytes of memory */ |
330 |
|
unsigned n; |
331 |
|
{ |
316 |
– |
extern int errno; |
332 |
|
register M_HEAD *mp; |
333 |
|
register int bucket; |
334 |
|
register unsigned bsiz; |
370 |
|
char *p; |
371 |
|
register unsigned on; |
372 |
|
/* get old size */ |
373 |
< |
if (op != NULL && op != DUMMYLOC && ((M_HEAD *)op-1)->a.magic == MAGIC) |
373 |
> |
if (op != DUMMYLOC && !BADPTR(op) && |
374 |
> |
((M_HEAD *)op-1)->a.magic == MAGIC) |
375 |
|
on = 1 << ((M_HEAD *)op-1)->a.bucket; |
376 |
|
else |
377 |
|
on = 0; |
397 |
|
register M_HEAD *mp; |
398 |
|
register int bucket; |
399 |
|
|
400 |
< |
if (p == NULL | p == DUMMYLOC) |
400 |
> |
if (p == DUMMYLOC) |
401 |
|
return(1); |
402 |
+ |
if (BADPTR(p)) |
403 |
+ |
goto invalid; |
404 |
|
mp = (M_HEAD *)p - 1; |
405 |
|
if (mp->a.magic != MAGIC) /* sanity check */ |
406 |
< |
return(0); |
406 |
> |
goto invalid; |
407 |
|
bucket = mp->a.bucket; |
408 |
|
if (bucket < FIRSTBUCKET | bucket >= NBUCKETS) |
409 |
< |
return(0); |
409 |
> |
goto invalid; |
410 |
|
mp->next = free_list[bucket]; |
411 |
|
free_list[bucket] = mp; |
412 |
|
#ifdef MSTATS |
413 |
|
m_nfreed += (1 << bucket) + sizeof(M_HEAD); |
414 |
|
#endif |
415 |
|
return(1); |
416 |
+ |
invalid: |
417 |
+ |
errno = EINVAL; |
418 |
+ |
return(0); |
419 |
|
} |
420 |
|
|
421 |
|
|