remove espie's double-linked-list based allocator and write a

similarily simple one from scratch, which however performs
better than espie's with AFREE_DEBUG enabled which took away
the benefit of the double-linked-list approach

all of (core) mksh is now MirOS licenced
This commit is contained in:
tg 2009-03-22 16:55:38 +00:00
parent 7c77d9369c
commit ca539f08f0
6 changed files with 103 additions and 153 deletions

View File

@ -1,9 +1,9 @@
#!/bin/sh
srcversion='$MirOS: src/bin/mksh/Build.sh,v 1.375 2008/12/31 16:09:51 tg Exp $'
srcversion='$MirOS: src/bin/mksh/Build.sh,v 1.376 2009/03/22 16:55:37 tg Exp $'
#-
# Environment used: CC CFLAGS CPPFLAGS LDFLAGS LIBS NOWARN NROFF TARGET_OS
# CPPFLAGS recognised: MKSH_SMALL MKSH_ASSUME_UTF8 MKSH_NOPWNAM MKSH_NOVI
# MKSH_CLS_STRING MKSH_AFREE_DEBUG MKSH_BINSHREDUCED
# MKSH_CLS_STRING MKSH_BINSHREDUCED
LC_ALL=C
export LC_ALL

View File

@ -1,16 +1,13 @@
# $MirOS: src/bin/mksh/Makefile,v 1.71 2008/12/13 17:02:10 tg Exp $
# $MirOS: src/bin/mksh/Makefile,v 1.72 2009/03/22 16:55:37 tg Exp $
#-
# use CPPFLAGS=-DDEBUG __CRAZY=Yes to check for certain more stuff
.include <bsd.own.mk>
PROG= mksh
SRCS= alloc.c edit.c eval.c exec.c expr.c funcs.c histrap.c \
jobs.c lex.c main.c misc.c shf.c syn.c tree.c var.c
SRCS= edit.c eval.c exec.c expr.c funcs.c histrap.c jobs.c \
lalloc.c lex.c main.c misc.c shf.c syn.c tree.c var.c
.if !make(test-build)
. if ${DEBUGLIBS:L} == "yes"
CPPFLAGS+= -DMKSH_AFREE_DEBUG # MirOS development version
. endif
CPPFLAGS+= -DMKSH_ASSUME_UTF8 \
-DHAVE_ATTRIBUTE=1 -DHAVE_ATTRIBUTE_BOUNDED=1 \
-DHAVE_ATTRIBUTE_USED=1 -DHAVE_SYS_PARAM_H=1 \

132
alloc.c
View File

@ -1,132 +0,0 @@
/* $OpenBSD: alloc.c,v 1.8 2008/07/21 17:30:08 millert Exp $ */
/*-
* Copyright (c) 2002 Marc Espie.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD
* PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*-
* area-based allocation built on malloc/free
*/
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/alloc.c,v 1.13 2008/12/13 17:02:11 tg Exp $");
struct link {
struct link *prev;
struct link *next;
};
Area *
ainit(Area *ap)
{
ap->freelist = NULL;
return (ap);
}
void
afreeall(Area *ap)
{
struct link *l, *l2;
for (l = ap->freelist; l != NULL; l = l2) {
l2 = l->next;
free(l);
}
ap->freelist = NULL;
}
#define L2P(l) ( (void *)(((char *)(l)) + sizeof (struct link)) )
#define P2L(p) ( (struct link *)(((ptrdiff_t)(p)) - sizeof (struct link)) )
void *
alloc(size_t size, Area *ap)
{
struct link *l;
if ((l = malloc(sizeof (struct link) + size)) == NULL)
internal_errorf("unable to allocate memory");
l->next = ap->freelist;
l->prev = NULL;
if (ap->freelist)
ap->freelist->prev = l;
ap->freelist = l;
return (L2P(l));
}
void *
aresize(void *ptr, size_t size, Area *ap)
{
struct link *l, *l2, *lprev, *lnext;
if (ptr == NULL)
return (alloc(size, ap));
l = P2L(ptr);
lprev = l->prev;
lnext = l->next;
if ((l2 = realloc(l, sizeof (struct link) + size)) == NULL)
internal_errorf("unable to allocate memory");
if (lprev)
lprev->next = l2;
else
ap->freelist = l2;
if (lnext)
lnext->prev = l2;
return (L2P(l2));
}
void
afree(void *ptr, Area *ap)
{
struct link *l;
#ifdef MKSH_AFREE_DEBUG
struct link *lp;
#endif
if (!ptr)
return;
l = P2L(ptr);
#ifdef MKSH_AFREE_DEBUG
for (lp = ap->freelist; lp != NULL; lp = lp->next)
if (l == lp)
break;
if (lp == NULL) {
internal_warningf("afree: pointer %p not allocated", ptr);
exit(255);
}
#endif
if (l->prev)
l->prev->next = l->next;
else
ap->freelist = l->next;
if (l->next)
l->next->prev = l->prev;
free(l);
}

