]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Summary: mem_hdr testing and corrections.
authorrobertc <>
Tue, 24 Jun 2003 18:30:59 +0000 (18:30 +0000)
committerrobertc <>
Tue, 24 Jun 2003 18:30:59 +0000 (18:30 +0000)
Keywords:

* Introduce operator < for mem_nodes, to provide ordering.
* Create a mem_hdr constructor and destructor.
* bugfix: writing before the lowest offset was broken.
* Reuse the Range template for mem_hdr::hasContigousContentRange.
* Prepare splay trees for replacing the list of mem_nodes.

include/splay.h
src/MemObject.cc
src/mem_node.cc
src/mem_node.h
src/stmem.cc
src/stmem.h
test-suite/Makefile.am
test-suite/mem_hdr_test.cc [new file with mode: 0644]
test-suite/mem_node_test.cc
test-suite/splay.cc
test-suite/test_tools.cc [new file with mode: 0644]

index 72c716585b277b3ac826df98299b20d440480299..5b3fde814859a0dc0bbef293b663c18cca9ae47c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: splay.h,v 1.19 2003/06/09 03:39:08 robertc Exp $
+ * $Id: splay.h,v 1.20 2003/06/24 12:30:59 robertc Exp $
  */
 
 #ifndef SQUID_SPLAY_H
@@ -39,6 +39,7 @@ class SplayNode {
     mutable SplayNode<V> *right;
     void destroy(SPLAYFREE *);
     void walk(SPLAYWALKEE *, void *callerState);
+    SplayNode<V> const * start() const;
     SplayNode<V> * remove(const Value data, SPLAYCMP * compare);
     SplayNode<V> * insert(Value data, SPLAYCMP * compare);
     SplayNode<V> * splay(const Value &data, SPLAYCMP * compare) const;
@@ -56,6 +57,7 @@ class Splay {
     Value const *find (Value const &, SPLAYCMP *compare) const;
     void insert(Value const &, SPLAYCMP *compare);
     void remove(Value const &, SPLAYCMP *compare);
+    SplayNode<V> const * start() const;
     size_t elements;
 };
 
@@ -82,6 +84,15 @@ SplayNode<V>::walk(SPLAYWALKEE * walkee, void *state)
        right->walk(walkee, state);
 }
 
+template<class V>
+SplayNode<V> const *
+SplayNode<V>::start() const
+{
+    if (this && left)
+       return left->start();
+    return this;
+}
+
 template<class V>
 void
 SplayNode<V>::destroy(SPLAYFREE * free_func)
@@ -236,6 +247,14 @@ Splay<V>::remove(Value const &value, SPLAYCMP *compare)
     --elements;
 }
 
+template <class V>
+SplayNode<V> const * 
+Splay<V>:: start() const{
+    if (head)
+       return head->start();
+    return NULL;
+}
+
 #endif
 
 #endif /* SQUID_SPLAY_H */
index 19c6f2b3f5a27f488b3225f7342210c933c73f11..d846ef1b127424f668950d66e8e5958f1886fe04 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: MemObject.cc,v 1.6 2003/03/04 01:40:25 robertc Exp $
+ * $Id: MemObject.cc,v 1.7 2003/06/24 12:30:59 robertc Exp $
  *
  * DEBUG: section 19    Store Memory Primitives
  * AUTHOR: Robert Collins
@@ -395,7 +395,7 @@ MemObject::trimUnSwappable()
 bool
 MemObject::isContiguous() const
 {
-    bool result = data_hdr.hasContigousContentRange (inmem_lo, endOffset());
+    bool result = data_hdr.hasContigousContentRange (Range<size_t>(inmem_lo, endOffset()));
     /* XXX : make this higher level */
     debug (19, result ? 4 :3) ("MemObject::isContiguous: Returning %s\n",
                                result ? "true" : "false");
index 37f0cd2d8d132fb19dacd724c66c1a39099e44af..561d834816a5cf7cfa0282306a13d040d0787555 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: mem_node.cc,v 1.2 2003/02/21 22:50:10 robertc Exp $
+ * $Id: mem_node.cc,v 1.3 2003/06/24 12:30:59 robertc Exp $
  *
  * DEBUG: section 19    Store Memory Primitives
  * AUTHOR: Robert Collins
@@ -115,3 +115,9 @@ mem_node::canAccept (size_t const &location) const
 
     return false;
 }
