as_gc.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.h
  25. //
  26. // The garbage collector is used to resolve cyclic references
  27. //
  28. #ifndef AS_GC_H
  29. #define AS_GC_H
  30. #include "as_config.h"
  31. #include "as_array.h"
  32. #include "as_map.h"
  33. #include "as_thread.h"
  34. BEGIN_AS_NAMESPACE
  35. class asCScriptEngine;
  36. class asCObjectType;
  37. class asCGarbageCollector
  38. {
  39. public:
  40. asCGarbageCollector();
  41. int GarbageCollect(asDWORD flags);
  42. void GetStatistics(asUINT *currentSize, asUINT *totalDestroyed, asUINT *totalDetected, asUINT *newObjects, asUINT *totalNewDestroyed) const;
  43. void GCEnumCallback(void *reference);
  44. void AddScriptObjectToGC(void *obj, asCObjectType *objType);
  45. void ReportUndestroyedObjects();
  46. asCScriptEngine *engine;
  47. protected:
  48. struct asSObjTypePair {void *obj; asCObjectType *type; int count;};
  49. struct asSIntTypePair {int i; asCObjectType *type;};
  50. enum egcDestroyState
  51. {
  52. destroyGarbage_init = 0,
  53. destroyGarbage_loop,
  54. destroyGarbage_haveMore
  55. };
  56. enum egcDetectState
  57. {
  58. clearCounters_init = 0,
  59. clearCounters_loop,
  60. buildMap_init,
  61. buildMap_loop,
  62. countReferences_init,
  63. countReferences_loop,
  64. detectGarbage_init,
  65. detectGarbage_loop1,
  66. detectGarbage_loop2,
  67. verifyUnmarked_init,
  68. verifyUnmarked_loop,
  69. breakCircles_init,
  70. breakCircles_loop,
  71. breakCircles_haveGarbage
  72. };
  73. int DestroyNewGarbage();
  74. int DestroyOldGarbage();
  75. int IdentifyGarbageWithCyclicRefs();
  76. asSObjTypePair GetNewObjectAtIdx(int idx);
  77. asSObjTypePair GetOldObjectAtIdx(int idx);
  78. void RemoveNewObjectAtIdx(int idx);
  79. void RemoveOldObjectAtIdx(int idx);
  80. void MoveObjectToOldList(int idx);
  81. void IncreaseCounterForNewObject(int idx);
  82. // Holds all the objects known by the garbage collector
  83. asCArray<asSObjTypePair> gcNewObjects;
  84. asCArray<asSObjTypePair> gcOldObjects;
  85. // This array temporarily holds references to objects known to be live objects
  86. asCArray<void*> liveObjects;
  87. // This map holds objects currently being searched for cyclic references, it also holds a
  88. // counter that gives the number of references to the object that the GC can't reach
  89. asCMap<void*, asSIntTypePair> gcMap;
  90. // State variables
  91. egcDestroyState destroyNewState;
  92. egcDestroyState destroyOldState;
  93. asUINT destroyNewIdx;
  94. asUINT destroyOldIdx;
  95. asUINT numDestroyed;
  96. asUINT numNewDestroyed;
  97. egcDetectState detectState;
  98. asUINT detectIdx;
  99. asUINT numDetected;
  100. asSMapNode<void*, asSIntTypePair> *gcMapCursor;
  101. // Critical section for multithreaded access
  102. DECLARECRITICALSECTION(gcCritical); // Used for adding/removing objects
  103. DECLARECRITICALSECTION(gcCollecting); // Used for processing
  104. };
  105. END_AS_NAMESPACE
  106. #endif