gc.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #ifndef AFUN_GC_H
  2. #define AFUN_GC_H
  3. #include <queue>
  4. #include <list>
  5. #include "aFuntool.h"
  6. #include "aFunCoreExport.h"
  7. namespace aFuncore {
  8. typedef unsigned GcCount;
  9. class Inter;
  10. class AFUN_CORE_EXPORT GcObjectBase {
  11. public:
  12. GcObjectBase(const GcObjectBase &) = delete;
  13. GcObjectBase &operator=(const GcObjectBase &) = delete;
  14. inline void addReference();
  15. inline void delReference();
  16. [[nodiscard]] inline GcCount getReference() const;
  17. inline void setClear(bool clear=false);
  18. static void checkReachable(std::list<GcObjectBase *> &list);
  19. static void setReachable(std::list<GcObjectBase *> &list, std::queue<GcObjectBase *> &des, std::queue<GcObjectBase *> &del);
  20. static void destructUnreachable(std::queue<GcObjectBase *> &des, Inter &gc_inter);
  21. static void deleteUnreachable(std::queue<GcObjectBase *> &del);
  22. static void destructAll(std::list<GcObjectBase *> &list, Inter &gc_inter);
  23. static void deleteAll(std::list<GcObjectBase *> &list);
  24. protected:
  25. std::mutex lock;
  26. inline GcObjectBase();
  27. virtual ~GcObjectBase() = default;
  28. virtual void destruct(Inter &gc_inter);
  29. virtual void linkObject(std::queue<GcObjectBase *> &queue);
  30. private:
  31. bool done_destruct;
  32. bool not_clear; // 不清除
  33. bool reachable; // 可达标记 [同时标识已迭代]
  34. GcCount reference; // 引用计数
  35. };
  36. };
  37. #include "gc.inline.h"
  38. #endif //AFUN_GC_H