diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/input/input.cpp | 4 | ||||
-rw-r--r-- | core/io/packet_peer.cpp | 6 | ||||
-rw-r--r-- | core/math/geometry_2d.h | 26 | ||||
-rw-r--r-- | core/object/class_db.cpp | 7 | ||||
-rw-r--r-- | core/object/object.cpp | 2 | ||||
-rw-r--r-- | core/object/worker_thread_pool.cpp | 6 | ||||
-rw-r--r-- | core/os/condition_variable.h | 14 | ||||
-rw-r--r-- | core/os/mutex.cpp | 4 | ||||
-rw-r--r-- | core/os/mutex.h | 40 | ||||
-rw-r--r-- | core/os/os.cpp | 6 | ||||
-rw-r--r-- | core/os/semaphore.h | 17 | ||||
-rw-r--r-- | core/os/thread.cpp | 9 | ||||
-rw-r--r-- | core/os/thread.h | 64 | ||||
-rw-r--r-- | core/templates/cowdata.h | 152 | ||||
-rw-r--r-- | core/templates/ring_buffer.h | 2 | ||||
-rw-r--r-- | core/templates/vector.h | 69 | ||||
-rw-r--r-- | core/variant/variant_call.cpp | 2 |
17 files changed, 323 insertions, 107 deletions
diff --git a/core/input/input.cpp b/core/input/input.cpp index 2ba4b1d1e8..4e33d3087d 100644 --- a/core/input/input.cpp +++ b/core/input/input.cpp @@ -865,6 +865,8 @@ Point2i Input::warp_mouse_motion(const Ref<InputEventMouseMotion> &p_motion, con } void Input::action_press(const StringName &p_action, float p_strength) { + ERR_FAIL_COND_MSG(!InputMap::get_singleton()->has_action(p_action), InputMap::get_singleton()->suggest_actions(p_action)); + // Create or retrieve existing action. ActionState &action_state = action_states[p_action]; @@ -879,6 +881,8 @@ void Input::action_press(const StringName &p_action, float p_strength) { } void Input::action_release(const StringName &p_action) { + ERR_FAIL_COND_MSG(!InputMap::get_singleton()->has_action(p_action), InputMap::get_singleton()->suggest_actions(p_action)); + // Create or retrieve existing action. ActionState &action_state = action_states[p_action]; action_state.cache.pressed = 0; diff --git a/core/io/packet_peer.cpp b/core/io/packet_peer.cpp index 48806fba73..0329ace313 100644 --- a/core/io/packet_peer.cpp +++ b/core/io/packet_peer.cpp @@ -318,9 +318,9 @@ int PacketPeerStream::get_output_buffer_max_size() const { } PacketPeerStream::PacketPeerStream() { - int rbsize = GLOBAL_GET("network/limits/packet_peer_stream/max_buffer_po2"); + int64_t rbsize = GLOBAL_GET("network/limits/packet_peer_stream/max_buffer_po2"); ring_buffer.resize(rbsize); - input_buffer.resize(1 << rbsize); - output_buffer.resize(1 << rbsize); + input_buffer.resize(int64_t(1) << rbsize); + output_buffer.resize(int64_t(1) << rbsize); } diff --git a/core/math/geometry_2d.h b/core/math/geometry_2d.h index b37fce9e9c..9907d579a5 100644 --- a/core/math/geometry_2d.h +++ b/core/math/geometry_2d.h @@ -119,6 +119,10 @@ public: } } + static real_t get_distance_to_segment(const Vector2 &p_point, const Vector2 *p_segment) { + return p_point.distance_to(get_closest_point_to_segment(p_point, p_segment)); + } + static bool is_point_in_triangle(const Vector2 &s, const Vector2 &a, const Vector2 &b, const Vector2 &c) { Vector2 an = a - s; Vector2 bn = b - s; @@ -249,6 +253,28 @@ public: return -1; } + static bool segment_intersects_rect(const Vector2 &p_from, const Vector2 &p_to, const Rect2 &p_rect) { + if (p_rect.has_point(p_from) || p_rect.has_point(p_to)) { + return true; + } + + const Vector2 rect_points[4] = { + p_rect.position, + p_rect.position + Vector2(p_rect.size.x, 0), + p_rect.position + p_rect.size, + p_rect.position + Vector2(0, p_rect.size.y) + }; + + // Check if any of the rect's edges intersect the segment. + for (int i = 0; i < 4; i++) { + if (segment_intersects_segment(p_from, p_to, rect_points[i], rect_points[(i + 1) % 4], nullptr)) { + return true; + } + } + + return false; + } + enum PolyBooleanOperation { OPERATION_UNION, OPERATION_DIFFERENCE, diff --git a/core/object/class_db.cpp b/core/object/class_db.cpp index bf1bd0de93..45fbb19f88 100644 --- a/core/object/class_db.cpp +++ b/core/object/class_db.cpp @@ -31,6 +31,7 @@ #include "class_db.h" #include "core/config/engine.h" +#include "core/core_string_names.h" #include "core/io/resource_loader.h" #include "core/object/script_language.h" #include "core/os/mutex.h" @@ -1299,6 +1300,12 @@ bool ClassDB::get_property(Object *p_object, const StringName &p_property, Varia check = check->inherits_ptr; } + // The "free()" method is special, so we assume it exists and return a Callable. + if (p_property == CoreStringNames::get_singleton()->_free) { + r_value = Callable(p_object, p_property); + return true; + } + return false; } diff --git a/core/object/object.cpp b/core/object/object.cpp index 5a776e2106..3901c4835d 100644 --- a/core/object/object.cpp +++ b/core/object/object.cpp @@ -728,7 +728,7 @@ Variant Object::callp(const StringName &p_method, const Variant **p_args, int p_ r_error.expected = 0; return Variant(); } - if (Object::cast_to<RefCounted>(this)) { + if (is_ref_counted()) { r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; ERR_FAIL_V_MSG(Variant(), "Can't 'free' a reference."); } diff --git a/core/object/worker_thread_pool.cpp b/core/object/worker_thread_pool.cpp index 8e8a2ef06b..e2ab473b01 100644 --- a/core/object/worker_thread_pool.cpp +++ b/core/object/worker_thread_pool.cpp @@ -47,6 +47,7 @@ WorkerThreadPool *WorkerThreadPool::singleton = nullptr; thread_local CommandQueueMT *WorkerThreadPool::flushing_cmd_queue = nullptr; void WorkerThreadPool::_process_task(Task *p_task) { +#ifdef THREADS_ENABLED int pool_thread_index = thread_ids[Thread::get_caller_id()]; ThreadData &curr_thread = threads[pool_thread_index]; Task *prev_task = nullptr; // In case this is recursively called. @@ -69,6 +70,7 @@ void WorkerThreadPool::_process_task(Task *p_task) { curr_thread.current_task = p_task; task_mutex.unlock(); } +#endif if (p_task->group) { // Handling a group @@ -143,6 +145,7 @@ void WorkerThreadPool::_process_task(Task *p_task) { } } +#ifdef THREADS_ENABLED { curr_thread.current_task = prev_task; if (p_task->low_priority) { @@ -159,6 +162,7 @@ void WorkerThreadPool::_process_task(Task *p_task) { } set_current_thread_safe_for_nodes(safe_for_nodes_backup); +#endif } void WorkerThreadPool::_thread_function(void *p_user) { @@ -542,6 +546,7 @@ bool WorkerThreadPool::is_group_task_completed(GroupID p_group) const { } void WorkerThreadPool::wait_for_group_task_completion(GroupID p_group) { +#ifdef THREADS_ENABLED task_mutex.lock(); Group **groupp = groups.getptr(p_group); task_mutex.unlock(); @@ -574,6 +579,7 @@ void WorkerThreadPool::wait_for_group_task_completion(GroupID p_group) { task_mutex.lock(); // This mutex is needed when Physics 2D and/or 3D is selected to run on a separate thread. groups.erase(p_group); task_mutex.unlock(); +#endif } int WorkerThreadPool::get_thread_index() { diff --git a/core/os/condition_variable.h b/core/os/condition_variable.h index 6a49ced31b..2b6b272e18 100644 --- a/core/os/condition_variable.h +++ b/core/os/condition_variable.h @@ -33,6 +33,8 @@ #include "core/os/mutex.h" +#ifdef THREADS_ENABLED + #ifdef MINGW_ENABLED #define MINGW_STDTHREAD_REDUNDANCY_WARNING #include "thirdparty/mingw-std-threads/mingw.condition_variable.h" @@ -66,4 +68,16 @@ public: } }; +#else // No threads. + +class ConditionVariable { +public: + template <class BinaryMutexT> + void wait(const MutexLock<BinaryMutexT> &p_lock) const {} + void notify_one() const {} + void notify_all() const {} +}; + +#endif // THREADS_ENABLED + #endif // CONDITION_VARIABLE_H diff --git a/core/os/mutex.cpp b/core/os/mutex.cpp index 5d4e457c5f..9a8a2a2961 100644 --- a/core/os/mutex.cpp +++ b/core/os/mutex.cpp @@ -40,7 +40,11 @@ void _global_unlock() { _global_mutex.unlock(); } +#ifdef THREADS_ENABLED + template class MutexImpl<THREADING_NAMESPACE::recursive_mutex>; template class MutexImpl<THREADING_NAMESPACE::mutex>; template class MutexLock<MutexImpl<THREADING_NAMESPACE::recursive_mutex>>; template class MutexLock<MutexImpl<THREADING_NAMESPACE::mutex>>; + +#endif diff --git a/core/os/mutex.h b/core/os/mutex.h index 03af48ca7c..69f494d9cd 100644 --- a/core/os/mutex.h +++ b/core/os/mutex.h @@ -43,6 +43,8 @@ #define THREADING_NAMESPACE std #endif +#ifdef THREADS_ENABLED + template <class MutexT> class MutexLock; @@ -125,8 +127,8 @@ class MutexLock { THREADING_NAMESPACE::unique_lock<typename MutexT::StdMutexType> lock; public: - _ALWAYS_INLINE_ explicit MutexLock(const MutexT &p_mutex) : - lock(p_mutex.mutex){}; + explicit MutexLock(const MutexT &p_mutex) : + lock(p_mutex.mutex) {} }; // This specialization is needed so manual locking and MutexLock can be used @@ -155,4 +157,38 @@ extern template class MutexImpl<THREADING_NAMESPACE::mutex>; extern template class MutexLock<MutexImpl<THREADING_NAMESPACE::recursive_mutex>>; extern template class MutexLock<MutexImpl<THREADING_NAMESPACE::mutex>>; +#else // No threads. + +class MutexImpl { + mutable THREADING_NAMESPACE::mutex mutex; + +public: + void lock() const {} + void unlock() const {} + bool try_lock() const { return true; } +}; + +template <int Tag> +class SafeBinaryMutex : public MutexImpl { + static thread_local uint32_t count; +}; + +template <class MutexT> +class MutexLock { +public: + MutexLock(const MutexT &p_mutex) {} +}; + +template <int Tag> +class MutexLock<SafeBinaryMutex<Tag>> { +public: + MutexLock(const SafeBinaryMutex<Tag> &p_mutex) {} + ~MutexLock() {} +}; + +using Mutex = MutexImpl; +using BinaryMutex = MutexImpl; + +#endif // THREADS_ENABLED + #endif // MUTEX_H diff --git a/core/os/os.cpp b/core/os/os.cpp index 26ae286979..d5d9988cc1 100644 --- a/core/os/os.cpp +++ b/core/os/os.cpp @@ -504,6 +504,12 @@ bool OS::has_feature(const String &p_feature) { } #endif +#ifdef THREADS_ENABLED + if (p_feature == "threads") { + return true; + } +#endif + if (_check_internal_feature_support(p_feature)) { return true; } diff --git a/core/os/semaphore.h b/core/os/semaphore.h index b8ae35b86b..19ef1dedc0 100644 --- a/core/os/semaphore.h +++ b/core/os/semaphore.h @@ -31,6 +31,10 @@ #ifndef SEMAPHORE_H #define SEMAPHORE_H +#include <cstdint> + +#ifdef THREADS_ENABLED + #include "core/error/error_list.h" #include "core/typedefs.h" #ifdef DEBUG_ENABLED @@ -132,4 +136,17 @@ public: #endif }; +#else // No threads. + +class Semaphore { +public: + void post(uint32_t p_count = 1) const {} + void wait() const {} + bool try_wait() const { + return true; + } +}; + +#endif // THREADS_ENABLED + #endif // SEMAPHORE_H diff --git a/core/os/thread.cpp b/core/os/thread.cpp index 2ba90ba42c..afc74364f6 100644 --- a/core/os/thread.cpp +++ b/core/os/thread.cpp @@ -33,19 +33,22 @@ #include "thread.h" +#ifdef THREADS_ENABLED #include "core/object/script_language.h" #include "core/templates/safe_refcount.h" -Thread::PlatformFunctions Thread::platform_functions; - SafeNumeric<uint64_t> Thread::id_counter(1); // The first value after .increment() is 2, hence by default the main thread ID should be 1. thread_local Thread::ID Thread::caller_id = Thread::UNASSIGNED_ID; +#endif + +Thread::PlatformFunctions Thread::platform_functions; void Thread::_set_platform_functions(const PlatformFunctions &p_functions) { platform_functions = p_functions; } +#ifdef THREADS_ENABLED void Thread::callback(ID p_caller_id, const Settings &p_settings, Callback p_callback, void *p_userdata) { Thread::caller_id = p_caller_id; if (platform_functions.set_priority) { @@ -107,4 +110,6 @@ Thread::~Thread() { } } +#endif // THREADS_ENABLED + #endif // PLATFORM_THREAD_OVERRIDE diff --git a/core/os/thread.h b/core/os/thread.h index cc954678f9..a0ecc24c91 100644 --- a/core/os/thread.h +++ b/core/os/thread.h @@ -53,6 +53,8 @@ class String; +#ifdef THREADS_ENABLED + class Thread { public: typedef void (*Callback)(void *p_userdata); @@ -86,6 +88,8 @@ public: private: friend class Main; + static PlatformFunctions platform_functions; + ID id = UNASSIGNED_ID; static SafeNumeric<uint64_t> id_counter; static thread_local ID caller_id; @@ -93,8 +97,6 @@ private: static void callback(ID p_caller_id, const Settings &p_settings, Thread::Callback p_callback, void *p_userdata); - static PlatformFunctions platform_functions; - static void make_main_thread() { caller_id = MAIN_ID; } static void release_main_thread() { caller_id = UNASSIGNED_ID; } @@ -125,6 +127,64 @@ public: ~Thread(); }; +#else // No threads. + +class Thread { +public: + typedef void (*Callback)(void *p_userdata); + + typedef uint64_t ID; + + enum : ID { + UNASSIGNED_ID = 0, + MAIN_ID = 1 + }; + + enum Priority { + PRIORITY_LOW, + PRIORITY_NORMAL, + PRIORITY_HIGH + }; + + struct Settings { + Priority priority; + Settings() { priority = PRIORITY_NORMAL; } + }; + + struct PlatformFunctions { + Error (*set_name)(const String &) = nullptr; + void (*set_priority)(Thread::Priority) = nullptr; + void (*init)() = nullptr; + void (*wrapper)(Thread::Callback, void *) = nullptr; + void (*term)() = nullptr; + }; + +private: + friend class Main; + + static PlatformFunctions platform_functions; + + static void make_main_thread() {} + static void release_main_thread() {} + +public: + static void _set_platform_functions(const PlatformFunctions &p_functions); + + _FORCE_INLINE_ ID get_id() const { return 0; } + _FORCE_INLINE_ static ID get_caller_id() { return MAIN_ID; } + _FORCE_INLINE_ static ID get_main_id() { return MAIN_ID; } + + _FORCE_INLINE_ static bool is_main_thread() { return true; } + + static Error set_name(const String &p_name) { return ERR_UNAVAILABLE; } + + void start(Thread::Callback p_callback, void *p_user, const Settings &p_settings = Settings()) {} + bool is_started() const { return false; } + void wait_to_finish() {} +}; + +#endif // THREADS_ENABLED + #endif // THREAD_H #endif // PLATFORM_THREAD_OVERRIDE diff --git a/core/templates/cowdata.h b/core/templates/cowdata.h index 46d9797d6c..d43cf8107f 100644 --- a/core/templates/cowdata.h +++ b/core/templates/cowdata.h @@ -46,7 +46,7 @@ class CharString; template <class T, class V> class VMap; -SAFE_NUMERIC_TYPE_PUN_GUARANTEES(uint32_t) +SAFE_NUMERIC_TYPE_PUN_GUARANTEES(uint64_t) // Silence a false positive warning (see GH-52119). #if defined(__GNUC__) && !defined(__clang__) @@ -64,45 +64,71 @@ class CowData { template <class TV, class VV> friend class VMap; +public: + typedef int64_t Size; + typedef uint64_t USize; + static constexpr USize MAX_INT = INT64_MAX; + private: + // Function to find the next power of 2 to an integer. + static _FORCE_INLINE_ USize next_po2(USize x) { + if (x == 0) { + return 0; + } + + --x; + x |= x >> 1; + x |= x >> 2; + x |= x >> 4; + x |= x >> 8; + x |= x >> 16; + if (sizeof(USize) == 8) { + x |= x >> 32; + } + + return ++x; + } + + static constexpr USize ALLOC_PAD = sizeof(USize) * 2; // For size and atomic refcount. + mutable T *_ptr = nullptr; // internal helpers - _FORCE_INLINE_ SafeNumeric<uint32_t> *_get_refcount() const { + _FORCE_INLINE_ SafeNumeric<USize> *_get_refcount() const { if (!_ptr) { return nullptr; } - return reinterpret_cast<SafeNumeric<uint32_t> *>(_ptr) - 2; + return reinterpret_cast<SafeNumeric<USize> *>(_ptr) - 2; } - _FORCE_INLINE_ uint32_t *_get_size() const { + _FORCE_INLINE_ USize *_get_size() const { if (!_ptr) { return nullptr; } - return reinterpret_cast<uint32_t *>(_ptr) - 1; + return reinterpret_cast<USize *>(_ptr) - 1; } - _FORCE_INLINE_ size_t _get_alloc_size(size_t p_elements) const { - return next_power_of_2(p_elements * sizeof(T)); + _FORCE_INLINE_ USize _get_alloc_size(USize p_elements) const { + return next_po2(p_elements * sizeof(T)); } - _FORCE_INLINE_ bool _get_alloc_size_checked(size_t p_elements, size_t *out) const { + _FORCE_INLINE_ bool _get_alloc_size_checked(USize p_elements, USize *out) const { if (unlikely(p_elements == 0)) { *out = 0; return true; } -#if defined(__GNUC__) - size_t o; - size_t p; +#if defined(__GNUC__) && defined(IS_32_BIT) + USize o; + USize p; if (__builtin_mul_overflow(p_elements, sizeof(T), &o)) { *out = 0; return false; } - *out = next_power_of_2(o); - if (__builtin_add_overflow(o, static_cast<size_t>(32), &p)) { + *out = next_po2(o); + if (__builtin_add_overflow(o, static_cast<USize>(32), &p)) { return false; // No longer allocated here. } #else @@ -116,7 +142,7 @@ private: void _unref(void *p_data); void _ref(const CowData *p_from); void _ref(const CowData &p_from); - uint32_t _copy_on_write(); + USize _copy_on_write(); public: void operator=(const CowData<T> &p_from) { _ref(p_from); } @@ -130,8 +156,8 @@ public: return _ptr; } - _FORCE_INLINE_ int size() const { - uint32_t *size = (uint32_t *)_get_size(); + _FORCE_INLINE_ Size size() const { + USize *size = (USize *)_get_size(); if (size) { return *size; } else { @@ -142,42 +168,42 @@ public: _FORCE_INLINE_ void clear() { resize(0); } _FORCE_INLINE_ bool is_empty() const { return _ptr == nullptr; } - _FORCE_INLINE_ void set(int p_index, const T &p_elem) { + _FORCE_INLINE_ void set(Size p_index, const T &p_elem) { ERR_FAIL_INDEX(p_index, size()); _copy_on_write(); _ptr[p_index] = p_elem; } - _FORCE_INLINE_ T &get_m(int p_index) { + _FORCE_INLINE_ T &get_m(Size p_index) { CRASH_BAD_INDEX(p_index, size()); _copy_on_write(); return _ptr[p_index]; } - _FORCE_INLINE_ const T &get(int p_index) const { + _FORCE_INLINE_ const T &get(Size p_index) const { CRASH_BAD_INDEX(p_index, size()); return _ptr[p_index]; } template <bool p_ensure_zero = false> - Error resize(int p_size); + Error resize(Size p_size); - _FORCE_INLINE_ void remove_at(int p_index) { + _FORCE_INLINE_ void remove_at(Size p_index) { ERR_FAIL_INDEX(p_index, size()); T *p = ptrw(); - int len = size(); - for (int i = p_index; i < len - 1; i++) { + Size len = size(); + for (Size i = p_index; i < len - 1; i++) { p[i] = p[i + 1]; } resize(len - 1); } - Error insert(int p_pos, const T &p_val) { + Error insert(Size p_pos, const T &p_val) { ERR_FAIL_INDEX_V(p_pos, size() + 1, ERR_INVALID_PARAMETER); resize(size() + 1); - for (int i = (size() - 1); i > p_pos; i--) { + for (Size i = (size() - 1); i > p_pos; i--) { set(i, get(i - 1)); } set(p_pos, p_val); @@ -185,9 +211,9 @@ public: return OK; } - int find(const T &p_val, int p_from = 0) const; - int rfind(const T &p_val, int p_from = -1) const; - int count(const T &p_val) const; + Size find(const T &p_val, Size p_from = 0) const; + Size rfind(const T &p_val, Size p_from = -1) const; + Size count(const T &p_val) const; _FORCE_INLINE_ CowData() {} _FORCE_INLINE_ ~CowData(); @@ -200,7 +226,7 @@ void CowData<T>::_unref(void *p_data) { return; } - SafeNumeric<uint32_t> *refc = _get_refcount(); + SafeNumeric<USize> *refc = _get_refcount(); if (refc->decrement() > 0) { return; // still in use @@ -208,35 +234,36 @@ void CowData<T>::_unref(void *p_data) { // clean up if (!std::is_trivially_destructible<T>::value) { - uint32_t *count = _get_size(); + USize *count = _get_size(); T *data = (T *)(count + 1); - for (uint32_t i = 0; i < *count; ++i) { + for (USize i = 0; i < *count; ++i) { // call destructors data[i].~T(); } } // free mem - Memory::free_static((uint8_t *)p_data, true); + Memory::free_static(((uint8_t *)p_data) - ALLOC_PAD, false); } template <class T> -uint32_t CowData<T>::_copy_on_write() { +typename CowData<T>::USize CowData<T>::_copy_on_write() { if (!_ptr) { return 0; } - SafeNumeric<uint32_t> *refc = _get_refcount(); + SafeNumeric<USize> *refc = _get_refcount(); - uint32_t rc = refc->get(); + USize rc = refc->get(); if (unlikely(rc > 1)) { /* in use by more than me */ - uint32_t current_size = *_get_size(); + USize current_size = *_get_size(); - uint32_t *mem_new = (uint32_t *)Memory::alloc_static(_get_alloc_size(current_size), true); + USize *mem_new = (USize *)Memory::alloc_static(_get_alloc_size(current_size) + ALLOC_PAD, false); + mem_new += 2; - new (mem_new - 2) SafeNumeric<uint32_t>(1); //refcount + new (mem_new - 2) SafeNumeric<USize>(1); //refcount *(mem_new - 1) = current_size; //size T *_data = (T *)(mem_new); @@ -246,7 +273,7 @@ uint32_t CowData<T>::_copy_on_write() { memcpy(mem_new, _ptr, current_size * sizeof(T)); } else { - for (uint32_t i = 0; i < current_size; i++) { + for (USize i = 0; i < current_size; i++) { memnew_placement(&_data[i], T(_ptr[i])); } } @@ -261,10 +288,10 @@ uint32_t CowData<T>::_copy_on_write() { template <class T> template <bool p_ensure_zero> -Error CowData<T>::resize(int p_size) { +Error CowData<T>::resize(Size p_size) { ERR_FAIL_COND_V(p_size < 0, ERR_INVALID_PARAMETER); - int current_size = size(); + Size current_size = size(); if (p_size == current_size) { return OK; @@ -278,27 +305,29 @@ Error CowData<T>::resize(int p_size) { } // possibly changing size, copy on write - uint32_t rc = _copy_on_write(); + USize rc = _copy_on_write(); - size_t current_alloc_size = _get_alloc_size(current_size); - size_t alloc_size; + USize current_alloc_size = _get_alloc_size(current_size); + USize alloc_size; ERR_FAIL_COND_V(!_get_alloc_size_checked(p_size, &alloc_size), ERR_OUT_OF_MEMORY); if (p_size > current_size) { if (alloc_size != current_alloc_size) { if (current_size == 0) { // alloc from scratch - uint32_t *ptr = (uint32_t *)Memory::alloc_static(alloc_size, true); + USize *ptr = (USize *)Memory::alloc_static(alloc_size + ALLOC_PAD, false); + ptr += 2; ERR_FAIL_NULL_V(ptr, ERR_OUT_OF_MEMORY); *(ptr - 1) = 0; //size, currently none - new (ptr - 2) SafeNumeric<uint32_t>(1); //refcount + new (ptr - 2) SafeNumeric<USize>(1); //refcount _ptr = (T *)ptr; } else { - uint32_t *_ptrnew = (uint32_t *)Memory::realloc_static(_ptr, alloc_size, true); + USize *_ptrnew = (USize *)Memory::realloc_static(((uint8_t *)_ptr) - ALLOC_PAD, alloc_size + ALLOC_PAD, false); ERR_FAIL_NULL_V(_ptrnew, ERR_OUT_OF_MEMORY); - new (_ptrnew - 2) SafeNumeric<uint32_t>(rc); //refcount + _ptrnew += 2; + new (_ptrnew - 2) SafeNumeric<USize>(rc); //refcount _ptr = (T *)(_ptrnew); } @@ -307,7 +336,7 @@ Error CowData<T>::resize(int p_size) { // construct the newly created elements if (!std::is_trivially_constructible<T>::value) { - for (int i = *_get_size(); i < p_size; i++) { + for (Size i = *_get_size(); i < p_size; i++) { memnew_placement(&_ptr[i], T); } } else if (p_ensure_zero) { @@ -319,16 +348,17 @@ Error CowData<T>::resize(int p_size) { } else if (p_size < current_size) { if (!std::is_trivially_destructible<T>::value) { // deinitialize no longer needed elements - for (uint32_t i = p_size; i < *_get_size(); i++) { + for (USize i = p_size; i < *_get_size(); i++) { T *t = &_ptr[i]; t->~T(); } } if (alloc_size != current_alloc_size) { - uint32_t *_ptrnew = (uint32_t *)Memory::realloc_static(_ptr, alloc_size, true); + USize *_ptrnew = (USize *)Memory::realloc_static(((uint8_t *)_ptr) - ALLOC_PAD, alloc_size + ALLOC_PAD, false); ERR_FAIL_NULL_V(_ptrnew, ERR_OUT_OF_MEMORY); - new (_ptrnew - 2) SafeNumeric<uint32_t>(rc); //refcount + _ptrnew += 2; + new (_ptrnew - 2) SafeNumeric<USize>(rc); //refcount _ptr = (T *)(_ptrnew); } @@ -340,14 +370,14 @@ Error CowData<T>::resize(int p_size) { } template <class T> -int CowData<T>::find(const T &p_val, int p_from) const { - int ret = -1; +typename CowData<T>::Size CowData<T>::find(const T &p_val, Size p_from) const { + Size ret = -1; if (p_from < 0 || size() == 0) { return ret; } - for (int i = p_from; i < size(); i++) { + for (Size i = p_from; i < size(); i++) { if (get(i) == p_val) { ret = i; break; @@ -358,8 +388,8 @@ int CowData<T>::find(const T &p_val, int p_from) const { } template <class T> -int CowData<T>::rfind(const T &p_val, int p_from) const { - const int s = size(); +typename CowData<T>::Size CowData<T>::rfind(const T &p_val, Size p_from) const { + const Size s = size(); if (p_from < 0) { p_from = s + p_from; @@ -368,7 +398,7 @@ int CowData<T>::rfind(const T &p_val, int p_from) const { p_from = s - 1; } - for (int i = p_from; i >= 0; i--) { + for (Size i = p_from; i >= 0; i--) { if (get(i) == p_val) { return i; } @@ -377,9 +407,9 @@ int CowData<T>::rfind(const T &p_val, int p_from) const { } template <class T> -int CowData<T>::count(const T &p_val) const { - int amount = 0; - for (int i = 0; i < size(); i++) { +typename CowData<T>::Size CowData<T>::count(const T &p_val) const { + Size amount = 0; + for (Size i = 0; i < size(); i++) { if (get(i) == p_val) { amount++; } diff --git a/core/templates/ring_buffer.h b/core/templates/ring_buffer.h index d878894946..54148a59bf 100644 --- a/core/templates/ring_buffer.h +++ b/core/templates/ring_buffer.h @@ -197,7 +197,7 @@ public: int old_size = size(); int new_size = 1 << p_power; int mask = new_size - 1; - data.resize(1 << p_power); + data.resize(int64_t(1) << int64_t(p_power)); if (old_size < new_size && read_pos > write_pos) { for (int i = 0; i < write_pos; i++) { data.write[(old_size + i) & mask] = data[i]; diff --git a/core/templates/vector.h b/core/templates/vector.h index d8bac0870f..361a7f56d5 100644 --- a/core/templates/vector.h +++ b/core/templates/vector.h @@ -48,7 +48,7 @@ template <class T> class VectorWriteProxy { public: - _FORCE_INLINE_ T &operator[](int p_index) { + _FORCE_INLINE_ T &operator[](typename CowData<T>::Size p_index) { CRASH_BAD_INDEX(p_index, ((Vector<T> *)(this))->_cowdata.size()); return ((Vector<T> *)(this))->_cowdata.ptrw()[p_index]; @@ -61,6 +61,7 @@ class Vector { public: VectorWriteProxy<T> write; + typedef typename CowData<T>::Size Size; private: CowData<T> _cowdata; @@ -70,9 +71,9 @@ public: _FORCE_INLINE_ bool append(const T &p_elem) { return push_back(p_elem); } //alias void fill(T p_elem); - void remove_at(int p_index) { _cowdata.remove_at(p_index); } + void remove_at(Size p_index) { _cowdata.remove_at(p_index); } _FORCE_INLINE_ bool erase(const T &p_val) { - int idx = find(p_val); + Size idx = find(p_val); if (idx >= 0) { remove_at(idx); return true; @@ -87,17 +88,17 @@ public: _FORCE_INLINE_ void clear() { resize(0); } _FORCE_INLINE_ bool is_empty() const { return _cowdata.is_empty(); } - _FORCE_INLINE_ T get(int p_index) { return _cowdata.get(p_index); } - _FORCE_INLINE_ const T &get(int p_index) const { return _cowdata.get(p_index); } - _FORCE_INLINE_ void set(int p_index, const T &p_elem) { _cowdata.set(p_index, p_elem); } - _FORCE_INLINE_ int size() const { return _cowdata.size(); } - Error resize(int p_size) { return _cowdata.resize(p_size); } - Error resize_zeroed(int p_size) { return _cowdata.template resize<true>(p_size); } - _FORCE_INLINE_ const T &operator[](int p_index) const { return _cowdata.get(p_index); } - Error insert(int p_pos, T p_val) { return _cowdata.insert(p_pos, p_val); } - int find(const T &p_val, int p_from = 0) const { return _cowdata.find(p_val, p_from); } - int rfind(const T &p_val, int p_from = -1) const { return _cowdata.rfind(p_val, p_from); } - int count(const T &p_val) const { return _cowdata.count(p_val); } + _FORCE_INLINE_ T get(Size p_index) { return _cowdata.get(p_index); } + _FORCE_INLINE_ const T &get(Size p_index) const { return _cowdata.get(p_index); } + _FORCE_INLINE_ void set(Size p_index, const T &p_elem) { _cowdata.set(p_index, p_elem); } + _FORCE_INLINE_ Size size() const { return _cowdata.size(); } + Error resize(Size p_size) { return _cowdata.resize(p_size); } + Error resize_zeroed(Size p_size) { return _cowdata.template resize<true>(p_size); } + _FORCE_INLINE_ const T &operator[](Size p_index) const { return _cowdata.get(p_index); } + Error insert(Size p_pos, T p_val) { return _cowdata.insert(p_pos, p_val); } + Size find(const T &p_val, Size p_from = 0) const { return _cowdata.find(p_val, p_from); } + Size rfind(const T &p_val, Size p_from = -1) const { return _cowdata.rfind(p_val, p_from); } + Size count(const T &p_val) const { return _cowdata.count(p_val); } void append_array(Vector<T> p_other); @@ -109,7 +110,7 @@ public: template <class Comparator, bool Validate = SORT_ARRAY_VALIDATE_ENABLED, class... Args> void sort_custom(Args &&...args) { - int len = _cowdata.size(); + Size len = _cowdata.size(); if (len == 0) { return; } @@ -119,12 +120,12 @@ public: sorter.sort(data, len); } - int bsearch(const T &p_value, bool p_before) { + Size bsearch(const T &p_value, bool p_before) { return bsearch_custom<_DefaultComparator<T>>(p_value, p_before); } template <class Comparator, class Value, class... Args> - int bsearch_custom(const Value &p_value, bool p_before, Args &&...args) { + Size bsearch_custom(const Value &p_value, bool p_before, Args &&...args) { SearchArray<T, Comparator> search{ args... }; return search.bisect(ptrw(), size(), p_value, p_before); } @@ -134,7 +135,7 @@ public: } void ordered_insert(const T &p_val) { - int i; + Size i; for (i = 0; i < _cowdata.size(); i++) { if (p_val < operator[](i)) { break; @@ -157,28 +158,28 @@ public: return ret; } - Vector<T> slice(int p_begin, int p_end = INT_MAX) const { + Vector<T> slice(Size p_begin, Size p_end = CowData<T>::MAX_INT) const { Vector<T> result; - const int s = size(); + const Size s = size(); - int begin = CLAMP(p_begin, -s, s); + Size begin = CLAMP(p_begin, -s, s); if (begin < 0) { begin += s; } - int end = CLAMP(p_end, -s, s); + Size end = CLAMP(p_end, -s, s); if (end < 0) { end += s; } ERR_FAIL_COND_V(begin > end, result); - int result_size = end - begin; + Size result_size = end - begin; result.resize(result_size); const T *const r = ptr(); T *const w = result.ptrw(); - for (int i = 0; i < result_size; ++i) { + for (Size i = 0; i < result_size; ++i) { w[i] = r[begin + i]; } @@ -186,11 +187,11 @@ public: } bool operator==(const Vector<T> &p_arr) const { - int s = size(); + Size s = size(); if (s != p_arr.size()) { return false; } - for (int i = 0; i < s; i++) { + for (Size i = 0; i < s; i++) { if (operator[](i) != p_arr[i]) { return false; } @@ -199,11 +200,11 @@ public: } bool operator!=(const Vector<T> &p_arr) const { - int s = size(); + Size s = size(); if (s != p_arr.size()) { return true; } - for (int i = 0; i < s; i++) { + for (Size i = 0; i < s; i++) { if (operator[](i) != p_arr[i]) { return true; } @@ -280,7 +281,7 @@ public: Error err = _cowdata.resize(p_init.size()); ERR_FAIL_COND(err); - int i = 0; + Size i = 0; for (const T &element : p_init) { _cowdata.set(i++, element); } @@ -292,7 +293,7 @@ public: template <class T> void Vector<T>::reverse() { - for (int i = 0; i < size() / 2; i++) { + for (Size i = 0; i < size() / 2; i++) { T *p = ptrw(); SWAP(p[i], p[size() - i - 1]); } @@ -300,13 +301,13 @@ void Vector<T>::reverse() { template <class T> void Vector<T>::append_array(Vector<T> p_other) { - const int ds = p_other.size(); + const Size ds = p_other.size(); if (ds == 0) { return; } - const int bs = size(); + const Size bs = size(); resize(bs + ds); - for (int i = 0; i < ds; ++i) { + for (Size i = 0; i < ds; ++i) { ptrw()[bs + i] = p_other[i]; } } @@ -323,7 +324,7 @@ bool Vector<T>::push_back(T p_elem) { template <class T> void Vector<T>::fill(T p_elem) { T *p = ptrw(); - for (int i = 0; i < size(); i++) { + for (Size i = 0; i < size(); i++) { p[i] = p_elem; } } diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index 03836985f3..90f7e7b44a 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -666,7 +666,7 @@ struct _VariantCall { CharString cs; cs.resize(p_instance->size() + 1); memcpy(cs.ptrw(), r, p_instance->size()); - cs[p_instance->size()] = 0; + cs[(int)p_instance->size()] = 0; s = cs.get_data(); } |