kernel.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #pragma once
  2. #include <pocketpy.h>
  3. namespace pybind11
  4. {
  5. inline pkpy::VM* vm = nullptr;
  6. inline std::map<pkpy::PyVar, int*>* _ref_counts_map = nullptr;
  7. inline void initialize(bool enable_os = true)
  8. {
  9. vm = new pkpy::VM(enable_os);
  10. _ref_counts_map = new std::map<pkpy::PyVar, int*>();
  11. // use to keep alive PyObject, when the object is hold by C++ side.
  12. vm->heap._gc_marker_ex = [](pkpy::VM* vm)
  13. {
  14. for(auto iter = _ref_counts_map->begin(); iter != _ref_counts_map->end();)
  15. {
  16. auto ref_count = iter->second;
  17. if(*ref_count != 0)
  18. {
  19. // if ref count is not zero, then mark it.
  20. PK_OBJ_MARK(iter->first);
  21. ++iter;
  22. }
  23. else
  24. {
  25. // if ref count is zero, then delete it.
  26. iter = _ref_counts_map->erase(iter);
  27. delete ref_count;
  28. }
  29. }
  30. };
  31. }
  32. inline void finalize()
  33. {
  34. delete _ref_counts_map;
  35. delete vm;
  36. }
  37. enum class return_value_policy : uint8_t
  38. {
  39. /**
  40. * This is the default return value policy, which falls back to the policy
  41. * return_value_policy::take_ownership when the return value is a pointer.
  42. * Otherwise, it uses return_value::move or return_value::copy for rvalue
  43. * and lvalue references, respectively. See below for a description of what
  44. * all of these different policies do.
  45. */
  46. automatic = 0,
  47. /**
  48. * As above, but use policy return_value_policy::reference when the return
  49. * value is a pointer. This is the default conversion policy for function
  50. * arguments when calling Python functions manually from C++ code (i.e. via
  51. * handle::operator()). You probably won't need to use this.
  52. */
  53. automatic_reference,
  54. /**
  55. * Reference an existing object (i.e. do not create a new copy) and take
  56. * ownership. Python will call the destructor and delete operator when the
  57. * object's reference count reaches zero. Undefined behavior ensues when
  58. * the C++ side does the same..
  59. */
  60. take_ownership,
  61. /**
  62. * Create a new copy of the returned object, which will be owned by
  63. * Python. This policy is comparably safe because the lifetimes of the two
  64. * instances are decoupled.
  65. */
  66. copy,
  67. /**
  68. * Use std::move to move the return value contents into a new instance
  69. * that will be owned by Python. This policy is comparably safe because the
  70. * lifetimes of the two instances (move source and destination) are
  71. * decoupled.
  72. */
  73. move,
  74. /**
  75. * Reference an existing object, but do not take ownership. The C++ side
  76. * is responsible for managing the object's lifetime and deallocating it
  77. * when it is no longer used. Warning: undefined behavior will ensue when
  78. * the C++ side deletes an object that is still referenced and used by
  79. * Python.
  80. */
  81. reference,
  82. /**
  83. * This policy only applies to methods and properties. It references the
  84. * object without taking ownership similar to the above
  85. * return_value_policy::reference policy. In contrast to that policy, the
  86. * function or property's implicit this argument (called the parent) is
  87. * considered to be the the owner of the return value (the child).
  88. * pybind11 then couples the lifetime of the parent to the child via a
  89. * reference relationship that ensures that the parent cannot be garbage
  90. * collected while Python is still using the child. More advanced
  91. * variations of this scheme are also possible using combinations of
  92. * return_value_policy::reference and the keep_alive call policy
  93. */
  94. reference_internal
  95. };
  96. } // namespace pybind11