]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/MemBuf.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / MemBuf.cc
index 723a2e56626a8b69292ec42beb2b01d2eb6dc5c4..33fb8e45c737a09dd268ba35667443092dd894ee 100644 (file)
@@ -1,47 +1,19 @@
 /*
- * $Id$
- *
- * DEBUG: section 59    auto-growing Memory Buffer with printf
- * AUTHOR: Alex Rousskov
- *
- * 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) 1996-2017 The Squid Software Foundation and contributors
  *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
  */
 
-/**
- \todo use memory pools for .buf recycling @?@ @?@
- */
+/* DEBUG: section 59    auto-growing Memory Buffer with printf */
 
 /**
  \verbatim
  * Rationale:
  * ----------
  *
- * Here is how one would comm_write an object without MemBuffer:
+ * Here is how one would Comm::Write an object without MemBuffer:
  *
  * {
  * -- allocate:
@@ -53,7 +25,7 @@
  * ...
  *
  * -- write
- * comm_write(buf, free, ...);
+ * Comm::Write(buf, free, ...);
  * }
  *
  * The whole "packing" idea is quite messy: We are given a buffer of fixed
@@ -91,7 +63,7 @@
  * ...
  *
  * -- write
- * comm_write_mbuf(fd, buf, handler, data);
+ * Comm::Write(fd, buf, callback);
  *
  * -- *iff* you did not give the buffer away, free it yourself
  * -- buf.clean();
  \endverbatim
  */
 
-/* if you have configure you can use this */
-#if defined(HAVE_CONFIG_H)
-#include "config.h"
-#endif
+#include "squid.h"
+#include "mem/forward.h"
+#include "MemBuf.h"
+#include "profiler/Profiler.h"
 
 #ifdef VA_COPY
 #undef VA_COPY
 #define VA_COPY __va_copy
 #endif
 
-#include "squid.h"
-#include "MemBuf.h"
-
 /* local constants */
 
 /* default values for buffer sizes, used by memBufDefInit */
@@ -131,7 +100,6 @@ MemBuf::init()
     init(MEM_BUF_INIT_SIZE, MEM_BUF_MAX_SIZE);
 }
 
-
 /** init with specific sizes */
 void
 MemBuf::init(mb_size_t szInit, mb_size_t szMax)
@@ -143,6 +111,7 @@ MemBuf::init(mb_size_t szInit, mb_size_t szMax)
     capacity = 0;
     stolen = 0;
     grow(szInit);
+    terminate();
 }
 
 /**
@@ -156,7 +125,7 @@ MemBuf::clean()
         // nothing to do
     } else {
         assert(buf);
-        assert(!stolen);       /* not frozen */
+        assert(!stolen);    /* not frozen */
 
         memFreeBuf(capacity, buf);
         buf = NULL;
@@ -174,7 +143,7 @@ MemBuf::reset()
     if (isNull()) {
         init();
     } else {
-        assert(!stolen);       /* not frozen */
+        assert(!stolen);    /* not frozen */
         /* reset */
         memset(buf, 0, capacity);
         size = 0;
@@ -185,12 +154,12 @@ MemBuf::reset()
  * Unfortunate hack to test if the buffer has been Init()ialized
  */
 int
-MemBuf::isNull()
+MemBuf::isNull() const
 {
     if (!buf && !max_capacity && !capacity && !size)
-        return 1;              /* is null (not initialized) */
+        return 1;       /* is null (not initialized) */
 
-    assert(buf && max_capacity && capacity);   /* paranoid */
+    assert(buf && max_capacity && capacity);    /* paranoid */
 
     return 0;
 }
@@ -217,7 +186,7 @@ void MemBuf::consume(mb_size_t shiftSize)
     PROF_start(MemBuf_consume);
     if (shiftSize > 0) {
         if (shiftSize < cSize)
-            xmemmove(buf, buf + shiftSize, cSize - shiftSize);
+            memmove(buf, buf + shiftSize, cSize - shiftSize);
 
         size -= shiftSize;
 
@@ -226,11 +195,34 @@ void MemBuf::consume(mb_size_t shiftSize)
     PROF_stop(MemBuf_consume);
 }
 
+/// removes all whitespace prefix bytes and "packs" by moving content left
+void MemBuf::consumeWhitespacePrefix()
+{
+    PROF_start(MemBuf_consumeWhitespace);
+    if (contentSize() > 0) {
+        const char *end = buf + contentSize();
+        const char *p = buf;
+        for (; p<end && xisspace(*p); ++p);
+        if (p-buf > 0)
+            consume(p-buf);
+    }
+    PROF_stop(MemBuf_consumeWhitespace);
+}
+
+// removes last tailSize bytes
+void MemBuf::truncate(mb_size_t tailSize)
+{
+    const mb_size_t cSize = contentSize();
+    assert(0 <= tailSize && tailSize <= cSize);
+    assert(!stolen); /* not frozen */
+    size -= tailSize;
+}
+
 /**
  * calls memcpy, appends exactly size bytes,
  * extends buffer or creates buffer if needed.
  */
-void MemBuf::append(const char *newContent, mb_size_t sz)
+void MemBuf::append(const char *newContent, int sz)
 {
     assert(sz >= 0);
     assert(buf || (0==capacity && 0==size));
@@ -242,9 +234,7 @@ void MemBuf::append(const char *newContent, mb_size_t sz)
             grow(size + sz + 1);
 
         assert(size + sz <= capacity); /* paranoid */
-
-        xmemcpy(space(), newContent, sz);
-
+        memcpy(space(), newContent, sz);
         appended(sz);
     }
     PROF_stop(MemBuf_append);
@@ -272,22 +262,12 @@ void MemBuf::terminate()
     *space() = '\0';
 }
 
