mono_gc_handle.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*************************************************************************/
  2. /* mono_gc_handle.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef CSHARP_GC_HANDLE_H
  31. #define CSHARP_GC_HANDLE_H
  32. #include <mono/jit/jit.h>
  33. #include "core/object/reference.h"
  34. namespace gdmono {
  35. enum class GCHandleType : char {
  36. NIL,
  37. STRONG_HANDLE,
  38. WEAK_HANDLE
  39. };
  40. }
  41. // Manual release of the GC handle must be done when using this struct
  42. struct MonoGCHandleData {
  43. uint32_t handle = 0;
  44. gdmono::GCHandleType type = gdmono::GCHandleType::NIL;
  45. _FORCE_INLINE_ bool is_released() const { return !handle; }
  46. _FORCE_INLINE_ bool is_weak() const { return type == gdmono::GCHandleType::WEAK_HANDLE; }
  47. _FORCE_INLINE_ MonoObject *get_target() const { return handle ? mono_gchandle_get_target(handle) : nullptr; }
  48. void release();
  49. MonoGCHandleData &operator=(const MonoGCHandleData &p_other) {
  50. #ifdef DEBUG_ENABLED
  51. CRASH_COND(!is_released());
  52. #endif
  53. handle = p_other.handle;
  54. type = p_other.type;
  55. return *this;
  56. }
  57. MonoGCHandleData(const MonoGCHandleData &) = default;
  58. MonoGCHandleData() {}
  59. MonoGCHandleData(uint32_t p_handle, gdmono::GCHandleType p_type) :
  60. handle(p_handle),
  61. type(p_type) {
  62. }
  63. static MonoGCHandleData new_strong_handle(MonoObject *p_object);
  64. static MonoGCHandleData new_strong_handle_pinned(MonoObject *p_object);
  65. static MonoGCHandleData new_weak_handle(MonoObject *p_object);
  66. };
  67. class MonoGCHandleRef : public Reference {
  68. GDCLASS(MonoGCHandleRef, Reference);
  69. MonoGCHandleData data;
  70. public:
  71. static Ref<MonoGCHandleRef> create_strong(MonoObject *p_object);
  72. static Ref<MonoGCHandleRef> create_weak(MonoObject *p_object);
  73. _FORCE_INLINE_ bool is_released() const { return data.is_released(); }
  74. _FORCE_INLINE_ bool is_weak() const { return data.is_weak(); }
  75. _FORCE_INLINE_ MonoObject *get_target() const { return data.get_target(); }
  76. void release() { data.release(); }
  77. _FORCE_INLINE_ void set_handle(uint32_t p_handle, gdmono::GCHandleType p_handle_type) {
  78. data = MonoGCHandleData(p_handle, p_handle_type);
  79. }
  80. MonoGCHandleRef(const MonoGCHandleData &p_gc_handle_data) :
  81. data(p_gc_handle_data) {
  82. }
  83. ~MonoGCHandleRef() { release(); }
  84. };
  85. #endif // CSHARP_GC_HANDLE_H