"Fossies" - the Fresh Open Source Software Archive

Member "Hoard-3.13/src/include/hoard/redirectfree.h" (2 Jan 2019, 2916 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 "redirectfree.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_REDIRECTFREE_H
   29 #define HOARD_REDIRECTFREE_H
   30 
   31 #include "heaplayers.h"
   32 
   33 namespace Hoard {
   34 
   35   /**
   36    * @class RedirectFree
   37    * @brief Routes free calls to the Superblock's owner heap.
   38    * @note  We also lock the heap on calls to malloc.
   39    */
   40 
   41   template <class Heap,
   42         typename SuperblockType_>
   43   class RedirectFree {
   44   public:
   45 
   46     enum { Alignment = Heap::Alignment };
   47 
   48     typedef SuperblockType_ SuperblockType;
   49 
   50     RedirectFree()
   51     {
   52     }
   53 
   54     inline void * malloc (size_t sz) {
   55       void * ptr = _theHeap.malloc (sz);
   56       assert (getSize(ptr) >= sz);
   57       assert ((size_t) ptr % Alignment == 0);
   58       return ptr;
   59     }
   60 
   61     size_t getSize (void * ptr) {
   62       return Heap::getSize (ptr);
   63     }
   64 
   65     SuperblockType * getSuperblock (void * ptr) {
   66       return Heap::getSuperblock (ptr);
   67     }
   68 
   69     /// Free the given object, obeying the required locking protocol.
   70     static inline void free (void * ptr) {
   71       // Get the superblock header.
   72       SuperblockType * s = reinterpret_cast<SuperblockType *>(Heap::getSuperblock (ptr));
   73 
   74       assert (s->isValidSuperblock());
   75 
   76       // Find out who the owner is.
   77 
   78       typedef BaseHoardManager<SuperblockType> * baseHeapType;
   79       baseHeapType owner;
   80 
   81       s->lock();
   82 
   83       // By acquiring the lock on the superblock (above),
   84       // we prevent it from moving up to a higher heap.
   85       // This eventually pins it down in one heap,
   86       // so this loop is guaranteed to terminate.
   87       // (It should generally take no more than two iterations.)
   88 
   89       for (;;) {
   90     owner = reinterpret_cast<baseHeapType>(s->getOwner());
   91     assert (owner != nullptr);
   92     assert (owner->isValid());
   93     // Lock the owner. If ownership changed between these two lines,
   94     // we'll detect it and try again.
   95     owner->lock();
   96     if (owner == reinterpret_cast<baseHeapType>(s->getOwner())) {
   97       owner->free (ptr);
   98       owner->unlock();
   99       s->unlock();
  100       return;
  101     }
  102     owner->unlock();
  103 
  104     // Sleep a little.
  105     HL::Fred::yield();
  106       }
  107     }
  108 
  109   private:
  110 
  111     Heap _theHeap;
  112 
  113   };
  114 
  115 }
  116 
  117 #endif