as_gc.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2011 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. [email protected]
  22. */
  23. //
  24. // as_gc.cpp
  25. //
  26. // The implementation of the garbage collector
  27. //
  28. #include <stdlib.h>
  29. #include "as_gc.h"
  30. #include "as_scriptengine.h"
  31. #include "as_scriptobject.h"
  32. BEGIN_AS_NAMESPACE
  33. asCGarbageCollector::asCGarbageCollector()
  34. {
  35. engine = 0;
  36. detectState = clearCounters_init;
  37. destroyNewState = destroyGarbage_init;
  38. destroyOldState = destroyGarbage_init;
  39. numDestroyed = 0;
  40. numNewDestroyed = 0;
  41. numDetected = 0;
  42. }
  43. void asCGarbageCollector::AddScriptObjectToGC(void *obj, asCObjectType *objType)
  44. {
  45. engine->CallObjectMethod(obj, objType->beh.addref);
  46. asSObjTypePair ot = {obj, objType, 0};
  47. // Invoke the garbage collector to destroy a little garbage as new comes in
  48. // This will maintain the number of objects in the GC at a maintainable level without
  49. // halting the application, and without burdening the application with manually invoking the
  50. // garbage collector.
  51. if( engine->ep.autoGarbageCollect && gcNewObjects.GetLength() )
  52. {
  53. // If the GC is already processing in another thread, then don't try this again
  54. // TODO: What if it is already processing in this thread?
  55. if( TRYENTERCRITICALSECTION(gcCollecting) )
  56. {
  57. // TODO: The number of iterations should be dynamic, and increase
  58. // if the number of objects in the garbage collector grows high
  59. // Run one step of DetectGarbage
  60. if( gcOldObjects.GetLength() )
  61. {
  62. IdentifyGarbageWithCyclicRefs();
  63. DestroyOldGarbage();
  64. }
  65. // Run a few steps of DestroyGarbage
  66. int iter = (int)gcNewObjects.GetLength();
  67. if( iter > 10 ) iter = 10;
  68. while( iter-- > 0 )
  69. DestroyNewGarbage();
  70. LEAVECRITICALSECTION(gcCollecting);
  71. }
  72. }
  73. // Add the data to the gcObjects array in a critical section as
  74. // another thread might be calling this method at the same time
  75. ENTERCRITICALSECTION(gcCritical);
  76. gcNewObjects.PushLast(ot);
  77. LEAVECRITICALSECTION(gcCritical);
  78. }
  79. int asCGarbageCollector::GarbageCollect(asDWORD flags)
  80. {
  81. // If the GC is already processing in another thread, then don't enter here again
  82. // TODO: What if it is already processing in this thread?
  83. if( TRYENTERCRITICALSECTION(gcCollecting) )
  84. {
  85. bool doDetect = (flags & asGC_DETECT_GARBAGE) || !(flags & asGC_DESTROY_GARBAGE);
  86. bool doDestroy = (flags & asGC_DESTROY_GARBAGE) || !(flags & asGC_DETECT_GARBAGE);
  87. if( flags & asGC_FULL_CYCLE )
  88. {
  89. // Reset the state
  90. if( doDetect )
  91. {
  92. // Move all objects to the old list, so we guarantee that all is detected
  93. for( asUINT n = (asUINT)gcNewObjects.GetLength(); n-- > 0; )
  94. MoveObjectToOldList(n);
  95. detectState = clearCounters_init;
  96. }
  97. if( doDestroy )
  98. {
  99. destroyNewState = destroyGarbage_init;
  100. destroyOldState = destroyGarbage_init;
  101. }
  102. int r = 1;
  103. unsigned int count = (unsigned int)(gcNewObjects.GetLength() + gcOldObjects.GetLength());
  104. for(;;)
  105. {
  106. // Detect all garbage with cyclic references
  107. if( doDetect )
  108. while( (r = IdentifyGarbageWithCyclicRefs()) == 1 );
  109. // Now destroy all known garbage
  110. if( doDestroy )
  111. {
  112. while( (r = DestroyNewGarbage()) == 1 );
  113. while( (r = DestroyOldGarbage()) == 1 );
  114. }
  115. // Run another iteration if any garbage was destroyed
  116. if( count != (unsigned int)(gcNewObjects.GetLength() + gcOldObjects.GetLength()) )
  117. count = (unsigned int)(gcNewObjects.GetLength() + gcOldObjects.GetLength());
  118. else
  119. break;
  120. }
  121. // Take the opportunity to clear unused types as well
  122. engine->ClearUnusedTypes();
  123. LEAVECRITICALSECTION(gcCollecting);
  124. return 0;
  125. }
  126. else
  127. {
  128. // Destroy the garbage that we know of
  129. if( doDestroy )
  130. {
  131. DestroyNewGarbage();
  132. DestroyOldGarbage();
  133. }
  134. // Run another incremental step of the identification of cyclic references
  135. if( doDetect )
  136. IdentifyGarbageWithCyclicRefs();
  137. }
  138. LEAVECRITICALSECTION(gcCollecting);
  139. }
  140. // Return 1 to indicate that the cycle wasn't finished
  141. return 1;
  142. }
  143. void asCGarbageCollector::GetStatistics(asUINT *currentSize, asUINT *totalDestroyed, asUINT *totalDetected, asUINT *newObjects, asUINT *totalNewDestroyed) const
  144. {
  145. // It's not necessary to protect this access, as
  146. // it doesn't matter if another thread is currently
  147. // appending a new object.
  148. if( currentSize )
  149. *currentSize = (asUINT)(gcNewObjects.GetLength() + gcOldObjects.GetLength());
  150. if( totalDestroyed )
  151. *totalDestroyed = numDestroyed;
  152. if( totalDetected )
  153. *totalDetected = numDetected;
  154. if( newObjects )
  155. *newObjects = (asUINT)gcNewObjects.GetLength();
  156. if( totalNewDestroyed )
  157. *totalNewDestroyed = numNewDestroyed;
  158. }
  159. asCGarbageCollector::asSObjTypePair asCGarbageCollector::GetNewObjectAtIdx(int idx)
  160. {
  161. // We need to protect this access with a critical section as
  162. // another thread might be appending an object at the same time
  163. ENTERCRITICALSECTION(gcCritical);
  164. asSObjTypePair gcObj = gcNewObjects[idx];
  165. LEAVECRITICALSECTION(gcCritical);
  166. return gcObj;
  167. }
  168. asCGarbageCollector::asSObjTypePair asCGarbageCollector::GetOldObjectAtIdx(int idx)
  169. {
  170. // We need to protect this access with a critical section as
  171. // another thread might be appending an object at the same time
  172. ENTERCRITICALSECTION(gcCritical);
  173. asSObjTypePair gcObj = gcOldObjects[idx];
  174. LEAVECRITICALSECTION(gcCritical);
  175. return gcObj;
  176. }
  177. void asCGarbageCollector::RemoveNewObjectAtIdx(int idx)
  178. {
  179. // We need to protect this update with a critical section as
  180. // another thread might be appending an object at the same time
  181. ENTERCRITICALSECTION(gcCritical);
  182. if( idx == (int)gcNewObjects.GetLength() - 1)
  183. gcNewObjects.PopLast();
  184. else
  185. gcNewObjects[idx] = gcNewObjects.PopLast();
  186. LEAVECRITICALSECTION(gcCritical);
  187. }
  188. void asCGarbageCollector::RemoveOldObjectAtIdx(int idx)
  189. {
  190. // We need to protect this update with a critical section as
  191. // another thread might be appending an object at the same time
  192. ENTERCRITICALSECTION(gcCritical);
  193. if( idx == (int)gcOldObjects.GetLength() - 1)
  194. gcOldObjects.PopLast();
  195. else
  196. gcOldObjects[idx] = gcOldObjects.PopLast();
  197. LEAVECRITICALSECTION(gcCritical);
  198. }
  199. void asCGarbageCollector::MoveObjectToOldList(int idx)
  200. {
  201. // We need to protect this update with a critical section as
  202. // another thread might be appending an object at the same time
  203. ENTERCRITICALSECTION(gcCritical);
  204. gcOldObjects.PushLast(gcNewObjects[idx]);
  205. if( idx == (int)gcNewObjects.GetLength() - 1)
  206. gcNewObjects.PopLast();
  207. else
  208. gcNewObjects[idx] = gcNewObjects.PopLast();
  209. LEAVECRITICALSECTION(gcCritical);
  210. }
  211. void asCGarbageCollector::IncreaseCounterForNewObject(int idx)
  212. {
  213. // We need to protect this update with a critical section as
  214. // another thread might be appending an object at the same time
  215. ENTERCRITICALSECTION(gcCritical);
  216. gcNewObjects[idx].count++;
  217. LEAVECRITICALSECTION(gcCritical);
  218. }
  219. int asCGarbageCollector::DestroyNewGarbage()
  220. {
  221. for(;;)
  222. {
  223. switch( destroyNewState )
  224. {
  225. case destroyGarbage_init:
  226. {
  227. // If there are no objects to be freed then don't start
  228. if( gcNewObjects.GetLength() == 0 )
  229. return 0;
  230. destroyNewIdx = (asUINT)-1;
  231. destroyNewState = destroyGarbage_loop;
  232. }
  233. break;
  234. case destroyGarbage_loop:
  235. case destroyGarbage_haveMore:
  236. {
  237. // If the refCount has reached 1, then only the GC still holds a
  238. // reference to the object, thus we don't need to worry about the
  239. // application touching the objects during collection.
  240. // Destroy all objects that have refCount == 1. If any objects are
  241. // destroyed, go over the list again, because it may have made more
  242. // objects reach refCount == 1.
  243. if( ++destroyNewIdx < gcNewObjects.GetLength() )
  244. {
  245. asSObjTypePair gcObj = GetNewObjectAtIdx(destroyNewIdx);
  246. if( engine->CallObjectMethodRetInt(gcObj.obj, gcObj.type->beh.gcGetRefCount) == 1 )
  247. {
  248. // Release the object immediately
  249. // Make sure the refCount is really 0, because the
  250. // destructor may have increased the refCount again.
  251. bool addRef = false;
  252. if( gcObj.type->flags & asOBJ_SCRIPT_OBJECT )
  253. {
  254. // Script objects may actually be resurrected in the destructor
  255. int refCount = ((asCScriptObject*)gcObj.obj)->Release();
  256. if( refCount > 0 ) addRef = true;
  257. }
  258. else
  259. engine->CallObjectMethod(gcObj.obj, gcObj.type->beh.release);
  260. // Was the object really destroyed?
  261. if( !addRef )
  262. {
  263. numDestroyed++;
  264. numNewDestroyed++;
  265. RemoveNewObjectAtIdx(destroyNewIdx);
  266. destroyNewIdx--;
  267. }
  268. else
  269. {
  270. // Since the object was resurrected in the
  271. // destructor, we must add our reference again
  272. engine->CallObjectMethod(gcObj.obj, gcObj.type->beh.addref);
  273. }
  274. destroyNewState = destroyGarbage_haveMore;
  275. }
  276. else if( gcObj.count == 3 )
  277. {
  278. // We've already verified this object multiple times. It is likely
  279. // to live for quite a long time so we'll move it to the list if old objects
  280. MoveObjectToOldList(destroyNewIdx);
  281. destroyNewIdx--;
  282. }
  283. else
  284. {
  285. // Increase the counter for the number of times the object has been verified
  286. IncreaseCounterForNewObject(destroyNewIdx);
  287. }
  288. // Allow the application to work a little
  289. return 1;
  290. }
  291. else
  292. {
  293. if( destroyNewState == destroyGarbage_haveMore )
  294. {
  295. // Restart the cycle
  296. destroyNewState = destroyGarbage_init;
  297. }
  298. else
  299. {
  300. // Restart the cycle
  301. destroyNewState = destroyGarbage_init;
  302. // Return 0 to tell the application that there
  303. // is no more garbage to destroy at the moment
  304. return 0;
  305. }
  306. }
  307. }
  308. break;
  309. }
  310. }
  311. // Shouldn't reach this point
  312. UNREACHABLE_RETURN;
  313. }
  314. int asCGarbageCollector::DestroyOldGarbage()
  315. {
  316. for(;;)
  317. {
  318. switch( destroyOldState )
  319. {
  320. case destroyGarbage_init:
  321. {
  322. // If there are no objects to be freed then don't start
  323. if( gcOldObjects.GetLength() == 0 )
  324. return 0;
  325. destroyOldIdx = (asUINT)-1;
  326. destroyOldState = destroyGarbage_loop;
  327. }
  328. break;
  329. case destroyGarbage_loop:
  330. case destroyGarbage_haveMore:
  331. {
  332. // If the refCount has reached 1, then only the GC still holds a
  333. // reference to the object, thus we don't need to worry about the
  334. // application touching the objects during collection.
  335. // Destroy all objects that have refCount == 1. If any objects are
  336. // destroyed, go over the list again, because it may have made more
  337. // objects reach refCount == 1.
  338. if( ++destroyOldIdx < gcOldObjects.GetLength() )
  339. {
  340. asSObjTypePair gcObj = GetOldObjectAtIdx(destroyOldIdx);
  341. if( engine->CallObjectMethodRetInt(gcObj.obj, gcObj.type->beh.gcGetRefCount) == 1 )
  342. {
  343. // Release the object immediately
  344. // Make sure the refCount is really 0, because the
  345. // destructor may have increased the refCount again.
  346. bool addRef = false;
  347. if( gcObj.type->flags & asOBJ_SCRIPT_OBJECT )
  348. {
  349. // Script objects may actually be resurrected in the destructor
  350. int refCount = ((asCScriptObject*)gcObj.obj)->Release();
  351. if( refCount > 0 ) addRef = true;
  352. }
  353. else
  354. engine->CallObjectMethod(gcObj.obj, gcObj.type->beh.release);
  355. // Was the object really destroyed?
  356. if( !addRef )
  357. {
  358. numDestroyed++;
  359. RemoveOldObjectAtIdx(destroyOldIdx);
  360. destroyOldIdx--;
  361. }
  362. else
  363. {
  364. // Since the object was resurrected in the
  365. // destructor, we must add our reference again
  366. engine->CallObjectMethod(gcObj.obj, gcObj.type->beh.addref);
  367. }
  368. destroyOldState = destroyGarbage_haveMore;
  369. }
  370. // Allow the application to work a little
  371. return 1;
  372. }
  373. else
  374. {
  375. if( destroyOldState == destroyGarbage_haveMore )
  376. {
  377. // Restart the cycle
  378. destroyOldState = destroyGarbage_init;
  379. }
  380. else
  381. {
  382. // Restart the cycle
  383. destroyOldState = destroyGarbage_init;
  384. // Return 0 to tell the application that there
  385. // is no more garbage to destroy at the moment
  386. return 0;
  387. }
  388. }
  389. }
  390. break;
  391. }
  392. }
  393. // Shouldn't reach this point
  394. UNREACHABLE_RETURN;
  395. }
  396. int asCGarbageCollector::IdentifyGarbageWithCyclicRefs()
  397. {
  398. for(;;)
  399. {
  400. switch( detectState )
  401. {
  402. case clearCounters_init:
  403. detectState = clearCounters_loop;
  404. break;
  405. case clearCounters_loop:
  406. {
  407. // Decrease reference counter for all objects removed from the map
  408. asSMapNode<void*, asSIntTypePair> *cursor = 0;
  409. gcMap.MoveFirst(&cursor);
  410. if( cursor )
  411. {
  412. void *obj = gcMap.GetKey(cursor);
  413. asSIntTypePair it = gcMap.GetValue(cursor);
  414. engine->CallObjectMethod(obj, it.type->beh.release);
  415. gcMap.Erase(cursor);
  416. return 1;
  417. }
  418. detectState = buildMap_init;
  419. }
  420. break;
  421. case buildMap_init:
  422. detectIdx = 0;
  423. detectState = buildMap_loop;
  424. break;
  425. case buildMap_loop:
  426. {
  427. // Build a map of objects that will be checked, the map will
  428. // hold the object pointer as key, and the gcCount and the
  429. // object's type as value. As objects are added to the map the
  430. // gcFlag must be set in the objects, so we can be verify if
  431. // the object is accessed during the GC cycle.
  432. // If an object is removed from the gcObjects list during the
  433. // iteration of this step, it is possible that an object won't
  434. // be used during the analyzing for cyclic references. This
  435. // isn't a problem, as the next time the GC cycle starts the
  436. // object will be verified.
  437. if( detectIdx < gcOldObjects.GetLength() )
  438. {
  439. // Add the gc count for this object
  440. asSObjTypePair gcObj = GetOldObjectAtIdx(detectIdx);
  441. int refCount = engine->CallObjectMethodRetInt(gcObj.obj, gcObj.type->beh.gcGetRefCount);
  442. if( refCount > 1 )
  443. {
  444. asSIntTypePair it = {refCount-1, gcObj.type};
  445. gcMap.Insert(gcObj.obj, it);
  446. // Increment the object's reference counter when putting it in the map
  447. engine->CallObjectMethod(gcObj.obj, gcObj.type->beh.addref);
  448. // Mark the object so that we can
  449. // see if it has changed since read
  450. engine->CallObjectMethod(gcObj.obj, gcObj.type->beh.gcSetFlag);
  451. }
  452. detectIdx++;
  453. // Let the application work a little
  454. return 1;
  455. }
  456. else
  457. detectState = countReferences_init;
  458. }
  459. break;
  460. case countReferences_init:
  461. {
  462. gcMap.MoveFirst(&gcMapCursor);
  463. detectState = countReferences_loop;
  464. }
  465. break;
  466. case countReferences_loop:
  467. {
  468. // Call EnumReferences on all objects in the map to count the number
  469. // of references reachable from between objects in the map. If all
  470. // references for an object in the map is reachable from other objects
  471. // in the map, then we know that no outside references are held for
  472. // this object, thus it is a potential dead object in a circular reference.
  473. // If the gcFlag is cleared for an object we consider the object alive
  474. // and referenced from outside the GC, thus we don't enumerate its references.
  475. // Any new objects created after this step in the GC cycle won't be
  476. // in the map, and is thus automatically considered alive.
  477. if( gcMapCursor )
  478. {
  479. void *obj = gcMap.GetKey(gcMapCursor);
  480. asCObjectType *type = gcMap.GetValue(gcMapCursor).type;
  481. gcMap.MoveNext(&gcMapCursor, gcMapCursor);
  482. if( engine->CallObjectMethodRetBool(obj, type->beh.gcGetFlag) )
  483. {
  484. engine->CallObjectMethod(obj, engine, type->beh.gcEnumReferences);
  485. }
  486. // Allow the application to work a little
  487. return 1;
  488. }
  489. else
  490. detectState = detectGarbage_init;
  491. }
  492. break;
  493. case detectGarbage_init:
  494. {
  495. gcMap.MoveFirst(&gcMapCursor);
  496. liveObjects.SetLength(0);
  497. detectState = detectGarbage_loop1;
  498. }
  499. break;
  500. case detectGarbage_loop1:
  501. {
  502. // All objects that are known not to be dead must be removed from the map,
  503. // along with all objects they reference. What remains in the map after
  504. // this pass is sure to be dead objects in circular references.
  505. // An object is considered alive if its gcFlag is cleared, or all the
  506. // references were not found in the map.
  507. // Add all alive objects from the map to the liveObjects array
  508. if( gcMapCursor )
  509. {
  510. asSMapNode<void*, asSIntTypePair> *cursor = gcMapCursor;
  511. gcMap.MoveNext(&gcMapCursor, gcMapCursor);
  512. void *obj = gcMap.GetKey(cursor);
  513. asSIntTypePair it = gcMap.GetValue(cursor);
  514. bool gcFlag = engine->CallObjectMethodRetBool(obj, it.type->beh.gcGetFlag);
  515. if( !gcFlag || it.i > 0 )
  516. {
  517. liveObjects.PushLast(obj);
  518. }
  519. // Allow the application to work a little
  520. return 1;
  521. }
  522. else
  523. detectState = detectGarbage_loop2;
  524. }
  525. break;
  526. case detectGarbage_loop2:
  527. {
  528. // In this step we are actually removing the alive objects from the map.
  529. // As the object is removed, all the objects it references are added to the
  530. // liveObjects list, by calling EnumReferences. Only objects still in the map
  531. // will be added to the liveObjects list.
  532. if( liveObjects.GetLength() )
  533. {
  534. void *gcObj = liveObjects.PopLast();
  535. asCObjectType *type = 0;
  536. // Remove the object from the map to mark it as alive
  537. asSMapNode<void*, asSIntTypePair> *cursor = 0;
  538. if( gcMap.MoveTo(&cursor, gcObj) )
  539. {
  540. type = gcMap.GetValue(cursor).type;
  541. gcMap.Erase(cursor);
  542. // We need to decrease the reference count again as we remove the object from the map
  543. engine->CallObjectMethod(gcObj, type->beh.release);
  544. // Enumerate all the object's references so that they too can be marked as alive
  545. engine->CallObjectMethod(gcObj, engine, type->beh.gcEnumReferences);
  546. }
  547. // Allow the application to work a little
  548. return 1;
  549. }
  550. else
  551. detectState = verifyUnmarked_init;
  552. }
  553. break;
  554. case verifyUnmarked_init:
  555. gcMap.MoveFirst(&gcMapCursor);
  556. detectState = verifyUnmarked_loop;
  557. break;
  558. case verifyUnmarked_loop:
  559. {
  560. // In this step we must make sure that none of the objects still in the map
  561. // has been touched by the application. If they have then we must run the
  562. // detectGarbage loop once more.
  563. if( gcMapCursor )
  564. {
  565. void *gcObj = gcMap.GetKey(gcMapCursor);
  566. asCObjectType *type = gcMap.GetValue(gcMapCursor).type;
  567. bool gcFlag = engine->CallObjectMethodRetBool(gcObj, type->beh.gcGetFlag);
  568. if( !gcFlag )
  569. {
  570. // The unmarked object was touched, rerun the detectGarbage loop
  571. detectState = detectGarbage_init;
  572. }
  573. else
  574. gcMap.MoveNext(&gcMapCursor, gcMapCursor);
  575. // Allow the application to work a little
  576. return 1;
  577. }
  578. else
  579. {
  580. // No unmarked object was touched, we can now be sure
  581. // that objects that have gcCount == 0 really is garbage
  582. detectState = breakCircles_init;
  583. }
  584. }
  585. break;
  586. case breakCircles_init:
  587. {
  588. gcMap.MoveFirst(&gcMapCursor);
  589. detectState = breakCircles_loop;
  590. }
  591. break;
  592. case breakCircles_loop:
  593. case breakCircles_haveGarbage:
  594. {
  595. // All objects in the map are now known to be dead objects
  596. // kept alive through circular references. To be able to free
  597. // these objects we need to force the breaking of the circle
  598. // by having the objects release their references.
  599. if( gcMapCursor )
  600. {
  601. numDetected++;
  602. void *gcObj = gcMap.GetKey(gcMapCursor);
  603. asCObjectType *type = gcMap.GetValue(gcMapCursor).type;
  604. engine->CallObjectMethod(gcObj, engine, type->beh.gcReleaseAllReferences);
  605. gcMap.MoveNext(&gcMapCursor, gcMapCursor);
  606. detectState = breakCircles_haveGarbage;
  607. // Allow the application to work a little
  608. return 1;
  609. }
  610. else
  611. {
  612. // If no garbage was detected we can finish now
  613. if( detectState != breakCircles_haveGarbage )
  614. {
  615. // Restart the GC
  616. detectState = clearCounters_init;
  617. return 0;
  618. }
  619. else
  620. {
  621. // Restart the GC
  622. detectState = clearCounters_init;
  623. return 1;
  624. }
  625. }
  626. }
  627. break;
  628. } // switch
  629. }
  630. // Shouldn't reach this point
  631. UNREACHABLE_RETURN;
  632. }
  633. void asCGarbageCollector::GCEnumCallback(void *reference)
  634. {
  635. if( detectState == countReferences_loop )
  636. {
  637. // Find the reference in the map
  638. asSMapNode<void*, asSIntTypePair> *cursor = 0;
  639. if( gcMap.MoveTo(&cursor, reference) )
  640. {
  641. // Decrease the counter in the map for the reference
  642. gcMap.GetValue(cursor).i--;
  643. }
  644. }
  645. else if( detectState == detectGarbage_loop2 )
  646. {
  647. // Find the reference in the map
  648. asSMapNode<void*, asSIntTypePair> *cursor = 0;
  649. if( gcMap.MoveTo(&cursor, reference) )
  650. {
  651. // Add the object to the list of objects to mark as alive
  652. liveObjects.PushLast(reference);
  653. }
  654. }
  655. }
  656. END_AS_NAMESPACE