kernel.h 3.6 KB

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