2017-10-03 05:24:00 +08:00
|
|
|
/**************************************************************************/
|
|
|
|
/* mono_gc_handle.h */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
2021-09-13 02:23:05 +08:00
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
2017-10-03 05:24:00 +08: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 07:50:27 +08:00
|
|
|
|
2022-07-24 05:41:51 +08:00
|
|
|
#ifndef MONO_GC_HANDLE_H
|
|
|
|
#define MONO_GC_HANDLE_H
|
2017-10-03 05:24:00 +08:00
|
|
|
|
2021-06-05 00:03:15 +08:00
|
|
|
#include "core/object/ref_counted.h"
|
2017-10-03 05:24:00 +08:00
|
|
|
|
2019-12-12 00:08:40 +08:00
|
|
|
namespace gdmono {
|
2017-10-03 05:24:00 +08:00
|
|
|
|
2019-12-12 00:08:40 +08:00
|
|
|
enum class GCHandleType : char {
|
|
|
|
NIL,
|
|
|
|
STRONG_HANDLE,
|
|
|
|
WEAK_HANDLE
|
|
|
|
};
|
|
|
|
}
|
2020-03-15 02:20:17 +08:00
|
|
|
|
2021-09-13 02:23:05 +08:00
|
|
|
extern "C" {
|
2021-09-13 02:21:15 +08:00
|
|
|
struct GCHandleIntPtr {
|
2022-02-28 04:57:30 +08: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-13 02:21:15 +08:00
|
|
|
};
|
2021-09-13 02:23:05 +08:00
|
|
|
}
|
2021-09-13 02:21:15 +08:00
|
|
|
|
|
|
|
static_assert(sizeof(GCHandleIntPtr) == sizeof(void *));
|
|
|
|
|
2019-12-12 00:08:40 +08:00
|
|
|
// Manual release of the GC handle must be done when using this struct
|
|
|
|
struct MonoGCHandleData {
|
2022-02-28 04:57:30 +08:00
|
|
|
GCHandleIntPtr handle = { nullptr };
|
2020-05-12 23:01:17 +08:00
|
|
|
gdmono::GCHandleType type = gdmono::GCHandleType::NIL;
|
2017-10-03 05:24:00 +08:00
|
|
|
|
2021-09-13 02:21:15 +08:00
|
|
|
_FORCE_INLINE_ bool is_released() const { return !handle.value; }
|
2019-12-12 00:08:40 +08:00
|
|
|
_FORCE_INLINE_ bool is_weak() const { return type == gdmono::GCHandleType::WEAK_HANDLE; }
|
2021-09-13 02:21:15 +08:00
|
|
|
_FORCE_INLINE_ GCHandleIntPtr get_intptr() const { return handle; }
|
2017-10-03 05:24:00 +08:00
|
|
|
|
2019-12-12 00:08:40 +08:00
|
|
|
void release();
|
2017-10-03 05:24:00 +08:00
|
|
|
|
2021-09-13 02:21:15 +08:00
|
|
|
static void free_gchandle(GCHandleIntPtr p_gchandle);
|
|
|
|
|
2021-11-30 22:19:26 +08:00
|
|
|
void operator=(const MonoGCHandleData &p_other) {
|
2019-12-12 00:08:40 +08:00
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
CRASH_COND(!is_released());
|
|
|
|
#endif
|
|
|
|
handle = p_other.handle;
|
|
|
|
type = p_other.type;
|
|
|
|
}
|
2018-02-22 22:34:08 +08:00
|
|
|
|
2019-12-12 00:08:40 +08:00
|
|
|
MonoGCHandleData(const MonoGCHandleData &) = default;
|
2017-10-03 05:24:00 +08:00
|
|
|
|
2020-05-12 23:01:17 +08:00
|
|
|
MonoGCHandleData() {}
|
2017-10-03 05:24:00 +08:00
|
|
|
|
2021-09-13 02:21:15 +08:00
|
|
|
MonoGCHandleData(GCHandleIntPtr p_handle, gdmono::GCHandleType p_type) :
|
2019-12-12 00:08:40 +08:00
|
|
|
handle(p_handle),
|
|
|
|
type(p_type) {
|
|
|
|
}
|
2017-10-03 05:24:00 +08:00
|
|
|
};
|
|
|
|
|
2022-07-24 05:41:51 +08:00
|
|
|
#endif // MONO_GC_HANDLE_H
|