"Fossies" - the Fresh Open Source Software Archive

Member "Hoard-3.13/src/include/superblocks/tlab.h" (2 Jan 2019, 4856 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 "tlab.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 /*
    4 
    5   The Hoard Multiprocessor Memory Allocator
    6   www.hoard.org
    7 
    8   Author: Emery Berger, http://www.cs.umass.edu/~emery
    9  
   10   Copyright (c) 1998-2012 Emery Berger
   11   
   12   This program is free software; you can redistribute it and/or modify
   13   it under the terms of the GNU General Public License as published by
   14   the Free Software Foundation; either version 2 of the License, or
   15   (at your option) any later version.
   16   
   17   This program is distributed in the hope that it will be useful,
   18   but WITHOUT ANY WARRANTY; without even the implied warranty of
   19   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   20   GNU General Public License for more details.
   21   
   22   You should have received a copy of the GNU General Public License
   23   along with this program; if not, write to the Free Software
   24   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   25 
   26 */
   27 
   28 /**
   29  *
   30  * @class  ThreadLocalAllocationBuffer
   31  * @author Emery Berger <http://www.cs.umass.edu/~emery>
   32  * @brief  An allocator, meant to be used for thread-local allocation.
   33  */
   34 
   35 #ifndef HOARD_TLAB_H
   36 #define HOARD_TLAB_H
   37 
   38 #include "heaplayers.h"
   39 
   40 #if defined(__clang__)
   41 #pragma clang diagnostic push
   42 #pragma clang diagnostic ignored "-Wunused-variable"
   43 #endif
   44 
   45 namespace Hoard {
   46 
   47   template <int NumBins,
   48         int (*getSizeClass) (size_t),
   49         size_t (*getClassSize) (int),
   50         size_t LargestObject,
   51         size_t LocalHeapThreshold,
   52         class SuperblockType,
   53         unsigned int SuperblockSize,
   54         class ParentHeap>
   55 
   56   class ThreadLocalAllocationBuffer {
   57 
   58     enum { DesiredAlignment = HL::MallocInfo::Alignment };
   59 
   60   public:
   61 
   62     enum { Alignment = ParentHeap::Alignment };
   63 
   64     ThreadLocalAllocationBuffer (ParentHeap * parent)
   65       : _parentHeap (parent),
   66         _localHeapBytes (0)
   67     {
   68       static_assert(gcd<Alignment, DesiredAlignment>::value == DesiredAlignment,
   69             "Alignment mismatch.");
   70       static_assert((Alignment >= 2 * sizeof(size_t)),
   71             "Alignment must be enough to hold two pointers.");
   72     }
   73 
   74     ~ThreadLocalAllocationBuffer() {
   75       clear();
   76     }
   77 
   78     inline static size_t getSize (void * ptr) {
   79       return getSuperblock(ptr)->getSize (ptr);
   80     }
   81 
   82     inline void * malloc (size_t sz) {
   83 #if 0
   84       if (sz < Alignment) {
   85         sz = Alignment;
   86       }
   87 #endif
   88       // Get memory from the local heap,
   89       // and deduct that amount from the local heap bytes counter.
   90       if (sz <= LargestObject) {
   91         auto c = getSizeClass (sz);
   92         auto * ptr = _localHeap(c).get();
   93         if (ptr) {
   94           assert (_localHeapBytes >= sz);
   95           _localHeapBytes -= getClassSize (c); // sz; 
   96           assert (getSize(ptr) >= sz);
   97           assert ((size_t) ptr % Alignment == 0);
   98           return ptr;
   99         }
  100       }
  101 
  102       // No more local memory (for this size, at least).
  103       // Now get the memory from our parent.
  104       auto * ptr = _parentHeap->malloc (sz);
  105       assert ((size_t) ptr % Alignment == 0);
  106       return ptr;
  107     }
  108 
  109 
  110     inline void free (void * ptr) {
  111       auto * s = getSuperblock (ptr);
  112       // If this isn't a valid superblock, just return.
  113 
  114       if (s && s->isValidSuperblock()) {
  115 
  116         ptr = s->normalize (ptr);
  117         auto sz = s->getObjectSize ();
  118 
  119         if ((sz <= LargestObject) && (sz + _localHeapBytes <= LocalHeapThreshold)) {
  120           // Free small objects locally, unless we are out of space.
  121 
  122           assert (getSize(ptr) >= sizeof(HL::SLList::Entry *));
  123           auto c = getSizeClass (sz);
  124 
  125           _localHeap(c).insert ((HL::SLList::Entry *) ptr);
  126           _localHeapBytes += getClassSize(c); // sz;
  127           
  128         } else {
  129 
  130           // Free it to the parent.
  131           _parentHeap->free (ptr);
  132         }
  133 
  134       } else {
  135         // Illegal pointer.
  136       }
  137     }
  138 
  139     void clear() {
  140       // Free every object to the 'parent' heap.
  141       int i = NumBins - 1;
  142       while ((_localHeapBytes > 0) && (i >= 0)) {
  143         auto sz = getClassSize (i);
  144         while (!_localHeap(i).isEmpty()) {
  145           auto * e = _localHeap(i).get();
  146           _parentHeap->free (e);
  147           _localHeapBytes -= sz;
  148         }
  149         i--;
  150       }
  151     }
  152 
  153     static inline SuperblockType * getSuperblock (void * ptr) {
  154       return SuperblockType::getSuperblock (ptr);
  155     }
  156 
  157   private:
  158 
  159     // Disable assignment and copying.
  160 
  161     ThreadLocalAllocationBuffer (const ThreadLocalAllocationBuffer&);
  162     ThreadLocalAllocationBuffer& operator=(const ThreadLocalAllocationBuffer&);
  163 
  164     /// Padding to prevent false sharing and ensure alignment.
  165     double _pad[128 / sizeof(double)];
  166 
  167     /// This heap's 'parent' (where to go for more memory).
  168     ParentHeap * _parentHeap;
  169 
  170     /// The number of bytes we currently have on this thread.
  171     size_t _localHeapBytes;
  172 
  173     /// The local heap itself.
  174     Array<NumBins, HL::SLList> _localHeap;
  175   };
  176 
  177 }
  178 
  179 #if defined(__clang__)
  180 #pragma clang diagnostic pop
  181 #endif
  182 
  183 #endif
  184