00001 #ifndef SRC_INC_MEMORY_MANAGER_HPP
00002 #define SRC_INC_MEMORY_MANAGER_HPP
00003
00004 #include <fixed_size_allocator.hpp>
00005 #include <intrusive_list.hpp>
00006 #include <string>
00007 #include <vector>
00008
00009 namespace GCpp
00010 {
00011
00015 typedef void (*DestroyItem) (void*);
00016
00022 class GCPP_PUBLIC_SYMBOL MemoryAllocatorBase : protected FixedSizeAllocator
00023 {
00024 public:
00027 MemoryAllocatorBase* next;
00028
00031 MemoryAllocatorBase* prior;
00032
00033 private:
00036 Byte f_status;
00037
00040 size_t f_destroyed;
00041
00044 size_t f_allocated;
00045
00048 void* f_blocks;
00049
00052 std::vector<size_t> f_offsets;
00053
00056 void analyze (void* p_block);
00057
00060 void mark_contained_pointers (void* p_block);
00061
00064 void sweep ();
00065
00068 void unmark ();
00069
00072 void compact ()
00073 {
00074 FixedSizeAllocator::compact ();
00075 }
00076
00077 protected:
00080 DestroyItem f_destroy_item;
00081
00082 protected:
00085 MemoryAllocatorBase (size_t f_block_size);
00086
00087 public:
00090 ~MemoryAllocatorBase ();
00091
00094 size_t destroyed ()
00095 {
00096 return f_destroyed;
00097 }
00098
00101 size_t allocated ()
00102 {
00103 return f_allocated;
00104 }
00105
00108 void* malloc ();
00109
00116 void* confirm (void* p_storage);
00117
00118 friend class GC;
00119 };
00120
00123 template <class T>
00124 void destroy_item (void* p_item)
00125 {
00126 static_cast<T*> (p_item)->~T ();
00127 }
00128
00131 template <class T>
00132 class MemoryAllocator : public MemoryAllocatorBase
00133 {
00134 public:
00135 MemoryAllocator ()
00136 : MemoryAllocatorBase (sizeof (T))
00137 {
00138 f_destroy_item = destroy_item<T>;
00139 }
00140 };
00141
00144 class GC
00145 {
00146 private:
00149 static bool f_stop;
00150
00153 static size_t f_allocated;
00154
00157 static size_t f_threshold;
00158
00161 static IntrusiveList<MemoryAllocatorBase> f_allocators;
00162
00165 static void mark_recursively (void* p_pointer);
00166
00169 static void mark (void* p_pointer);
00170
00173 static void internal_collect ();
00174
00175 public:
00178 static void start ();
00179
00182 static void stop ();
00183
00189 static void collect ();
00190
00193 static bool stopped ();
00194
00197 static size_t destroyed ();
00198
00201 static size_t allocated ();
00202
00205 static std::string version ();
00206
00209 static size_t in_use ();
00210
00213 static size_t threshold ();
00214
00217 static void compact (bool p_new_limit=false);
00218
00225 static void finalize ();
00226
00227 friend class MemoryAllocatorBase;
00228 friend class GCBasePointer;
00229 friend Byte* OS_malloc (size_t p_size);
00230 friend void OS_free (Byte* p_block);
00231 };
00232
00233 }
00234
00235 #endif