"Fossies" - the Fresh Open Source Software Archive

Member "Hoard-3.13/src/include/superblocks/alignedsuperblockheap.h" (2 Jan 2019, 3215 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 "alignedsuperblockheap.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_ALIGNEDSUPERBLOCKHEAP_H
   29 #define HOARD_ALIGNEDSUPERBLOCKHEAP_H
   30 
   31 #include "heaplayers.h"
   32 
   33 #include "conformantheap.h"
   34 #include "fixedrequestheap.h"
   35 
   36 namespace Hoard {
   37 
   38   template <size_t SuperblockSize,
   39         class TheLockType,
   40         class MmapSource>
   41   class SuperblockStore {
   42   public:
   43 
   44     enum { Alignment = MmapSource::Alignment };
   45 
   46     SuperblockStore() {
   47 #if defined(__SVR4)
   48       // We only get 64K chunks from mmap on Solaris, so we need to grab
   49       // more chunks (and align them to 64K!) for smaller superblock sizes.
   50       // Right now, we do not handle this case and just assert here that
   51       // we are getting chunks of 64K.
   52       static_assert(SuperblockSize == 65536,
   53             "This is needed to prevent mmap fragmentation.");
   54 #endif
   55     }
   56     
   57     void * malloc (size_t) {
   58       if (_freeSuperblocks.isEmpty()) {
   59     // Get more memory.
   60     void * ptr = _superblockSource.malloc (ChunksToGrab * SuperblockSize);
   61     if (!ptr) {
   62       return nullptr;
   63     }
   64     char * p = (char *) ptr;
   65     for (int i = 0; i < ChunksToGrab; i++) {
   66       _freeSuperblocks.insert ((DLList::Entry *) p);
   67       p += SuperblockSize;
   68     }
   69       }
   70       return _freeSuperblocks.get();
   71     }
   72 
   73     void free (void * ptr) {
   74       _freeSuperblocks.insert ((DLList::Entry *) ptr);
   75     }
   76 
   77   private:
   78 
   79 #if defined(__SVR4)
   80     enum { ChunksToGrab = 1 };
   81 #else
   82     enum { ChunksToGrab = 1 };
   83 #endif
   84 
   85     MmapSource _superblockSource;
   86     DLList _freeSuperblocks;
   87 
   88   };
   89 
   90 }
   91 
   92 
   93 namespace Hoard {
   94 
   95   template <class TheLockType,
   96         size_t SuperblockSize,
   97         class MmapSource>
   98   class AlignedSuperblockHeapHelper :
   99     public ConformantHeap<HL::LockedHeap<TheLockType,
  100                      FixedRequestHeap<SuperblockSize, 
  101                               SuperblockStore<SuperblockSize, TheLockType, MmapSource> > > > {};
  102 
  103 
  104 #if 0
  105 
  106   template <class TheLockType,
  107         size_t SuperblockSize>
  108   class AlignedSuperblockHeap : public AlignedMmap<SuperblockSize,TheLockType> {};
  109 
  110 
  111 #else
  112 
  113   template <class TheLockType,
  114         size_t SuperblockSize,
  115         class MmapSource>
  116   class AlignedSuperblockHeap :
  117     public AlignedSuperblockHeapHelper<TheLockType, SuperblockSize, MmapSource> {
  118   public:
  119     AlignedSuperblockHeap() {
  120       static_assert(AlignedSuperblockHeapHelper<TheLockType, SuperblockSize, MmapSource>::Alignment % SuperblockSize == 0,
  121             "Ensure proper alignment.");
  122     }
  123 
  124   };
  125 #endif
  126 
  127 }
  128 
  129 #endif