employ some "nice" constants and comment lalloc.c

This commit is contained in:
tg 2009-04-07 18:56:51 +00:00
parent 4de58fe6fa
commit d43f4efe60
2 changed files with 36 additions and 15 deletions

View File

@ -1,6 +1,6 @@
#include "sh.h" #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 */ /* build with CPPFLAGS+= -DUSE_REALLOC_MALLOC=0 on ancient systems */
#if defined(USE_REALLOC_MALLOC) && (USE_REALLOC_MALLOC == 0) #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)) #define remalloc(p,n) realloc((p), (n))
#endif #endif
static struct lalloc *findptr(struct lalloc **, char *, Area *); static ALLOC_ITEM *findptr(ALLOC_ITEM **, char *, Area *);
void void
ainit(Area *ap) ainit(Area *ap)
{ {
/* area pointer is an ALLOC_ITEM, just the head of the list */
ap->next = NULL; ap->next = NULL;
} }
static struct lalloc * static ALLOC_ITEM *
findptr(struct lalloc **lpp, char *ptr, Area *ap) 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) while (ap->next != *lpp)
if ((ap = ap->next) == NULL) if ((ap = ap->next) == NULL)
internal_errorf("rogue pointer %p", ptr); internal_errorf("rogue pointer %p", ptr);
@ -30,32 +33,37 @@ findptr(struct lalloc **lpp, char *ptr, Area *ap)
void * void *
aresize(void *ptr, size_t numb, Area *ap) aresize(void *ptr, size_t numb, Area *ap)
{ {
struct lalloc *lp = NULL; ALLOC_ITEM *lp = NULL;
/* resizing (true) or newly allocating? */
if (ptr != NULL) { if (ptr != NULL) {
struct lalloc *pp; ALLOC_ITEM *pp;
pp = findptr(&lp, ptr, ap); pp = findptr(&lp, ptr, ap);
pp->next = lp->next; pp->next = lp->next;
} }
if ((numb >= SIZE_MAX - sizeof (struct lalloc)) || if ((numb >= SIZE_MAX - ALLOC_SIZE) ||
(lp = remalloc(lp, numb + sizeof (struct lalloc))) == NULL) (lp = remalloc(lp, numb + ALLOC_SIZE)) == NULL)
internal_errorf("cannot allocate %lu data bytes", internal_errorf("cannot allocate %lu data bytes",
(unsigned long)numb); (unsigned long)numb);
/* this only works because Area is an ALLOC_ITEM */
lp->next = ap->next; lp->next = ap->next;
ap->next = lp; ap->next = lp;
return ((char *)lp + sizeof (struct lalloc)); /* return user item address */
return ((char *)lp + ALLOC_SIZE);
} }
void void
afree(void *ptr, Area *ap) afree(void *ptr, Area *ap)
{ {
if (ptr != NULL) { if (ptr != NULL) {
struct lalloc *lp, *pp; ALLOC_ITEM *lp, *pp;
pp = findptr(&lp, ptr, ap); pp = findptr(&lp, ptr, ap);
/* unhook */
pp->next = lp->next; pp->next = lp->next;
/* now free ALLOC_ITEM */
free(lp); free(lp);
} }
} }
@ -63,10 +71,13 @@ afree(void *ptr, Area *ap)
void void
afreeall(Area *ap) afreeall(Area *ap)
{ {
struct lalloc *lp; ALLOC_ITEM *lp;
/* traverse group (linked list) */
while ((lp = ap->next) != NULL) { while ((lp = ap->next) != NULL) {
/* make next ALLOC_ITEM head of list */
ap->next = lp->next; ap->next = lp->next;
/* free old head */
free(lp); free(lp);
} }
} }

16
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.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 #endif
#define MKSH_VERSION "R37 2009/04/05" #define MKSH_VERSION "R37 2009/04/05"
@ -423,9 +423,19 @@ char *ucstrstr(char *, const char *);
/* /*
* simple grouping allocator * simple grouping allocator
*/ */
typedef struct lalloc {
/* 1. internal structure */
struct lalloc {
struct lalloc *next; 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 */ EXTERN Area aperm; /* permanent object space */
#define APERM &aperm #define APERM &aperm