2008-08-02 19:45:12 +02:00
|
|
|
/* $OpenBSD: alloc.c,v 1.8 2008/07/21 17:30:08 millert Exp $ */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* 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.
|
2005-06-08 23:51:22 +02:00
|
|
|
*-
|
2005-05-23 05:06:10 +02:00
|
|
|
* area-based allocation built on malloc/free
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sh.h"
|
|
|
|
|
2008-11-12 01:54:52 +01:00
|
|
|
__RCSID("$MirOS: src/bin/mksh/alloc.c,v 1.11 2008/11/12 00:54:46 tg Exp $");
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
struct link {
|
|
|
|
struct link *prev;
|
|
|
|
struct link *next;
|
|
|
|
};
|
|
|
|
|
2008-11-12 01:27:57 +01:00
|
|
|
struct TArea {
|
|
|
|
struct link *freelist; /* free list */
|
|
|
|
};
|
2008-11-12 00:50:31 +01:00
|
|
|
|
|
|
|
PArea
|
2008-11-12 01:27:57 +01:00
|
|
|
anew(void)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2008-11-12 01:27:57 +01:00
|
|
|
PArea ap;
|
|
|
|
|
|
|
|
if ((ap = malloc(sizeof (struct TArea))) == NULL)
|
|
|
|
internal_errorf("unable to allocate memory");
|
2005-05-23 05:06:10 +02:00
|
|
|
ap->freelist = NULL;
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
return (ap);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-11-12 01:27:57 +01:00
|
|
|
adelete(PArea *pap)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
struct link *l, *l2;
|
|
|
|
|
2008-11-12 01:27:57 +01:00
|
|
|
for (l = (*pap)->freelist; l != NULL; l = l2) {
|
2005-05-23 05:06:10 +02:00
|
|
|
l2 = l->next;
|
|
|
|
free(l);
|
|
|
|
}
|
2008-11-12 01:27:57 +01:00
|
|
|
free(*pap);
|
|
|
|
*pap = NULL;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
#define L2P(l) ( (void *)(((char *)(l)) + sizeof (struct link)) )
|
2007-06-05 20:59:54 +02:00
|
|
|
#define P2L(p) ( (struct link *)(((ptrdiff_t)(p)) - sizeof (struct link)) )
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
void *
|
2008-11-12 01:54:52 +01:00
|
|
|
alloc(size_t nmemb, size_t size, PArea ap)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
struct link *l;
|
|
|
|
|
2008-11-12 01:54:52 +01:00
|
|
|
size *= nmemb;
|
|
|
|
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
if ((l = malloc(sizeof (struct link) + size)) == NULL)
|
2007-05-13 19:51:24 +02:00
|
|
|
internal_errorf("unable to allocate memory");
|
2005-05-23 05:06:10 +02:00
|
|
|
l->next = ap->freelist;
|
|
|
|
l->prev = NULL;
|
|
|
|
if (ap->freelist)
|
|
|
|
ap->freelist->prev = l;
|
|
|
|
ap->freelist = l;
|
|
|
|
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
return (L2P(l));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2008-11-12 01:54:52 +01:00
|
|
|
aresize(void *ptr, size_t nmemb, size_t size, PArea ap)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
struct link *l, *l2, *lprev, *lnext;
|
|
|
|
|
|
|
|
if (ptr == NULL)
|
2008-11-12 01:54:52 +01:00
|
|
|
return (alloc(nmemb, size, ap));
|
|
|
|
|
|
|
|
size *= nmemb;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
l = P2L(ptr);
|
|
|
|
lprev = l->prev;
|
|
|
|
lnext = l->next;
|
|
|
|
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
if ((l2 = realloc(l, sizeof (struct link) + size)) == NULL)
|
2007-05-13 19:51:24 +02:00
|
|
|
internal_errorf("unable to allocate memory");
|
2005-05-23 05:06:10 +02:00
|
|
|
if (lprev)
|
|
|
|
lprev->next = l2;
|
|
|
|
else
|
|
|
|
ap->freelist = l2;
|
|
|
|
if (lnext)
|
|
|
|
lnext->prev = l2;
|
|
|
|
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
return (L2P(l2));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-11-12 00:50:31 +01:00
|
|
|
afree(void *ptr, PArea ap)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
struct link *l;
|
2008-07-13 18:43:55 +02:00
|
|
|
#ifdef MKSH_AFREE_DEBUG
|
|
|
|
struct link *lp;
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (!ptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
l = P2L(ptr);
|
|
|
|
|
2008-07-13 18:43:55 +02:00
|
|
|
#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
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
if (l->prev)
|
|
|
|
l->prev->next = l->next;
|
|
|
|
else
|
|
|
|
ap->freelist = l->next;
|
|
|
|
if (l->next)
|
|
|
|
l->next->prev = l->prev;
|
|
|
|
|
|
|
|
free(l);
|
|
|
|
}
|