bbgc.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #ifndef BB_GC_H
  2. #define BB_GC_H
  3. #include "bbstd.h"
  4. #include "bbtypes.h"
  5. #include "bbmemory.h"
  6. //how much to allocate before a sweep occurs
  7. //#define BBGC_TRIGGER 0
  8. //#define BBGC_TRIGGER 64
  9. //#define BBGC_TRIGGER 256
  10. //#define BBGC_TRIGGER 65536
  11. //#define BBGC_TRIGGER 1*1024*1024
  12. #define BBGC_TRIGGER 4*1024*1024
  13. //#define BBGC_TRIGGER 16*1024*1024
  14. //mark while allocating, slower but smoother...
  15. #define BBGC_INCREMENTAL 1
  16. //reclaim all memory after a sweep, lumpier...
  17. //#define BBGC_AGGRESSIVE 1
  18. //check for use of deleted objects, MUCH leakier...
  19. //#define BBGC_DEBUG 1
  20. #if BBGC_DEBUG
  21. #define BBGC_VALIDATE( P ) \
  22. if( (P) && (P)->flags==3 ){ \
  23. printf( "Attempt to use deleted object %p of type '%s'\n",(P),(P)->typeName() ); \
  24. fflush( stdout ); \
  25. abort(); \
  26. }
  27. #else
  28. #define BBGC_VALIDATE( P )
  29. #endif
  30. struct bbGCNode;
  31. struct bbGCFiber;
  32. struct bbGCFrame;
  33. struct bbGCRoot;
  34. namespace bbGC{
  35. extern bbGCRoot *roots;
  36. extern bbGCNode *markQueue;
  37. extern bbGCNode *unmarkedList;
  38. extern bbGCFiber *fibers;
  39. extern bbGCFiber *currentFiber;
  40. extern int markedBit;
  41. extern int unmarkedBit;
  42. void init();
  43. void collect();
  44. bbGCNode *alloc( size_t size );
  45. }
  46. struct bbGCNode{
  47. bbGCNode *succ;
  48. bbGCNode *pred;
  49. size_t flags; //0=lonely, 1/2=marked/unmarked; 3=destroyed
  50. bbGCNode(){
  51. }
  52. virtual ~bbGCNode(){
  53. }
  54. virtual void gcMark(){
  55. }
  56. virtual void dbEmit(){
  57. }
  58. virtual const char *typeName()const{
  59. return "bbGCNode";
  60. }
  61. };
  62. struct bbGCFiber{
  63. bbGCFiber *succ;
  64. bbGCFiber *pred;
  65. bbGCFrame *frames;
  66. bbGCNode *ctoring;
  67. bbGCFiber():succ( this ),pred( this ),frames( nullptr ),ctoring( nullptr ){
  68. }
  69. void link(){
  70. succ=bbGC::fibers;
  71. pred=bbGC::fibers->pred;
  72. bbGC::fibers->pred=this;
  73. pred->succ=this;
  74. }
  75. void unlink(){
  76. pred->succ=succ;
  77. succ->pred=pred;
  78. }
  79. };
  80. struct bbGCFrame{
  81. bbGCFrame *succ;
  82. bbGCFrame():succ( bbGC::currentFiber->frames ){
  83. bbGC::currentFiber->frames=this;
  84. }
  85. ~bbGCFrame(){
  86. bbGC::currentFiber->frames=succ;
  87. }
  88. virtual void gcMark(){
  89. }
  90. };
  91. struct bbGCRoot{
  92. bbGCRoot *succ;
  93. bbGCRoot():succ( bbGC::roots ){
  94. bbGC::roots=this;
  95. }
  96. virtual void gcMark(){
  97. }
  98. };
  99. namespace bbGC{
  100. inline void insert( bbGCNode *p,bbGCNode *succ ){
  101. p->succ=succ;
  102. p->pred=succ->pred;
  103. p->pred->succ=p;
  104. succ->pred=p;
  105. }
  106. inline void remove( bbGCNode *p ){
  107. p->pred->succ=p->succ;
  108. p->succ->pred=p->pred;
  109. }
  110. inline void enqueue( bbGCNode *p ){
  111. BBGC_VALIDATE( p )
  112. if( !p || p->flags!=unmarkedBit ) return;
  113. remove( p );
  114. p->succ=markQueue;
  115. markQueue=p;
  116. p->flags=markedBit;
  117. }
  118. inline void beginCtor( bbGCNode *p ){
  119. p->succ=currentFiber->ctoring;
  120. currentFiber->ctoring=p;
  121. }
  122. inline void endCtor( bbGCNode *p ){
  123. currentFiber->ctoring=p->succ;
  124. #if BBGC_INCREMENTAL
  125. p->succ=markQueue;
  126. markQueue=p;
  127. p->flags=markedBit;
  128. #else
  129. p->flags=unmarkedBit;
  130. insert( p,unmarkedList );
  131. #endif
  132. }
  133. }
  134. template<class T> struct bbGCVar{
  135. public:
  136. T *_ptr;
  137. void enqueue(){
  138. #if BBGC_INCREMENTAL
  139. bbGC::enqueue( dynamic_cast<bbGCNode*>( _ptr ) );
  140. #endif
  141. }
  142. bbGCVar():_ptr( nullptr ){
  143. }
  144. bbGCVar( T *p ):_ptr( p ){
  145. enqueue();
  146. }
  147. bbGCVar( const bbGCVar &p ):_ptr( p._ptr ){
  148. enqueue();
  149. }
  150. bbGCVar &operator=( T *p ){
  151. _ptr=p;
  152. enqueue();
  153. return *this;
  154. }
  155. bbGCVar &operator=( const bbGCVar &p ){
  156. _ptr=p._ptr;
  157. enqueue();
  158. return *this;
  159. }
  160. T *get()const{
  161. return _ptr;
  162. }
  163. T *operator->()const{
  164. return _ptr;
  165. }
  166. operator T*()const{
  167. return _ptr;
  168. }
  169. };
  170. template<class T> struct bbGCRootVar : public bbGCVar<T>,public bbGCRoot{
  171. using bbGCVar<T>::_ptr;
  172. using bbGCVar<T>::enqueue;
  173. bbGCRootVar(){
  174. }
  175. bbGCRootVar( T *p ){
  176. _ptr=p;
  177. enqueue();
  178. }
  179. bbGCRootVar( const bbGCVar<T> &p ){
  180. _ptr=p._ptr;
  181. enqueue();
  182. }
  183. bbGCRootVar &operator=( T *p ){
  184. _ptr=p;
  185. enqueue();
  186. return *this;
  187. }
  188. bbGCRootVar &operator=( const bbGCVar<T> &p ){
  189. _ptr=p._ptr;
  190. enqueue();
  191. return *this;
  192. }
  193. virtual void gcMark(){
  194. bbGC::enqueue( dynamic_cast<bbGCNode*>( _ptr ) );
  195. }
  196. };
  197. template<class T> void bbGCMark( const T &t ){}
  198. template<class T> void bbGCMark( const bbGCVar<T> &v ){
  199. bbGC::enqueue( dynamic_cast<bbGCNode*>( v._ptr ) );
  200. }
  201. template<class T> void bbGCMarkPtr( T *p ){
  202. bbGC::enqueue( dynamic_cast<bbGCNode*>( p ) );
  203. }
  204. #endif