00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifdef DELTA_COMPILER
00025
00026
00027
00028
00029
00030 class HeapChunk;
00031 class ChunkKlass;
00032 class FreeList;
00033
00034 class Heap: public CHeapObj {
00035 protected:
00036 int size;
00037 public:
00038 int blockSize;
00039 int nfree;
00040 protected:
00041 int log2BS;
00042
00043 int bytesUsed;
00044 int total;
00045 int ifrag;
00046 char* _base;
00047 char* base;
00048 ChunkKlass* heapKlass;
00049 FreeList* freeList;
00050 FreeList* bigList;
00051 ChunkKlass* lastCombine;
00052 public:
00053 Heap* newHeap;
00054 bool combineMode;
00055
00056 public:
00057
00058 Heap(int s, int bs);
00059 ~Heap();
00060
00061
00062 void clear();
00063
00064
00065 void* allocate(int wantedBytes);
00066 void deallocate(void* p, int bytes);
00067
00068
00069 char* compact(void move(char* from, char* to, int nbytes));
00070
00071
00072 int capacity() const { return size; }
00073 int usedBytes() const { return bytesUsed; }
00074 int freeBytes() const { return size - bytesUsed; }
00075
00076
00077 double intFrag() const { return usedBytes() ? (float)ifrag / usedBytes() : 0 ; }
00078 double extFrag() const { return usedBytes() ? 1.0 - (float)usedBytes() / capacity() : 0; }
00079
00080
00081 char* startAddr() const { return base; }
00082 char* endAddr() const { return startAddr() + capacity(); }
00083
00084
00085 bool contains(void* p) const { return (void*)base <= p && p < (void*)(base + capacity()); }
00086 void* firstUsed() const;
00087 void* nextUsed(void* prev) const;
00088 void* findStartOfBlock(void* start) const;
00089 int sizeOfBlock(void* nm) const;
00090
00091
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
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