From: robertc <> Date: Mon, 14 Jul 2003 16:36:41 +0000 (+0000) Subject: Summary: Convert Stack to typesafe usage. X-Git-Tag: SQUID_3_0_PRE1~29 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=91caca8381c0ed673bf752a2e7425ba1ba74d822;p=thirdparty%2Fsquid.git Summary: Convert Stack to typesafe usage. Keywords: Convert the Stack Header. Add a StackTest testcase. Remove Stack.h from squid.h. Adjust cbdata to use typesage stacks. Move cbdata's foreach to Generic.h. Adjust store.cc to use a typesafe Stack. Remove unused Stack defines. --- diff --git a/include/Stack.h b/include/Stack.h index c03a0f2655..62da2773ab 100644 --- a/include/Stack.h +++ b/include/Stack.h @@ -1,5 +1,5 @@ /* - * $Id: Stack.h,v 1.14 2003/01/23 00:36:47 robertc Exp $ + * $Id: Stack.h,v 1.15 2003/07/14 10:36:41 robertc Exp $ * * AUTHOR: Alex Rousskov * @@ -36,33 +36,35 @@ #include "Array.h" -typedef Array Stack; +/* RBC: 20030714 Composition might be better long-term, but for now, + * there's no reason to do so. + */ -#define stackCreate arrayCreate -#define stackInit arrayInit -#define stackClean arrayClean -#define stackDestroy arrayDestroy -#define stackPush arrayAppend -#define stackPrePush arrayPreAppend +template -template -typename S::value_type -stackPop(S * s) +class Stack : public Vector { - assert(s); - if (!s->count) - return typename S::value_type(); - typename S::value_type result = s->items[--s->count]; - s->items[s->count] = typename S::value_type(); - return result; -} -/* todo, fatal on empty Top call */ -template -typename S::value_type -stackTop(S * s) -{ - assert(s); - return s->count ? s->items[s->count - 1] : typename S::value_type(); -} +public: + typedef typename Vector::value_type value_type; + typedef typename Vector::pointer pointer; + value_type pop() + { + if (!count) + return value_type(); + + value_type result = items[--count]; + + items[count] = value_type(); + + return result; + } + + /* todo, fatal on empty Top call */ + value_type top() const + { + return count ? items[count - 1] : value_type(); + } +}; + #endif /* SQUID_STACK_H */ diff --git a/src/Generic.h b/src/Generic.h index fd5e15e78f..7eaa0f2ffa 100644 --- a/src/Generic.h +++ b/src/Generic.h @@ -1,6 +1,6 @@ /* - * $Id: Generic.h,v 1.4 2003/02/21 22:50:05 robertc Exp $ + * $Id: Generic.h,v 1.5 2003/07/14 10:36:41 robertc Exp $ * * * SQUID Web Proxy Cache http://www.squid-cache.org/ @@ -51,7 +51,6 @@ T& for_each(L const &head, T& visitor) return visitor; } - template T& for_each(dlink_list const &collection, T& visitor) { @@ -61,4 +60,17 @@ T& for_each(dlink_list const &collection, T& visitor) return visitor; } +template + +class Stack; + +template +T& for_each(Stack const &collection, T& visitor) +{ + for (size_t index = 0; index < collection.count; ++index) + visitor(*(typename T::argument_type const *)collection.items[index]); + + return visitor; +}; + #endif /* SQUID_GENERIC_H */ diff --git a/src/cbdata.cc b/src/cbdata.cc index be884aa270..d893c55278 100644 --- a/src/cbdata.cc +++ b/src/cbdata.cc @@ -1,6 +1,6 @@ /* - * $Id: cbdata.cc,v 1.57 2003/07/14 08:21:56 robertc Exp $ + * $Id: cbdata.cc,v 1.58 2003/07/14 10:36:42 robertc Exp $ * * DEBUG: section 45 Callback Data Registry * ORIGINAL AUTHOR: Duane Wessels @@ -89,18 +89,18 @@ public: int type; #if CBDATA_DEBUG - void addHistory(char const *label, char const *file, int line) const + void addHistory(char const *label, char const *file, int line) { - if (calls->count > 1000) + if (calls.size() > 1000) return; - stackPush (calls, new CBDataCall(label, file, line)); + calls.push_back(new CBDataCall(label, file, line)); } dlink_node link; const char *file; int line; - Stack *calls; + Stack calls; #endif /* cookie used while debugging */ @@ -147,11 +147,9 @@ cbdata::deleteSelf() #if CBDATA_DEBUG CBDataCall *aCall; - while ((aCall = (CBDataCall *)stackPop(calls))) + while ((aCall = calls.pop())) delete aCall; - stackDestroy(calls); - #endif FREE *free_func = cbdata_index[type].free_func; @@ -252,7 +250,7 @@ cbdataInternalAlloc(cbdata_type type) p->file = file; p->line = line; - p->calls = stackCreate(); + p->calls = Stack (); p->addHistory("Alloc", file, line); dlinkAdd(p, &p->link, &cbdataEntries); debug(45, 3) ("cbdataAlloc: %p %s:%d\n", &p->data, file, line); @@ -484,15 +482,6 @@ cbdataDump(StoreEntry * sentry) #if CBDATA_DEBUG -template -T& for_each(Stack const &collection, T& visitor) -{ - for (size_t index = 0; index < collection.count; ++index) - visitor(*(typename T::argument_type const *)collection.items[index]); - - return visitor; -}; - struct CBDataCallDumper : public unary_function { CBDataCallDumper (StoreEntry *anEntry):where(anEntry){} @@ -514,7 +503,7 @@ struct CBDataHistoryDumper : public CBDataDumper CBDataDumper::operator()(x); storeAppendPrintf(where, "\n"); storeAppendPrintf(where, "Action\tFile\tLine\n"); - for_each (*x.calls,callDumper); + for_each (x.calls,callDumper); storeAppendPrintf(where, "\n"); } diff --git a/src/squid.h b/src/squid.h index ef64d529e5..d00fa10914 100644 --- a/src/squid.h +++ b/src/squid.h @@ -1,6 +1,6 @@ /* - * $Id: squid.h,v 1.235 2003/04/25 21:45:29 robertc Exp $ + * $Id: squid.h,v 1.236 2003/07/14 10:36:42 robertc Exp $ * * AUTHOR: Duane Wessels * @@ -328,8 +328,6 @@ extern "C" #include "ssl_support.h" #endif -#include "Stack.h" - /* Needed for poll() on Linux at least */ #if HAVE_POLL #ifndef POLLRDNORM diff --git a/src/store.cc b/src/store.cc index 33664766ce..f2e270eb4e 100644 --- a/src/store.cc +++ b/src/store.cc @@ -1,6 +1,6 @@ /* - * $Id: store.cc,v 1.570 2003/06/26 12:51:58 robertc Exp $ + * $Id: store.cc,v 1.571 2003/07/14 10:36:42 robertc Exp $ * * DEBUG: section 20 Storage Manager * AUTHOR: Harvest Derived @@ -46,6 +46,7 @@ #if DELAY_POOLS #include "DelayPools.h" #endif +#include "Stack.h" static STMCB storeWriteComplete; @@ -108,7 +109,7 @@ static EVH storeLateRelease; /* * local variables */ -static Stack LateReleaseStack; +static Stack LateReleaseStack; MemPool *StoreEntry::pool = NULL; void * @@ -1218,7 +1219,7 @@ storeRelease(StoreEntry * e) */ e->lock_count++; e->setReleaseFlag(); - stackPush(&LateReleaseStack, e); + LateReleaseStack.push_back(e); PROF_stop(storeRelease); return; } else { @@ -1264,7 +1265,7 @@ storeLateRelease(void *unused) } for (i = 0; i < 10; i++) { - e = static_cast(stackPop(&LateReleaseStack)); + e = LateReleaseStack.pop(); if (e == NULL) { /* done! */ @@ -1382,7 +1383,6 @@ storeInit(void) mem_policy = createRemovalPolicy(Config.memPolicy); storeDigestInit(); storeLogOpen(); - stackInit(&LateReleaseStack); eventAdd("storeLateRelease", storeLateRelease, NULL, 1.0, 1); storeDirInit(); storeRebuildStart(); diff --git a/test-suite/Makefile.am b/test-suite/Makefile.am index a1e899219e..cd6f72a080 100644 --- a/test-suite/Makefile.am +++ b/test-suite/Makefile.am @@ -1,7 +1,7 @@ # # Makefile for the Squid Object Cache server # -# $Id: Makefile.am,v 1.12 2003/07/10 01:31:51 robertc Exp $ +# $Id: Makefile.am,v 1.13 2003/07/14 10:36:44 robertc Exp $ # AUTOMAKE_OPTIONS = subdir-objects @@ -16,6 +16,7 @@ EXTRA_PROGRAMS = mem_node_test membanger splay tcp-banger2 rfc1738 ## Sort by dependencies - test lowest layers first TESTS = debug \ syntheticoperators \ + StackTest \ rfc1738 \ refcount\ splay\ @@ -33,6 +34,7 @@ check_PROGRAMS= debug \ refcount\ rfc1738\ splay \ + StackTest \ syntheticoperators LDADD = -L$(top_builddir)/lib -lmiscutil @@ -60,6 +62,8 @@ http_range_test_LDADD = $(top_builddir)/src/HttpHdrRange.o \ splay_SOURCES = splay.cc +StackTest_SOURCES = StackTest.cc test_tools.cc + syntheticoperators_SOURCES = syntheticoperators.cc test_tools.cc rfc1738_SOURCES = rfc1738.cc diff --git a/test-suite/StackTest.cc b/test-suite/StackTest.cc new file mode 100644 index 0000000000..66c38b04c2 --- /dev/null +++ b/test-suite/StackTest.cc @@ -0,0 +1,58 @@ + +/* + * $Id: StackTest.cc,v 1.1 2003/07/14 10:36:44 robertc Exp $ + * + * DEBUG: section 19 Store Memory Primitives + * AUTHOR: Robert Collins + * + * SQUID Web Proxy Cache http://www.squid-cache.org/ + * ---------------------------------------------------------- + * + * Squid is the result of efforts by numerous individuals from + * the Internet community; see the CONTRIBUTORS file for full + * details. Many organizations have provided support for Squid's + * development; see the SPONSORS file for full details. Squid is + * Copyrighted (C) 2001 by the Regents of the University of + * California; see the COPYRIGHT file for full details. Squid + * incorporates software developed and/or copyrighted by other + * sources; see the CREDITS file for full details. + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111, USA. + * + * Copyright (c) 2003 Robert Collins + */ + +#include "squid.h" +#include "Stack.h" + +int +main (int argc, char *argv) +{ + Stack aStack; + assert (aStack.size() == 0); + aStack.push_back(2); + assert (aStack.size() == 1); + assert (aStack.top() == 2); + assert (aStack.pop() == 2); + assert (aStack.size() == 0); + Stack<> oldStack; + assert (oldStack.size() == 0); + oldStack.push_back(&aStack); + assert (oldStack.size() == 1); + assert (oldStack.top() == &aStack); + assert (oldStack.pop() == &aStack); + assert (oldStack.size() == 0); + return 0; +}