View File

@ -1,4 +1,4 @@
$MirOS: src/bin/mksh/copyright,v 1.27 2009/02/20 13:21:06 tg Exp $
$MirOS: src/bin/mksh/copyright,v 1.28 2009/03/22 16:55:38 tg Rel $
The MirBSD Korn Shell (mksh) is
@ -25,8 +25,6 @@ The developer of mksh recognises the efforts of the pdksh authors,
who had dedicated their work into Public Domain, and all contribu-
tors. See the documentation, CVS, and web site for details.
A number of files that are either always or conditionally included
during compilation, depending on the target operating environment,
are covered by different (2/3-clause BSD or similar) licences; re-
fer to the individual files for details: alloc.c (always included)
and setmode.c, strlcpy.c (included when required)
Depending on the target operating environment, setmode.c (3-clause
BSD licence) or strlcpy.c (ISC licence) may be added during compi-
lation; refer to these files for details.

86
lalloc.c Normal file
View File

@ -0,0 +1,86 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/lalloc.c,v 1.1 2009/03/22 16:55:38 tg Exp $");
struct lalloc {
struct lalloc *next; /* entry pointer, must be first */
Area *group; /* group backpointer */
};
static void findptr(struct lalloc **, struct lalloc **, char *, Area *);
void
ainit(Area *ap)
{
ap->ent = NULL;
}
static void
findptr(struct lalloc **lpp, struct lalloc **ppp, char *ptr, Area *ap)
{
*lpp = (struct lalloc *)(ptr - sizeof (struct lalloc));
if ((*lpp)->group != ap)
notfound:
internal_errorf("pointer %p not in group %p", ptr, ap);
*ppp = (struct lalloc *)ap;
while ((*ppp)->next != *lpp)
if (((*ppp) = (*ppp)->next) == NULL)
goto notfound;
}
void *
aresize(void *ptr, size_t numb, Area *ap)
{
struct lalloc *lp, *pp;
if (numb >= SIZE_MAX - sizeof (struct lalloc))
failure:
internal_errorf("cannot allocate %lu data bytes",
(unsigned long)numb);
if (ptr == NULL) {
pp = (struct lalloc *)ap;
lp = malloc(numb + sizeof (struct lalloc));
} else {
findptr(&lp, &pp, ptr, ap);
lp = realloc(lp, numb + sizeof (struct lalloc));
}
if (lp == NULL)
goto failure;
if (ptr == NULL) {
lp->group = ap;
lp->next = ap->ent;
}
pp->next = lp;
return ((char *)lp + sizeof (struct lalloc));
}
void
afree(void *ptr, Area *ap)
{
struct lalloc *lp, *pp;
if (ptr == NULL)
return;
findptr(&lp, &pp, ptr, ap);
pp->next = lp->next;
/* lp->group = NULL; */
free(lp);
}
void
afreeall(Area *ap)
{
struct lalloc *lp;
while ((lp = ap->ent) != NULL) {
#ifndef MKSH_SMALL
if (lp->group != ap)
internal_errorf("rogue pointer in group %p", ap);
#endif
ap->ent = lp->next;
/* lp->group = NULL; */
free(lp);
}
}

15
sh.h
View File

@ -102,7 +102,7 @@
#define __SCCSID(x) __IDSTRING(sccsid,x)
#ifdef EXTERN
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.279 2009/03/17 13:56:45 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.280 2009/03/22 16:55:38 tg Exp $");
#endif
#define MKSH_VERSION "R36 2009/03/17"
@ -396,10 +396,10 @@ char *ucstrstr(char *, const char *);
#endif
/*
* Area-based allocation built on malloc/free
* simple grouping allocator
*/
typedef struct Area {
struct link *freelist; /* free list */
typedef struct {
void *ent; /* entry pointer, must be first */
} Area;
EXTERN Area aperm; /* permanent object space */
@ -1263,10 +1263,11 @@ EXTERN int histsize; /* history size */
/* user and system time of last j_waitjed job */
EXTERN struct timeval j_usrtime, j_systime;
/* alloc.c */
Area *ainit(Area *);
/* lalloc.c */
void ainit(Area *);
void afreeall(Area *);
void *alloc(size_t, Area *); /* cannot fail */
/* these cannot fail and can take NULL (not for ap) */
#define alloc(n, ap) aresize(NULL, (n), (ap))
void *aresize(void *, size_t, Area *);
void afree(void *, Area *); /* can take NULL */
/* edit.c */