diff options
author | mp <mp@FreeBSD.org> | 2007-03-11 22:33:41 +0000 |
---|---|---|
committer | mp <mp@FreeBSD.org> | 2007-03-11 22:33:41 +0000 |
commit | a40980339b13e3b506c2317b5b4864127039eb2c (patch) | |
tree | 34aefea92d30b614247ef1f2671f2362f4761785 /contrib/tcsh/tc.alloc.c | |
parent | 32837fb336d4709f0a121130a3a78f29be0db5ed (diff) | |
download | FreeBSD-src-a40980339b13e3b506c2317b5b4864127039eb2c.zip FreeBSD-src-a40980339b13e3b506c2317b5b4864127039eb2c.tar.gz |
Import of tcsh-6.15.00
Diffstat (limited to 'contrib/tcsh/tc.alloc.c')
-rw-r--r-- | contrib/tcsh/tc.alloc.c | 145 |
1 files changed, 63 insertions, 82 deletions
diff --git a/contrib/tcsh/tc.alloc.c b/contrib/tcsh/tc.alloc.c index d9c9205..53546fe 100644 --- a/contrib/tcsh/tc.alloc.c +++ b/contrib/tcsh/tc.alloc.c @@ -1,4 +1,4 @@ -/* $Header: /src/pub/tcsh/tc.alloc.c,v 3.39 2005/01/05 16:06:14 christos Exp $ */ +/* $Header: /p/tcsh/cvsroot/tcsh/tc.alloc.c,v 3.46 2006/03/02 18:46:44 christos Exp $ */ /* * tc.alloc.c (Caltech) 2/21/82 * Chris Kingsley, kingsley@cit-20. @@ -40,7 +40,10 @@ */ #include "sh.h" -RCSID("$Id: tc.alloc.c,v 3.39 2005/01/05 16:06:14 christos Exp $") +RCSID("$tcsh: tc.alloc.c,v 3.46 2006/03/02 18:46:44 christos Exp $") + +#define RCHECK +#define DEBUG static char *memtop = NULL; /* PWP: top of current memory */ static char *membot = NULL; /* PWP: bottom of allocatable memory */ @@ -54,10 +57,18 @@ int dont_free = 0; # define realloc frealloc #endif /* WINNT_NATIVE */ -#ifndef SYSMALLOC +#if !defined(DEBUG) || defined(SYSMALLOC) +static void +out_of_memory (void) +{ + static const char msg[] = "Out of memory\n"; + + write(didfds ? 2 : SHDIAG, msg, strlen(msg)); + _exit(1); +} +#endif -#undef RCHECK -#undef DEBUG +#ifndef SYSMALLOC #ifdef SX extern void* sbrk(); @@ -134,8 +145,8 @@ static union overhead *nextf[NBUCKETS] IZERO_STRUCT; static U_int nmalloc[NBUCKETS] IZERO_STRUCT; #ifndef lint -static int findbucket __P((union overhead *, int)); -static void morecore __P((int)); +static int findbucket (union overhead *, int); +static void morecore (int); #endif @@ -143,21 +154,20 @@ static void morecore __P((int)); # define CHECK(a, str, p) \ if (a) { \ xprintf(str, p); \ - xprintf(" (memtop = %lx membot = %lx)\n", memtop, membot); \ + xprintf(" (memtop = %p membot = %p)\n", memtop, membot); \ abort(); \ } #else # define CHECK(a, str, p) \ if (a) { \ xprintf(str, p); \ - xprintf(" (memtop = %lx membot = %lx)\n", memtop, membot); \ + xprintf(" (memtop = %p membot = %p)\n", memtop, membot); \ return; \ } #endif memalign_t -malloc(nbytes) - size_t nbytes; +malloc(size_t nbytes) { #ifndef lint union overhead *p; @@ -193,13 +203,13 @@ malloc(nbytes) */ if (nextf[bucket] == NULL) morecore(bucket); - if ((p = (union overhead *) nextf[bucket]) == NULL) { + if ((p = nextf[bucket]) == NULL) { child++; #ifndef DEBUG - stderror(ERR_NOMEM); + out_of_memory(); #else showall(NULL, NULL); - xprintf(CGETS(19, 1, "nbytes=%d: Out of memory\n"), nbytes); + xprintf(CGETS(19, 1, "nbytes=%zu: Out of memory\n"), nbytes); abort(); #endif /* fool lint */ @@ -232,8 +242,7 @@ malloc(nbytes) * Allocate more memory to the indicated bucket. */ static void -morecore(bucket) - int bucket; +morecore(int bucket) { union overhead *op; int rnu; /* 2^rnu bytes will be requested */ @@ -251,14 +260,14 @@ morecore(bucket) if (membot == NULL) membot = memtop; if ((long) op & 0x3ff) { - memtop = (char *) sbrk((int) (1024 - ((long) op & 0x3ff))); + memtop = sbrk((int) (1024 - ((long) op & 0x3ff))); memtop += (long) (1024 - ((long) op & 0x3ff)); } /* take 2k unless the block is bigger than that */ rnu = (bucket <= 8) ? 11 : bucket + 3; nblks = 1 << (rnu - (bucket + 3)); /* how many blocks to get */ - memtop = (char *) sbrk(1 << rnu); /* PWP */ + memtop = sbrk(1 << rnu); /* PWP */ op = (union overhead *) memtop; /* no more room! */ if ((long) op == -1) @@ -287,8 +296,7 @@ morecore(bucket) #endif void -free(cp) - ptr_t cp; +free(ptr_t cp) { #ifndef lint int size; @@ -301,22 +309,22 @@ free(cp) if (cp == NULL || dont_free) return; CHECK(!memtop || !membot, - CGETS(19, 2, "free(%lx) called before any allocations."), cp); + CGETS(19, 2, "free(%p) called before any allocations."), cp); CHECK(cp > (ptr_t) memtop, - CGETS(19, 3, "free(%lx) above top of memory."), cp); + CGETS(19, 3, "free(%p) above top of memory."), cp); CHECK(cp < (ptr_t) membot, - CGETS(19, 4, "free(%lx) below bottom of memory."), cp); + CGETS(19, 4, "free(%p) below bottom of memory."), cp); op = (union overhead *) (((caddr_t) cp) - MEMALIGN(sizeof(union overhead))); CHECK(op->ov_magic != MAGIC, - CGETS(19, 5, "free(%lx) bad block."), cp); + CGETS(19, 5, "free(%p) bad block."), cp); #ifdef RCHECK if (op->ov_index <= 13) CHECK(*(U_int *) ((caddr_t) op + op->ov_size + 1 - RSLOP) != RMAGIC, - CGETS(19, 6, "free(%lx) bad range check."), cp); + CGETS(19, 6, "free(%p) bad range check."), cp); #endif CHECK(op->ov_index >= NBUCKETS, - CGETS(19, 7, "free(%lx) bad block index."), cp); + CGETS(19, 7, "free(%p) bad block index."), cp); size = op->ov_index; op->ov_next = nextf[size]; nextf[size] = op; @@ -330,20 +338,16 @@ free(cp) } memalign_t -calloc(i, j) - size_t i, j; +calloc(size_t i, size_t j) { #ifndef lint - char *cp, *scp; + char *cp; i *= j; - scp = cp = (char *) xmalloc((size_t) i); - if (i != 0) - do - *cp++ = 0; - while (--i); + cp = xmalloc(i); + memset(cp, 0, i); - return ((memalign_t) scp); + return ((memalign_t) cp); #else if (i && j) return ((memalign_t) 0); @@ -369,9 +373,7 @@ static int realloc_srchlen = 4; #endif /* lint */ memalign_t -realloc(cp, nbytes) - ptr_t cp; - size_t nbytes; +realloc(ptr_t cp, size_t nbytes) { #ifndef lint U_int onb; @@ -423,8 +425,7 @@ realloc(cp, nbytes) * smaller of the old and new size */ onb = (1 << (i + 3)) - MEMALIGN(sizeof(union overhead)) - RSLOP; - (void) memmove((ptr_t) res, (ptr_t) cp, - (size_t) (onb < nbytes ? onb : nbytes)); + (void) memmove(res, cp, onb < nbytes ? onb : nbytes); } if (was_alloced) free(cp); @@ -446,9 +447,7 @@ realloc(cp, nbytes) * Return bucket number, or -1 if not found. */ static int -findbucket(freep, srchlen) - union overhead *freep; - int srchlen; +findbucket(union overhead *freep, int srchlen) { union overhead *p; size_t i; @@ -483,8 +482,7 @@ findbucket(freep, srchlen) ** Also we call our error routine if we run out of memory. **/ memalign_t -smalloc(n) - size_t n; +smalloc(size_t n) { ptr_t ptr; @@ -492,26 +490,22 @@ smalloc(n) #ifdef HAVE_SBRK if (membot == NULL) - membot = (char*) sbrk(0); + membot = sbrk(0); #endif /* HAVE_SBRK */ - if ((ptr = malloc(n)) == (ptr_t) 0) { - child++; - stderror(ERR_NOMEM); - } + if ((ptr = malloc(n)) == NULL) + out_of_memory(); #ifndef HAVE_SBRK if (memtop < ((char *) ptr) + n) memtop = ((char *) ptr) + n; if (membot == NULL) - membot = (char*) ptr; + membot = ptr; #endif /* !HAVE_SBRK */ return ((memalign_t) ptr); } memalign_t -srealloc(p, n) - ptr_t p; - size_t n; +srealloc(ptr_t p, size_t n) { ptr_t ptr; @@ -519,27 +513,23 @@ srealloc(p, n) #ifdef HAVE_SBRK if (membot == NULL) - membot = (char*) sbrk(0); + membot = sbrk(0); #endif /* HAVE_SBRK */ - if ((ptr = (p ? realloc(p, n) : malloc(n))) == (ptr_t) 0) { - child++; - stderror(ERR_NOMEM); - } + if ((ptr = (p ? realloc(p, n) : malloc(n))) == NULL) + out_of_memory(); #ifndef HAVE_SBRK if (memtop < ((char *) ptr) + n) memtop = ((char *) ptr) + n; if (membot == NULL) - membot = (char*) ptr; + membot = ptr; #endif /* !HAVE_SBRK */ return ((memalign_t) ptr); } memalign_t -scalloc(s, n) - size_t s, n; +scalloc(size_t s, size_t n) { - char *sptr; ptr_t ptr; n *= s; @@ -547,33 +537,26 @@ scalloc(s, n) #ifdef HAVE_SBRK if (membot == NULL) - membot = (char*) sbrk(0); + membot = sbrk(0); #endif /* HAVE_SBRK */ - if ((ptr = malloc(n)) == (ptr_t) 0) { - child++; - stderror(ERR_NOMEM); - } + if ((ptr = malloc(n)) == NULL) + out_of_memory(); - sptr = (char *) ptr; - if (n != 0) - do - *sptr++ = 0; - while (--n); + memset (ptr, 0, n); #ifndef HAVE_SBRK if (memtop < ((char *) ptr) + n) memtop = ((char *) ptr) + n; if (membot == NULL) - membot = (char*) ptr; + membot = ptr; #endif /* !HAVE_SBRK */ return ((memalign_t) ptr); } void -sfree(p) - ptr_t p; +sfree(ptr_t p) { if (p && !dont_free) free(p); @@ -590,9 +573,7 @@ sfree(p) */ /*ARGSUSED*/ void -showall(v, c) - Char **v; - struct command *c; +showall(Char **v, struct command *c) { #ifndef SYSMALLOC size_t i, j; @@ -603,12 +584,12 @@ showall(v, c) for (i = 0; i < NBUCKETS; i++) { for (j = 0, p = nextf[i]; p; p = p->ov_next, j++) continue; - xprintf(" %4d", j); + xprintf(" %4zd", j); totfree += j * (1 << (i + 3)); } xprintf(CGETS(19, 9, "\nused:\t")); for (i = 0; i < NBUCKETS; i++) { - xprintf(" %4u", nmalloc[i]); + xprintf(" %4d", nmalloc[i]); totused += nmalloc[i] * (1 << (i + 3)); } xprintf(CGETS(19, 10, "\n\tTotal in use: %d, total free: %d\n"), @@ -619,7 +600,7 @@ showall(v, c) (unsigned long) sbrk(0)); #else #ifdef HAVE_SBRK - memtop = (char *) sbrk(0); + memtop = sbrk(0); #endif /* HAVE_SBRK */ xprintf(CGETS(19, 12, "Allocated memory from 0x%lx to 0x%lx (%ld).\n"), (unsigned long) membot, (unsigned long) memtop, |