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