From 41f61a6ef58d6ad7a7d9625f07203ac890b6211e Mon Sep 17 00:00:00 2001 From: Relintai Date: Mon, 23 Oct 2023 18:31:57 +0200 Subject: [PATCH] Fix Vector and use it instead of std::vector. --- SConstruct | 1 + core/containers/cowdata.h | 2 +- core/containers/sort_array.h | 3 +- core/containers/vector.h | 2 +- core/defs.h | 2 +- core/os/memory.cpp | 201 +++++++++++++++++++++ core/os/memory.h | 204 +++++++++++++++++++++ core/os/safe_refcount.cpp | 45 +++++ core/os/safe_refcount.h | 332 +++++++++++++++++++++++++++++++++++ core/projection.cpp | 6 +- core/projection.h | 4 +- 11 files changed, 792 insertions(+), 10 deletions(-) create mode 100644 core/os/memory.cpp create mode 100644 core/os/memory.h create mode 100644 core/os/safe_refcount.cpp create mode 100644 core/os/safe_refcount.h diff --git a/SConstruct b/SConstruct index d8200a1..8ac8dc3 100644 --- a/SConstruct +++ b/SConstruct @@ -495,6 +495,7 @@ env.Append(CPPPATH=[[env.Dir(d) for d in [".", env["headers_dir"], "gen", "core" # Sources to compile sources = [] add_sources(sources, "core", "cpp") +add_sources(sources, "core/os", "cpp") sources.extend(f for f in bindings if str(f).endswith(".cpp")) arch_suffix = env["bits"] diff --git a/core/containers/cowdata.h b/core/containers/cowdata.h index d5b4d17..8a9dacf 100644 --- a/core/containers/cowdata.h +++ b/core/containers/cowdata.h @@ -32,7 +32,7 @@ #include -#include "core/error/error_macros.h" +#include "core/defs.h" #include "core/os/memory.h" #include "core/os/safe_refcount.h" diff --git a/core/containers/sort_array.h b/core/containers/sort_array.h index 8dea045..c0e2a94 100644 --- a/core/containers/sort_array.h +++ b/core/containers/sort_array.h @@ -30,8 +30,7 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#include "core/error/error_macros.h" -#include "core/typedefs.h" +#include "core/defs.h" #define ERR_BAD_COMPARE(cond) \ if (unlikely(cond)) { \ diff --git a/core/containers/vector.h b/core/containers/vector.h index 1aebcc0..f0439f4 100644 --- a/core/containers/vector.h +++ b/core/containers/vector.h @@ -38,7 +38,7 @@ #include "core/containers/cowdata.h" #include "core/containers/sort_array.h" -#include "core/error/error_macros.h" +#include "core/defs.h" #include "core/os/memory.h" template diff --git a/core/defs.h b/core/defs.h index 66fb7fc..84311d4 100644 --- a/core/defs.h +++ b/core/defs.h @@ -31,7 +31,7 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -enum class Error { +enum Error { OK, FAILED, ///< Generic fail error ERR_UNAVAILABLE, ///< What is requested is unsupported/unavailable diff --git a/core/os/memory.cpp b/core/os/memory.cpp new file mode 100644 index 0000000..b4ab181 --- /dev/null +++ b/core/os/memory.cpp @@ -0,0 +1,201 @@ +/*************************************************************************/ +/* memory.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* 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. */ +/*************************************************************************/ + +#include "memory.h" + +#include "core/defs.h" +#include "core/os/safe_refcount.h" + +#include +#include + +void *operator new(size_t p_size, const char *p_description) { + return Memory::alloc_static(p_size, false); +} + +void *operator new(size_t p_size, void *(*p_allocfunc)(size_t p_size)) { + return p_allocfunc(p_size); +} + +#ifdef _MSC_VER +void operator delete(void *p_mem, const char *p_description) { + CRASH_NOW_MSG("Call to placement delete should not happen."); +} + +void operator delete(void *p_mem, void *(*p_allocfunc)(size_t p_size)) { + CRASH_NOW_MSG("Call to placement delete should not happen."); +} + +void operator delete(void *p_mem, void *p_pointer, size_t check, const char *p_description) { + CRASH_NOW_MSG("Call to placement delete should not happen."); +} +#endif + +#ifdef DEBUG_ENABLED +SafeNumeric Memory::mem_usage; +SafeNumeric Memory::max_usage; +#endif + +SafeNumeric Memory::alloc_count; + +void *Memory::alloc_static(size_t p_bytes, bool p_pad_align) { +#ifdef DEBUG_ENABLED + bool prepad = true; +#else + bool prepad = p_pad_align; +#endif + + void *mem = malloc(p_bytes + (prepad ? PAD_ALIGN : 0)); + + ERR_FAIL_COND_V(!mem, nullptr); + + alloc_count.increment(); + + if (prepad) { + uint64_t *s = (uint64_t *)mem; + *s = p_bytes; + + uint8_t *s8 = (uint8_t *)mem; + +#ifdef DEBUG_ENABLED + uint64_t new_mem_usage = mem_usage.add(p_bytes); + max_usage.exchange_if_greater(new_mem_usage); +#endif + return s8 + PAD_ALIGN; + } else { + return mem; + } +} + +void *Memory::realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align) { + if (p_memory == nullptr) { + return alloc_static(p_bytes, p_pad_align); + } + + uint8_t *mem = (uint8_t *)p_memory; + +#ifdef DEBUG_ENABLED + bool prepad = true; +#else + bool prepad = p_pad_align; +#endif + + if (prepad) { + mem -= PAD_ALIGN; + uint64_t *s = (uint64_t *)mem; + +#ifdef DEBUG_ENABLED + if (p_bytes > *s) { + uint64_t new_mem_usage = mem_usage.add(p_bytes - *s); + max_usage.exchange_if_greater(new_mem_usage); + } else { + mem_usage.sub(*s - p_bytes); + } +#endif + + if (p_bytes == 0) { + free(mem); + return nullptr; + } else { + *s = p_bytes; + + mem = (uint8_t *)realloc(mem, p_bytes + PAD_ALIGN); + ERR_FAIL_COND_V(!mem, nullptr); + + s = (uint64_t *)mem; + + *s = p_bytes; + + return mem + PAD_ALIGN; + } + } else { + mem = (uint8_t *)realloc(mem, p_bytes); + + ERR_FAIL_COND_V(mem == nullptr && p_bytes > 0, nullptr); + + return mem; + } +} + +void Memory::free_static(void *p_ptr, bool p_pad_align) { + ERR_FAIL_COND(p_ptr == nullptr); + + uint8_t *mem = (uint8_t *)p_ptr; + +#ifdef DEBUG_ENABLED + bool prepad = true; +#else + bool prepad = p_pad_align; +#endif + + alloc_count.decrement(); + + if (prepad) { + mem -= PAD_ALIGN; + +#ifdef DEBUG_ENABLED + uint64_t *s = (uint64_t *)mem; + mem_usage.sub(*s); +#endif + + free(mem); + } else { + free(mem); + } +} + +uint64_t Memory::get_mem_available() { + return -1; // 0xFFFF... +} + +uint64_t Memory::get_mem_usage() { +#ifdef DEBUG_ENABLED + return mem_usage.get(); +#else + return 0; +#endif +} + +uint64_t Memory::get_mem_max_usage() { +#ifdef DEBUG_ENABLED + return max_usage.get(); +#else + return 0; +#endif +} + +_GlobalNil::_GlobalNil() { + color = 1; + left = this; + right = this; + parent = this; +} + +_GlobalNil _GlobalNilClass::_nil; diff --git a/core/os/memory.h b/core/os/memory.h new file mode 100644 index 0000000..934a152 --- /dev/null +++ b/core/os/memory.h @@ -0,0 +1,204 @@ +#ifndef MEMORY_H +#define MEMORY_H +/*************************************************************************/ +/* memory.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* 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. */ +/*************************************************************************/ + +#include "core/defs.h" +#include "core/os/safe_refcount.h" + +#include + +#ifndef PAD_ALIGN +#define PAD_ALIGN 16 //must always be greater than this at much +#endif + +class Memory { +#ifdef DEBUG_ENABLED + static SafeNumeric mem_usage; + static SafeNumeric max_usage; +#endif + + static SafeNumeric alloc_count; + +public: + static void *alloc_static(size_t p_bytes, bool p_pad_align = false); + static void *realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align = false); + static void free_static(void *p_ptr, bool p_pad_align = false); + + static uint64_t get_mem_available(); + static uint64_t get_mem_usage(); + static uint64_t get_mem_max_usage(); +}; + +class DefaultAllocator { +public: + _FORCE_INLINE_ static void *alloc(size_t p_memory) { return Memory::alloc_static(p_memory, false); } + _FORCE_INLINE_ static void free(void *p_ptr) { Memory::free_static(p_ptr, false); } +}; + +void *operator new(size_t p_size, const char *p_description); ///< operator new that takes a description and uses MemoryStaticPool +void *operator new(size_t p_size, void *(*p_allocfunc)(size_t p_size)); ///< operator new that takes a description and uses MemoryStaticPool + +void *operator new(size_t p_size, void *p_pointer, size_t check, const char *p_description); ///< operator new that takes a description and uses a pointer to the preallocated memory + +#ifdef _MSC_VER +// When compiling with VC++ 2017, the above declarations of placement new generate many irrelevant warnings (C4291). +// The purpose of the following definitions is to muffle these warnings, not to provide a usable implementation of placement delete. +void operator delete(void *p_mem, const char *p_description); +void operator delete(void *p_mem, void *(*p_allocfunc)(size_t p_size)); +void operator delete(void *p_mem, void *p_pointer, size_t check, const char *p_description); +#endif + +#define memalloc(m_size) Memory::alloc_static(m_size) +#define memrealloc(m_mem, m_size) Memory::realloc_static(m_mem, m_size) +#define memfree(m_mem) Memory::free_static(m_mem) + +_ALWAYS_INLINE_ void postinitialize_handler(void *) {} + +template +_ALWAYS_INLINE_ T *_post_initialize(T *p_obj) { + postinitialize_handler(p_obj); + return p_obj; +} + +#define memnew(m_class) _post_initialize(new ("") m_class) + +_ALWAYS_INLINE_ void *operator new(size_t p_size, void *p_pointer, size_t check, const char *p_description) { + //void *failptr=0; + //ERR_FAIL_COND_V( check < p_size , failptr); /** bug, or strange compiler, most likely */ + + return p_pointer; +} + +#define memnew_allocator(m_class, m_allocator) _post_initialize(new (m_allocator::alloc) m_class) +#define memnew_placement(m_placement, m_class) _post_initialize(new (m_placement, sizeof(m_class), "") m_class) + +_ALWAYS_INLINE_ bool predelete_handler(void *) { + return true; +} + +template +void memdelete(T *p_class) { + if (!predelete_handler(p_class)) { + return; // doesn't want to be deleted + } + if (!HAS_TRIVIAL_DESTRUCTOR(T)) { + p_class->~T(); + } + + Memory::free_static(p_class, false); +} + +template +void memdelete_allocator(T *p_class) { + if (!predelete_handler(p_class)) { + return; // doesn't want to be deleted + } + if (!HAS_TRIVIAL_DESTRUCTOR(T)) { + p_class->~T(); + } + + A::free(p_class); +} + +#define memdelete_notnull(m_v) \ + { \ + if (m_v) \ + memdelete(m_v); \ + } + +#define memnew_arr(m_class, m_count) memnew_arr_template(m_count) + +template +T *memnew_arr_template(size_t p_elements, const char *p_descr = "") { + if (p_elements == 0) { + return nullptr; + } + /** overloading operator new[] cannot be done , because it may not return the real allocated address (it may pad the 'element count' before the actual array). Because of that, it must be done by hand. This is the + same strategy used by std::vector, and the PoolVector class, so it should be safe.*/ + + size_t len = sizeof(T) * p_elements; + uint64_t *mem = (uint64_t *)Memory::alloc_static(len, true); + T *failptr = nullptr; //get rid of a warning + ERR_FAIL_COND_V(!mem, failptr); + *(mem - 1) = p_elements; + + if (!HAS_TRIVIAL_CONSTRUCTOR(T)) { + T *elems = (T *)mem; + + /* call operator new */ + for (size_t i = 0; i < p_elements; i++) { + new (&elems[i], sizeof(T), p_descr) T; + } + } + + return (T *)mem; +} + +/** + * Wonders of having own array functions, you can actually check the length of + * an allocated-with memnew_arr() array + */ + +template +size_t memarr_len(const T *p_class) { + uint64_t *ptr = (uint64_t *)p_class; + return *(ptr - 1); +} + +template +void memdelete_arr(T *p_class) { + uint64_t *ptr = (uint64_t *)p_class; + + if (!HAS_TRIVIAL_DESTRUCTOR(T)) { + uint64_t elem_count = *(ptr - 1); + + for (uint64_t i = 0; i < elem_count; i++) { + p_class[i].~T(); + } + } + + Memory::free_static(ptr, true); +} + +struct _GlobalNil { + int color; + _GlobalNil *right; + _GlobalNil *left; + _GlobalNil *parent; + + _GlobalNil(); +}; + +struct _GlobalNilClass { + static _GlobalNil _nil; +}; + +#endif diff --git a/core/os/safe_refcount.cpp b/core/os/safe_refcount.cpp new file mode 100644 index 0000000..2f5a6de --- /dev/null +++ b/core/os/safe_refcount.cpp @@ -0,0 +1,45 @@ +/*************************************************************************/ +/* safe_refcount.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* 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. */ +/*************************************************************************/ + +#if defined(DEBUG_ENABLED) && !defined(NO_THREADS) + +#include "safe_refcount.h" + +#include "core/defs.h" + +// On C++14 we don't have std::atomic::is_always_lockfree, so this is the best we can do +void check_lockless_atomics() { + // Doing the check for the types we actually care about + if (!std::atomic{}.is_lock_free() || !std::atomic{}.is_lock_free() || !std::atomic_bool{}.is_lock_free()) { + WARN_PRINT("Your compiler doesn't seem to support lockless atomics. Performance will be degraded. Please consider upgrading to a different or newer compiler."); + } +} + +#endif diff --git a/core/os/safe_refcount.h b/core/os/safe_refcount.h new file mode 100644 index 0000000..e787452 --- /dev/null +++ b/core/os/safe_refcount.h @@ -0,0 +1,332 @@ +#ifndef SAFE_REFCOUNT_H +#define SAFE_REFCOUNT_H +/*************************************************************************/ +/* safe_refcount.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* 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. */ +/*************************************************************************/ + +#include "core/typedefs.h" + +#if !defined(NO_THREADS) + +#include +#include + +// Design goals for these classes: +// - No automatic conversions or arithmetic operators, +// to keep explicit the use of atomics everywhere. +// - Using acquire-release semantics, even to set the first value. +// The first value may be set relaxedly in many cases, but adding the distinction +// between relaxed and unrelaxed operation to the interface would make it needlessly +// flexible. There's negligible waste in having release semantics for the initial +// value and, as an important benefit, you can be sure the value is properly synchronized +// even with threads that are already running. + +// This is used in very specific areas of the engine where it's critical that these guarantees are held +#define SAFE_NUMERIC_TYPE_PUN_GUARANTEES(m_type) \ + static_assert(sizeof(SafeNumeric) == sizeof(m_type), ""); \ + static_assert(alignof(SafeNumeric) == alignof(m_type), ""); \ + static_assert(std::is_trivially_destructible>::value, ""); + +#if defined(DEBUG_ENABLED) +void check_lockless_atomics(); +#endif + +template +class SafeNumeric { + std::atomic value; + +public: + _ALWAYS_INLINE_ void set(T p_value) { + value.store(p_value, std::memory_order_release); + } + + _ALWAYS_INLINE_ T get() const { + return value.load(std::memory_order_acquire); + } + + _ALWAYS_INLINE_ T increment() { + return value.fetch_add(1, std::memory_order_acq_rel) + 1; + } + + // Returns the original value instead of the new one + _ALWAYS_INLINE_ T postincrement() { + return value.fetch_add(1, std::memory_order_acq_rel); + } + + _ALWAYS_INLINE_ T decrement() { + return value.fetch_sub(1, std::memory_order_acq_rel) - 1; + } + + // Returns the original value instead of the new one + _ALWAYS_INLINE_ T postdecrement() { + return value.fetch_sub(1, std::memory_order_acq_rel); + } + + _ALWAYS_INLINE_ T add(T p_value) { + return value.fetch_add(p_value, std::memory_order_acq_rel) + p_value; + } + + // Returns the original value instead of the new one + _ALWAYS_INLINE_ T postadd(T p_value) { + return value.fetch_add(p_value, std::memory_order_acq_rel); + } + + _ALWAYS_INLINE_ T sub(T p_value) { + return value.fetch_sub(p_value, std::memory_order_acq_rel) - p_value; + } + + // Returns the original value instead of the new one + _ALWAYS_INLINE_ T postsub(T p_value) { + return value.fetch_sub(p_value, std::memory_order_acq_rel); + } + + _ALWAYS_INLINE_ T exchange_if_greater(T p_value) { + while (true) { + T tmp = value.load(std::memory_order_acquire); + if (tmp >= p_value) { + return tmp; // already greater, or equal + } + if (value.compare_exchange_weak(tmp, p_value, std::memory_order_acq_rel)) { + return p_value; + } + } + } + + _ALWAYS_INLINE_ T conditional_increment() { + while (true) { + T c = value.load(std::memory_order_acquire); + if (c == 0) { + return 0; + } + if (value.compare_exchange_weak(c, c + 1, std::memory_order_acq_rel)) { + return c + 1; + } + } + } + + _ALWAYS_INLINE_ explicit SafeNumeric(T p_value = static_cast(0)) { + set(p_value); + } +}; + +class SafeFlag { + std::atomic_bool flag; + +public: + _ALWAYS_INLINE_ bool is_set() const { + return flag.load(std::memory_order_acquire); + } + + _ALWAYS_INLINE_ void set() { + flag.store(true, std::memory_order_release); + } + + _ALWAYS_INLINE_ void clear() { + flag.store(false, std::memory_order_release); + } + + _ALWAYS_INLINE_ void set_to(bool p_value) { + flag.store(p_value, std::memory_order_release); + } + + _ALWAYS_INLINE_ explicit SafeFlag(bool p_value = false) { + set_to(p_value); + } +}; + +class SafeRefCount { + SafeNumeric count; + +public: + _ALWAYS_INLINE_ bool ref() { // true on success + return count.conditional_increment() != 0; + } + + _ALWAYS_INLINE_ uint32_t refval() { // none-zero on success + return count.conditional_increment(); + } + + _ALWAYS_INLINE_ bool unref() { // true if must be disposed of + return count.decrement() == 0; + } + + _ALWAYS_INLINE_ uint32_t unrefval() { // 0 if must be disposed of + return count.decrement(); + } + + _ALWAYS_INLINE_ uint32_t get() const { + return count.get(); + } + + _ALWAYS_INLINE_ void init(uint32_t p_value = 1) { + count.set(p_value); + } +}; + +#else + +template +class SafeNumeric { +protected: + T value; + +public: + _ALWAYS_INLINE_ void set(T p_value) { + value = p_value; + } + + _ALWAYS_INLINE_ T get() const { + return value; + } + + _ALWAYS_INLINE_ T increment() { + return ++value; + } + + _ALWAYS_INLINE_ T postincrement() { + return value++; + } + + _ALWAYS_INLINE_ T decrement() { + return --value; + } + + _ALWAYS_INLINE_ T postdecrement() { + return value--; + } + + _ALWAYS_INLINE_ T add(T p_value) { + return value += p_value; + } + + _ALWAYS_INLINE_ T postadd(T p_value) { + T old = value; + value += p_value; + return old; + } + + _ALWAYS_INLINE_ T sub(T p_value) { + return value -= p_value; + } + + _ALWAYS_INLINE_ T postsub(T p_value) { + T old = value; + value -= p_value; + return old; + } + + _ALWAYS_INLINE_ T exchange_if_greater(T p_value) { + if (value < p_value) { + value = p_value; + } + return value; + } + + _ALWAYS_INLINE_ T conditional_increment() { + if (value == 0) { + return 0; + } else { + return ++value; + } + } + + _ALWAYS_INLINE_ explicit SafeNumeric(T p_value = static_cast(0)) : + value(p_value) { + } +}; + +class SafeFlag { +protected: + bool flag; + +public: + _ALWAYS_INLINE_ bool is_set() const { + return flag; + } + + _ALWAYS_INLINE_ void set() { + flag = true; + } + + _ALWAYS_INLINE_ void clear() { + flag = false; + } + + _ALWAYS_INLINE_ void set_to(bool p_value) { + flag = p_value; + } + + _ALWAYS_INLINE_ explicit SafeFlag(bool p_value = false) : + flag(p_value) {} +}; + +class SafeRefCount { + uint32_t count; + +public: + _ALWAYS_INLINE_ bool ref() { // true on success + if (count != 0) { + ++count; + return true; + } else { + return false; + } + } + + _ALWAYS_INLINE_ uint32_t refval() { // none-zero on success + if (count != 0) { + return ++count; + } else { + return 0; + } + } + + _ALWAYS_INLINE_ bool unref() { // true if must be disposed of + return --count == 0; + } + + _ALWAYS_INLINE_ uint32_t unrefval() { // 0 if must be disposed of + return --count; + } + + _ALWAYS_INLINE_ uint32_t get() const { + return count; + } + + _ALWAYS_INLINE_ void init(uint32_t p_value = 1) { + count = p_value; + } + + SafeRefCount() : + count(0) {} +}; + +#endif + +#endif // SAFE_REFCOUNT_H diff --git a/core/projection.cpp b/core/projection.cpp index 7b2e452..c3a2893 100644 --- a/core/projection.cpp +++ b/core/projection.cpp @@ -260,7 +260,7 @@ Vector2 Projection::get_viewport_half_extents() const { } bool Projection::get_endpoints(const Transform &p_transform, Vector3 *p_8points) const { - std::vector planes = get_projection_planes(Transform()); + Vector planes = get_projection_planes(Transform()); const Planes intersections[8][3] = { { PLANE_FAR, PLANE_LEFT, PLANE_TOP }, { PLANE_FAR, PLANE_LEFT, PLANE_BOTTOM }, @@ -282,14 +282,14 @@ bool Projection::get_endpoints(const Transform &p_transform, Vector3 *p_8points) return true; } -std::vector Projection::get_projection_planes(const Transform &p_transform) const { +Vector Projection::get_projection_planes(const Transform &p_transform) const { /** Fast Plane Extraction from combined modelview/projection matrices. * References: * https://web.archive.org/web/20011221205252/http://www.markmorley.com/opengl/frustumculling.html * https://web.archive.org/web/20061020020112/http://www2.ravensoft.com/users/ggribb/plane%20extraction.pdf */ - std::vector planes; + Vector planes; const real_t *matrix = (const real_t *)this->matrix; diff --git a/core/projection.h b/core/projection.h index 7f5edfe..0c82b04 100644 --- a/core/projection.h +++ b/core/projection.h @@ -37,7 +37,7 @@ #include "rect2.h" #include "transform.h" -#include +#include "core/containers/vector.h" struct Projection { enum Planes { @@ -83,7 +83,7 @@ struct Projection { real_t get_fov() const; bool is_orthogonal() const; - std::vector get_projection_planes(const Transform &p_transform) const; + Vector get_projection_planes(const Transform &p_transform) const; bool get_endpoints(const Transform &p_transform, Vector3 *p_8points) const; Vector2 get_viewport_half_extents() const;