]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Summary: Convert Stack to typesafe usage.
authorrobertc <>
Mon, 14 Jul 2003 16:36:41 +0000 (16:36 +0000)
committerrobertc <>
Mon, 14 Jul 2003 16:36:41 +0000 (16:36 +0000)
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.

include/Stack.h
src/Generic.h
src/cbdata.cc
src/squid.h
src/store.cc
test-suite/Makefile.am
test-suite/StackTest.cc [new file with mode: 0644]

index c03a0f265553339f1fd3681ed65ed79ec1acb438..62da2773ab515768a179c0ee095eb8c8ab845142 100644 (file)
@@ -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
  *
 
 #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 <class S = void *>
 
-template <class S>
-typename S::value_type
-stackPop(S * s)
+class Stack : public Vector<S>
 {
-    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 <class S>
-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<S>::value_type value_type;
+    typedef typename Vector<S>::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 */
index fd5e15e78f3f2bf7d4574a6573f3f4307ddce8c2..7eaa0f2ffac917feed0caea1c47d02255031d6dc 100644 (file)
@@ -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 <class T>
 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 S>
+
+class Stack;
+
+template <class E, class T>
+T& for_each(Stack<E> 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 */
index be884aa270877fccca4f884d848b0b6b83534d36..d893c552789abe6e27c9a30c553188dbe070935b 100644 (file)
@@ -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<CBDataCall*> 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<CBDataCall *> ();
     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 <class T>
-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<CBDataCall, void>
 {
     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");
     }
 
index ef64d529e565726639e5927cddf8b0b5f705bcd1..d00fa10914aee0d68c9f72bdb2b35ce8369791a2 100644 (file)
@@ -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
index 33664766ce913e3e0265d1cd16487b1318f16380..f2e270eb4e511e9771f7fcef88870e463ef8c044 100644 (file)
@@ -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<StoreEntry*> 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<StoreEntry*>(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();
index a1e899219e1122a7f6c81e3e0cc5e19b98415832..cd6f72a080c1bf8a29fdb8d3a0b1ecf2a7430e82 100644 (file)
@@ -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 (file)
index 0000000..66c38b0
--- /dev/null
@@ -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 <robertc@squid-cache.org>
+ */
+
+#include "squid.h"
+#include "Stack.h"
+
+int
+main (int argc, char *argv)
+{
+    Stack<int> 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;
+}