Merge pull request #95469 from bruvzg/cowdata_unref

`CowData` remove hardcoded offset and unused argument from `_unref`.
This commit is contained in:
Rémi Verschelde 2024-08-16 10:36:28 +02:00
commit f1d6677713
No known key found for this signature in database
GPG key ID: C3336907360768E1

View file

@ -160,7 +160,7 @@ private:
return *out; return *out;
} }
void _unref(void *p_data); void _unref();
void _ref(const CowData *p_from); void _ref(const CowData *p_from);
void _ref(const CowData &p_from); void _ref(const CowData &p_from);
USize _copy_on_write(); USize _copy_on_write();
@ -245,30 +245,29 @@ public:
}; };
template <typename T> template <typename T>
void CowData<T>::_unref(void *p_data) { void CowData<T>::_unref() {
if (!p_data) { if (!_ptr) {
return; return;
} }
SafeNumeric<USize> *refc = _get_refcount(); SafeNumeric<USize> *refc = _get_refcount();
if (refc->decrement() > 0) { if (refc->decrement() > 0) {
return; // still in use return; // still in use
} }
// clean up // clean up
if constexpr (!std::is_trivially_destructible_v<T>) { if constexpr (!std::is_trivially_destructible_v<T>) {
USize *count = _get_size(); USize current_size = *_get_size();
T *data = (T *)(count + 1);
for (USize i = 0; i < *count; ++i) { for (USize i = 0; i < current_size; ++i) {
// call destructors // call destructors
data[i].~T(); T *t = &_ptr[i];
t->~T();
} }
} }
// free mem // free mem
Memory::free_static(((uint8_t *)p_data) - DATA_OFFSET, false); Memory::free_static(((uint8_t *)_ptr) - DATA_OFFSET, false);
} }
template <typename T> template <typename T>
@ -303,7 +302,7 @@ typename CowData<T>::USize CowData<T>::_copy_on_write() {
} }
} }
_unref(_ptr); _unref();
_ptr = _data_ptr; _ptr = _data_ptr;
rc = 1; rc = 1;
@ -324,7 +323,7 @@ Error CowData<T>::resize(Size p_size) {
if (p_size == 0) { if (p_size == 0) {
// wants to clean up // wants to clean up
_unref(_ptr); _unref();
_ptr = nullptr; _ptr = nullptr;
return OK; return OK;
} }
@ -463,7 +462,7 @@ void CowData<T>::_ref(const CowData &p_from) {
return; // self assign, do nothing. return; // self assign, do nothing.
} }
_unref(_ptr); _unref();
_ptr = nullptr; _ptr = nullptr;
if (!p_from._ptr) { if (!p_from._ptr) {
@ -477,7 +476,7 @@ void CowData<T>::_ref(const CowData &p_from) {
template <typename T> template <typename T>
CowData<T>::~CowData() { CowData<T>::~CowData() {
_unref(_ptr); _unref();
} }
#if defined(__GNUC__) && !defined(__clang__) #if defined(__GNUC__) && !defined(__clang__)