usermode/library/malloc-original/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 const 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 private:
00084 
00085 #if HEAP_DEBUG
00086   union {
00087     unsigned long _magic;
00088     double _d3; // For alignment.
00089   };
00090 #endif
00091 
00092   block *       _next;          // The next block in a linked-list of blocks.
00093   superblock *  _mySuperblock;  // A pointer to my superblock.
00094 
00095 #if HEAP_FRAG_STATS
00096   union {
00097     double _d4; // This is just for alignment purposes.
00098     size_t _requestedSize;      // The amount of space requested (vs. allocated).
00099   };
00100 #endif
00101 
00102   // Disable copying.
00103 
00104   block (const block&);
00105 };
00106 
00107 
00108 superblock * block::getSuperblock (void)
00109 {
00110 #if HEAP_DEBUG
00111   assert (isValid());
00112 #endif
00113   return _mySuperblock;
00114 }
00115 
00116 
00117 void block::markFree (void)
00118 {
00119 #if HEAP_DEBUG
00120   assert (_magic == ALLOCATED_BLOCK_MAGIC);
00121   _magic = FREE_BLOCK_MAGIC;
00122 #endif
00123 }
00124 
00125 
00126 void block::markAllocated (void)
00127 {
00128 #if HEAP_DEBUG
00129   assert (_magic == FREE_BLOCK_MAGIC);
00130   _magic = ALLOCATED_BLOCK_MAGIC;
00131 #endif
00132 }
00133 
00134 
00135 const int block::isValid (void) const 
00136 {
00137 #if HEAP_DEBUG
00138   return ((_magic == FREE_BLOCK_MAGIC)
00139           || (_magic == ALLOCATED_BLOCK_MAGIC));
00140 #else
00141   return 1;
00142 #endif
00143 }
00144 
00145 #endif // _BLOCK_H_

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