From d43f4efe6075555041ceacb2a50e8a15ff8244f0 Mon Sep 17 00:00:00 2001 From: tg Date: Tue, 7 Apr 2009 18:56:51 +0000 Subject: [PATCH] employ some "nice" constants and comment lalloc.c --- lalloc.c | 35 +++++++++++++++++++++++------------ sh.h | 16 +++++++++++++--- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/lalloc.c b/lalloc.c index 8803038..bc3b28e 100644 --- a/lalloc.c +++ b/lalloc.c @@ -1,6 +1,6 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/lalloc.c,v 1.8 2009/04/07 18:46:07 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/lalloc.c,v 1.9 2009/04/07 18:56:51 tg Exp $"); /* build with CPPFLAGS+= -DUSE_REALLOC_MALLOC=0 on ancient systems */ #if defined(USE_REALLOC_MALLOC) && (USE_REALLOC_MALLOC == 0) @@ -9,18 +9,21 @@ __RCSID("$MirOS: src/bin/mksh/lalloc.c,v 1.8 2009/04/07 18:46:07 tg Exp $"); #define remalloc(p,n) realloc((p), (n)) #endif -static struct lalloc *findptr(struct lalloc **, char *, Area *); +static ALLOC_ITEM *findptr(ALLOC_ITEM **, char *, Area *); void ainit(Area *ap) { + /* area pointer is an ALLOC_ITEM, just the head of the list */ ap->next = NULL; } -static struct lalloc * -findptr(struct lalloc **lpp, char *ptr, Area *ap) +static ALLOC_ITEM * +findptr(ALLOC_ITEM **lpp, char *ptr, Area *ap) { - *lpp = (struct lalloc *)(ptr - sizeof (struct lalloc)); + /* get address of ALLOC_ITEM from user item */ + *lpp = (ALLOC_ITEM *)(ptr - ALLOC_SIZE); + /* search for allocation item in group list */ while (ap->next != *lpp) if ((ap = ap->next) == NULL) internal_errorf("rogue pointer %p", ptr); @@ -30,32 +33,37 @@ findptr(struct lalloc **lpp, char *ptr, Area *ap) void * aresize(void *ptr, size_t numb, Area *ap) { - struct lalloc *lp = NULL; + ALLOC_ITEM *lp = NULL; + /* resizing (true) or newly allocating? */ if (ptr != NULL) { - struct lalloc *pp; + ALLOC_ITEM *pp; pp = findptr(&lp, ptr, ap); pp->next = lp->next; } - if ((numb >= SIZE_MAX - sizeof (struct lalloc)) || - (lp = remalloc(lp, numb + sizeof (struct lalloc))) == NULL) + if ((numb >= SIZE_MAX - ALLOC_SIZE) || + (lp = remalloc(lp, numb + ALLOC_SIZE)) == NULL) internal_errorf("cannot allocate %lu data bytes", (unsigned long)numb); + /* this only works because Area is an ALLOC_ITEM */ lp->next = ap->next; ap->next = lp; - return ((char *)lp + sizeof (struct lalloc)); + /* return user item address */ + return ((char *)lp + ALLOC_SIZE); } void afree(void *ptr, Area *ap) { if (ptr != NULL) { - struct lalloc *lp, *pp; + ALLOC_ITEM *lp, *pp; pp = findptr(&lp, ptr, ap); + /* unhook */ pp->next = lp->next; + /* now free ALLOC_ITEM */ free(lp); } } @@ -63,10 +71,13 @@ afree(void *ptr, Area *ap) void afreeall(Area *ap) { - struct lalloc *lp; + ALLOC_ITEM *lp; + /* traverse group (linked list) */ while ((lp = ap->next) != NULL) { + /* make next ALLOC_ITEM head of list */ ap->next = lp->next; + /* free old head */ free(lp); } } diff --git a/sh.h b/sh.h index 01b12e2..c9db78d 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.291 2009/04/07 18:46:07 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.292 2009/04/07 18:56:51 tg Exp $"); #endif #define MKSH_VERSION "R37 2009/04/05" @@ -423,9 +423,19 @@ char *ucstrstr(char *, const char *); /* * simple grouping allocator */ -typedef struct lalloc { + +/* 1. internal structure */ +struct lalloc { struct lalloc *next; -} Area; +}; + +/* 2. sizes */ +#define ALLOC_ITEM struct lalloc +#define ALLOC_SIZE (sizeof (ALLOC_ITEM)) + +/* 3. group structure (only the same for lalloc.c) */ +typedef struct lalloc Area; + EXTERN Area aperm; /* permanent object space */ #define APERM &aperm