]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/stmem.cc
Source Format Enforcement (#1234)
[thirdparty/squid.git] / src / stmem.cc
index b264138f69565c596c9daa97c4a41bf1e08bfdda..146cf9565632066afc35fde3af4d4c2cf9b1c0fe 100644 (file)
@@ -1,44 +1,19 @@
-
 /*
- * $Id$
- *
- * DEBUG: section 19    Store Memory Primitives
- * AUTHOR: Harvest Derived
- *
- * 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.
+ * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
  *
- *  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>
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
  */
 
+/* DEBUG: section 19    Store Memory Primitives */
+
 #include "squid.h"
-#include "stmem.h"
+#include "Generic.h"
+#include "HttpReply.h"
 #include "mem_node.h"
 #include "MemObject.h"
-#include "Generic.h"
+#include "stmem.h"
 
 /*
  * NodeGet() is called to get the data buffer to pass to storeIOWrite().
@@ -51,7 +26,7 @@ char *
 mem_hdr::NodeGet(mem_node * aNode)
 {
     assert(!aNode->write_pending);
-    aNode->write_pending = 1;
+    aNode->write_pending = true;
     return aNode->data;
 }
 
@@ -83,18 +58,20 @@ mem_hdr::endOffset () const
 void
 mem_hdr::freeContent()
 {
-    nodes.destroy(SplayNode<mem_node *>::DefaultFree);
+    nodes.destroy();
     inmem_hi = 0;
+    debugs(19, 9, this << " hi: " << inmem_hi);
 }
 
 bool
 mem_hdr::unlink(mem_node *aNode)
 {
     if (aNode->write_pending) {
-        debugs(0, 0, "cannot unlink mem_node " << aNode << " while write_pending");
+        debugs(0, DBG_CRITICAL, "ERROR: cannot unlink mem_node " << aNode << " while write_pending");
         return false;
     }
 
+    debugs(19, 8, this << " removing " << aNode);
     nodes.remove (aNode, NodeCompare);
     delete aNode;
     return true;
@@ -103,9 +80,9 @@ mem_hdr::unlink(mem_node *aNode)
 int64_t
 mem_hdr::freeDataUpto(int64_t target_offset)
 {
+    debugs(19, 8, this << " up to " << target_offset);
     /* keep the last one to avoid change to other part of code */
-
-    SplayNode<mem_node*> const * theStart = nodes.start();
+    SplayNode<mem_node*> const * theStart;
 
     while ((theStart = nodes.start())) {
         if (theStart == nodes.finish())
@@ -126,7 +103,7 @@ mem_hdr::freeDataUpto(int64_t target_offset)
 int
 mem_hdr::appendToNode(mem_node *aNode, const char *data, int maxLength)
 {
-    size_t result = writeAvailable (aNode, aNode->nodeBuffer.offset + aNode->nodeBuffer.length ,maxLength, data);
+    size_t result = writeAvailable (aNode, aNode->nodeBuffer.offset + aNode->nodeBuffer.length,maxLength, data);
     return result;
 }
 
@@ -142,14 +119,17 @@ mem_hdr::writeAvailable(mem_node *aNode, int64_t location, size_t amount, char c
     assert (location - aNode->nodeBuffer.offset == (int64_t)aNode->nodeBuffer.length);
     size_t copyLen = min(amount, aNode->space());
 
-    xmemcpy(aNode->nodeBuffer.data + aNode->nodeBuffer.length, source, copyLen);
+    memcpy(aNode->nodeBuffer.data + aNode->nodeBuffer.length, source, copyLen);
 
+    debugs(19, 9, this << " hi: " << inmem_hi);
     if (inmem_hi <= location)
         inmem_hi = location + copyLen;
 
     /* Adjust the ptr and len according to what was deposited in the page */
     aNode->nodeBuffer.length += copyLen;
 
+    debugs(19, 9, this << " hi: " << inmem_hi);
+    debugs(19, 9, this << " hi: " << endOffset());
     return copyLen;
 }
 
@@ -176,7 +156,7 @@ mem_hdr::makeAppendSpace()
 void
 mem_hdr::internalAppend(const char *data, int len)
 {
-    debugs(19, 6, "memInternalAppend: len " << len);
+    debugs(19, 6, "memInternalAppend: " << this << " len " << len);
 
     while (len > 0) {
         makeAppendSpace();
@@ -194,6 +174,7 @@ mem_hdr::internalAppend(const char *data, int len)
 mem_node *
 mem_hdr::getBlockContainingLocation (int64_t location) const
 {
+    // Optimize: do not create a whole mem_node just to store location
     mem_node target (location);
     target.nodeBuffer.length = 1;
     mem_node *const *result = nodes.find (&target, NodeCompare);
@@ -201,7 +182,7 @@ mem_hdr::getBlockContainingLocation (int64_t location) const
     if (result)
         return *result;
 
-    return NULL;
+    return nullptr;
 }
 
 size_t
@@ -218,7 +199,7 @@ mem_hdr::copyAvailable(mem_node *aNode, int64_t location, size_t amount, char *t
 
     size_t copyLen = min(amount, aNode->nodeBuffer.length - copyOffset);
 
-    xmemcpy(target, aNode->nodeBuffer.data + copyOffset, copyLen);
+    memcpy(target, aNode->nodeBuffer.data + copyOffset, copyLen);
 
     return copyLen;
 }
@@ -229,11 +210,11 @@ mem_hdr::debugDump() const
     debugs (19, 0, "mem_hdr::debugDump: lowest offset: " << lowestOffset() << " highest offset + 1: " << endOffset() << ".");
     std::ostringstream result;
     PointerPrinter<mem_node *> foo(result, " - ");
-    for_each (getNodes().begin(), getNodes().end(), foo);
+    getNodes().visit(foo);
     debugs (19, 0, "mem_hdr::debugDump: Current available data is: " << result.str() << ".");
 }
 
-/* FIXME: how do we deal with sparse results -
+/* XXX: how do we deal with sparse results -
  * where we have (say)
  * 0-500 and 1000-1500, but are asked for
  * 0-2000
@@ -245,12 +226,12 @@ mem_hdr::copy(StoreIOBuffer const &target) const
 {
 
     assert(target.range().end > target.range().start);
-    debugs(19, 6, "memCopy: " << target.range());
+    debugs(19, 6, "memCopy: " << this << " " << target.range());
 
     /* we shouldn't ever ask for absent offsets */
 
     if (nodes.size() == 0) {
-        debugs(19, 1, "mem_hdr::copy: No data to read");
+        debugs(19, DBG_IMPORTANT, "mem_hdr::copy: No data to read");
         debugDump();
         assert (0);
         return 0;
@@ -263,10 +244,10 @@ mem_hdr::copy(StoreIOBuffer const &target) const
     mem_node *p = getBlockContainingLocation(target.offset);
 
     if (!p) {
-        debugs(19, 1, "memCopy: could not find start of " << target.range() <<
+        debugs(19, DBG_IMPORTANT, "ERROR: memCopy: could not find start of " << target.range() <<
                " in memory.");
         debugDump();
-        fatal("Squid has attempted to read data from memory that is not present. This is an indication of of (pre-3.0) code that hasn't been updated to deal with sparse objects in memory. Squid should coredump.allowing to review the cause. Immediately preceeding this message is a dump of the available data in the format [start,end). The [ means from the value, the ) means up to the value. I.e. [1,5) means that there are 4 bytes of data, at offsets 1,2,3,4.\n");
+        fatal_dump("Squid has attempted to read data from memory that is not present. This is an indication of of (pre-3.0) code that hasn't been updated to deal with sparse objects in memory. Squid should coredump.allowing to review the cause. Immediately preceding this message is a dump of the available data in the format [start,end). The [ means from the value, the ) means up to the value. I.e. [1,5) means that there are 4 bytes of data, at offsets 1,2,3,4.\n");
         return 0;
     }
 
@@ -274,7 +255,7 @@ mem_hdr::copy(StoreIOBuffer const &target) const
     char *ptr_to_buf = target.data;
     int64_t location = target.offset;
 
-    /* Start copying begining with this block until
+    /* Start copying beginning with this block until
      * we're satiated */
 
     while (p && bytes_to_go > 0) {
@@ -310,7 +291,7 @@ mem_hdr::hasContigousContentRange(Range<int64_t> const & range) const
             return true;
     }
 
-    return false;
+    return !range.size(); // empty range is contiguous
 }
 
 bool
@@ -332,7 +313,7 @@ mem_hdr::nodeToRecieve(int64_t offset)
         return nodes.start()->data;
     }
 
-    mem_node *candidate = NULL;
+    mem_node *candidate = nullptr;
     /* case 2: location fits within an extant node */
 
     if (offset > 0) {
@@ -356,18 +337,15 @@ mem_hdr::nodeToRecieve(int64_t offset)
     return candidate;
 }
 
-
 bool
 mem_hdr::write (StoreIOBuffer const &writeBuffer)
 {
-    PROF_start(mem_hdr_write);
-    debugs(19, 6, "mem_hdr::write: " << writeBuffer.range() << " object end " << endOffset());
+    debugs(19, 6, "mem_hdr::write: " << this << " " << writeBuffer.range() << " object end " << endOffset());
 
     if (unionNotEmpty(writeBuffer)) {
-        debugs(19,0,"mem_hdr::write: writeBuffer: " << writeBuffer.range());
+        debugs(19, DBG_CRITICAL, "mem_hdr::write: writeBuffer: " << writeBuffer.range());
         debugDump();
-        fatal("Attempt to overwrite already in-memory data. Preceeding this there should be a mem_hdr::write output that lists the attempted write, and the currently present data. Please get a 'backtrace full' from this error - using the generated core, and file a bug report with the squid developers including the last 10 lines of cache.log and the backtrace.\n");
-        PROF_stop(mem_hdr_write);
+        fatal_dump("Attempt to overwrite already in-memory data. Preceding this there should be a mem_hdr::write output that lists the attempted write, and the currently present data. Please get a 'backtrace full' from this error - using the generated core, and file a bug report with the squid developers including the last 10 lines of cache.log and the backtrace.\n");
         return false;
     }
 
@@ -386,12 +364,13 @@ mem_hdr::write (StoreIOBuffer const &writeBuffer)
         currentSource += wrote;
     }
 
-    PROF_stop(mem_hdr_write);
     return true;
 }
 
 mem_hdr::mem_hdr() : inmem_hi(0)
-{}
+{
+    debugs(19, 9, this << " hi: " << inmem_hi);
+}
 
 mem_hdr::~mem_hdr()
 {
@@ -417,8 +396,8 @@ mem_hdr::NodeCompare(mem_node * const &left, mem_node * const &right)
 void
 mem_hdr::dump() const
 {
-    debugs(20, 1, "mem_hdr: " << (void *)this << " nodes.start() " << nodes.start());
-    debugs(20, 1, "mem_hdr: " << (void *)this << " nodes.finish() " << nodes.finish());
+    debugs(20, DBG_IMPORTANT, "mem_hdr: " << (void *)this << " nodes.start() " << nodes.start());
+    debugs(20, DBG_IMPORTANT, "mem_hdr: " << (void *)this << " nodes.finish() " << nodes.finish());
 }
 
 size_t
@@ -435,7 +414,7 @@ mem_hdr::start() const
     if (result)
         return result->data;
 
-    return NULL;
+    return nullptr;
 }
 
 const Splay<mem_node *> &
@@ -443,3 +422,4 @@ mem_hdr::getNodes() const
 {
     return nodes;
 }
+