-/* calls memBufVPrintf */
-void
-MemBuf::Printf(const char *fmt,...)
-{
-    va_list args;
-    va_start(args, fmt);
-    vPrintf(fmt, args);
-    va_end(args);
-}
-
-
 /**
- * vPrintf for other printf()'s to use; calls vsnprintf, extends buf if needed
+ * vappendf for other printf()'s to use; calls vsnprintf, extends buf if needed
  */
 void
-MemBuf::vPrintf(const char *fmt, va_list vargs) {
+MemBuf::vappendf(const char *fmt, va_list vargs)
+{
 #ifdef VA_COPY
     va_list ap;
 #endif
@@ -295,7 +275,7 @@ MemBuf::vPrintf(const char *fmt, va_list vargs) {
     int sz = 0;
     assert(fmt);
     assert(buf);
-    assert(!stolen);   /* not frozen */
+    assert(!stolen);    /* not frozen */
     /* assert in Grow should quit first, but we do not want to have a scary infinite loop */
 
     while (capacity <= max_capacity) {
@@ -332,7 +312,7 @@ MemBuf::vPrintf(const char *fmt, va_list vargs) {
     if (!size || buf[size - 1]) {
         assert(!buf[size]);
     } else {
-        size--;
+        --size;
     }
 }
 
@@ -345,13 +325,14 @@ MemBuf::vPrintf(const char *fmt, va_list vargs) {
  \retval free() function to be used.
  */
 FREE *
-MemBuf::freeFunc() {
+MemBuf::freeFunc()
+{
     FREE *ff;
     assert(buf);
-    assert(!stolen);   /* not frozen */
+    assert(!stolen);    /* not frozen */
 
     ff = memFreeBufFunc((size_t) capacity);
-    stolen = 1;                /* freeze */
+    stolen = 1;     /* freeze */
     return ff;
 }
 
@@ -359,7 +340,8 @@ MemBuf::freeFunc() {
  * Grows (doubles) internal buffer to satisfy required minimal capacity
  */
 void
-MemBuf::grow(mb_size_t min_cap) {
+MemBuf::grow(mb_size_t min_cap)
+{
     size_t new_cap;
     size_t buf_cap;
 
@@ -374,7 +356,7 @@ MemBuf::grow(mb_size_t min_cap) {
         new_cap = 64 * 1024;
 
         while (new_cap < (size_t) min_cap)
-            new_cap += 64 * 1024;      /* increase in reasonable steps */
+            new_cap += 64 * 1024;   /* increase in reasonable steps */
     } else {
         new_cap = (size_t) min_cap;
     }
@@ -383,9 +365,9 @@ MemBuf::grow(mb_size_t min_cap) {
     if (new_cap > (size_t) max_capacity)
         new_cap = (size_t) max_capacity;
 
-    assert(new_cap <= (size_t) max_capacity);  /* no overflow */
+    assert(new_cap <= (size_t) max_capacity);   /* no overflow */
 
-    assert(new_cap > (size_t) capacity);       /* progress */
+    assert(new_cap > (size_t) capacity);    /* progress */
 
     buf_cap = (size_t) capacity;
 
@@ -396,18 +378,15 @@ MemBuf::grow(mb_size_t min_cap) {
     PROF_stop(MemBuf_grow);
 }
 
-
 /* Reports */
 
 /**
  * Puts report on MemBuf _module_ usage into mb
  */
 void
-memBufReport(MemBuf * mb) {
+memBufReport(MemBuf * mb)
+{
     assert(mb);
-    mb->Printf("memBufReport is not yet implemented @?@\n");
+    mb->appendf("memBufReport is not yet implemented @?@\n");
 }
 
-#ifndef _USE_INLINE_
-#include "MemBuf.cci"
-#endif