__gc.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #ifndef AFUN_GC_H_
  2. #define AFUN_GC_H_
  3. #include "tool.h"
  4. typedef struct GC_Var GC_Var;
  5. typedef struct GC_VarSpace GC_VarSpace;
  6. typedef struct GC_Object GC_Object;
  7. typedef struct GC_ObjectData GC_ObjectData;
  8. typedef struct af_GcList af_GcList;
  9. typedef struct gc_Analyzed gc_Analyzed, **pgc_Analyzed;
  10. #define GC_FREE_EXCHANGE(obj, Type, Env) do { \
  11. {if ((obj)->gc.prev != NULL) {(obj)->gc.prev->gc.next = (obj)->gc.next;} \
  12. else {(Env)->gc_##Type = (obj)->gc.next;}} \
  13. {if ((obj)->gc.next != NULL) {(obj)->gc.next->gc.prev = (obj)->gc.prev;}}} while(0)
  14. #define GC_CHAIN(type) struct type *next, *prev
  15. typedef uint32_t GcCount;
  16. struct gc_info {
  17. bool not_clear; // 不清除
  18. GcCount reference; // 引用计数
  19. bool reachable; // 可达标记 [同时标识已迭代]
  20. };
  21. struct GC_ObjectData {
  22. struct gc_info info;
  23. GC_CHAIN(af_ObjectData);
  24. bool done_destruct; // 是否已析构
  25. };
  26. struct GC_Object {
  27. struct gc_info info;
  28. GC_CHAIN(af_Object);
  29. };
  30. struct GC_Var {
  31. struct gc_info info;
  32. GC_CHAIN(af_Var);
  33. };
  34. struct GC_VarSpace {
  35. struct gc_info info;
  36. GC_CHAIN(af_VarSpace);
  37. };
  38. #undef GC_CHAIN
  39. #include "__env.h" // 这部分内容依赖上面的定义
  40. #include "gc.h"
  41. struct af_GcList {
  42. enum af_GcListType type;
  43. union {
  44. void *data;
  45. struct af_Object *obj;
  46. struct af_VarSpace *vs;
  47. struct af_Var *var;
  48. struct af_VarSpaceListNode *vsl;
  49. };
  50. struct af_GcList *next;
  51. };
  52. struct gc_Analyzed {
  53. struct af_Object *obj;
  54. struct gc_Analyzed *next;
  55. };
  56. /* 重新定义包括af_ObjectData的 gc Reference 函数 */
  57. #ifdef core_shared_t_EXPORTS
  58. #undef gc_addReference
  59. #undef gc_delReference
  60. #undef gc_getReference
  61. #define gc_addReference(obj) ((_Generic((obj), \
  62. af_ObjectData *: gc_addObjectDataReference, \
  63. af_Object *: gc_addObjectReference, \
  64. af_Var *: gc_addVarReference, \
  65. af_VarSpace *: gc_addVarSpaceReference))(obj))
  66. #define gc_delReference(obj) ((_Generic((obj), \
  67. af_ObjectData *: gc_delObjectDataReference, \
  68. af_Object *: gc_delObjectReference, \
  69. af_Var *: gc_delVarReference, \
  70. af_VarSpace *: gc_delVarSpaceReference))(obj))
  71. #define gc_getReference(obj) ((_Generic((obj), \
  72. af_ObjectData *: gc_getObjectDataReference, \
  73. af_Object *: gc_getObjectReference, \
  74. af_Var *: gc_getVarReference, \
  75. af_VarSpace *: gc_getVarSpaceReference))(obj))
  76. #endif
  77. /* gc 对象新增函数 */
  78. AFUN_CORE_NO_EXPORT void gc_addObject(af_Object *obj, af_Environment *env);
  79. AFUN_CORE_NO_EXPORT void gc_addVar(af_Var *obj, af_Environment *env);
  80. AFUN_CORE_NO_EXPORT void gc_addVarSpace(af_VarSpace *obj, af_Environment *env);
  81. AFUN_CORE_NO_EXPORT void gc_addObjectData(struct af_ObjectData *obj, af_Environment *env);
  82. /* gc Reference 管理函数 : 涉及af_ObjectData 不对外公开 */
  83. AFUN_CORE_NO_EXPORT void gc_addObjectDataReference(af_ObjectData *obj);
  84. AFUN_CORE_NO_EXPORT void gc_delObjectDataReference(af_ObjectData *obj);
  85. AFUN_CORE_NO_EXPORT GcCount gc_getObjectDataReference(af_ObjectData *obj);
  86. /* gc 操控函数 : gc的启动由解释器完全管理 */
  87. AFUN_CORE_NO_EXPORT af_GuardianList *gc_RunGC(af_Environment *env);
  88. AFUN_CORE_NO_EXPORT paf_GuardianList checkAllDestruct(af_Environment *env, paf_GuardianList pgl);
  89. AFUN_CORE_NO_EXPORT void gc_freeAllValueData(af_Environment *env);
  90. AFUN_CORE_NO_EXPORT void gc_freeAllValue(af_Environment *env);
  91. /* gc 信息函数 */
  92. AFUN_CORE_NO_EXPORT void printGCByCore(af_Environment *env);
  93. /* gc 运行时函数 */
  94. AFUN_CORE_NO_EXPORT void resetGC(af_Environment *env);
  95. #endif //AFUN_GC_H_