Eneboo - Documentación para desarrolladores
src/hoard/src/hoardsuperblock.h
Ir a la documentación de este archivo.
00001 // -*- C++ -*-
00002 
00003 #ifndef _HOARDSUPERBLOCK_H_
00004 #define _HOARDSUPERBLOCK_H_
00005 
00006 #include <cassert>
00007 #include <cstdlib>
00008 
00009 #include "hldefines.h"
00010 #include "freesllist.h"
00011 #include "hoardsuperblockheader.h"
00012 
00013 namespace Hoard {
00014 
00015   template <class LockType,
00016             int SuperblockSize,
00017             typename HeapType>
00018   class HoardSuperblock {
00019   public:
00020 
00021     HoardSuperblock (size_t sz)
00022       : _header (sz, BufferSize)
00023     {
00024       assert (_header.isValid());
00025     }
00026     
00029     static inline HoardSuperblock * getSuperblock (void * ptr) {
00030       return (HoardSuperblock *)
00031         (((size_t) ptr) & ~((size_t) SuperblockSize-1));
00032     }
00033 
00034     INLINE size_t getSize (void * ptr) const {
00035       if (_header.isValid() && inRange (ptr)) {
00036         return _header.getSize (ptr);
00037       } else {
00038         return 0;
00039       }
00040     }
00041 
00042 
00043     INLINE size_t getObjectSize (void) const {
00044       if (_header.isValid()) {
00045         return _header.getObjectSize();
00046       } else {
00047         return 0;
00048       }
00049     }
00050 
00051     MALLOC_FUNCTION INLINE void * malloc (size_t sz) {
00052       sz = sz; // avoid warning
00053       assert (_header.isValid());
00054       void * ptr = _header.malloc();
00055       if (ptr) {
00056         assert (inRange (ptr));
00057       }
00058       return ptr;
00059     }
00060 
00061     INLINE void free (void * ptr) {
00062       if (_header.isValid() && inRange (ptr)) {
00063         // Pointer is in range.
00064         _header.free (ptr);
00065       } else {
00066         // Invalid free.
00067       }
00068     }
00069     
00070     void clear (void) {
00071       if (_header.isValid())
00072         _header.clear();
00073     }
00074     
00075     // ----- below here are non-conventional heap methods ----- //
00076     
00077     INLINE bool isValidSuperblock (void) const {
00078       assert (_header.isValid());
00079       bool b = _header.isValid();
00080       return b;
00081     }
00082     
00083     INLINE int getTotalObjects (void) const {
00084       assert (_header.isValid());
00085       return _header.getTotalObjects();
00086     }
00087     
00089     INLINE int getObjectsFree (void) const {
00090       assert (_header.isValid());
00091       assert (_header.getObjectsFree() >= 0);
00092       assert (_header.getObjectsFree() <= _header.getTotalObjects());
00093       return _header.getObjectsFree();
00094     }
00095     
00096     inline void lock (void) {
00097       assert (_header.isValid());
00098       _header.lock();
00099     }
00100     
00101     inline void unlock (void) {
00102       assert (_header.isValid());
00103       _header.unlock();
00104     }
00105     
00106     inline HeapType * getOwner (void) const {
00107       assert (_header.isValid());
00108       return _header.getOwner();
00109     }
00110 
00111     inline void setOwner (HeapType * o) {
00112       assert (_header.isValid());
00113       assert (o != NULL);
00114       _header.setOwner (o);
00115     }
00116     
00117     inline HoardSuperblock * getNext (void) const {
00118       assert (_header.isValid());
00119       return _header.getNext();
00120     }
00121 
00122     inline HoardSuperblock * getPrev (void) const {
00123       assert (_header.isValid());
00124       return _header.getPrev();
00125     }
00126     
00127     inline void setNext (HoardSuperblock * f) {
00128       assert (_header.isValid());
00129       assert (f != this);
00130       _header.setNext (f);
00131     }
00132     
00133     inline void setPrev (HoardSuperblock * f) {
00134       assert (_header.isValid());
00135       assert (f != this);
00136       _header.setPrev (f);
00137     }
00138     
00139     INLINE bool inRange (void * ptr) const {
00140       // Returns true iff the pointer is valid.
00141       const size_t ptrValue = (size_t) ptr;
00142       return ((ptrValue >= (size_t) _buf) &&
00143               (ptrValue <= (size_t) &_buf[BufferSize]));
00144     }
00145     
00146     INLINE void * normalize (void * ptr) const {
00147       void * ptr2 = _header.normalize (ptr);
00148       assert (inRange (ptr));
00149       assert (inRange (ptr2));
00150       return ptr2;
00151     }
00152 
00153     typedef Hoard::HoardSuperblockHeader<LockType, SuperblockSize, HeapType> Header;
00154 
00155   private:
00156     
00157     
00158     // Disable copying and assignment.
00159     
00160     HoardSuperblock (const HoardSuperblock&);
00161     HoardSuperblock& operator=(const HoardSuperblock&);
00162     
00163     enum { BufferSize = SuperblockSize - sizeof(Header) };
00164     
00166     Header _header;
00167 
00168     
00170     char _buf[BufferSize];
00171   };
00172 
00173 }
00174 
00175 
00176 #endif
 Todo Clases Namespaces Archivos Funciones Variables 'typedefs' Enumeraciones Valores de enumeraciones Propiedades Amigas 'defines'