usermode/library/pmalloc/src/block.h

00001 
00002 //
00003 // The Hoard Multiprocessor Memory Allocator
00004 // www.hoard.org
00005 //
00006 // Author: Emery Berger, http://www.cs.utexas.edu/users/emery
00007 //
00008 // Copyright (c) 1998-2001, The University of Texas at Austin.
00009 //
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Library General Public License as
00012 // published by the Free Software Foundation, http://www.fsf.org.
00013 //
00014 // This library is distributed in the hope that it will be useful, but
00015 // WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 // Library General Public License for more details.
00018 //
00020 
00021 #ifndef _BLOCK_H_
00022 #define _BLOCK_H_
00023 
00024 #include "config.h"
00025 
00026 #include <assert.h>
00027 
00028 
00029 class superblock;
00030 
00031 class block {
00032 public:
00033 
00034   block (superblock * sb)
00035     : 
00036 #if HEAP_DEBUG
00037       _magic (FREE_BLOCK_MAGIC),
00038 #endif
00039       _next (NULL),
00040       _mySuperblock (sb)
00041     {}
00042 
00043   block& operator= (const block& b) {
00044 #if HEAP_DEBUG
00045     _magic = b._magic;
00046 #endif
00047     _next = b._next;
00048     _mySuperblock = b._mySuperblock;
00049 #if HEAP_FRAG_STATS
00050     _requestedSize = b._requestedSize;
00051 #endif
00052     return *this;
00053   }
00054 
00055   enum { ALLOCATED_BLOCK_MAGIC = 0xcafecafe,
00056          FREE_BLOCK_MAGIC = 0xbabebabe };
00057 
00058   // Mark this block as free.
00059   inline void markFree (void);
00060 
00061   // Mark this block as allocated.
00062   inline void markAllocated (void);
00063 
00064   // Is this block valid? (i.e.,
00065   // does it have the right magic number?)
00066   inline int isValid (void) const;
00067 
00068   // Return the block's superblock pointer.
00069   inline superblock * getSuperblock (void);
00070 
00071 #if HEAP_FRAG_STATS
00072   void setRequestedSize (size_t s) 
00073   {   
00074     _requestedSize = s;
00075   }
00076 
00077   size_t getRequestedSize (void) { return _requestedSize; }
00078 #endif
00079 
00080   void setNext (block * b) { _next = b; }
00081   block * getNext (void) { return _next; }
00082 
00083   void setId(int id) { _id = id; }
00084   int getId(void) { return _id; }
00085 
00086 private:
00087 
00088 #if HEAP_DEBUG
00089   union {
00090     unsigned long _magic;
00091     double _d3; // For alignment.
00092   };
00093 #endif
00094 
00095   block *       _next;          // The next block in a linked-list of blocks.
00096   superblock *  _mySuperblock;  // A pointer to my superblock.
00097   int _id; // My index identifier as known by the persistent superblock.
00098 
00099 #if HEAP_FRAG_STATS
00100   union {
00101     double _d4; // This is just for alignment purposes.
00102     size_t _requestedSize;      // The amount of space requested (vs. allocated).
00103   };
00104 #endif
00105 
00106   // Disable copying.
00107 
00108   block (const block&);
00109 };
00110 
00111 
00112 superblock * block::getSuperblock (void)
00113 {
00114 #if HEAP_DEBUG
00115   assert (isValid());
00116 #endif
00117   return _mySuperblock;
00118 }
00119 
00120 
00121 void block::markFree (void)
00122 {
00123 #if HEAP_DEBUG
00124   assert (_magic == ALLOCATED_BLOCK_MAGIC);
00125   _magic = FREE_BLOCK_MAGIC;
00126 #endif
00127 }
00128 
00129 
00130 void block::markAllocated (void)
00131 {
00132 #if HEAP_DEBUG
00133   assert (_magic == FREE_BLOCK_MAGIC);
00134   _magic = ALLOCATED_BLOCK_MAGIC;
00135 #endif
00136 }
00137 
00138 
00139 int block::isValid (void) const 
00140 {
00141 #if HEAP_DEBUG
00142   return ((_magic == FREE_BLOCK_MAGIC)
00143           || (_magic == ALLOCATED_BLOCK_MAGIC));
00144 #else
00145   return 1;
00146 #endif
00147 }
00148 
00149 #endif // _BLOCK_H_

Generated on Sat Apr 23 11:43:35 2011 for Mnemosyne by  doxygen 1.4.7