Guest User

Untitled

a guest
May 24th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.51 KB | None | 0 0
  1. ## old/vm/gc_roots.hpp
  2.  
  3. #ifndef RBX_GC_ROOT_HPP
  4. #define RBX_GC_ROOT_HPP
  5.  
  6. #include "prelude.hpp"
  7. #include "oop.hpp"
  8. #include <stdexcept>
  9.  
  10. #include "linkedlist.hpp"
  11.  
  12. namespace rubinius {
  13. class Root;
  14.  
  15. class Roots : public LinkedList {
  16. public:
  17. Roots(size_t _dummy = 0) : LinkedList() { }
  18. Root* front();
  19. };
  20.  
  21. class Root : public LinkedList::Node {
  22. protected:
  23. OBJECT object;
  24.  
  25. private:
  26. Roots* roots;
  27.  
  28. public:
  29. Root(Roots* roots, OBJECT obj) : object(obj), roots(roots) {
  30. roots->add(this);
  31. }
  32.  
  33. Root(Roots* roots) : object(NULL), roots(roots) { }
  34.  
  35. Root(STATE);
  36. Root(STATE, OBJECT obj);
  37.  
  38. explicit Root() : object(NULL), roots(NULL) { }
  39.  
  40. Root(const Root& other) {
  41. set(other.object, other.roots);
  42. }
  43.  
  44. ~Root() {
  45. if(roots && object) roots->remove(this);
  46. }
  47.  
  48. OBJECT get() {
  49. if(object) {
  50. return object;
  51. } else {
  52. return Qnil;
  53. }
  54. }
  55.  
  56. void set(OBJECT obj, Roots* r) {
  57. // Still in the same set, no problem, just repoint
  58. // object.
  59. if(roots == r) {
  60.  
  61. // We don't add the root until it's got an object.
  62. if(!object) roots->add(this);
  63. object = obj;
  64.  
  65. // Moving to a new set. Remove ourselves from
  66. // the current set if we added ourself (we have an
  67. // object)
  68. } else {
  69. if(object) roots->remove(this);
  70. object = obj;
  71. roots = r;
  72. if(object) roots->add(this);
  73. }
  74. }
  75.  
  76. void set(OBJECT obj) {
  77. if(!roots) {
  78. throw std::runtime_error("invalid Root usage. Set object before roots");
  79. }
  80.  
  81. set(obj, roots);
  82. }
  83.  
  84. void set(Roots* r) {
  85. set(object, roots);
  86. }
  87.  
  88. Root& operator=(Root& other) {
  89. set(other.object, other.roots);
  90. return *this;
  91. }
  92. };
  93.  
  94. template <typename T>
  95. class TypedRoot : public Root {
  96. public:
  97. T operator->() {
  98. if(object) {
  99. return (T)object;
  100. } else {
  101. return (T)Qnil;
  102. }
  103. }
  104.  
  105. TypedRoot() : Root() { }
  106. TypedRoot(Roots* roots) : Root(roots) { }
  107. TypedRoot(STATE) : Root(state) { }
  108. TypedRoot(STATE, T obj) : Root(state, obj) { }
  109. T get() { return (T)object; }
  110. };
  111. }
  112.  
  113. #endif
  114.  
  115.  
  116. ## new/vm/gc_root.hpp
  117.  
  118. #ifndef RBX_GC_ROOT_HPP
  119. #define RBX_GC_ROOT_HPP
  120.  
  121.  
  122. #include <stdexcept>
  123.  
  124. #include "vm/linkedlist.hpp"
  125. #include "vm/object.hpp"
  126. #include "vm/oop.hpp"
  127. #include "vm/prelude.hpp"
  128.  
  129.  
  130. namespace rubinius {
  131.  
  132. class Root;
  133.  
  134.  
  135. /**
  136. * Roots is a structure comprising of Root objects.
  137. *
  138. * TODO: Add more information about class. --rue
  139. * TODO: Document methods. --rue
  140. */
  141. class Roots : public LinkedList {
  142. public: /* Ctors */
  143.  
  144. Roots();
  145.  
  146.  
  147. public: /* Interface */
  148.  
  149. Root* front();
  150. };
  151.  
  152.  
  153. /**
  154. * A Root envelops an Object.
  155. *
  156. * Each Root is also associated with a certain Roots structure
  157. * and could be used to access its other members. The Root can
  158. * be associated with (or "migrated to") a different Roots.
  159. *
  160. * TODO: Document remaining methods. --rue
  161. */
  162. class Root : public LinkedList::Node {
  163. public: /* Ctors */
  164.  
  165. /** Default-constructed root is blank. */
  166. explicit Root();
  167.  
  168. Root(STATE);
  169. Root(STATE, OBJECT obj);
  170. Root(Roots* roots);
  171. Root(Roots* roots, OBJECT obj);
  172.  
  173. /** Copy construction uses set() semantics. */
  174. Root(const Root& other);
  175.  
  176. /** Assignment uses set() semantics. */
  177. Root& operator=(Root& other);
  178.  
  179. /** Destruction removes this one from the roots. */
  180. ~Root();
  181.  
  182.  
  183. public: /* Interface */
  184.  
  185. /** Obtain the enveloped Object or Qnil if none. */
  186. OBJECT get();
  187.  
  188. /**
  189. * Redoes the set of the existing Object and Roots.
  190. *
  191. * TODO: Review. This method makes no sense to me. --rue
  192. */
  193. void set(Roots* r);
  194.  
  195. /** Envelope the given Object. Must have roots already. */
  196. void set(OBJECT obj);
  197.  
  198. /** Envelope the given Object, migrating to given Roots if it is new. */
  199. void set(OBJECT obj, Roots* r);
  200.  
  201.  
  202. protected: /* Instance vars */
  203.  
  204. /** Enveloped Object. */
  205. OBJECT object;
  206.  
  207.  
  208. private: /* Instance vars */
  209.  
  210. /** The Roots structure this Root belongs to. */
  211. Roots* roots;
  212. };
  213.  
  214.  
  215. /**
  216. * TypedRoot is a Root that knows the type of its Object.
  217. *
  218. * TODO: Use base type of object rather than pointer type
  219. * as ObjType and change usage accordingly? This
  220. * allows, among other things, using `as()` rather
  221. * than a direct C++ cast. --rue
  222. */
  223. template <typename ObjType>
  224. class TypedRoot : public Root {
  225. public: /* Ctors */
  226.  
  227. /** Default construction gives an empty TypedRoot. */
  228. explicit TypedRoot();
  229.  
  230. /** As Root::Root(STATE), but retains object's type. */
  231. TypedRoot(STATE);
  232.  
  233. /** As Root::Root(STATE, OBJECT), but retains object's type. */
  234. TypedRoot(STATE, ObjType obj);
  235.  
  236. /** As Root::Root(roots), but retains object's type. */
  237. TypedRoot(Roots* roots);
  238.  
  239.  
  240. public: /* Operators */
  241.  
  242. /** Transparently delegate dereferencing to enveloped object. */
  243. ObjType operator->();
  244.  
  245.  
  246. public: /* Interface */
  247.  
  248. /** Return the enveloped object as the real ObjType. */
  249. ObjType get();
  250. };
  251.  
  252.  
  253. /* Roots inlines */
  254.  
  255. inline Roots::Roots():
  256. LinkedList()
  257. { }
  258.  
  259.  
  260. /* Root inlines */
  261.  
  262.  
  263. inline Root::Root():
  264. LinkedList::Node(), object(NULL), roots(NULL)
  265. { }
  266.  
  267. inline Root::Root(Roots* roots):
  268. LinkedList::Node(), object(NULL), roots(roots)
  269. { }
  270.  
  271. inline Root::Root(Roots* roots, OBJECT obj):
  272. LinkedList::Node(), object(obj), roots(roots)
  273. {
  274. roots->add(this);
  275. }
  276.  
  277. inline Root::Root(const Root& other):
  278. LinkedList::Node(), object(NULL), roots(NULL)
  279. {
  280. set(other.object, other.roots);
  281. }
  282.  
  283. inline Root::~Root() {
  284. if(roots && object) {
  285. roots->remove(this);
  286. }
  287. }
  288.  
  289. inline Root& Root::operator=(Root& other) {
  290. set(other.object, other.roots);
  291. return *this;
  292. }
  293.  
  294. inline OBJECT Root::get() {
  295. if(object) {
  296. return object;
  297. } else {
  298. return Qnil;
  299. }
  300. }
  301.  
  302. inline void Root::set(Roots* /* other_roots */) {
  303. set(object, roots);
  304. }
  305.  
  306. inline void Root::set(OBJECT obj) {
  307. if(!roots) {
  308. throw std::runtime_error("invalid Root usage. Cannot set object before roots");
  309. }
  310.  
  311. set(obj, roots);
  312. }
  313.  
  314.  
  315. /* TypedRoot inlines */
  316.  
  317. template<typename ObjType>
  318. inline TypedRoot<ObjType>::TypedRoot():
  319. Root()
  320. { }
  321.  
  322. template<typename ObjType>
  323. inline TypedRoot<ObjType>::TypedRoot(Roots* roots):
  324. Root(roots)
  325. { }
  326.  
  327. template<typename ObjType>
  328. inline TypedRoot<ObjType>::TypedRoot(STATE):
  329. Root(state)
  330. { }
  331.  
  332. template<typename ObjType>
  333. inline TypedRoot<ObjType>::TypedRoot(STATE, ObjType obj):
  334. Root(state, obj)
  335. { }
  336.  
  337. /** TODO: Use as<ObjType>() when using base type instead of pointer. --rue */
  338. template<typename ObjType>
  339. inline ObjType TypedRoot<ObjType>::operator->() {
  340. if(object) {
  341. return reinterpret_cast<ObjType>(object);
  342. }
  343. else {
  344. return reinterpret_cast<ObjType>(Qnil);
  345. }
  346. }
  347.  
  348. /** TODO: Use as<ObjType>() when using base type instead of pointer. --rue */
  349. template<typename ObjType>
  350. inline ObjType TypedRoot<ObjType>::get() {
  351. return reinterpret_cast<ObjType>(object);
  352. }
  353.  
  354. }
  355.  
  356. #endif
Add Comment
Please, Sign In to add comment