as_gc.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2018 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. andreas@angelcode.com
  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. ~asCGarbageCollector();
  42. int GarbageCollect(asDWORD flags, asUINT iterations);
  43. void GetStatistics(asUINT *currentSize, asUINT *totalDestroyed, asUINT *totalDetected, asUINT *newObjects, asUINT *totalNewDestroyed) const;
  44. void GCEnumCallback(void *reference);
  45. int AddScriptObjectToGC(void *obj, asCObjectType *objType);
  46. int GetObjectInGC(asUINT idx, asUINT *seqNbr, void **obj, asITypeInfo **type);
  47. int ReportAndReleaseUndestroyedObjects();
  48. asCScriptEngine *engine;
  49. // Callback for when circular reference are detected
  50. asCIRCULARREFFUNC_t circularRefDetectCallbackFunc;
  51. void * circularRefDetectCallbackParam;
  52. protected:
  53. struct asSObjTypePair {void *obj; asCObjectType *type; asUINT seqNbr;};
  54. struct asSIntTypePair {int i; asCObjectType *type;};
  55. typedef asSMapNode<void*, asSIntTypePair> asSMapNode_t;
  56. enum egcDestroyState
  57. {
  58. destroyGarbage_init = 0,
  59. destroyGarbage_loop,
  60. destroyGarbage_haveMore
  61. };
  62. enum egcDetectState
  63. {
  64. clearCounters_init = 0,
  65. clearCounters_loop,
  66. buildMap_init,
  67. buildMap_loop,
  68. countReferences_init,
  69. countReferences_loop,
  70. detectGarbage_init,
  71. detectGarbage_loop1,
  72. detectGarbage_loop2,
  73. verifyUnmarked_init,
  74. verifyUnmarked_loop,
  75. breakCircles_init,
  76. breakCircles_loop,
  77. breakCircles_haveGarbage
  78. };
  79. int DestroyNewGarbage();
  80. int DestroyOldGarbage();
  81. int IdentifyGarbageWithCyclicRefs();
  82. asSObjTypePair GetNewObjectAtIdx(int idx);
  83. asSObjTypePair GetOldObjectAtIdx(int idx);
  84. void RemoveNewObjectAtIdx(int idx);
  85. void RemoveOldObjectAtIdx(int idx);
  86. void MoveObjectToOldList(int idx);
  87. void MoveAllObjectsToOldList();
  88. // Holds all the objects known by the garbage collector
  89. asCArray<asSObjTypePair> gcNewObjects;
  90. asCArray<asSObjTypePair> gcOldObjects;
  91. // This array temporarily holds references to objects known to be live objects
  92. asCArray<void*> liveObjects;
  93. // This map holds objects currently being searched for cyclic references, it also holds a
  94. // counter that gives the number of references to the object that the GC can't reach
  95. asCMap<void*, asSIntTypePair> gcMap;
  96. // State variables
  97. egcDestroyState destroyNewState;
  98. egcDestroyState destroyOldState;
  99. asUINT destroyNewIdx;
  100. asUINT destroyOldIdx;
  101. asUINT numDestroyed;
  102. asUINT numNewDestroyed;
  103. egcDetectState detectState;
  104. asUINT detectIdx;
  105. asUINT numDetected;
  106. asUINT numAdded;
  107. asUINT seqAtSweepStart[3];
  108. asSMapNode_t *gcMapCursor;
  109. bool isProcessing;
  110. // We'll keep a pool of nodes to avoid allocating memory all the time
  111. asSMapNode_t *GetNode(void *obj, asSIntTypePair it);
  112. void ReturnNode(asSMapNode_t *node);
  113. asCArray<asSMapNode_t*> freeNodes;
  114. // Critical section for multithreaded access
  115. DECLARECRITICALSECTION(gcCritical) // Used for adding/removing objects
  116. DECLARECRITICALSECTION(gcCollecting) // Used for processing
  117. };
  118. END_AS_NAMESPACE
  119. #endif