+
+bool
+mem_node::operator < (mem_node const & rhs) const
+{
+    return start() < rhs.start();
+}
index 67ac51a66a54e592a3316ebb61a3f8bd6a19b21c..96673e3b9b28670e21a6ba8e4eba50a0c77038b9 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: mem_node.h,v 1.2 2003/02/21 22:50:10 robertc Exp $
+ * $Id: mem_node.h,v 1.3 2003/06/24 12:30:59 robertc Exp $
  *
  *
  * SQUID Web Proxy Cache          http://www.squid-cache.org/
@@ -52,6 +52,7 @@ public:
     size_t end() const;
     bool contains (size_t const &location) const;
     bool canAccept (size_t const &location) const;
+    bool operator < (mem_node const & rhs) const;
     /* public */
     StoreIOBuffer nodeBuffer;
     mem_node *next;
index fd26673bcab548394676b6648197687db532d5e6..176d97f314b31a05b4cd949f289208c88662e2a5 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: stmem.cc,v 1.76 2003/02/21 22:50:11 robertc Exp $
+ * $Id: stmem.cc,v 1.77 2003/06/24 12:30:59 robertc Exp $
  *
  * DEBUG: section 19    Store Memory Primitives
  * AUTHOR: Harvest Derived
@@ -142,7 +142,7 @@ mem_hdr::appendNode (mem_node *aNode)
         if (!pointer) {
             /* prepend to list */
             aNode->next = head;
-            head = aNode->next;
+            head = aNode;
         } else {
             /* Append it to existing chain */
             aNode->next = pointer->next;
@@ -300,14 +300,14 @@ mem_hdr::copy(off_t offset, char *buf, size_t size) const
 }
 
 bool
