00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 # include <string.h>
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 class CHeapObj {
00057 public:
00058 void* operator new(size_t size);
00059 void operator delete(void* p);
00060 void* new_array(size_t size);
00061 };
00062
00063
00064
00065 class PrintableCHeapObj : public CHeapObj {
00066 public:
00067 virtual void print() = 0;
00068 virtual void print_short();
00069 };
00070
00071
00072
00073 class StackObj {
00074 public:
00075 void* operator new(size_t size);
00076 void operator delete(void* p);
00077 };
00078
00079
00080
00081 class PrintableStackObj : StackObj {
00082 public:
00083 virtual void print() = 0;
00084 virtual void print_short();
00085 };
00086
00087
00088
00089 class ValueObj {
00090 public:
00091 void* operator new(size_t size);
00092 void operator delete(void* p);
00093 };
00094
00095
00096 class AllStatic {
00097 public:
00098 void* operator new(size_t size);
00099 void operator delete(void* p);
00100 };
00101
00102
00103
00104
00105 # define NEW_RESOURCE_ARRAY( type, size )\
00106 (type*) allocateResource( (size) * sizeof(type))
00107
00108 # define NEW_C_HEAP_ARRAY( type, size )\
00109 (type*) malloc( (size) * sizeof(type)); //XSTR(type) " in " __FILE__)
00110
00111 # define NEW_RESOURCE_OBJ( type ) NEW_RESOURCE_ARRAY( type, 1 )
00112 # define NEW_C_HEAP_OBJ( type ) NEW_C_HEAP_ARRAY( type, 1 )
00113
00114
00115
00116
00117
00118
00119 const int min_resource_chunk_size = 256 * K;
00120 const int min_resource_free_size = 32 * K;
00121
00122 char* AllocatePageAligned(int size, char* name);
00123 char* AllocateHeap(int size, char* name);
00124 void FreeHeap(void* p);
00125
00126 extern "C" bool PrintResourceAllocation;
00127
00128 class ResourceAreaChunk: public PrintableCHeapObj {
00129 friend class ResourceMark;
00130 friend class ResourceArea;
00131 friend class Resources;
00132 char* bottom;
00133 char* top;
00134 char* first_free;
00135 ResourceAreaChunk* prev;
00136
00137 int _allocated;
00138 int _previous_used;
00139
00140 void clear(char *start, char *end) { memset(start, 33, end - start); }
00141 void clear() { clear(bottom, first_free); }
00142 void freeTo(char *new_first_free);
00143
00144 public:
00145 char* allocate_bytes(int size) {
00146 char* p = first_free;
00147 if (first_free + size <= top) {
00148 #ifndef PRODUCT
00149 if (PrintResourceAllocation) print_alloc(p, size);
00150 #endif
00151 first_free += size;
00152 return p;
00153 } else return NULL;
00154 }
00155
00156 ResourceAreaChunk(int min_capacity, ResourceAreaChunk* previous);
00157 ~ResourceAreaChunk();
00158
00159 void initialize(ResourceAreaChunk* previous);
00160
00161 int capacity() { return top - bottom; }
00162 int used() { return first_free - bottom; }
00163
00164 bool contains(void* p) {
00165 if (p >= (void*) bottom && p < (void*) top) return true;
00166 else if (prev) return prev->contains(p);
00167 else return false; }
00168
00169 void print();
00170 void print_short();
00171 protected:
00172 void print_alloc(char* addr, int size);
00173 };
00174
00175 class ResourceArea {
00176 public:
00177 ResourceAreaChunk* chunk;
00178 # ifdef ASSERT
00179 int nesting;
00180
00181 # endif
00182
00183 ResourceArea();
00184 ~ResourceArea();
00185
00186 char* allocate_more_bytes(int size);
00187 char* allocate_bytes(int size) {
00188 assert(size >= 0, "negative size in allocate_bytes");
00189 #ifdef ASSERT
00190
00191
00192
00193
00194
00195 static int warned = 0;
00196 if (nesting < 1 && !warned++) error("memory leak: allocating w/o ResourceMark!");
00197 #endif
00198 if (size == 0) {
00199
00200
00201 return (char*) 1;
00202 }
00203 size = roundTo(size, oopSize);
00204 char* p;
00205 if (chunk && (p = chunk->allocate_bytes(size))) return p;
00206 return allocate_more_bytes(size);
00207 }
00208
00209 int capacity() { return chunk ? chunk->_allocated : 0; }
00210
00211 int used();
00212 bool contains(void* p) { return chunk != NULL && chunk->contains(p); }
00213 };
00214
00215
00216
00217
00218 class ResourceMark: StackObj {
00219 protected:
00220 static bool enabled;
00221 ResourceArea* area;
00222 ResourceAreaChunk* chunk;
00223 char* top;
00224 public:
00225 ResourceMark();
00226 ~ResourceMark();
00227 };
00228
00229 class FinalResourceMark: public ResourceMark {
00230 public:
00231 FinalResourceMark();
00232 ~FinalResourceMark();
00233 };
00234
00235
00236
00237
00238 class NoGCVerifier: StackObj {
00239 private:
00240 int old_scavenge_count;
00241 public:
00242 NoGCVerifier();
00243 ~NoGCVerifier();
00244 };
00245
00246
00247 class Resources {
00248 private:
00249 ResourceAreaChunk* freeChunks;
00250 int _allocated;
00251 bool _in_consistent_state;
00252 ResourceAreaChunk* getFromFreeList(int min_capacity);
00253 public:
00254 Resources();
00255 ResourceAreaChunk* new_chunk(int min_capacity, ResourceAreaChunk* area);
00256
00257 void addToFreeList(ResourceAreaChunk* c);
00258 bool in_consistent_state() { return _in_consistent_state; }
00259
00260 bool contains(char* p);
00261 int capacity();
00262 int used();
00263 };
00264
00265 extern Resources resources;
00266 extern ResourceArea resource_area;
00267
00268 inline char* allocateResource(int size) {
00269 return resource_area.allocate_bytes(size);
00270 }
00271
00272
00273
00274
00275 class ResourceObj {
00276 public:
00277 void* operator new(size_t size, bool on_C_heap = false) {
00278 return on_C_heap ? (char*) malloc(size) : allocateResource(size);
00279 }
00280
00281 void operator delete(void* p) {}
00282 };
00283
00284
00285
00286 class PrintableResourceObj : public ResourceObj {
00287 public:
00288 virtual void print() = 0;
00289 virtual void print_short();
00290 };
00291