From: rousskov <> Date: Sat, 21 Mar 1998 01:07:34 +0000 (+0000) Subject: - Split Stack into Array and Stack. X-Git-Tag: SQUID_3_0_PRE1~3776 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f12361a11f1a4c60db9584d8f080c09af0c8ae70;p=thirdparty%2Fsquid.git - Split Stack into Array and Stack. --- diff --git a/include/Array.h b/include/Array.h new file mode 100644 index 0000000000..2b2ab5ffa4 --- /dev/null +++ b/include/Array.h @@ -0,0 +1,50 @@ +/* + * $Id: Array.h,v 1.1 1998/03/20 18:07:34 rousskov Exp $ + * + * AUTHOR: Alex Rousskov + * + * SQUID Internet Object Cache http://squid.nlanr.net/Squid/ + * -------------------------------------------------------- + * + * Squid is the result of efforts by numerous individuals from the + * Internet community. Development is led by Duane Wessels of the + * National Laboratory for Applied Network Research and funded by + * the National Science Foundation. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ + +#ifndef _ARRAY_H_ +#define _ARRAY_H_ + +/* see Array.c for more documentation */ + +typedef struct { + int capacity; + int count; + void **items; +} Array; + + +extern Array *arrayCreate(); +extern void arrayInit(Array * s); +extern void arrayClean(Array * s); +extern void arrayDestroy(Array *s); +extern void arrayAppend(Array *s, void *obj); +extern void arrayPreAppend(Array * s, int app_count); + + +#endif /* ndef _ARRAY_H_ */ diff --git a/include/Stack.h b/include/Stack.h index e313fe6190..30abb4bc9e 100644 --- a/include/Stack.h +++ b/include/Stack.h @@ -1,5 +1,5 @@ /* - * $Id: Stack.h,v 1.5 1998/03/11 22:18:42 rousskov Exp $ + * $Id: Stack.h,v 1.6 1998/03/20 18:07:35 rousskov Exp $ * * AUTHOR: Alex Rousskov * @@ -30,26 +30,16 @@ #ifndef _STACK_H_ #define _STACK_H_ -/* see Stack.c for more documentation */ +#include "Array.h" -struct _Stack { - /* public, read only */ - size_t capacity; +typedef Array Stack; - /* protected, do not use these, use interface functions instead */ - int count; - void **items; -}; - -typedef struct _Stack Stack; - -extern Stack *stackCreate(); -extern void stackInit(Stack * s); -extern void stackClean(Stack * s); -extern void stackDestroy(Stack *s); +#define stackCreate arrayCreate +#define stackInit arrayInit +#define stackClean arrayClean +#define stackDestroy arrayDestroy extern void *stackPop(Stack *s); -extern void stackPush(Stack *s, void *obj); -extern void stackPrePush(Stack * s, int push_count); - +#define stackPush arrayAppend +#define stackPrePush arrayPreAppend #endif /* ndef _STACK_H_ */ diff --git a/lib/Array.cc b/lib/Array.cc new file mode 100644 index 0000000000..55e1d4b2bf --- /dev/null +++ b/lib/Array.cc @@ -0,0 +1,121 @@ +/* + * $Id: Array.cc,v 1.1 1998/03/20 18:07:35 rousskov Exp $ + * + * AUTHOR: Alex Rousskov + * + * SQUID Internet Object Cache http://squid.nlanr.net/Squid/ + * -------------------------------------------------------- + * + * Squid is the result of efforts by numerous individuals from the + * Internet community. Development is led by Duane Wessels of the + * National Laboratory for Applied Network Research and funded by + * the National Science Foundation. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ + +/* + * Array is an array of (void*) items with unlimited capacity + * + * Array grows when arrayAppend() is called and no space is left + * Currently, array does not have an interface for deleting an item because + * we do not need such an interface yet. + */ + + +#include "config.h" +#if HAVE_ASSERT_H +#include +#endif +#if HAVE_STRING_H +#include +#endif +#include "util.h" +#include "Array.h" + +static void arrayGrow(Array * a, int min_capacity); + +Array * +arrayCreate() +{ + Array *a = xmalloc(sizeof(Array)); + arrayInit(a); + return a; +} + +void +arrayInit(Array * a) +{ + assert(a); + memset(a, 0, sizeof(Array)); +} + +void +arrayClean(Array * a) +{ + assert(a); + /* could also warn if some objects are left */ + xfree(a->items); + a->items = NULL; +} + +void +arrayDestroy(Array * a) +{ + assert(a); + arrayClean(a); + xfree(a); +} + +void +arrayAppend(Array * a, void *obj) +{ + assert(a); + if (a->count >= a->capacity) + arrayGrow(a, a->count+1); + a->items[a->count++] = obj; +} + +/* if you are going to append a known and large number of items, call this first */ +void +arrayPreAppend(Array * a, int app_count) +{ + assert(a); + if (a->count + app_count > a->capacity) + arrayGrow(a, a->count + app_count); +} + +/* grows internal buffer to satisfy required minimal capacity */ +static void +arrayGrow(Array * a, int min_capacity) +{ + const int min_delta = 16; + int delta; + assert(a->capacity < min_capacity); + delta = min_capacity; + /* make delta a multiple of min_delta */ + delta += min_delta-1; + delta /= min_delta; + delta *= min_delta; + /* actual grow */ + assert(delta > 0); + a->capacity += delta; + a->items = a->items ? + xrealloc(a->items, a->capacity * sizeof(void*)) : + xmalloc(a->capacity * sizeof(void*)); + /* reset, just in case */ + memset(a->items+a->count, 0, (a->capacity-a->count) * sizeof(void*)); +} diff --git a/lib/Makefile.in b/lib/Makefile.in index 5c4550be53..fd7707836e 100644 --- a/lib/Makefile.in +++ b/lib/Makefile.in @@ -1,5 +1,5 @@ # -# $Id: Makefile.in,v 1.36 1998/03/08 07:34:41 rousskov Exp $ +# $Id: Makefile.in,v 1.37 1998/03/20 18:07:36 rousskov Exp $ # prefix = @prefix@ top_srcdir = @top_srcdir@ @@ -35,6 +35,7 @@ UTILOBJS = rfc1123.o \ md5.o \ radix.o \ stub_memaccount.o \ + Array.o \ Stack.o \ $(LIBOBJS) REGEXOBJS = GNUregex.o diff --git a/lib/Stack.c b/lib/Stack.c index b828f467f5..5deed6aceb 100644 --- a/lib/Stack.c +++ b/lib/Stack.c @@ -1,5 +1,5 @@ /* - * $Id: Stack.c,v 1.4 1998/03/03 00:30:57 rousskov Exp $ + * $Id: Stack.c,v 1.5 1998/03/20 18:07:36 rousskov Exp $ * * AUTHOR: Alex Rousskov * @@ -28,7 +28,7 @@ */ /* - * Stack is a (void*) stack with unlimited capacity and limited accounting. + * Stack is a (void*) stack with unlimited capacity; based on Array */ @@ -42,39 +42,6 @@ #include "util.h" #include "Stack.h" -static void stackGrow(Stack * s, int min_capacity); - -Stack * -stackCreate() -{ - Stack *s = xmalloc(sizeof(Stack)); - stackInit(s); - return s; -} - -void -stackInit(Stack * s) -{ - assert(s); - memset(s, 0, sizeof(Stack)); -} - -void -stackClean(Stack * s) -{ - assert(s); - /* could also warn if some objects are left */ - xfree(s->items); - s->items = NULL; -} - -void -stackDestroy(Stack * s) -{ - assert(s); - stackClean(s); - xfree(s); -} void * stackPop(Stack * s) @@ -83,43 +50,3 @@ stackPop(Stack * s) assert(s->count); return s->items[--s->count]; } - -void -stackPush(Stack * s, void *obj) -{ - assert(s); - if (s->count >= s->capacity) - stackGrow(s, s->count+1); - s->items[s->count++] = obj; -} - -/* if you are going to push a known and large number of items, call this first */ -void -stackPrePush(Stack * s, int push_count) -{ - assert(s); - if (s->count + push_count > s->capacity) - stackGrow(s, s->count + push_count); -} - -/* grows internal buffer to satisfy required minimal capacity */ -static void -stackGrow(Stack * s, int min_capacity) -{ - static const int min_delta = 16; - int delta; - assert(s->capacity < min_capacity); - delta = min_capacity; - /* make delta a multiple of min_delta */ - delta += min_delta-1; - delta /= min_delta; - delta *= min_delta; - /* actual grow */ - assert(delta > 0); - s->capacity += delta; - s->items = s->items ? - xrealloc(s->items, s->capacity * sizeof(void*)) : - xmalloc(s->capacity * sizeof(void*)); - /* reset, just in case */ - memset(s->items+s->count, 0, (s->capacity-s->count) * sizeof(void*)); -}