2017-10-02 23:24:00 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* mono_gc_handle.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2021-09-12 20:23:05 +02:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2017-10-02 23:24:00 +02:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2022-07-23 23:41:51 +02:00
|
|
|
#ifndef MONO_GC_HANDLE_H
|
|
|
|
#define MONO_GC_HANDLE_H
|
2017-10-02 23:24:00 +02:00
|
|
|
|
2021-06-04 18:03:15 +02:00
|
|
|
#include "core/object/ref_counted.h"
|
2017-10-02 23:24:00 +02:00
|
|
|
|
2019-12-11 17:08:40 +01:00
|
|
|
namespace gdmono {
|
2017-10-02 23:24:00 +02:00
|
|
|
|
2019-12-11 17:08:40 +01:00
|
|
|
enum class GCHandleType : char {
|
|
|
|
NIL,
|
|
|
|
STRONG_HANDLE,
|
|
|
|
WEAK_HANDLE
|
|
|
|
};
|
|
|
|
}
|
2020-03-14 19:20:17 +01:00
|
|
|
|
2021-09-12 20:23:05 +02:00
|
|
|
extern "C" {
|
2021-09-12 20:21:15 +02:00
|
|
|
struct GCHandleIntPtr {
|
2022-02-27 21:57:30 +01:00
|
|
|
void *value;
|
|
|
|
|
|
|
|
_FORCE_INLINE_ bool operator==(const GCHandleIntPtr &p_other) { return value == p_other.value; }
|
|
|
|
_FORCE_INLINE_ bool operator!=(const GCHandleIntPtr &p_other) { return value != p_other.value; }
|
|
|
|
|
|
|
|
GCHandleIntPtr() = delete;
|
2021-09-12 20:21:15 +02:00
|
|
|
};
|
2021-09-12 20:23:05 +02:00
|
|
|
}
|
2021-09-12 20:21:15 +02:00
|
|
|
|
|
|
|
static_assert(sizeof(GCHandleIntPtr) == sizeof(void *));
|
|
|
|
|
2019-12-11 17:08:40 +01:00
|
|
|
// Manual release of the GC handle must be done when using this struct
|
|
|
|
struct MonoGCHandleData {
|
2022-02-27 21:57:30 +01:00
|
|
|
GCHandleIntPtr handle = { nullptr };
|
2020-05-12 17:01:17 +02:00
|
|
|
gdmono::GCHandleType type = gdmono::GCHandleType::NIL;
|
2017-10-02 23:24:00 +02:00
|
|
|
|
2021-09-12 20:21:15 +02:00
|
|
|
_FORCE_INLINE_ bool is_released() const { return !handle.value; }
|
2019-12-11 17:08:40 +01:00
|
|
|
_FORCE_INLINE_ bool is_weak() const { return type == gdmono::GCHandleType::WEAK_HANDLE; }
|
2021-09-12 20:21:15 +02:00
|
|
|
_FORCE_INLINE_ GCHandleIntPtr get_intptr() const { return handle; }
|
2017-10-02 23:24:00 +02:00
|
|
|
|
2019-12-11 17:08:40 +01:00
|
|
|
void release();
|
2017-10-02 23:24:00 +02:00
|
|
|
|
2021-09-12 20:21:15 +02:00
|
|
|
static void free_gchandle(GCHandleIntPtr p_gchandle);
|
|
|
|
|
2021-11-30 15:19:26 +01:00
|
|
|
void operator=(const MonoGCHandleData &p_other) {
|
2019-12-11 17:08:40 +01:00
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
CRASH_COND(!is_released());
|
|
|
|
#endif
|
|
|
|
handle = p_other.handle;
|
|
|
|
type = p_other.type;
|
|
|
|
}
|
2018-02-22 15:34:08 +01:00
|
|
|
|
2019-12-11 17:08:40 +01:00
|
|
|
MonoGCHandleData(const MonoGCHandleData &) = default;
|
2017-10-02 23:24:00 +02:00
|
|
|
|
2020-05-12 17:01:17 +02:00
|
|
|
MonoGCHandleData() {}
|
2017-10-02 23:24:00 +02:00
|
|
|
|
2021-09-12 20:21:15 +02:00
|
|
|
MonoGCHandleData(GCHandleIntPtr p_handle, gdmono::GCHandleType p_type) :
|
2019-12-11 17:08:40 +01:00
|
|
|
handle(p_handle),
|
|
|
|
type(p_type) {
|
|
|
|
}
|
2017-10-02 23:24:00 +02:00
|
|
|
};
|
|
|
|
|
2022-07-23 23:41:51 +02:00
|
|
|
#endif // MONO_GC_HANDLE_H
|