2009-05-16 18:59:42 +02:00
|
|
|
/*-
|
2014-11-25 21:00:39 +01:00
|
|
|
* Copyright (c) 2009, 2010, 2011, 2013, 2014
|
2009-05-16 18:59:42 +02:00
|
|
|
* Thorsten Glaser <tg@mirbsd.org>
|
|
|
|
*
|
|
|
|
* Provided that these terms and disclaimer and all copyright notices
|
|
|
|
* are retained or reproduced in an accompanying document, permission
|
2010-08-28 22:22:24 +02:00
|
|
|
* is granted to deal in this work without restriction, including un-
|
2009-05-16 18:59:42 +02:00
|
|
|
* limited rights to use, publicly perform, distribute, sell, modify,
|
|
|
|
* merge, give away, or sublicence.
|
|
|
|
*
|
2010-08-28 22:22:24 +02:00
|
|
|
* This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
|
2009-05-16 18:59:42 +02:00
|
|
|
* 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
|
2010-08-28 22:22:24 +02:00
|
|
|
* of said person's immediate fault when using the work as intended.
|
2009-05-16 18:59:42 +02:00
|
|
|
*/
|
|
|
|
|
2009-03-22 17:55:38 +01:00
|
|
|
#include "sh.h"
|
|
|
|
|
2014-11-25 21:00:39 +01:00
|
|
|
__RCSID("$MirOS: src/bin/mksh/lalloc.c,v 1.21 2014/11/25 20:00:39 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)
|
2011-03-05 22:48:09 +01:00
|
|
|
#define remalloc(p,n) ((p) == NULL ? malloc_osi(n) : realloc_osi((p), (n)))
|
2009-03-24 19:34:39 +01:00
|
|
|
#else
|
2011-03-05 22:48:09 +01:00
|
|
|
#define remalloc(p,n) realloc_osi((p), (n))
|
2009-03-24 19:34:39 +01:00
|
|
|
#endif
|
|
|
|
|
2014-11-25 21:00:39 +01:00
|
|
|
#define ALLOC_ISUNALIGNED(p) (((size_t)(p)) % ALLOC_SIZE)
|
2009-08-08 15:08:53 +02:00
|
|
|
|
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
|
2011-03-13 11:50:44 +01:00
|
|
|
#ifdef DEBUG
|
2011-08-27 20:06:52 +02:00
|
|
|
internal_warningf("rogue pointer %zX in ap %zX",
|
|
|
|
(size_t)ptr, (size_t)ap);
|
2011-03-13 11:50:44 +01:00
|
|
|
/* try to get a coredump */
|
|
|
|
abort();
|
|
|
|
#else
|
2011-08-27 20:06:52 +02:00
|
|
|
internal_errorf("rogue pointer %zX", (size_t)ptr);
|
2011-03-13 11:50:44 +01:00
|
|
|
#endif
|
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
|
|
|
}
|
|
|
|
|
2010-09-14 23:26:19 +02:00
|
|
|
void *
|
|
|
|
aresize2(void *ptr, size_t fac1, size_t fac2, Area *ap)
|
|
|
|
{
|
2011-01-09 22:57:29 +01:00
|
|
|
if (notoktomul(fac1, fac2))
|
2011-09-07 17:24:22 +02:00
|
|
|
internal_errorf(Tintovfl, fac1, '*', fac2);
|
2010-09-14 23:26:19 +02:00
|
|
|
return (aresize(ptr, fac1 * fac2, 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
|
|
|
|
2010-09-14 23:26:19 +02:00
|
|
|
if (notoktoadd(numb, ALLOC_SIZE) ||
|
2009-08-08 15:08:53 +02:00
|
|
|
(lp = remalloc(lp, numb + ALLOC_SIZE)) == NULL
|
|
|
|
#ifndef MKSH_SMALL
|
|
|
|
|| ALLOC_ISUNALIGNED(lp)
|
|
|
|
#endif
|
|
|
|
)
|
2013-06-04 00:28:33 +02:00
|
|
|
internal_errorf(Toomem, 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 */
|
2011-03-05 22:48:09 +01:00
|
|
|
free_osimalloc(lp);
|
2009-03-24 09:53:45 +01:00
|
|
|
}
|
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 */
|
2011-03-05 22:48:09 +01:00
|
|
|
free_osimalloc(lp);
|
2009-03-22 17:55:38 +01:00
|
|
|
}
|
|
|
|
}
|