Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CowData remove hardcoded offset and unused argument from _unref. #95469

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions core/templates/cowdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class CowData {
return *out;
}

void _unref(void *p_data);
void _unref();
void _ref(const CowData *p_from);
void _ref(const CowData &p_from);
USize _copy_on_write();
Expand Down Expand Up @@ -242,30 +242,29 @@ class CowData {
};

template <typename T>
void CowData<T>::_unref(void *p_data) {
if (!p_data) {
void CowData<T>::_unref() {
if (!_ptr) {
return;
}

SafeNumeric<USize> *refc = _get_refcount();

if (refc->decrement() > 0) {
return; // still in use
}
// clean up

if constexpr (!std::is_trivially_destructible_v<T>) {
USize *count = _get_size();
T *data = (T *)(count + 1);
USize current_size = *_get_size();

for (USize i = 0; i < *count; ++i) {
for (USize i = 0; i < current_size; ++i) {
// call destructors
data[i].~T();
T *t = &_ptr[i];
t->~T();
}
}

// free mem
Memory::free_static(((uint8_t *)p_data) - DATA_OFFSET, false);
Memory::free_static(((uint8_t *)_ptr) - DATA_OFFSET, false);
}

template <typename T>
Expand Down Expand Up @@ -300,7 +299,7 @@ typename CowData<T>::USize CowData<T>::_copy_on_write() {
}
}

_unref(_ptr);
_unref();
_ptr = _data_ptr;

rc = 1;
Expand All @@ -321,7 +320,7 @@ Error CowData<T>::resize(Size p_size) {

if (p_size == 0) {
// wants to clean up
_unref(_ptr);
_unref();
_ptr = nullptr;
return OK;
}
Expand Down Expand Up @@ -460,7 +459,7 @@ void CowData<T>::_ref(const CowData &p_from) {
return; // self assign, do nothing.
}

_unref(_ptr);
_unref();
_ptr = nullptr;

if (!p_from._ptr) {
Expand All @@ -474,7 +473,7 @@ void CowData<T>::_ref(const CowData &p_from) {

template <typename T>
CowData<T>::~CowData() {
_unref(_ptr);
_unref();
}

#if defined(__GNUC__) && !defined(__clang__)
Expand Down
Loading