00001 /* 00002 FALCON - The Falcon Programming Language. 00003 FILE: mempool.h 00004 00005 garbage basket class 00006 ------------------------------------------------------------------- 00007 Author: Giancarlo Niccolai 00008 Begin: Sun, 08 Feb 2009 16:08:50 +0100 00009 00010 ------------------------------------------------------------------- 00011 (C) Copyright 2009: the FALCON developers (see list in AUTHORS file) 00012 00013 See LICENSE file for licensing details. 00014 */ 00015 00016 #ifndef FALCON_MEMPOOL_H 00017 #define FALCON_MEMPOOL_H 00018 00023 #include <falcon/setup.h> 00024 #include <falcon/item.h> 00025 #include <falcon/basealloc.h> 00026 #include <falcon/mt.h> 00027 #include <falcon/rampmode.h> 00028 00029 namespace Falcon { 00030 00031 class Garbageable; 00032 class GarbageableBase; 00033 class GarbageLock; 00034 00038 #if 0 00039 class FALCON_DYN_CLASS PoolRing: public BaseAlloc 00040 { 00041 Garbageable* m_head; 00042 00043 public: 00044 PoolRing(); 00045 ~PoolRing(); 00046 00050 void add( Garbageable * ); 00051 void transfer( PoolRing *target ); 00052 Garbageable *linearize(); 00053 }; 00054 #endif 00055 00069 class FALCON_DYN_CLASS MemPool: public Runnable, public BaseAlloc 00070 { 00071 00072 protected: 00073 size_t m_thresholdNormal; 00074 size_t m_thresholdActive; 00075 00079 uint32 m_mingen; 00080 00082 GarbageableBase *m_garbageRoot; 00083 00085 GarbageableBase *m_newRoot; 00086 00088 bool m_bNewReady; 00089 00091 VMachine *m_olderVM; 00092 00094 VMachine *m_vmRing; 00095 00096 int32 m_vmCount; 00097 00099 VMachine *m_vmIdle_head; 00100 VMachine *m_vmIdle_tail; 00101 00102 // for gc 00103 uint32 m_generation; 00104 int32 m_allocatedItems; 00105 uint32 m_allocatedMem; 00106 00107 SysThread *m_th; 00108 bool m_bLive; 00109 00110 Event m_eRequest; 00111 Mutex m_mtxa; 00112 00113 00122 mutable Mutex m_mtx_newitem; 00123 00131 Mutex m_mtx_vms; 00132 00141 Mutex m_mtx_idlevm; 00142 00144 mutable Mutex m_mtx_ramp; 00145 00146 mutable Mutex m_mtx_gen; 00147 00148 RampMode* m_ramp[RAMP_MODE_COUNT]; 00149 RampMode* m_curRampMode; 00150 int m_curRampID; 00151 00152 Mutex m_mtxRequest; 00153 Event m_eGCPerformed; 00154 bool m_bRequestSweep; 00155 00157 Mutex m_mtx_lockitem; 00158 00160 GarbageLock *m_lockRoot; 00161 00165 uint32 m_lockGen; 00166 00167 //================================================== 00168 // Private functions 00169 //================================================== 00170 00171 bool markVM( VMachine *vm ); 00172 void gcSweep(); 00173 00174 /* 00175 To reimplement this, we need to have anti-recursion checks on item, which are 00176 currently being under consideration. However, I would prefer not to need to 00177 have this functions back, as they were meant to be used when the memory 00178 model wasn't complete. 00179 00180 In other words, I want items to be in garbage as soon as they are created, 00181 and to exit when they are destroyed. 00182 00183 void removeFromGarbage( String *ptr ); 00184 void removeFromGarbage( Garbageable *ptr ); 00185 00186 void storeForGarbageDeep( const Item &item ); 00187 void removeFromGarbageDeep( const Item &item ); 00188 */ 00189 00190 void clearRing( GarbageableBase *ringRoot ); 00191 void rollover(); 00192 void remark(uint32 mark); 00193 void electOlderVM(); // to be called with m_mtx_vms locked 00194 00195 void promote( uint32 oldgen, uint32 curgen ); 00196 void advanceGeneration( VMachine* vm, uint32 oldGeneration ); 00197 void markLocked(); 00198 00199 friend class GarbageLock; 00200 void addGarbageLock( GarbageLock* lock ); 00201 void removeGarbageLock( GarbageLock* lock ); 00202 00203 public: 00204 enum constants { 00205 MAX_GENERATION = 0xFFFFFFFE, 00206 SWEEP_GENERATION = 0xFFFFFFFF 00207 }; 00208 00212 MemPool(); 00213 00217 virtual ~MemPool(); 00218 00223 void registerVM( VMachine *vm ); 00224 00228 void unregisterVM( VMachine *vm ); 00229 00234 void markItem( const Item &itm ); 00235 00237 int32 allocatedItems() const; 00238 00240 uint32 generation() const { return m_generation; } 00241 /* 00242 void generation( uint32 i ); 00243 uint32 incgen(); 00244 */ 00248 void storeForGarbage( Garbageable *ptr ); 00249 00250 virtual void* run(); 00251 00253 void start(); 00254 00258 void stop(); 00259 00281 void safeArea(); 00282 00286 void unsafeArea(); 00287 00298 void idleVM( VMachine *vm, bool bPrio = false ); 00299 00301 void thresholdNormal( size_t mem ) { m_thresholdNormal = mem; } 00302 00304 void thresholdActive( size_t mem ) { m_thresholdActive = mem; } 00305 00306 size_t thresholdNormal() const { return m_thresholdNormal; } 00307 00308 size_t thresholdActive() const { return m_thresholdActive; } 00309 00321 bool rampMode( int mode ); 00322 int rampMode() const; 00323 00327 void accountItems( int itemCount ); 00328 00329 void performGC(); 00330 }; 00331 00332 00333 } 00334 00335 #endif 00336 /* end of mempool.h */