From: robertc <> Date: Tue, 24 Jun 2003 18:30:59 +0000 (+0000) Subject: Summary: mem_hdr testing and corrections. X-Git-Tag: SQUID_3_0_PRE1~85 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4c50505be6b9316c646cb0022533bfebe207fa89;p=thirdparty%2Fsquid.git Summary: mem_hdr testing and corrections. 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. --- diff --git a/include/splay.h b/include/splay.h index 72c716585b..5b3fde8148 100644 --- a/include/splay.h +++ b/include/splay.h @@ -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 *right; void destroy(SPLAYFREE *); void walk(SPLAYWALKEE *, void *callerState); + SplayNode const * start() const; SplayNode * remove(const Value data, SPLAYCMP * compare); SplayNode * insert(Value data, SPLAYCMP * compare); SplayNode * 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 const * start() const; size_t elements; }; @@ -82,6 +84,15 @@ SplayNode::walk(SPLAYWALKEE * walkee, void *state) right->walk(walkee, state); } +template +SplayNode const * +SplayNode::start() const +{ + if (this && left) + return left->start(); + return this; +} + template void SplayNode::destroy(SPLAYFREE * free_func) @@ -236,6 +247,14 @@ Splay::remove(Value const &value, SPLAYCMP *compare) --elements; } +template +SplayNode const * +Splay:: start() const{ + if (head) + return head->start(); + return NULL; +} + #endif #endif /* SQUID_SPLAY_H */ diff --git a/src/MemObject.cc b/src/MemObject.cc index 19c6f2b3f5..d846ef1b12 100644 --- a/src/MemObject.cc +++ b/src/MemObject.cc @@ -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(inmem_lo, endOffset())); /* XXX : make this higher level */ debug (19, result ? 4 :3) ("MemObject::isContiguous: Returning %s\n", result ? "true" : "false"); diff --git a/src/mem_node.cc b/src/mem_node.cc index 37f0cd2d8d..561d834816 100644 --- a/src/mem_node.cc +++ b/src/mem_node.cc @@ -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(); +} diff --git a/src/mem_node.h b/src/mem_node.h index 67ac51a66a..96673e3b9b 100644 --- a/src/mem_node.h +++ b/src/mem_node.h @@ -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; diff --git a/src/stmem.cc b/src/stmem.cc index fd26673bca..176d97f314 100644 --- a/src/stmem.cc +++ b/src/stmem.cc @@ -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 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(); +} diff --git a/src/stmem.h b/src/stmem.h index 8681901952..89f398b53c 100644 --- a/src/stmem.h +++ b/src/stmem.h @@ -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 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 nodes; }; #endif /* SQUID_STMEM_H */ diff --git a/test-suite/Makefile.am b/test-suite/Makefile.am index 47d153db09..22ea95109c 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.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 index 0000000000..fd20230a08 --- /dev/null +++ b/test-suite/mem_hdr_test.cc @@ -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 + */ + +#include "squid.h" +#include "stmem.h" +#include "mem_node.h" +#include + +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(10,11))); + assert (!aHeader.hasContigousContentRange(Range(10,12))); + assert (!aHeader.hasContigousContentRange(Range(10,101))); +} + +int +main (int argc, char *argv) +{ + testLowAndHigh(); + assert (mem_node::InUseCount() == 0); + return 0; +} diff --git a/test-suite/mem_node_test.cc b/test-suite/mem_node_test.cc index a661b0f8b0..f9ce140a0e 100644 --- a/test-suite/mem_node_test.cc +++ b/test-suite/mem_node_test.cc @@ -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; } diff --git a/test-suite/splay.cc b/test-suite/splay.cc index 509d49bb03..5a52b7dbed 100644 --- a/test-suite/splay.cc +++ b/test-suite/splay.cc @@ -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 *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 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 index 0000000000..76074ebc65 --- /dev/null +++ b/test-suite/test_tools.cc @@ -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 + */ + +#include "squid.h" +#include + +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); +}