mksh/lalloc.c

73 lines
1.4 KiB
C
Raw Normal View History

#include "sh.h"
2009-04-07 20:46:07 +02:00
__RCSID("$MirOS: src/bin/mksh/lalloc.c,v 1.8 2009/04/07 18:46:07 tg Exp $");
/* build with CPPFLAGS+= -DUSE_REALLOC_MALLOC=0 on ancient systems */
#if defined(USE_REALLOC_MALLOC) && (USE_REALLOC_MALLOC == 0)
#define remalloc(p,n) ((p) == NULL ? malloc(n) : realloc((p), (n)))
#else
#define remalloc(p,n) realloc((p), (n))
#endif
static struct lalloc *findptr(struct lalloc **, char *, Area *);
void
ainit(Area *ap)
{
ap->next = NULL;
}
static struct lalloc *
findptr(struct lalloc **lpp, char *ptr, Area *ap)
{
*lpp = (struct lalloc *)(ptr - sizeof (struct lalloc));
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 = NULL;
if (ptr != NULL) {
struct lalloc *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)
internal_errorf("cannot allocate %lu data bytes",
(unsigned long)numb);
lp->next = ap->next;
ap->next = lp;
return ((char *)lp + sizeof (struct lalloc));
}
void
afree(void *ptr, Area *ap)
{
if (ptr != NULL) {
struct lalloc *lp, *pp;
pp = findptr(&lp, ptr, ap);
pp->next = lp->next;
free(lp);
}
}
void
afreeall(Area *ap)
{
struct lalloc *lp;
while ((lp = ap->next) != NULL) {
ap->next = lp->next;
free(lp);
}
}