2009-05-16 18:59:42 +02:00
|
|
|
|
/*-
|
|
|
|
|
* Copyright © 2009
|
|
|
|
|
* Thorsten Glaser <tg@mirbsd.org>
|
|
|
|
|
*
|
|
|
|
|
* Provided that these terms and disclaimer and all copyright notices
|
|
|
|
|
* are retained or reproduced in an accompanying document, permission
|
|
|
|
|
* is granted to deal in this work without restriction, including un‐
|
|
|
|
|
* limited rights to use, publicly perform, distribute, sell, modify,
|
|
|
|
|
* merge, give away, or sublicence.
|
|
|
|
|
*
|
|
|
|
|
* This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to
|
|
|
|
|
* the utmost extent permitted by applicable law, neither express nor
|
|
|
|
|
* implied; without malicious intent or gross negligence. In no event
|
|
|
|
|
* may a licensor, author or contributor be held liable for indirect,
|
|
|
|
|
* direct, other damage, loss, or other issues arising in any way out
|
|
|
|
|
* of dealing in the work, even if advised of the possibility of such
|
|
|
|
|
* damage or existence of a defect, except proven that it results out
|
|
|
|
|
* of said person’s immediate fault when using the work as intended.
|
|
|
|
|
*/
|
|
|
|
|
|
2009-03-22 17:55:38 +01:00
|
|
|
|
#include "sh.h"
|
|
|
|
|
|
2009-08-08 15:08:53 +02:00
|
|
|
|
__RCSID("$MirOS: src/bin/mksh/lalloc.c,v 1.11 2009/08/08 13:08: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-08-08 15:08:53 +02:00
|
|
|
|
#define ALLOC_ISUNALIGNED(p) (((ptrdiff_t)(p)) % ALLOC_SIZE)
|
|
|
|
|
|
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-08-08 15:08:53 +02:00
|
|
|
|
void *lp;
|
|
|
|
|
|
|
|
|
|
#ifndef MKSH_SMALL
|
|
|
|
|
if (ALLOC_ISUNALIGNED(ptr))
|
|
|
|
|
goto fail;
|
|
|
|
|
#endif
|
2009-04-07 20:56:51 +02:00
|
|
|
|
/* get address of ALLOC_ITEM from user item */
|
2009-08-08 15:08:53 +02:00
|
|
|
|
/*
|
|
|
|
|
* note: the alignment of "ptr" to ALLOC_SIZE is checked
|
|
|
|
|
* above; the "void *" gets us rid of a gcc 2.95 warning
|
|
|
|
|
*/
|
|
|
|
|
*lpp = (lp = ptr - ALLOC_SIZE);
|
2009-04-07 20:56:51 +02:00
|
|
|
|
/* search for allocation item in group list */
|
2009-08-08 15:08:53 +02:00
|
|
|
|
while (ap->next != lp)
|
|
|
|
|
if ((ap = ap->next) == NULL) {
|
|
|
|
|
#ifndef MKSH_SMALL
|
|
|
|
|
fail:
|
|
|
|
|
#endif
|
2009-03-24 09:53:45 +01:00
|
|
|
|
internal_errorf("rogue pointer %p", ptr);
|
2009-08-08 15:08:53 +02:00
|
|
|
|
}
|
2009-03-24 09:53:45 +01:00
|
|
|
|
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) ||
|
2009-08-08 15:08:53 +02:00
|
|
|
|
(lp = remalloc(lp, numb + ALLOC_SIZE)) == NULL
|
|
|
|
|
#ifndef MKSH_SMALL
|
|
|
|
|
|| ALLOC_ISUNALIGNED(lp)
|
|
|
|
|
#endif
|
|
|
|
|
)
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|