24 |
|
|
25 |
|
#include <errno.h> |
26 |
|
|
27 |
+ |
#ifndef BSD |
28 |
+ |
#define bcopy(s,d,n) (void)memcpy(d,s,n) |
29 |
+ |
#define bzero(d,n) (void)memset(d,0,n) |
30 |
+ |
extern char *memcpy(), *memset(); |
31 |
+ |
#endif |
32 |
+ |
|
33 |
|
#ifdef MSTATS |
34 |
|
#include <stdio.h> |
35 |
< |
static unsigned b_nsbrked = 0; |
35 |
> |
static unsigned b_nsbrked = 0; |
36 |
|
static unsigned b_nalloced = 0; |
37 |
|
static unsigned b_nfreed = 0; |
38 |
|
static unsigned b_nscrounged = 0; |
71 |
|
|
72 |
|
static ALIGN dummy_mem; |
73 |
|
|
74 |
+ |
static char *memlim[2]; |
75 |
+ |
|
76 |
|
#define DUMMYLOC ((char *)&dummy_mem) |
77 |
|
|
78 |
+ |
#define BADPTR(p) ((p) < memlim[0] | (p) >= memlim[1]) |
79 |
+ |
|
80 |
|
#ifdef MCOMP /* memory compaction routines */ |
81 |
|
static char seedtab[1024]; /* seed for compaction table */ |
82 |
|
|
160 |
|
|
161 |
|
for ( ; ; ) { |
162 |
|
/* compact free lists */ |
163 |
< |
compactfree(); |
163 |
> |
while (compactfree()) |
164 |
> |
; |
165 |
|
/* find largest block */ |
166 |
|
tab = mtab(&cptab); tablen = mtablen(&cptab); |
167 |
|
big = tab; |
174 |
|
big->siz = 0; /* remove from table */ |
175 |
|
return(big->ptr); /* return it */ |
176 |
|
} |
177 |
< |
if (mtablen(big) < tablen+1) { |
177 |
> |
if (mtablen(big) <= tablen) { |
178 |
|
*np = 0; /* cannot grow table */ |
179 |
|
return(NULL); /* report failure */ |
180 |
|
} |
184 |
|
cptab.ptr = big->ptr; |
185 |
|
cptab.siz = big->siz; |
186 |
|
big->siz = 0; /* clear and copy */ |
176 |
– |
#ifdef BSD |
187 |
|
bcopy((char *)tab, (char *)(mtab(&cptab)+1), |
188 |
|
tablen*sizeof(struct mblk)); |
189 |
|
bzero((char *)(mtab(&cptab)+tablen+1), |
190 |
|
(mtablen(&cptab)-tablen-1)*sizeof(struct mblk)); |
181 |
– |
#else |
182 |
– |
(void)memcpy((char *)(mtab(&cptab)+1), (char *)tab, |
183 |
– |
tablen*sizeof(struct mblk)); |
184 |
– |
memset((char *)(mtab(&cptab)+tablen+1), 0, |
185 |
– |
(mtablen(&cptab)-tablen-1)*sizeof(struct mblk)); |
186 |
– |
#endif |
191 |
|
} /* next round */ |
192 |
|
} |
193 |
|
#endif /* MCOMP */ |
245 |
|
#endif |
246 |
|
bpos += nrem & (BYTES_WORD-1); /* align pointer */ |
247 |
|
nrem &= ~(BYTES_WORD-1); |
248 |
+ |
memlim[0] = bpos; |
249 |
+ |
memlim[1] = bpos + nrem; |
250 |
|
} |
251 |
|
|
252 |
|
n = (n+(BYTES_WORD-1))&~(BYTES_WORD-1); /* word align rqst. */ |
253 |
|
|
254 |
|
if (n > nrem) { /* need more core */ |
255 |
+ |
tryagain: |
256 |
|
if (n > amnt) { /* big chunk */ |
257 |
|
thisamnt = (n+(pagesz-1))&~(pagesz-1); |
258 |
|
if (thisamnt <= MAXINCR) /* increase amnt */ |
261 |
|
thisamnt = amnt; |
262 |
|
p = sbrk(thisamnt); |
263 |
|
if ((int)p == -1) { /* uh-oh, ENOMEM */ |
264 |
< |
thisamnt = n; /* search free lists */ |
265 |
< |
p = mscrounge(&thisamnt); |
266 |
< |
if (p == NULL) /* we're really out */ |
264 |
> |
errno = 0; /* call cavalry */ |
265 |
> |
if (thisamnt >= n+pagesz) { |
266 |
> |
amnt = pagesz; /* minimize request */ |
267 |
> |
goto tryagain; |
268 |
> |
} |
269 |
> |
thisamnt = n; |
270 |
> |
p = mscrounge(&thisamnt); /* search free lists */ |
271 |
> |
if (p == NULL) { /* we're really out */ |
272 |
> |
errno = ENOMEM; |
273 |
|
return(NULL); |
274 |
+ |
} |
275 |
|
} |
276 |
|
#ifdef MSTATS |
277 |
|
else b_nsbrked += thisamnt; |
282 |
|
nrem = thisamnt; |
283 |
|
} else /* otherwise tack on */ |
284 |
|
nrem += thisamnt; |
285 |
+ |
if (bpos < memlim[0]) |
286 |
+ |
memlim[0] = bpos; |
287 |
+ |
if (bpos + nrem > memlim[1]) |
288 |
+ |
memlim[1] = bpos + nrem; |
289 |
|
} |
290 |
|
p = bpos; |
291 |
|
bpos += n; /* advance */ |
315 |
|
p += bsiz; |
316 |
|
n -= bsiz; |
317 |
|
} |
318 |
+ |
if (p < memlim[0]) |
319 |
+ |
memlim[0] = p; |
320 |
+ |
if (p + n > memlim[1]) |
321 |
+ |
memlim[1] = p + n; |
322 |
|
/* fill big buckets first */ |
323 |
|
for (bucket = NBUCKETS-1, bsiz = 1<<(NBUCKETS-1); |
324 |
|
bucket >= FIRSTBUCKET; bucket--, bsiz >>= 1) |
335 |
|
malloc(n) /* allocate n bytes of memory */ |
336 |
|
unsigned n; |
337 |
|
{ |
316 |
– |
extern int errno; |
338 |
|
register M_HEAD *mp; |
339 |
|
register int bucket; |
340 |
|
register unsigned bsiz; |
376 |
|
char *p; |
377 |
|
register unsigned on; |
378 |
|
/* get old size */ |
379 |
< |
if (op != NULL && op != DUMMYLOC && ((M_HEAD *)op-1)->a.magic == MAGIC) |
379 |
> |
if (op != DUMMYLOC && !BADPTR(op) && |
380 |
> |
((M_HEAD *)op-1)->a.magic == MAGIC) |
381 |
|
on = 1 << ((M_HEAD *)op-1)->a.bucket; |
382 |
|
else |
383 |
|
on = 0; |
386 |
|
if ((p = malloc(n)) == NULL) |
387 |
|
return(n<=on ? op : NULL); |
388 |
|
if (on) { |
367 |
– |
#ifdef BSD |
389 |
|
bcopy(op, p, n>on ? on : n); |
369 |
– |
#else |
370 |
– |
(void)memcpy(p, op, n>on ? on : n); |
371 |
– |
#endif |
390 |
|
free(op); |
391 |
|
} |
392 |
|
return(p); |
399 |
|
register M_HEAD *mp; |
400 |
|
register int bucket; |
401 |
|
|
402 |
< |
if (p == NULL | p == DUMMYLOC) |
402 |
> |
if (p == DUMMYLOC) |
403 |
|
return(1); |
404 |
+ |
if (BADPTR(p)) |
405 |
+ |
goto invalid; |
406 |
|
mp = (M_HEAD *)p - 1; |
407 |
|
if (mp->a.magic != MAGIC) /* sanity check */ |
408 |
< |
return(0); |
408 |
> |
goto invalid; |
409 |
|
bucket = mp->a.bucket; |
410 |
|
if (bucket < FIRSTBUCKET | bucket >= NBUCKETS) |
411 |
< |
return(0); |
411 |
> |
goto invalid; |
412 |
|
mp->next = free_list[bucket]; |
413 |
|
free_list[bucket] = mp; |
414 |
|
#ifdef MSTATS |
415 |
|
m_nfreed += (1 << bucket) + sizeof(M_HEAD); |
416 |
|
#endif |
417 |
|
return(1); |
418 |
+ |
invalid: |
419 |
+ |
errno = EINVAL; |
420 |
+ |
return(0); |
421 |
|
} |
422 |
|
|
423 |
|
|