-mem_hdr::hasContigousContentRange(size_t start, size_t end) const
+mem_hdr::hasContigousContentRange(Range<size_t> const & range) const
 {
-    size_t currentStart = start;
+    size_t currentStart = range.start;
 
     while (mem_node *curr = getBlockContainingLocation(currentStart)) {
         currentStart = curr->end();
 
-        if (currentStart >= end)
+        if (currentStart >= range.end)
             return true;
     }
 
@@ -349,7 +349,7 @@ mem_hdr::nodeToRecieve(off_t offset)
     /* case 2: location fits within an extant node */
     mem_node *candidate = getHighestBlockBeforeLocation(offset);
 
-    /* case 2: no nodes before it */
+    /* case 3: no nodes before it */
     if (!candidate) {
         candidate = new mem_node(offset);
         appendNode (candidate);
@@ -397,3 +397,11 @@ mem_hdr::write (StoreIOBuffer const &writeBuffer)
 
     return true;
 }
+
+mem_hdr::mem_hdr() : head (NULL), tail(NULL), inmem_hi(0)
+{}
+
+mem_hdr::~mem_hdr()
+{
+    freeContent();
+}
index 86819019522bff504c64febccf05a060ba51b531..89f398b53c7e9b87e8b4f6d9df8078097a3473b7 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: stmem.h,v 1.2 2003/02/21 22:50:11 robertc Exp $
+ * $Id: stmem.h,v 1.3 2003/06/24 12:30:59 robertc Exp $
  *
  *
  * SQUID Web Proxy Cache          http://www.squid-cache.org/
@@ -34,6 +34,9 @@
 #ifndef SQUID_STMEM_H
 #define SQUID_STMEM_H
 
+#include "splay.h"
+#include "Range.h"
+
 class mem_node;
 
 class StoreIOBuffer;
@@ -42,12 +45,14 @@ class mem_hdr
 {
 
 public:
+    mem_hdr();
+    ~mem_hdr();
     void freeContent();
     int lowestOffset () const;
     off_t endOffset () const;
     int freeDataUpto (int);
     ssize_t copy (off_t, char *, size_t) const;
-    bool hasContigousContentRange(size_t start, size_t end) const;
+    bool hasContigousContentRange(Range<size_t> const &range) const;
     /* success or fail */
     bool write (StoreIOBuffer const &);
 
@@ -68,6 +73,7 @@ private:
     mem_node *nodeToRecieve(off_t offset);
     size_t writeAvailable(mem_node *aNode, size_t location, size_t amount, char const *source);
     off_t inmem_hi;
+    Splay<mem_node> nodes;
 };
 
 #endif /* SQUID_STMEM_H */
index 47d153db099deadc27da5fd942394ed29a7ddbea..22ea95109c57b5b128755cd380764cf9f9944223 100644 (file)
@@ -1,7 +1,7 @@
 #
 #  Makefile for the Squid Object Cache server
 #
-#  $Id: Makefile.am,v 1.7 2003/06/22 08:04:28 robertc Exp $
+#  $Id: Makefile.am,v 1.8 2003/06/24 12:31:00 robertc Exp $
 #
 
 AUTOMAKE_OPTIONS = subdir-objects
@@ -17,21 +17,26 @@ EXTRA_PROGRAMS = mem_node_test membanger splay tcp-banger2 rfc1738
 TESTS = rfc1738 \
        refcount\
        splay\
-       mem_node_test\
        MemPoolTest\
+       mem_node_test\
+       mem_hdr_test\
        http_range_test
 
-## Sort by dependencies - test lowest layers first
-check_PROGRAMS= rfc1738\
-               refcount\
-               splay\
+## Sort by alpha - any build failures are significant.
+check_PROGRAMS= http_range_test \
                MemPoolTest\
                mem_node_test\
-               http_range_test
+               mem_hdr_test \
+               refcount\
+               rfc1738\
+               splay
 
 LDADD = -L$(top_builddir)/lib -lmiscutil
 mem_node_test_SOURCES = mem_node_test.cc
 mem_node_test_LDADD = $(top_builddir)/src/mem_node.o $(LDADD)
+mem_hdr_test_SOURCES = mem_hdr_test.cc test_tools.cc
+mem_hdr_test_LDADD = $(top_builddir)/src/stmem.o \
+                    $(top_builddir)/src/mem_node.o $(LDADD)
 MemPoolTest_SOURCES = MemPoolTest.cc
 refcount_SOURCES = refcount.cc
 
diff --git a/test-suite/mem_hdr_test.cc b/test-suite/mem_hdr_test.cc
new file mode 100644 (file)
index 0000000..fd20230
--- /dev/null
@@ -0,0 +1,72 @@
+
+/*
+ * $Id: mem_hdr_test.cc,v 1.1 2003/06/24 12:31:02 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 "stmem.h"
+#include "mem_node.h"
+#include <iostream>
+
+void
+testLowAndHigh()
+{
+    mem_hdr aHeader;
+    assert (aHeader.lowestOffset() == 0);
+    assert (aHeader.write (StoreIOBuffer()));
+    assert (aHeader.lowestOffset() == 0);
+    assert (aHeader.write (StoreIOBuffer(0, 1, NULL)));
+    assert (aHeader.lowestOffset() == 0);
+    char * sampleData = xstrdup ("A");
+    assert (aHeader.write (StoreIOBuffer(1, 100, sampleData)));
+    safe_free (sampleData);
+    assert (aHeader.lowestOffset() == 100);
+    assert (aHeader.endOffset() == 101);
+    sampleData = xstrdup ("B");
+    assert (aHeader.write (StoreIOBuffer(1, 10, sampleData)));
+    safe_free (sampleData);
+    assert (aHeader.lowestOffset() == 10);
+    assert (aHeader.endOffset() == 101);
+    assert (aHeader.hasContigousContentRange(Range<size_t>(10,11)));
+    assert (!aHeader.hasContigousContentRange(Range<size_t>(10,12)));
+    assert (!aHeader.hasContigousContentRange(Range<size_t>(10,101)));
+}
+
+int
+main (int argc, char *argv)
+{
+    testLowAndHigh();
+    assert (mem_node::InUseCount() == 0);
+    return 0;
+}
index a661b0f8b0c24c282b12cff9618abedfacb3ba1e..f9ce140a0e7fab60b6c62b0fce5ee4481ce4360f 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: mem_node_test.cc,v 1.2 2003/06/23 20:53:47 robertc Exp $
+ * $Id: mem_node_test.cc,v 1.3 2003/06/24 12:31:00 robertc Exp $
  *
  * DEBUG: section 19    Store Memory Primitives
  * AUTHOR: Robert Collins
@@ -73,5 +73,8 @@ main (int argc, char *argv)
     aNode->nodeBuffer.length = SM_PAGE_SIZE - 1;
     assert (aNode->canAccept (50 + SM_PAGE_SIZE - 1));
     assert (!aNode->canAccept (50 + SM_PAGE_SIZE));
+    assert (mem_node (0) < mem_node (2));
+    assert (!(mem_node (0) < mem_node (0)));
+    assert (!(mem_node (2) < mem_node (0)));
     return 0;
 }
index 509d49bb0392a7e1ec29b62d26edd92fa9cf1e0b..5a52b7dbed1842ad38c070f424fd5143a616df90 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: splay.cc,v 1.3 2003/04/22 01:37:44 robertc Exp $
+ * $Id: splay.cc,v 1.4 2003/06/24 12:31:00 robertc Exp $
  *
  * based on ftp://ftp.cs.cmu.edu/user/sleator/splaying/top-down-splay.c
  * http://bobo.link.cs.cmu.edu/cgi-bin/splay/splay-cgi.pl
@@ -180,9 +180,38 @@ main(int argc, char *argv[])
     SplayCheck::BeginWalk();
     intnode I;
     I.i = 1;
+    /* check we don't segfault on NULL splay calls */
     SplayCheck::WalkNodeRef(I, NULL);
     I.i = 0;
     SplayCheck::ExpectedFail = true;
     SplayCheck::WalkNodeRef(I, NULL);
+    
+{
+    /* check for begin() */
+    SplayNode<intnode> *safeTop = NULL;
+    if (safeTop->start() != NULL)
+       exit (1);
+    for (int i = 0; i < 100; i++) {
+       intnode I;
+       I.i = random();
+       if (i > 50)
+           safeTop = safeTop->insert(I, compareintref);
+    }
+    {
+      intnode I;
+      I.i = 50;
+      safeTop = safeTop->insert (I, compareintref);
+    }
+    if (!safeTop->start())
+      exit (1);
+    if (safeTop->start()->data.i != 50)
+      exit (1);
+    safeTop->destroy(destintref);
+}
+    {
+      Splay<intnode *> aSplay;
+      if (aSplay.start() != NULL)
+         exit (1);
+    }
     return 0;
 }
