further simplify

XXX check whose platforms’ realloc(3) don’t take NULL
This commit is contained in:
tg 2009-03-24 08:53:45 +00:00
parent ebfce0fafd
commit c7b2af502e
2 changed files with 32 additions and 41 deletions

View File

@ -1,6 +1,6 @@
#include "sh.h" #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 #ifndef SIZE_MAX
#ifdef SIZE_T_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
#endif #endif
struct lalloc { static struct lalloc *findptr(struct lalloc **, char *, Area *);
struct lalloc *next; /* entry pointer, must be first */
};
static void findptr(struct lalloc **, struct lalloc **, char *, Area *);
void void
ainit(Area *ap) ainit(Area *ap)
{ {
ap->ent = NULL; ap->next = NULL;
} }
static void static struct lalloc *
findptr(struct lalloc **lpp, struct lalloc **ppp, char *ptr, Area *ap) findptr(struct lalloc **lpp, char *ptr, Area *ap)
{ {
*lpp = (struct lalloc *)(ptr - sizeof (struct lalloc)); *lpp = (struct lalloc *)(ptr - sizeof (struct lalloc));
*ppp = (struct lalloc *)ap; while (ap->next != *lpp)
while ((*ppp)->next != *lpp) if ((ap = ap->next) == NULL)
if (((*ppp) = (*ppp)->next) == NULL) internal_errorf("rogue pointer %p", ptr);
internal_errorf("pointer %p not in group %p", ptr, ap); return (ap);
} }
void * void *
aresize(void *ptr, size_t numb, Area *ap) aresize(void *ptr, size_t numb, Area *ap)
{ {
struct lalloc *lp, *pp; struct lalloc *lp;
if (numb >= SIZE_MAX - sizeof (struct lalloc)) if (ptr != NULL) {
goto failure; struct lalloc *pp;
if (ptr == NULL) { pp = findptr(&lp, ptr, ap);
pp = (struct lalloc *)ap; pp->next = lp->next;
lp = malloc(numb + sizeof (struct lalloc));
} else {
findptr(&lp, &pp, ptr, ap);
lp = realloc(lp, numb + sizeof (struct lalloc));
} }
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", internal_errorf("cannot allocate %lu data bytes",
(unsigned long)numb); (unsigned long)numb);
} lp->next = ap->next;
if (ptr == NULL) { ap->next = lp;
lp->next = ap->ent;
}
pp->next = lp;
return ((char *)lp + sizeof (struct lalloc)); return ((char *)lp + sizeof (struct lalloc));
} }
void void
afree(void *ptr, Area *ap) afree(void *ptr, Area *ap)
{ {
struct lalloc *lp, *pp; if (ptr != NULL) {
struct lalloc *lp, *pp;
if (ptr == NULL) pp = findptr(&lp, ptr, ap);
return; pp->next = lp->next;
free(lp);
findptr(&lp, &pp, ptr, ap); }
pp->next = lp->next;
free(lp);
} }
void void
@ -77,8 +68,8 @@ afreeall(Area *ap)
{ {
struct lalloc *lp; struct lalloc *lp;
while ((lp = ap->ent) != NULL) { while ((lp = ap->next) != NULL) {
ap->ent = lp->next; ap->next = lp->next;
free(lp); free(lp);
} }
} }

6
sh.h
View File

@ -102,7 +102,7 @@
#define __SCCSID(x) __IDSTRING(sccsid,x) #define __SCCSID(x) __IDSTRING(sccsid,x)
#ifdef EXTERN #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 #endif
#define MKSH_VERSION "R37 2009/03/22" #define MKSH_VERSION "R37 2009/03/22"
@ -398,8 +398,8 @@ char *ucstrstr(char *, const char *);
/* /*
* simple grouping allocator * simple grouping allocator
*/ */
typedef struct { typedef struct lalloc {
void *ent; /* entry pointer, must be first */ struct lalloc *next;
} Area; } Area;
EXTERN Area aperm; /* permanent object space */ EXTERN Area aperm; /* permanent object space */