PageRenderTime 86ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/rel-1.3.35/Lib/ruby/rubytracking.swg

#
Unknown | 160 lines | 132 code | 28 blank | 0 comment | 0 complexity | ad628af07c663ea199726858fb7ae52d MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* -----------------------------------------------------------------------------
  2. * See the LICENSE file for information on copyright, usage and redistribution
  3. * of SWIG, and the README file for authors - http://www.swig.org/release.html.
  4. *
  5. * rubytracking.swg
  6. *
  7. * This file contains support for tracking mappings from
  8. * Ruby objects to C++ objects. This functionality is needed
  9. * to implement mark functions for Ruby's mark and sweep
  10. * garbage collector.
  11. * ----------------------------------------------------------------------------- */
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. /* Ruby 1.8 actually assumes the first case. */
  16. #if SIZEOF_VOIDP == SIZEOF_LONG
  17. # define SWIG2NUM(v) LONG2NUM((unsigned long)v)
  18. # define NUM2SWIG(x) (unsigned long)NUM2LONG(x)
  19. #elif SIZEOF_VOIDP == SIZEOF_LONG_LONG
  20. # define SWIG2NUM(v) LL2NUM((unsigned long long)v)
  21. # define NUM2SWIG(x) (unsigned long long)NUM2LL(x)
  22. #else
  23. # error sizeof(void*) is not the same as long or long long
  24. #endif
  25. /* Global Ruby hash table to store Trackings from C/C++
  26. structs to Ruby Objects.
  27. */
  28. static VALUE swig_ruby_trackings = Qnil;
  29. /* Global variable that stores a reference to the ruby
  30. hash table delete function. */
  31. static ID swig_ruby_hash_delete;
  32. /* Setup a Ruby hash table to store Trackings */
  33. SWIGRUNTIME void SWIG_RubyInitializeTrackings(void) {
  34. /* Create a ruby hash table to store Trackings from C++
  35. objects to Ruby objects. */
  36. /* Try to see if some other .so has already created a
  37. tracking hash table, which we keep hidden in an instance var
  38. in the SWIG module.
  39. This is done to allow multiple DSOs to share the same
  40. tracking table.
  41. */
  42. ID trackings_id = rb_intern( "@__trackings__" );
  43. VALUE verbose = rb_gv_get("VERBOSE");
  44. rb_gv_set("VERBOSE", Qfalse);
  45. swig_ruby_trackings = rb_ivar_get( _mSWIG, trackings_id );
  46. rb_gv_set("VERBOSE", verbose);
  47. /* No, it hasn't. Create one ourselves */
  48. if ( swig_ruby_trackings == Qnil )
  49. {
  50. swig_ruby_trackings = rb_hash_new();
  51. rb_ivar_set( _mSWIG, trackings_id, swig_ruby_trackings );
  52. }
  53. /* Now store a reference to the hash table delete function
  54. so that we only have to look it up once.*/
  55. swig_ruby_hash_delete = rb_intern("delete");
  56. }
  57. /* Get a Ruby number to reference a pointer */
  58. SWIGRUNTIME VALUE SWIG_RubyPtrToReference(void* ptr) {
  59. /* We cast the pointer to an unsigned long
  60. and then store a reference to it using
  61. a Ruby number object. */
  62. /* Convert the pointer to a Ruby number */
  63. return SWIG2NUM(ptr);
  64. }
  65. /* Get a Ruby number to reference an object */
  66. SWIGRUNTIME VALUE SWIG_RubyObjectToReference(VALUE object) {
  67. /* We cast the object to an unsigned long
  68. and then store a reference to it using
  69. a Ruby number object. */
  70. /* Convert the Object to a Ruby number */
  71. return SWIG2NUM(object);
  72. }
  73. /* Get a Ruby object from a previously stored reference */
  74. SWIGRUNTIME VALUE SWIG_RubyReferenceToObject(VALUE reference) {
  75. /* The provided Ruby number object is a reference
  76. to the Ruby object we want.*/
  77. /* Convert the Ruby number to a Ruby object */
  78. return NUM2SWIG(reference);
  79. }
  80. /* Add a Tracking from a C/C++ struct to a Ruby object */
  81. SWIGRUNTIME void SWIG_RubyAddTracking(void* ptr, VALUE object) {
  82. /* In a Ruby hash table we store the pointer and
  83. the associated Ruby object. The trick here is
  84. that we cannot store the Ruby object directly - if
  85. we do then it cannot be garbage collected. So
  86. instead we typecast it as a unsigned long and
  87. convert it to a Ruby number object.*/
  88. /* Get a reference to the pointer as a Ruby number */
  89. VALUE key = SWIG_RubyPtrToReference(ptr);
  90. /* Get a reference to the Ruby object as a Ruby number */
  91. VALUE value = SWIG_RubyObjectToReference(object);
  92. /* Store the mapping to the global hash table. */
  93. rb_hash_aset(swig_ruby_trackings, key, value);
  94. }
  95. /* Get the Ruby object that owns the specified C/C++ struct */
  96. SWIGRUNTIME VALUE SWIG_RubyInstanceFor(void* ptr) {
  97. /* Get a reference to the pointer as a Ruby number */
  98. VALUE key = SWIG_RubyPtrToReference(ptr);
  99. /* Now lookup the value stored in the global hash table */
  100. VALUE value = rb_hash_aref(swig_ruby_trackings, key);
  101. if (value == Qnil) {
  102. /* No object exists - return nil. */
  103. return Qnil;
  104. }
  105. else {
  106. /* Convert this value to Ruby object */
  107. return SWIG_RubyReferenceToObject(value);
  108. }
  109. }
  110. /* Remove a Tracking from a C/C++ struct to a Ruby object. It
  111. is very important to remove objects once they are destroyed
  112. since the same memory address may be reused later to create
  113. a new object. */
  114. SWIGRUNTIME void SWIG_RubyRemoveTracking(void* ptr) {
  115. /* Get a reference to the pointer as a Ruby number */
  116. VALUE key = SWIG_RubyPtrToReference(ptr);
  117. /* Delete the object from the hash table by calling Ruby's
  118. do this we need to call the Hash.delete method.*/
  119. rb_funcall(swig_ruby_trackings, swig_ruby_hash_delete, 1, key);
  120. }
  121. /* This is a helper method that unlinks a Ruby object from its
  122. underlying C++ object. This is needed if the lifetime of the
  123. Ruby object is longer than the C++ object */
  124. SWIGRUNTIME void SWIG_RubyUnlinkObjects(void* ptr) {
  125. VALUE object = SWIG_RubyInstanceFor(ptr);
  126. if (object != Qnil) {
  127. DATA_PTR(object) = 0;
  128. }
  129. }
  130. #ifdef __cplusplus
  131. }
  132. #endif