BlitzMax Extended  0.8.11
Pushing Blitz to the Max.
blitz_gc.h
Go to the documentation of this file.
00001 
00023 #ifndef BLITZ_GC_H
00024 #define BLITZ_GC_H
00025 
00026 #include "_common.h"
00027 
00028 BB_BEGIN_DECLS
00029 
00030 #ifdef BB_THREADED
00031 #   include "blitz_thread.h"
00032 #   ifndef BB_GC_MS
00033 #       define BB_GC_MS
00034 #   endif
00035 #else
00036 #   ifndef BB_GC_RC
00037 #       define BB_GC_RC
00038 #   endif
00039 #endif
00040 
00041 #define BBGC_MANYREFS           0x40000000
00042 
00043 // Probably shouldn't be here...
00044 #if __ppc__
00045 #   define BBGC_NUM_ROOTREGS    19
00046 #else
00047 #   define BBGC_NUM_ROOTREGS    4
00048 #endif  // __ppc__
00049 
00050 void* bbGCRootRegs(void *ptr);
00051 void bbGCStartup();
00052 
00057 enum BBGCMode
00058 {
00065     BBGC_AUTOMATIC  = 1,
00066     
00074     BBGC_MANUAL     = 2,
00075     BBGC_AGGRESSIVE = -1
00076 };
00077 
00078 typedef enum BBGCMode BBGCMode;
00079 
00080 #define BBGC_MODE_VALID(mode) \
00081     ((mode) == BBGC_AUTOMATIC || (mode) == BBGC_MANUAL || (mode) == BBGC_AGGRESSIVE)
00082 
00089 BBGCMode bbGCGetMode();
00090 
00097 void bbGCSetMode(BBGCMode mode);
00098 
00102 enum BBGCDebugMode
00103 {
00104     BBGC_NODEBUG,
00105     BBGC_STDOUTDEBUG
00106 };
00107 
00108 typedef enum BBGCDebugMode BBGCDebugMode;
00109 
00116 void bbGCSetDebug(BBGCDebugMode mode);
00117 
00123 enum BBGCAllocFlags
00124 {
00128     BBGC_DEFAULT,
00129     
00133     BBGC_ATOMIC,
00134     
00138     BBGC_FINALIZE
00139 };
00140 
00147 void* bbGCMalloc(BBInt size, BBInt flags);
00148 
00157 BBObject* bbGCAllocObject(BBSize size, const BBClass *clas, BBInt flags);
00158 
00164 BBBool bbGCValidate(const void *ptr);
00165 
00170 BBInt bbGCMemAlloced();
00171 
00180 BBInt bbGCCollect();
00181 
00187 #define BBGC_MAX_SUSPENDED          (BBINT_MAX - 1)
00188 
00204 void bbGCSuspend();
00205 
00213 BBBool bbGCIsSuspended();
00214 
00224 void bbGCResume();
00225 
00229 void bbGCRetain(BBObject *obj);
00230 
00234 void bbGCRelease(BBObject *obj);
00235 
00236 // BB_RETAIN/BB_RELEASE should be used to prevent an object from garbage collection.
00237 //
00238 // This is mainly of use if an object is being stored outside of BlitzMax's 'sight' - say, in a C++ table.
00239 // You can also use bbGCRetain/bbGCRelease functions above if necessary - MACROS are just faster.
00240 
00241 #ifdef BB_GC_RC
00242 #   define BB_RETAIN(obj)   { ++(obj)->refs; }
00243 #   define BB_RELEASE(obj)  { if (!--(obj)->refs) { bbGCFree(obj); } }
00244 #else
00245 #   define BB_RETAIN(obj)   bbGCRetain(((BBObject *)(obj)));
00246 #   define BB_RELEASE(obj)  bbGCRelease(((BBObject *)(obj)));
00247 #endif
00248 
00249 //  Internal use only
00250 #ifdef BB_GC_RC
00251 
00252     /*
00253      * Called when there are no more references (obj->refs == 0)
00254      *
00255      * \param   obj The object to free
00256      */
00257     void    bbGCFree(BBObject *obj);
00258 
00259     /*
00260      * Called after the destructor to send the memory to nirvana.
00261      *
00262      * \param   obj The object to deallocate
00263      * \param   size The size of the object instance
00264      */
00265     void    bbGCDeallocObject(BBObject *obj, BBSize size);
00266 
00267 #   define  BB_INCREFS(obj) { ++(obj)->refs; }
00268 #   define  BB_DECREFS(obj) { if (!--(obj)->refs) { bbGCFree(obj); } }
00269 #else
00270 #   define  BB_INCREFS(obj) {}
00271 #   define  BB_DECREFS(obj) {}
00272 #endif
00273 
00274 #ifndef BB_DISABLE_DEPRECATED
00275 
00276 #   define BBRETAIN     BB_RETAIN
00277 #   define BBRELEASE    BB_RELEASE
00278 #   define BBINCREFS    BB_INCREFS
00279 #   define BBDECREFS    BB_DECREFS
00280 
00281 #endif
00282 
00283 BB_END_DECLS
00284 
00285 #endif  // BLITZ_GC_H