"Fossies" - the Fresh Open Source Software Archive

Member "Hoard-3.13/src/include/hoard/thresholdsegheap.h" (2 Jan 2019, 2772 Bytes) of package /linux/misc/Hoard-3.13.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "thresholdsegheap.h" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3.12_vs_3.13.

    1 // -*- C++ -*-
    2 
    3 #ifndef HOARD_THRESHOLD_SEGHEAP_H
    4 #define HOARD_THRESHOLD_SEGHEAP_H
    5 
    6 namespace Hoard {
    7 
    8   // Allows superheap to hold at least ThresholdSlop but no more than
    9   // ThresholdFraction% more memory than client currently holds.
   10 
   11   template <int ThresholdFraction, // % over current allowed in superheap.
   12         int ThresholdSlop,     // constant amount allowed in superheap.
   13         int NumBins,
   14         int (*getSizeClass) (const size_t),
   15         size_t (*getClassMaxSize) (const int),
   16         size_t MaxObjectSize,
   17         class LittleHeap,
   18         class BigHeap>
   19   class ThresholdSegHeap : public BigHeap
   20   {
   21   public:
   22 
   23     ThresholdSegHeap()
   24       : _currLive (0),
   25     _maxLive (0),
   26     _maxFraction (1.0 + (double) ThresholdFraction / 100.0),
   27     _cleared (false)
   28     {}
   29 
   30     size_t getSize (void * ptr) {
   31       return BigHeap::getSize(ptr);
   32     }
   33 
   34     void * malloc (size_t sz) {
   35       if (sz >= MaxObjectSize) {
   36     return BigHeap::malloc (sz);
   37       }
   38       // Once the amount of cached memory in the superheap exceeds the
   39       // desired threshold over max live requested by the client, dump
   40       // it all.
   41       const int sizeClass = getSizeClass (sz);
   42       const size_t maxSz = getClassMaxSize (sizeClass);
   43       if (sizeClass >= NumBins) {
   44     return BigHeap::malloc (maxSz);
   45       } else {
   46     void * ptr = _heap[sizeClass].malloc (maxSz);
   47     if (ptr == nullptr) {
   48       return BigHeap::malloc (maxSz);
   49     }
   50     assert (getSize(ptr) <= maxSz);
   51     _currLive += getSize (ptr);
   52     if (_currLive >= _maxLive) {
   53       _maxLive = _currLive;
   54       _cleared = false;
   55     }
   56     return ptr;
   57       }
   58     }
   59 
   60     void free (void * ptr) {
   61       // Update current live memory stats, then free the object.
   62       size_t sz = getSize(ptr);
   63       if (sz >= MaxObjectSize) {
   64     BigHeap::free (ptr);
   65     return;
   66       }
   67       int cl = getSizeClass (sz);
   68       if (cl >= NumBins) {
   69     BigHeap::free (ptr);
   70     return;
   71       }
   72       if (_currLive < sz) {
   73     _currLive = 0;
   74       } else {
   75     _currLive -= sz;
   76       }
   77       _heap[cl].free (ptr);
   78       bool crossedThreshold = (double) _maxLive > _maxFraction * (double) _currLive;
   79       if ((_currLive > ThresholdSlop) && crossedThreshold && !_cleared)
   80     {
   81       // When we drop below the threshold, clear the heap.
   82       for (int i = 0; i < NumBins; i++) {
   83         _heap[i].clear();
   84       }
   85       // We won't clear again until we reach maxlive again.
   86       _cleared = true;
   87       _maxLive = _currLive;
   88     }
   89     }
   90 
   91   private:
   92 
   93     /// The current amount of live memory held by a client of this heap.
   94     unsigned long _currLive;
   95 
   96     /// The maximum amount of live memory held by a client of this heap.
   97     unsigned long _maxLive;
   98 
   99     /// Maximum fraction calculation.
  100     const double _maxFraction;
  101 
  102     /// Have we already cleared out the superheap?
  103     bool _cleared;
  104 
  105     LittleHeap _heap[NumBins];
  106   };
  107 
  108 }
  109 
  110 #endif
  111