zoneHeap.hpp

Go to the documentation of this file.
00001 /* Copyright 1994, LongView Technologies L.L.C. $Revision: 1.8 $ */
00002 /* Copyright (c) 2006, Sun Microsystems, Inc.
00003 All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 
00006 following conditions are met:
00007 
00008     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
00009     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following 
00010           disclaimer in the documentation and/or other materials provided with the distribution.
00011     * Neither the name of Sun Microsystems nor the names of its contributors may be used to endorse or promote products derived 
00012           from this software without specific prior written permission.
00013 
00014 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 
00015 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
00016 THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
00017 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00018 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
00019 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
00020 
00021 
00022 */
00023 
00024 #ifdef DELTA_COMPILER
00025 
00026 // Basic heap management
00027 // maintains a map of the heap + free lists to reduce fragmentation
00028 // allocations are in multiples of block size (2**k)
00029 
00030 class HeapChunk;
00031 class ChunkKlass;
00032 class FreeList;
00033 
00034 class Heap: public CHeapObj {
00035  protected:
00036   int size;             // total size in bytes
00037  public:
00038   int blockSize;        // allocation unit in bytes (must be power of 2)
00039   int nfree;            // number of free lists
00040  protected:
00041   int log2BS;           // log2(blockSize)
00042   
00043   int bytesUsed;        // used bytes (rounded to block size)
00044   int total;            // total bytes allocated so far
00045   int ifrag;            // bytes wasted by internal fragmentation
00046   char* _base;          // for deallocation
00047   char* base;           // base addr of heap (aligned to block size)
00048   ChunkKlass* heapKlass;// map of heap (1 byte / block)
00049   FreeList* freeList;   // array of free lists for different chunk sizes
00050   FreeList* bigList;    // list of all big free blocks
00051   ChunkKlass* lastCombine;// result of last block combination
00052  public:
00053   Heap* newHeap;        // only set when growing a heap (i.e. replacing it)
00054   bool combineMode;     // do eager block combination on deallocs?
00055   
00056  public:
00057   // Constructor
00058   Heap(int s, int bs);
00059   ~Heap();
00060 
00061   // Initializes the Heap
00062   void clear();
00063   
00064   // Allocation
00065   void* allocate(int wantedBytes);      // returns NULL if allocation failed
00066   void  deallocate(void* p, int bytes);
00067 
00068   // Compaction
00069   char* compact(void move(char* from, char* to, int nbytes));   // returns first free byte  
00070 
00071   // Sizes  
00072   int capacity()  const { return size; }
00073   int usedBytes() const { return bytesUsed; }
00074   int freeBytes() const { return size - bytesUsed; }
00075 
00076   // Fragmentation
00077   double intFrag() const { return usedBytes() ? (float)ifrag / usedBytes() : 0 ; }
00078   double extFrag() const { return usedBytes() ? 1.0 - (float)usedBytes() / capacity() : 0; }
00079 
00080   // Location
00081   char* startAddr() const { return base; }
00082   char* endAddr()   const { return startAddr() + capacity(); }
00083 
00084   // Queries
00085   bool  contains(void* p) const { return (void*)base <= p && p < (void*)(base + capacity()); }
00086   void* firstUsed() const; // Address of first used object
00087   void* nextUsed(void* prev) const;
00088   void* findStartOfBlock(void* start) const;
00089   int   sizeOfBlock(void* nm) const;
00090   
00091   // Misc.
00092   void verify() const;
00093   void print() const;
00094 
00095  protected:
00096   int mapSize() const { return size >> log2BS; }
00097   char*  blockAddr(ChunkKlass* m) const {
00098     u_char* fm = (u_char*)heapKlass;
00099     u_char* bm = (u_char*)m;
00100     assert(bm >= fm && bm < fm + mapSize(), "not a heapKlass entry");
00101     return base + ((bm - fm) << log2BS);
00102   }
00103   ChunkKlass* mapAddr(void* p) const {
00104     char* pp = (char*)p;
00105     assert(pp >= base && pp < base + size, "not in this heap");
00106     assert(int(pp) % blockSize == 0, "must be block-aligned");
00107     u_char* fm = (u_char*)heapKlass;
00108     return (ChunkKlass*)(fm + ((pp - base) >> log2BS));
00109   }
00110 
00111   ChunkKlass* heapEnd() const { return (ChunkKlass*)((u_char*)heapKlass + mapSize()); }
00112 
00113   // Free list management
00114   void* allocFromLists(int wantedBytes);
00115   bool  addToFreeList(ChunkKlass* m);
00116   void  removeFromFreeList(ChunkKlass* m);
00117 
00118   int combineAll();
00119   int combine(HeapChunk*& m);
00120 };
00121 
00122 #endif

Generated on Mon Oct 9 13:37:31 2006 for Strongtalk VM by  doxygen 1.4.7