2009-03-22 17:55:38 +01:00
|
|
|
#include "sh.h"
|
|
|
|
|
2009-04-07 20:56:51 +02:00
|
|
|
__RCSID("$MirOS: src/bin/mksh/lalloc.c,v 1.9 2009/04/07 18:56:51 tg Exp $");
|
2009-03-22 17:55:38 +01:00
|
|
|
|
2009-03-24 19:34:39 +01:00
|
|
|
/* 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
|
|
|
|
|
2009-04-07 20:56:51 +02:00
|
|
|
static ALLOC_ITEM *findptr(ALLOC_ITEM **, char *, Area *);
|
2009-03-22 17:55:38 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
ainit(Area *ap)
|
|
|
|
{
|
2009-04-07 20:56:51 +02:00
|
|
|
/* area pointer is an ALLOC_ITEM, just the head of the list */
|
2009-03-24 09:53:45 +01:00
|
|
|
ap->next = NULL;
|
2009-03-22 17:55:38 +01:00
|
|
|
}
|
|
|
|
|
2009-04-07 20:56:51 +02:00
|
|
|
static ALLOC_ITEM *
|
|
|
|
findptr(ALLOC_ITEM **lpp, char *ptr, Area *ap)
|
2009-03-22 17:55:38 +01:00
|
|
|
{
|
2009-04-07 20:56:51 +02:00
|
|
|
/* get address of ALLOC_ITEM from user item */
|
|
|
|
*lpp = (ALLOC_ITEM *)(ptr - ALLOC_SIZE);
|
|
|
|
/* search for allocation item in group list */
|
2009-03-24 09:53:45 +01:00
|
|
|
while (ap->next != *lpp)
|
|
|
|
if ((ap = ap->next) == NULL)
|
|
|
|
internal_errorf("rogue pointer %p", ptr);
|
|
|
|
return (ap);
|
2009-03-22 17:55:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
aresize(void *ptr, size_t numb, Area *ap)
|
|
|
|
{
|
2009-04-07 20:56:51 +02:00
|
|
|
ALLOC_ITEM *lp = NULL;
|
2009-03-22 17:55:38 +01:00
|
|
|
|
2009-04-07 20:56:51 +02:00
|
|
|
/* resizing (true) or newly allocating? */
|
2009-03-24 09:53:45 +01:00
|
|
|
if (ptr != NULL) {
|
2009-04-07 20:56:51 +02:00
|
|
|
ALLOC_ITEM *pp;
|
2009-03-22 17:55:38 +01:00
|
|
|
|
2009-03-24 09:53:45 +01:00
|
|
|
pp = findptr(&lp, ptr, ap);
|
|
|
|
pp->next = lp->next;
|
2009-03-22 17:55:38 +01:00
|
|
|
}
|
2009-03-24 09:53:45 +01:00
|
|
|
|
2009-04-07 20:56:51 +02:00
|
|
|
if ((numb >= SIZE_MAX - ALLOC_SIZE) ||
|
|
|
|
(lp = remalloc(lp, numb + ALLOC_SIZE)) == NULL)
|
2009-03-23 10:08:35 +01:00
|
|
|
internal_errorf("cannot allocate %lu data bytes",
|
|
|
|
(unsigned long)numb);
|
2009-04-07 20:56:51 +02:00
|
|
|
/* this only works because Area is an ALLOC_ITEM */
|
2009-03-24 09:53:45 +01:00
|
|
|
lp->next = ap->next;
|
|
|
|
ap->next = lp;
|
2009-04-07 20:56:51 +02:00
|
|
|
/* return user item address */
|
|
|
|
return ((char *)lp + ALLOC_SIZE);
|
2009-03-22 17:55:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
afree(void *ptr, Area *ap)
|
|
|
|
{
|
2009-03-24 09:53:45 +01:00
|
|
|
if (ptr != NULL) {
|
2009-04-07 20:56:51 +02:00
|
|
|
ALLOC_ITEM *lp, *pp;
|
2009-03-22 17:55:38 +01:00
|
|
|
|
2009-03-24 09:53:45 +01:00
|
|
|
pp = findptr(&lp, ptr, ap);
|
2009-04-07 20:56:51 +02:00
|
|
|
/* unhook */
|
2009-03-24 09:53:45 +01:00
|
|
|
pp->next = lp->next;
|
2009-04-07 20:56:51 +02:00
|
|
|
/* now free ALLOC_ITEM */
|
2009-03-24 09:53:45 +01:00
|
|
|
free(lp);
|
|
|
|
}
|
2009-03-22 17:55:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
afreeall(Area *ap)
|
|
|
|
{
|
2009-04-07 20:56:51 +02:00
|
|
|
ALLOC_ITEM *lp;
|
2009-03-22 17:55:38 +01:00
|
|
|
|
2009-04-07 20:56:51 +02:00
|
|
|
/* traverse group (linked list) */
|
2009-03-24 09:53:45 +01:00
|
|
|
while ((lp = ap->next) != NULL) {
|
2009-04-07 20:56:51 +02:00
|
|
|
/* make next ALLOC_ITEM head of list */
|
2009-03-24 09:53:45 +01:00
|
|
|
ap->next = lp->next;
|
2009-04-07 20:56:51 +02:00
|
|
|
/* free old head */
|
2009-03-22 17:55:38 +01:00
|
|
|
free(lp);
|
|
|
|
}
|
|
|
|
}
|