"Fossies" - the Fresh Open Source Software Archive

Member "Hoard-3.13/src/include/hoard/hoardheap.h" (2 Jan 2019, 6498 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 "hoardheap.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.emeryberger.com
    9  
   10   Copyright (c) 1998-2018 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 #ifndef HOARD_HOARDHEAP_H
   29 #define HOARD_HOARDHEAP_H
   30 
   31 #include <assert.h>
   32 
   33 #include "heaplayers.h"
   34 
   35 using namespace HL;
   36 
   37 // The minimum allocation grain for a given object -
   38 // that is, we carve objects out of chunks of this size.
   39 //#define SUPERBLOCK_SIZE 65536
   40 
   41 #define SUPERBLOCK_SIZE (1UL << 21)
   42 
   43 //#define SUPERBLOCK_SIZE (256*1048576)
   44 //#define SUPERBLOCK_SIZE (512*1048576)
   45 //#define SUPERBLOCK_SIZE (1024UL*1048576)
   46 
   47 // The number of 'emptiness classes'; see the ASPLOS paper for details.
   48 #define EMPTINESS_CLASSES 8
   49 
   50 
   51 // Hoard-specific layers
   52 
   53 #include "thresholdheap.h"
   54 #include "hoardmanager.h"
   55 #include "addheaderheap.h"
   56 #include "threadpoolheap.h"
   57 #include "redirectfree.h"
   58 #include "ignoreinvalidfree.h"
   59 #include "conformantheap.h"
   60 #include "hoardsuperblock.h"
   61 #include "hoardsuperblockheader.h"
   62 #include "lockmallocheap.h"
   63 #include "alignedsuperblockheap.h"
   64 #include "alignedmmap.h"
   65 #include "globalheap.h"
   66 
   67 #include "thresholdsegheap.h"
   68 #include "geometricsizeclass.h"
   69 
   70 // Note from Emery Berger: I plan to eventually eliminate the use of
   71 // the spin lock, since the right place to do locking is in an
   72 // OS-supplied library, and platforms have substantially improved the
   73 // efficiency of these primitives.
   74 
   75 #if defined(_WIN32)
   76 typedef HL::WinLockType TheLockType;
   77 #elif defined(__APPLE__)
   78 // NOTE: On older versions of the Mac OS, Hoard CANNOT use Posix locks,
   79 // since they may call malloc themselves. However, as of Snow Leopard,
   80 // that problem seems to have gone away. Nonetheless, we use Mac-specific locks.
   81 typedef HL::MacLockType TheLockType;
   82 #elif defined(__SVR4)
   83 typedef HL::SpinLockType TheLockType;
   84 #else
   85 typedef HL::SpinLockType TheLockType;
   86 #endif
   87 
   88 #if defined(__clang__)
   89 #pragma clang diagnostic push
   90 #pragma clang diagnostic ignored "-Wunused-variable"
   91 #endif
   92 
   93 namespace Hoard {
   94 
   95   class MmapSource : public AlignedMmap<SUPERBLOCK_SIZE, TheLockType> {};
   96   
   97   //
   98   // There is just one "global" heap, shared by all of the per-process heaps.
   99   //
  100 
  101   typedef GlobalHeap<SUPERBLOCK_SIZE, HoardSuperblockHeader, EMPTINESS_CLASSES, MmapSource, TheLockType>
  102   TheGlobalHeap;
  103   
  104   //
  105   // When a thread frees memory and causes a per-process heap to fall
  106   // below the emptiness threshold given in the function below, it
  107   // moves a (nearly or completely empty) superblock to the global heap.
  108   //
  109 
  110   class hoardThresholdFunctionClass {
  111   public:
  112     inline static bool function (unsigned int u,
  113                  unsigned int a,
  114                  size_t objSize)
  115     {
  116       /*
  117     Returns 1 iff we've crossed the emptiness threshold:
  118     
  119     U < A - 2S   &&   U < EMPTINESS_CLASSES-1/EMPTINESS_CLASSES * A
  120     
  121       */
  122       auto r = ((EMPTINESS_CLASSES * u) < ((EMPTINESS_CLASSES-1) * a)) && ((u < a - (2 * SUPERBLOCK_SIZE) / objSize));
  123       return r;
  124     }
  125   };
  126   
  127 
  128   class SmallHeap;
  129   
  130   //  typedef Hoard::HoardSuperblockHeader<TheLockType, SUPERBLOCK_SIZE, SmallHeap> HSHeader;
  131   typedef HoardSuperblock<TheLockType, SUPERBLOCK_SIZE, SmallHeap, Hoard::HoardSuperblockHeader> SmallSuperblockType;
  132 
  133   //
  134   // The heap that manages small objects.
  135   //
  136   class SmallHeap : 
  137     public ConformantHeap<
  138     HoardManager<AlignedSuperblockHeap<TheLockType, SUPERBLOCK_SIZE, MmapSource>,
  139          TheGlobalHeap,
  140          SmallSuperblockType,
  141          EMPTINESS_CLASSES,
  142          TheLockType,
  143          hoardThresholdFunctionClass,
  144          SmallHeap> > 
  145   {};
  146 
  147   class BigHeap;
  148 
  149   typedef HoardSuperblock<TheLockType,
  150               SUPERBLOCK_SIZE,
  151               BigHeap,
  152               Hoard::HoardSuperblockHeader>
  153   BigSuperblockType;
  154 
  155   // The heap that manages large objects.
  156 
  157 #if 0
  158 
  159   // Old version: slow and now deprecated. Returns every large object
  160   // back to the system immediately.
  161   typedef ConformantHeap<HL::LockedHeap<TheLockType,
  162                     AddHeaderHeap<BigSuperblockType,
  163                               SUPERBLOCK_SIZE,
  164                               MmapSource > > >
  165   bigHeapType;
  166 
  167 #else
  168 
  169   // Experimental faster support for large objects.  MUCH MUCH faster
  170   // than the above (around 400x in some tests).  Keeps the amount of
  171   // retained memory at no more than X% more than currently allocated.
  172 
  173   class objectSource : public AddHeaderHeap<BigSuperblockType,
  174                         SUPERBLOCK_SIZE,
  175                         MmapSource> {};
  176 
  177   typedef HL::ThreadHeap<64, HL::LockedHeap<TheLockType,
  178                         ThresholdSegHeap<25,      // % waste
  179                                  1048576, // at least 1MB in any heap
  180                                  80,      // num size classes
  181                                  GeometricSizeClass<20>::size2class,
  182                                  GeometricSizeClass<20>::class2size,
  183                                  GeometricSizeClass<20>::MaxObjectSize,
  184                                  AdaptHeap<DLList, objectSource>,
  185                                  objectSource> > >
  186   bigHeapType;
  187 #endif
  188 
  189   class BigHeap : public bigHeapType {};
  190 
  191   enum { BigObjectSize = 
  192      HL::bins<SmallSuperblockType::Header, SUPERBLOCK_SIZE>::BIG_OBJECT };
  193 
  194   //
  195   // Each thread has its own heap for small objects.
  196   //
  197   class PerThreadHoardHeap :
  198     public RedirectFree<LockMallocHeap<SmallHeap>,
  199             SmallSuperblockType> {
  200   private:
  201     void nothing() {
  202       _dummy[0] = _dummy[0];
  203     }
  204     // Avoid false sharing.
  205     char _dummy[64];
  206   };
  207   
  208 
  209   template <int N, int NH>
  210   class HoardHeap :
  211     public HL::ANSIWrapper<
  212     IgnoreInvalidFree<
  213       HL::HybridHeap<Hoard::BigObjectSize,
  214              ThreadPoolHeap<N, NH, Hoard::PerThreadHoardHeap>,
  215              Hoard::BigHeap> > >
  216   {
  217   public:
  218     
  219     enum { BIG_OBJECT = Hoard::BigObjectSize };
  220     
  221     HoardHeap() {
  222       enum { BIG_HEADERS = sizeof(Hoard::BigSuperblockType::Header),
  223          SMALL_HEADERS = sizeof(Hoard::SmallSuperblockType::Header)};
  224       static_assert(BIG_HEADERS == SMALL_HEADERS,
  225             "Headers must be the same size.");
  226     }
  227   };
  228 
  229 }
  230 
  231 #if defined(__clang__)
  232 #pragma clang diagnostic pop
  233 #endif
  234 
  235 #endif