1 |
< |
/* Copyright (c) 1991 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
24 |
|
|
25 |
|
#include <errno.h> |
26 |
|
|
27 |
+ |
extern int errno; |
28 |
+ |
|
29 |
+ |
#ifndef BSD |
30 |
+ |
#define bcopy(s,d,n) (void)memcpy(d,s,n) |
31 |
+ |
#define bzero(d,n) (void)memset(d,0,n) |
32 |
+ |
extern char *memcpy(), *memset(); |
33 |
+ |
#endif |
34 |
+ |
|
35 |
|
#ifdef MSTATS |
36 |
|
#include <stdio.h> |
37 |
< |
static unsigned b_nsbrked = 0; |
37 |
> |
static unsigned b_nsbrked = 0; |
38 |
|
static unsigned b_nalloced = 0; |
39 |
|
static unsigned b_nfreed = 0; |
40 |
|
static unsigned b_nscrounged = 0; |
51 |
|
#endif |
52 |
|
#define BYTES_WORD sizeof(ALIGN) |
53 |
|
|
54 |
+ |
#ifndef MAXINCR |
55 |
|
#define MAXINCR (1<<16) /* largest sbrk(2) increment */ |
47 |
– |
|
48 |
– |
#ifdef NOVMEM |
49 |
– |
#define getpagesize() 1024 |
56 |
|
#endif |
57 |
|
/* malloc free lists */ |
58 |
|
typedef union m_head { |
73 |
|
|
74 |
|
static ALIGN dummy_mem; |
75 |
|
|
76 |
+ |
static char *memlim[2]; |
77 |
+ |
|
78 |
|
#define DUMMYLOC ((char *)&dummy_mem) |
79 |
|
|
80 |
+ |
#define BADPTR(p) ((p) < memlim[0] | (p) >= memlim[1]) |
81 |
+ |
|
82 |
|
#ifdef MCOMP /* memory compaction routines */ |
83 |
|
static char seedtab[1024]; /* seed for compaction table */ |
84 |
|
|
175 |
|
big->siz = 0; /* remove from table */ |
176 |
|
return(big->ptr); /* return it */ |
177 |
|
} |
178 |
< |
if (mtablen(big) < tablen+1) { |
178 |
> |
if (mtablen(big) <= tablen) { |
179 |
|
*np = 0; /* cannot grow table */ |
180 |
|
return(NULL); /* report failure */ |
181 |
|
} |
185 |
|
cptab.ptr = big->ptr; |
186 |
|
cptab.siz = big->siz; |
187 |
|
big->siz = 0; /* clear and copy */ |
178 |
– |
#ifdef BSD |
188 |
|
bcopy((char *)tab, (char *)(mtab(&cptab)+1), |
189 |
|
tablen*sizeof(struct mblk)); |
190 |
|
bzero((char *)(mtab(&cptab)+tablen+1), |
191 |
|
(mtablen(&cptab)-tablen-1)*sizeof(struct mblk)); |
183 |
– |
#else |
184 |
– |
(void)memcpy((char *)(mtab(&cptab)+1), (char *)tab, |
185 |
– |
tablen*sizeof(struct mblk)); |
186 |
– |
memset((char *)(mtab(&cptab)+tablen+1), 0, |
187 |
– |
(mtablen(&cptab)-tablen-1)*sizeof(struct mblk)); |
188 |
– |
#endif |
192 |
|
} /* next round */ |
193 |
|
} |
194 |
|
#endif /* MCOMP */ |
238 |
|
pagesz = amnt = getpagesize(); |
239 |
|
nrem = (int)sbrk(0); /* page align break */ |
240 |
|
nrem = pagesz - (nrem&(pagesz-1)); |
241 |
< |
bpos = sbrk(nrem); /* word aligned! */ |
241 |
> |
bpos = sbrk(nrem); |
242 |
|
if ((int)bpos == -1) |
243 |
|
return(NULL); |
244 |
|
#ifdef MSTATS |
245 |
|
b_nsbrked += nrem; |
246 |
|
#endif |
247 |
+ |
bpos += nrem & (BYTES_WORD-1); /* align pointer */ |
248 |
+ |
nrem &= ~(BYTES_WORD-1); |
249 |
+ |
memlim[0] = bpos; |
250 |
+ |
memlim[1] = bpos + nrem; |
251 |
|
} |
252 |
|
|
253 |
|
n = (n+(BYTES_WORD-1))&~(BYTES_WORD-1); /* word align rqst. */ |
275 |
|
nrem = thisamnt; |
276 |
|
} else /* otherwise tack on */ |
277 |
|
nrem += thisamnt; |
278 |
+ |
if (bpos < memlim[0]) |
279 |
+ |
memlim[0] = bpos; |
280 |
+ |
if (bpos + nrem > memlim[1]) |
281 |
+ |
memlim[1] = bpos + nrem; |
282 |
|
} |
283 |
|
p = bpos; |
284 |
|
bpos += n; /* advance */ |
308 |
|
p += bsiz; |
309 |
|
n -= bsiz; |
310 |
|
} |
311 |
+ |
if (p < memlim[0]) |
312 |
+ |
memlim[0] = p; |
313 |
+ |
if (p + n > memlim[1]) |
314 |
+ |
memlim[1] = p + n; |
315 |
|
/* fill big buckets first */ |
316 |
|
for (bucket = NBUCKETS-1, bsiz = 1<<(NBUCKETS-1); |
317 |
|
bucket >= FIRSTBUCKET; bucket--, bsiz >>= 1) |
328 |
|
malloc(n) /* allocate n bytes of memory */ |
329 |
|
unsigned n; |
330 |
|
{ |
316 |
– |
extern int errno; |
331 |
|
register M_HEAD *mp; |
332 |
|
register int bucket; |
333 |
|
register unsigned bsiz; |
369 |
|
char *p; |
370 |
|
register unsigned on; |
371 |
|
/* get old size */ |
372 |
< |
if (op != NULL && op != DUMMYLOC && ((M_HEAD *)op-1)->a.magic == MAGIC) |
372 |
> |
if (op != DUMMYLOC && !BADPTR(op) && |
373 |
> |
((M_HEAD *)op-1)->a.magic == MAGIC) |
374 |
|
on = 1 << ((M_HEAD *)op-1)->a.bucket; |
375 |
|
else |
376 |
|
on = 0; |
377 |
|
if (n <= on && (n > on>>1 || on == 1<<FIRSTBUCKET)) |
378 |
|
return(op); /* same bucket */ |
379 |
|
if ((p = malloc(n)) == NULL) |
380 |
< |
return(NULL); |
380 |
> |
return(n<=on ? op : NULL); |
381 |
|
if (on) { |
367 |
– |
#ifdef BSD |
382 |
|
bcopy(op, p, n>on ? on : n); |
369 |
– |
#else |
370 |
– |
(void)memcpy(p, op, n>on ? on : n); |
371 |
– |
#endif |
383 |
|
free(op); |
384 |
|
} |
385 |
|
return(p); |
392 |
|
register M_HEAD *mp; |
393 |
|
register int bucket; |
394 |
|
|
395 |
< |
if (p == NULL || p == DUMMYLOC) |
395 |
> |
if (p == DUMMYLOC) |
396 |
|
return(1); |
397 |
+ |
if (BADPTR(p)) |
398 |
+ |
goto invalid; |
399 |
|
mp = (M_HEAD *)p - 1; |
400 |
|
if (mp->a.magic != MAGIC) /* sanity check */ |
401 |
< |
return(0); |
401 |
> |
goto invalid; |
402 |
|
bucket = mp->a.bucket; |
403 |
+ |
if (bucket < FIRSTBUCKET | bucket >= NBUCKETS) |
404 |
+ |
goto invalid; |
405 |
|
mp->next = free_list[bucket]; |
406 |
|
free_list[bucket] = mp; |
407 |
|
#ifdef MSTATS |
408 |
|
m_nfreed += (1 << bucket) + sizeof(M_HEAD); |
409 |
|
#endif |
410 |
|
return(1); |
411 |
+ |
invalid: |
412 |
+ |
errno = EINVAL; |
413 |
+ |
return(0); |
414 |
|
} |
397 |
– |
|
398 |
– |
|
399 |
– |
#ifndef NOVMEM |
400 |
– |
#ifndef BSD |
401 |
– |
#include <sys/var.h> |
402 |
– |
int |
403 |
– |
getpagesize() /* use SYSV var structure to get page size */ |
404 |
– |
{ |
405 |
– |
struct var v; |
406 |
– |
|
407 |
– |
uvar(&v); |
408 |
– |
return(1 << v.v_pageshift); |
409 |
– |
} |
410 |
– |
#endif |
411 |
– |
#endif |
415 |
|
|
416 |
|
|
417 |
|
#ifdef MSTATS |