]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/StatHist.h
SourceFormat Enforcement
[thirdparty/squid.git] / src / StatHist.h
index 576525d21a16d348cc085e9a8fbbc914e4d30dbd..54f42e42e48643fa883ff597ea780dd2f612047e 100644 (file)
@@ -1,29 +1,9 @@
 /*
- * SQUID Web Proxy Cache          http://www.squid-cache.org/
- * ----------------------------------------------------------
+ * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
  *
- *  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.
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
  */
 
 #ifndef STATHIST_H_
@@ -32,7 +12,6 @@
 /* for StoreEntry */
 #include "Store.h"
 
-
 /// function signature for in/out StatHist adaptation
 typedef double hbase_f(double);
 
@@ -57,7 +36,7 @@ public:
      */
     StatHist();
     StatHist(const StatHist&); //not needed
-    ~StatHist();
+    ~StatHist() { clear(); };
 
     typedef uint64_t bins_type;
 
@@ -74,23 +53,36 @@ public:
      *  this and the supplied histogram.
      */
     double deltaPctile(const StatHist &B, double pctile) const;
+
     /** obtain the output-transformed value from the specified bin
      *
      */
     double val(unsigned int bin) const;
+
     /** increment the counter for the histogram entry
      * associated to the supplied value
      */
     void count(double val);
+
     /** iterate the supplied bd function over the histogram values
      */
     void dump(StoreEntry *sentry, StatHistBinDumper * bd) const;
+
     /** Initialize the Histogram using a logarithmic values distribution
      */
     void logInit(unsigned int capacity, double min, double max);
+
     /** initialize the histogram to count occurrences in an enum-represented set
      */
     void enumInit(unsigned int last_enum);
+
+    /** Import values from another histogram
+     *
+     * \note: the two histograms MUST have the same capicity, min and max or
+     *      an exception will be raised
+     */
+    StatHist &operator += (const StatHist &B);
+
 protected:
     /** low-level initialize function. called by *Init high-level functions
      * \note Important restrictions on val_in and val_out functions:
@@ -104,16 +96,21 @@ protected:
      *  See log and linear based histograms for examples
      */
     void init(unsigned int capacity, hbase_f * val_in, hbase_f * val_out, double min, double max);
+
     /// find what entry in the histogram corresponds to v, by applying
     /// the preset input transformation function
     unsigned int findBin(double v);
+
     /// the histogram counters
     bins_type *bins;
     unsigned int capacity_;
+
     /// minimum value to be stored, corresponding to the first bin
     double min_;
+
     /// value of the maximum counter in the histogram
     double max_;
+
     /// scaling factor when looking for a bin
     double scale_;
     hbase_f *val_in;        /* e.g., log() for log-based histogram */
@@ -130,30 +127,34 @@ StatHist::operator =(const StatHist & src)
 {
     if (this==&src) //handle self-assignment
         return *this;
-    xfree(bins); // xfree can handle NULL pointers, no need to check
-    capacity_=src.capacity_;
-    bins = static_cast<bins_type *>(xcalloc(src.capacity_, sizeof(bins_type)));
+    if (capacity_ != src.capacity_) {
+        xfree(bins); // xfree can handle NULL pointers, no need to check
+        capacity_=src.capacity_;
+        bins = static_cast<bins_type *>(xcalloc(src.capacity_, sizeof(bins_type)));
+    }
     min_=src.min_;
     max_=src.max_;
     scale_=src.scale_;
     val_in=src.val_in;
     val_out=src.val_out;
-    memcpy(bins,src.bins,capacity_*sizeof(*bins));
+    if (bins != NULL)
+        memcpy(bins,src.bins,capacity_*sizeof(*bins));
     return *this;
 }
 
 inline
 StatHist::StatHist() :
-        bins(NULL), capacity_(0), min_(0), max_(0),
-        scale_(1.0), val_in(NULL), val_out(NULL)
+    bins(NULL), capacity_(0), min_(0), max_(0),
+    scale_(1.0), val_in(NULL), val_out(NULL)
 {}
 
-inline
-StatHist::~StatHist()
+inline void
+StatHist::clear()
 {
-    xfree(bins); //can handle case of bins being NULL
+    xfree(bins); // can handle case of bins being NULL
     bins=NULL;
-    capacity_=0; //mark as destructed, may be needed for troubleshooting
+    capacity_=0; // mark as destructed, may be needed for troubleshooting
 }
 
 #endif /* STATHIST_H_ */
+