fork download
  1. #ifndef GC_HPP
  2. #define GC_HPP
  3.  
  4.  
  5. #include <cstddef>
  6. #include <cassert>
  7. #include <vector>
  8. #include <memory>
  9.  
  10.  
  11. template <class T> class gc_collection;
  12. class gc_ptr_base;
  13. class gc_object;
  14. template <class T> class gc_member_ptr;
  15.  
  16.  
  17. //collection node; must be part of the object (the collections are intrusive).
  18. template <class T> class gc_collection_node {
  19. public:
  20. //not allowed
  21. gc_collection_node(const gc_collection_node<T> &) = delete;
  22.  
  23. //not allowed
  24. gc_collection_node(gc_collection_node<T> &&) = delete;
  25.  
  26. //not allowed
  27. gc_collection_node<T> &operator = (const gc_collection_node<T> &) = delete;
  28.  
  29. //not allowed
  30. gc_collection_node<T> &operator = (gc_collection_node<T> &n) = delete;
  31.  
  32. protected:
  33. //the default constructor
  34. gc_collection_node() {
  35. assert((m_collection = nullptr) == nullptr);
  36. }
  37.  
  38. //does nothing
  39. ~gc_collection_node() {
  40. assert(m_collection == nullptr);
  41. }
  42.  
  43. private:
  44. //pointer to the collection
  45. gc_collection<T> *m_collection;
  46.  
  47. //index into the collection
  48. size_t m_collection_index;
  49.  
  50. friend class gc_collection<T>;
  51. friend class gc;
  52. friend class gc_ptr_base;
  53. friend class gc_object;
  54. };
  55.  
  56.  
  57. //collection of items.
  58. template <class T> class gc_collection {
  59. public:
  60.  
  61. //add an object
  62. void add(T *object) {
  63. assert(object->m_collection == nullptr);
  64. object->m_collection = this;
  65. object->m_collection_index = m_objects.size() - 1;
  66. m_objects.push_back(object);
  67. }
  68.  
  69. //remove an object
  70. void remove(T *object) {
  71. assert(object->m_collection == this);
  72. if (m_objects.size() > 1) {
  73. T *last = m_objects.back();
  74. if (last != object) {
  75. m_objects[object->m_collection_index] = last;
  76. last->m_collection_index = object->m_collection_index;
  77. }
  78. }
  79. m_objects.pop_back();
  80. assert((object->m_collection = nullptr) == nullptr);
  81. }
  82.  
  83. private:
  84. //objects
  85. std::vector<T *> m_objects;
  86.  
  87. friend class gc_object_collection;
  88. friend class gc;
  89. };
  90.  
  91.  
  92. //gc ptr collection
  93. typedef gc_collection<gc_ptr_base> gc_ptr_collection;
  94.  
  95.  
  96. //gc object collection
  97. class gc_object_collection : public gc_collection<gc_object> {
  98. public:
  99. //delete all objects
  100. ~gc_object_collection() {
  101. delete_objects();
  102. }
  103.  
  104. //deletes all objects
  105. void delete_objects();
  106. };
  107.  
  108.  
  109. //pointer to the collection of objects
  110. typedef std::shared_ptr<gc_object_collection> gc_object_collection_ptr;
  111.  
  112.  
  113. //the garbage collector
  114. class gc {
  115. public:
  116. //collects garbage, using the mark & sweep algorithm
  117. static void collect();
  118.  
  119. private:
  120. //get the collection of root ptrs
  121. static gc_ptr_collection *get_root_ptrs() {
  122. static gc_ptr_collection p;
  123. return &p;
  124. }
  125.  
  126. //get the current collection of objects
  127. static gc_object_collection_ptr &get_objects() {
  128. static gc_object_collection_ptr p{std::make_shared<gc_object_collection>()};
  129. return p;
  130. }
  131.  
  132. //scan ptrs
  133. static void _scan(gc_ptr_collection &ptrs, gc_object_collection &old, gc_object_collection &reached);
  134.  
  135. template <class T> friend class gc_root_ptr;
  136. friend class gc_object;
  137. };
  138.  
  139.  
  140. //gc ptr
  141. class gc_ptr_base : public gc_collection_node<gc_ptr_base> {
  142. public:
  143. //not allowed
  144. gc_ptr_base(const gc_ptr_base &) = delete;
  145.  
  146. //not allowed
  147. gc_ptr_base(gc_ptr_base &) = delete;
  148.  
  149. //not allowed
  150. gc_ptr_base &operator = (const gc_ptr_base &) = delete;
  151.  
  152. //not allowed
  153. gc_ptr_base &operator = (gc_ptr_base &) = delete;
  154.  
  155. protected:
  156. //the pointer to the object
  157. gc_object *m_object;
  158.  
  159. //the default constructor
  160. gc_ptr_base(gc_ptr_collection *owner, gc_object *object = nullptr) :
  161. m_object(object)
  162. {
  163. owner->add(this);
  164. }
  165.  
  166. //remove the pointer from the owning collection
  167. ~gc_ptr_base() {
  168. m_collection->remove(this);
  169. }
  170.  
  171. friend class gc;
  172. };
  173.  
  174.  
  175. //A root ptr.
  176. template <class T> class gc_root_ptr : public gc_ptr_base {
  177. public:
  178. //The default constructor.
  179. gc_root_ptr(T *object = nullptr) :
  180. gc_ptr_base(gc::get_root_ptrs(), object)
  181. {
  182. }
  183.  
  184. //The copy constructor.
  185. gc_root_ptr(const gc_root_ptr<T> &ptr) :
  186. gc_ptr_base(gc::get_root_ptrs(), ptr.m_object)
  187. {
  188. }
  189.  
  190. //The move constructor.
  191. gc_root_ptr(gc_root_ptr<T> &&ptr) :
  192. gc_ptr_base(gc::get_root_ptrs(), ptr.m_object)
  193. {
  194. }
  195.  
  196. //Assignment from object.
  197. gc_root_ptr<T> &operator = (T *object) {
  198. m_object = object;
  199. return *this;
  200. }
  201.  
  202. //Assignment from pointer.
  203. gc_root_ptr<T> &operator = (const gc_root_ptr<T> &ptr) {
  204. m_object = ptr.m_object;
  205. return *this;
  206. }
  207.  
  208. //Returns the value of the pointer.
  209. T *get() const {
  210. assert(m_object || static_cast<T *>(m_object) == nullptr);
  211. return static_cast<T *>(m_object);
  212. }
  213.  
  214. //Auto conversion to ptr value.
  215. operator T* () const {
  216. return get();
  217. }
  218.  
  219. //Accesses the underlying object's members.
  220. T *operator ->() const {
  221. assert(m_object);
  222. return get();
  223. }
  224. };
  225.  
  226.  
  227. //alternative type for gc root pointer
  228. template <class T> using gc_ptr = gc_root_ptr<T>;
  229.  
  230.  
  231. //gc object
  232. class gc_object : public gc_collection_node<gc_object> {
  233. public:
  234. //alternative type for gc root pointer
  235. template <class T> using gc_ptr = gc_member_ptr<T>;
  236.  
  237. //registers the object to the collector
  238. gc_object() {
  239. gc::get_objects()->add(this);
  240. }
  241.  
  242. //registers the object to the collector
  243. gc_object(const gc_object &obj) :
  244. gc_object()
  245. {
  246. }
  247.  
  248. //registers the object to the collector
  249. gc_object(gc_object &&obj) :
  250. gc_object()
  251. {
  252. }
  253.  
  254. //removes the object from the collector
  255. virtual ~gc_object() {
  256. m_collection->remove(this);
  257. }
  258.  
  259. //not allowed
  260. gc_object &operator = (const gc_object &) = delete;
  261.  
  262. //not allowed
  263. gc_object &operator = (gc_object &&) = delete;
  264.  
  265. private:
  266. //member pointers
  267. gc_ptr_collection m_member_ptrs;
  268.  
  269. friend class gc;
  270. template <class T> friend class gc_member_ptr;
  271. };
  272.  
  273.  
  274. //A member ptr.
  275. template <class T> class gc_member_ptr : public gc_ptr_base {
  276. public:
  277. //The default constructor.
  278. gc_member_ptr(gc_object *owner, T *object = nullptr) :
  279. gc_ptr_base(&owner->m_member_ptrs, object)
  280. {
  281. }
  282.  
  283. //The copy constructor.
  284. gc_member_ptr(gc_object *owner, const gc_member_ptr<T> &ptr) :
  285. gc_ptr_base(&owner->m_member_ptrs, ptr.m_object)
  286. {
  287. }
  288.  
  289. //The move constructor.
  290. gc_member_ptr(gc_object *owner, gc_member_ptr<T> &&ptr) :
  291. gc_ptr_base(&owner->m_member_ptrs, ptr.m_object)
  292. {
  293. }
  294.  
  295. //Assignment from object.
  296. gc_member_ptr<T> &operator = (T *object) {
  297. m_object = object;
  298. return *this;
  299. }
  300.  
  301. //Assignment from pointer.
  302. gc_member_ptr<T> &operator = (const gc_member_ptr<T> &ptr) {
  303. m_object = ptr.m_object;
  304. return *this;
  305. }
  306.  
  307. //Returns the value of the pointer.
  308. T *get() const {
  309. assert(m_object || static_cast<T *>(m_object) == nullptr);
  310. return static_cast<T *>(m_object);
  311. }
  312.  
  313. //Auto conversion to ptr value.
  314. operator T* () const {
  315. return get();
  316. }
  317.  
  318. //Accesses the underlying object's members.
  319. T *operator ->() const {
  320. assert(m_object);
  321. return get();
  322. }
  323. };
  324.  
  325.  
  326. #endif //GC_HPP
  327.  
  328.  
  329. //delete all objects
  330. void gc_object_collection::delete_objects() {
  331. while (!m_objects.empty())
  332. delete m_objects.back();
  333. }
  334.  
  335.  
  336. //collects garbage, using the mark & sweep algorithm
  337. void gc::collect() {
  338. gc_object_collection_ptr reachable{std::make_shared<gc_object_collection>()};
  339. _scan(*get_root_ptrs(), *get_objects().get(), *reachable.get());
  340. get_objects()->delete_objects();
  341. std::swap(get_objects(), reachable);
  342. }
  343.  
  344.  
  345. //scan ptrs
  346. void gc::_scan(gc_ptr_collection &ptrs, gc_object_collection &old, gc_object_collection &reached) {
  347. for(gc_ptr_base *ptr : ptrs.m_objects) {
  348. if (ptr->m_object && ptr->m_object->m_collection == &old) {
  349. old.remove(ptr->m_object);
  350. reached.add(ptr->m_object);
  351. _scan(ptr->m_object->m_member_ptrs, old, reached);
  352. }
  353. }
  354. }
  355.  
  356. // test code
  357. #include <random>
  358. #include <iostream>
  359. #include <new>
  360.  
  361. size_t objects = 0;
  362. class Cell;
  363. typedef gc_ptr<Cell> CellPtr;
  364.  
  365. class Cell : public gc_object {
  366. public:
  367. Cell(size_t tag) : tag(tag), next(nullptr) {
  368. ++objects;
  369. }
  370.  
  371. ~Cell() {
  372. --objects;
  373. }
  374.  
  375. size_t tag;
  376. CellPtr next;
  377.  
  378. void mangle() {
  379. if (next && next->next) {
  380. next = next->next;
  381. next->next->mangle();
  382. }
  383. }
  384.  
  385. };
  386.  
  387. CellPtr create_cell_list(size_t length) {
  388. gc_ptr<Cell> c = nullptr;
  389. if (length != 0) {
  390. c = new Cell(length);//make_collected<Cell>(length);
  391. c->next = create_cell_list(length-1);
  392. }
  393. return c;
  394. }
  395.  
  396. // try to allocate a gc_ptr<T>, running a garbage collection if we run out of memory.
  397. template <class T, class ...Args>
  398. gc_ptr<T> make_collected(Args &&...args) {
  399. static_assert(std::is_base_of<gc_object, T>::value, "");
  400. void *ptr = ::operator new(sizeof(T), std::nothrow);
  401. if (ptr == nullptr) {
  402. gc::collect();
  403. std::cout << "[Running automatic garbage collection]\n";
  404. // don't pass nothrow so that we throw bad_alloc on failure
  405. ptr = ::operator new(sizeof(T));
  406. }
  407. try {
  408. new (ptr) T(std::forward<Args>(args)...);
  409. } catch (...) {
  410. ::operator delete(ptr);
  411. throw;
  412. }
  413. return gc_ptr<T>{static_cast<T*>(ptr)};
  414. }
  415.  
  416. CellPtr create_cell_list_autocollect(size_t length) {
  417. gc_ptr<Cell> c = nullptr;
  418. if (length != 0) {
  419. c = make_collected<Cell>(length);
  420. c->next = create_cell_list(length-1);
  421. }
  422. return c;
  423. }
  424.  
  425. void test_cycle_collection() {
  426. std::cout << "Testing cycle collection\n"
  427. " number of live objects (pretest): " << objects << '\n';
  428.  
  429. CellPtr list = create_cell_list(100);
  430. std::cout << " number of live objects (initial): " << objects << '\n';
  431. // find the last Cell, and make it's next point to the first element
  432. {
  433. Cell *l = list.get();
  434. while (l && l->next) {
  435. l = l->next;
  436. }
  437. l->next = l;
  438. }
  439. std::cout << " collecting\n";
  440. gc::collect();
  441. std::cout << " number of live objects (after collection): " << objects << '\n';
  442. }
  443.  
  444.  
  445. void test_dropped_pointers() {
  446. std::cout << "Testing dropped pointers (single)\n"
  447. " number of live objects (pretest): " << objects << '\n';
  448.  
  449. CellPtr list = create_cell_list(100);
  450.  
  451. std::cout << " number of live objects (initial): " << objects << '\n';
  452. list->mangle();
  453.  
  454. std::cout << " collecting\n";
  455. gc::collect();
  456.  
  457. std::cout << " number of live objects (after collection): " << objects << '\n';
  458. }
  459.  
  460. void test_dropped_pointers_stress() {
  461. std::cout << "Testing dropped pointers (multiple, autocollecting)\n";
  462.  
  463. CellPtr list;
  464. for (size_t i = 0; i < 10; ++i) {
  465. std::cout << " number of live objects (pre " << i << "): " << objects << '\n';
  466. for (size_t j = 0; j < 100; ++j) {
  467. list = create_cell_list_autocollect(1000);
  468. list->mangle();
  469. }
  470. std::cout << " number of live objects (post " << i << "): " << objects << '\n';
  471. }
  472.  
  473. std::cout << " number of live objects (before collection): " << objects << '\n';
  474. gc::collect();
  475. std::cout << " number of live objects (after collection): " << objects << '\n';
  476. }
  477.  
  478.  
  479. int main() {
  480. test_cycle_collection();
  481. test_dropped_pointers();
  482. test_dropped_pointers_stress();
  483. return 0;
  484. }
  485.  
  486.  
  487.  
  488.  
  489.  
Runtime error #stdin #stdout 0s 3484KB
stdin
Standard input is empty
stdout
Standard output is empty