"Fossies" - the Fresh Open Source Software Archive

Member "Hoard-3.13/src/include/hoard/hoardsuperblock.h" (2 Jan 2019, 5069 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 "hoardsuperblock.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_HOARDSUPERBLOCK_H
   29 #define HOARD_HOARDSUPERBLOCK_H
   30 
   31 #include <cassert>
   32 #include <cstdlib>
   33 
   34 #include "heaplayers.h"
   35 
   36 namespace Hoard {
   37 
   38   template <class LockType,
   39         int SuperblockSize,
   40         typename HeapType,
   41         template <class LockType_,
   42               int SuperblockSize_,
   43               typename HeapType_> class Header_>
   44   class HoardSuperblock {
   45   public:
   46 
   47     HoardSuperblock (size_t sz)
   48       : _header (sz, BufferSize)
   49     {
   50       assert (_header.isValid());
   51       assert (this == (HoardSuperblock *)
   52           (((size_t) this) & ~((size_t) SuperblockSize-1)));
   53     }
   54     
   55     /// @brief Find the start of the superblock by bitmasking.
   56     /// @note  All superblocks <em>must</em> be naturally aligned, and powers of two.
   57     static inline HoardSuperblock * getSuperblock (void * ptr) {
   58       return (HoardSuperblock *)
   59     (((size_t) ptr) & ~((size_t) SuperblockSize-1));
   60     }
   61 
   62     INLINE size_t getSize (void * ptr) const {
   63       if (_header.isValid() && inRange (ptr)) {
   64     return _header.getSize (ptr);
   65       } else {
   66     return 0;
   67       }
   68     }
   69 
   70 
   71     INLINE size_t getObjectSize() const {
   72       if (_header.isValid()) {
   73     return _header.getObjectSize();
   74       } else {
   75     return 0;
   76       }
   77     }
   78 
   79     MALLOC_FUNCTION INLINE void * malloc (size_t) {
   80       assert (_header.isValid());
   81       auto * ptr = _header.malloc();
   82       if (ptr) {
   83     assert (inRange (ptr));
   84     assert ((size_t) ptr % HeapType::Alignment == 0);
   85       }
   86       return ptr;
   87     }
   88 
   89     INLINE void free (void * ptr) {
   90       if (_header.isValid() && inRange (ptr)) {
   91     // Pointer is in range.
   92     _header.free (ptr);
   93       } else {
   94     // Invalid free.
   95       }
   96     }
   97     
   98     void clear() {
   99       if (_header.isValid())
  100     _header.clear();
  101     }
  102     
  103     // ----- below here are non-conventional heap methods ----- //
  104     
  105     INLINE bool isValidSuperblock() const {
  106       auto b = _header.isValid();
  107       return b;
  108     }
  109     
  110     INLINE unsigned int getTotalObjects() const {
  111       assert (_header.isValid());
  112       return _header.getTotalObjects();
  113     }
  114     
  115     /// Return the number of free objects in this superblock.
  116     INLINE unsigned int getObjectsFree() const {
  117       assert (_header.isValid());
  118       assert (_header.getObjectsFree() >= 0);
  119       assert (_header.getObjectsFree() <= _header.getTotalObjects());
  120       return _header.getObjectsFree();
  121     }
  122     
  123     inline void lock() {
  124       assert (_header.isValid());
  125       _header.lock();
  126     }
  127     
  128     inline void unlock() {
  129       assert (_header.isValid());
  130       _header.unlock();
  131     }
  132     
  133     inline HeapType * getOwner() const {
  134       assert (_header.isValid());
  135       return _header.getOwner();
  136     }
  137 
  138     inline void setOwner (HeapType * o) {
  139       assert (_header.isValid());
  140       assert (o != nullptr);
  141       _header.setOwner (o);
  142     }
  143     
  144     inline HoardSuperblock * getNext() const {
  145       assert (_header.isValid());
  146       return _header.getNext();
  147     }
  148 
  149     inline HoardSuperblock * getPrev() const {
  150       assert (_header.isValid());
  151       return _header.getPrev();
  152     }
  153     
  154     inline void setNext (HoardSuperblock * f) {
  155       assert (_header.isValid());
  156       assert (f != this);
  157       _header.setNext (f);
  158     }
  159     
  160     inline void setPrev (HoardSuperblock * f) {
  161       assert (_header.isValid());
  162       assert (f != this);
  163       _header.setPrev (f);
  164     }
  165     
  166     INLINE bool inRange (void * ptr) const {
  167       // Returns true iff the pointer is valid.
  168       auto ptrValue = (size_t) ptr;
  169       return ((ptrValue >= (size_t) _buf) &&
  170           (ptrValue < (size_t) &_buf[BufferSize]));
  171     }
  172     
  173     INLINE void * normalize (void * ptr) const {
  174       auto * ptr2 = _header.normalize (ptr);
  175       assert (inRange (ptr));
  176       assert (inRange (ptr2));
  177       return ptr2;
  178     }
  179 
  180     typedef Header_<LockType, SuperblockSize, HeapType> Header;
  181 
  182   private:
  183     
  184     
  185     // Disable copying and assignment.
  186     
  187     HoardSuperblock (const HoardSuperblock&);
  188     HoardSuperblock& operator=(const HoardSuperblock&);
  189     
  190     enum { BufferSize = SuperblockSize - sizeof(Header) };
  191     
  192     /// The metadata.
  193     Header _header;
  194 
  195     
  196     /// The actual buffer. MUST immediately follow the header!
  197     char _buf[BufferSize];
  198   };
  199 
  200 }
  201 
  202 
  203 #endif