diff --git a/lalloc.c b/lalloc.c index 7dd771a..1e4961b 100644 --- a/lalloc.c +++ b/lalloc.c @@ -1,6 +1,6 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/lalloc.c,v 1.4 2009/03/24 08:37:37 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/lalloc.c,v 1.5 2009/03/24 08:53:45 tg Exp $"); #ifndef SIZE_MAX #ifdef SIZE_T_MAX @@ -10,66 +10,57 @@ __RCSID("$MirOS: src/bin/mksh/lalloc.c,v 1.4 2009/03/24 08:37:37 tg Exp $"); #endif #endif -struct lalloc { - struct lalloc *next; /* entry pointer, must be first */ -}; - -static void findptr(struct lalloc **, struct lalloc **, char *, Area *); +static struct lalloc *findptr(struct lalloc **, char *, Area *); void ainit(Area *ap) { - ap->ent = NULL; + ap->next = NULL; } -static void -findptr(struct lalloc **lpp, struct lalloc **ppp, char *ptr, Area *ap) +static struct lalloc * +findptr(struct lalloc **lpp, char *ptr, Area *ap) { *lpp = (struct lalloc *)(ptr - sizeof (struct lalloc)); - *ppp = (struct lalloc *)ap; - while ((*ppp)->next != *lpp) - if (((*ppp) = (*ppp)->next) == NULL) - internal_errorf("pointer %p not in group %p", ptr, ap); + while (ap->next != *lpp) + if ((ap = ap->next) == NULL) + internal_errorf("rogue pointer %p", ptr); + return (ap); } void * aresize(void *ptr, size_t numb, Area *ap) { - struct lalloc *lp, *pp; + struct lalloc *lp; - if (numb >= SIZE_MAX - sizeof (struct lalloc)) - goto failure; + if (ptr != NULL) { + struct lalloc *pp; - if (ptr == NULL) { - pp = (struct lalloc *)ap; - lp = malloc(numb + sizeof (struct lalloc)); - } else { - findptr(&lp, &pp, ptr, ap); - lp = realloc(lp, numb + sizeof (struct lalloc)); + pp = findptr(&lp, ptr, ap); + pp->next = lp->next; } - if (lp == NULL) { - failure: + + if ((numb >= SIZE_MAX - sizeof (struct lalloc)) || + /* here I wish all realloc(3)s would take NULL */ + (lp = ptr ? realloc(lp, numb + sizeof (struct lalloc)) : + malloc(numb + sizeof (struct lalloc))) == NULL) internal_errorf("cannot allocate %lu data bytes", (unsigned long)numb); - } - if (ptr == NULL) { - lp->next = ap->ent; - } - pp->next = lp; + lp->next = ap->next; + ap->next = lp; return ((char *)lp + sizeof (struct lalloc)); } void afree(void *ptr, Area *ap) { - struct lalloc *lp, *pp; + if (ptr != NULL) { + struct lalloc *lp, *pp; - if (ptr == NULL) - return; - - findptr(&lp, &pp, ptr, ap); - pp->next = lp->next; - free(lp); + pp = findptr(&lp, ptr, ap); + pp->next = lp->next; + free(lp); + } } void @@ -77,8 +68,8 @@ afreeall(Area *ap) { struct lalloc *lp; - while ((lp = ap->ent) != NULL) { - ap->ent = lp->next; + while ((lp = ap->next) != NULL) { + ap->next = lp->next; free(lp); } } diff --git a/sh.h b/sh.h index 04d5733..e49988e 100644 --- a/sh.h +++ b/sh.h @@ -102,7 +102,7 @@ #define __SCCSID(x) __IDSTRING(sccsid,x) #ifdef EXTERN -__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.284 2009/03/22 18:50:43 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.285 2009/03/24 08:53:45 tg Exp $"); #endif #define MKSH_VERSION "R37 2009/03/22" @@ -398,8 +398,8 @@ char *ucstrstr(char *, const char *); /* * simple grouping allocator */ -typedef struct { - void *ent; /* entry pointer, must be first */ +typedef struct lalloc { + struct lalloc *next; } Area; EXTERN Area aperm; /* permanent object space */