diff --git a/test-suite/test_tools.cc b/test-suite/test_tools.cc
new file mode 100644 (file)
index 0000000..76074eb
--- /dev/null
@@ -0,0 +1,120 @@
+
+/*
+ * $Id: test_tools.cc,v 1.1 2003/06/24 12:31:02 robertc Exp $
+ *
+ * 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 <iostream>
+
+void
+xassert(const char *msg, const char *file, int line)
+{
+    std::cout << "Assertion failed: (" << msg << ") at " << file << ":" << line << std::endl;
+    exit (1);
+}
+time_t squid_curtime = 0;
+
+int Debug::Levels[MAX_DEBUG_SECTIONS];
+int Debug::level;
+
+static void
+_db_print_stderr(const char *format, va_list args);
+
+void
+#if STDC_HEADERS
+_db_print(const char *format,...)
+{
+#else
+_db_print(va_alist)
+va_dcl
+{
+    const char *format = NULL;
+#endif
+
+    LOCAL_ARRAY(char, f, BUFSIZ);
+    va_list args1;
+#if STDC_HEADERS
+
+    va_list args2;
+    va_list args3;
+#else
+#define args2 args1
+#define args3 args1
+#endif
+
+#if STDC_HEADERS
+
+    va_start(args1, format);
+
+    va_start(args2, format);
+
+    va_start(args3, format);
+
+#else
+
+    format = va_arg(args1, const char *);
+
+#endif
+
+    snprintf(f, BUFSIZ, "%s| %s",
+             "stub time", //debugLogTime(squid_curtime),
+             format);
+
+    _db_print_stderr(f, args2);
+
+    va_end(args1);
+
+#if STDC_HEADERS
+
+    va_end(args2);
+
+    va_end(args3);
+
+#endif
+}
+
+static void
+_db_print_stderr(const char *format, va_list args) {
+    /* FIXME? */
+   // if (opt_debug_stderr < Debug::level)
+   if (1 < Debug::level)
+        return;
+
+    vfprintf(stderr, format, args);
+}
+
+void
+fatal(const char *message) {
+    debug (0,0) ("Fatal: %s",message);
+    exit (1);
+}