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

Ensure COWData does not always reallocate on resize, fixes #22561 #37373

Merged
merged 1 commit into from
Mar 28, 2020
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
41 changes: 24 additions & 17 deletions core/cowdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ Error CowData<T>::resize(int p_size) {

ERR_FAIL_COND_V(p_size < 0, ERR_INVALID_PARAMETER);

if (p_size == size())
int current_size = size();

if (p_size == current_size)
return OK;

if (p_size == 0) {
Expand All @@ -266,24 +268,27 @@ Error CowData<T>::resize(int p_size) {
// possibly changing size, copy on write
_copy_on_write();

size_t current_alloc_size = _get_alloc_size(current_size);
size_t alloc_size;
ERR_FAIL_COND_V(!_get_alloc_size_checked(p_size, &alloc_size), ERR_OUT_OF_MEMORY);

if (p_size > size()) {
if (p_size > current_size) {

if (size() == 0) {
// alloc from scratch
uint32_t *ptr = (uint32_t *)Memory::alloc_static(alloc_size, true);
ERR_FAIL_COND_V(!ptr, ERR_OUT_OF_MEMORY);
*(ptr - 1) = 0; //size, currently none
*(ptr - 2) = 1; //refcount
if (alloc_size != current_alloc_size) {
if (current_size == 0) {
// alloc from scratch
uint32_t *ptr = (uint32_t *)Memory::alloc_static(alloc_size, true);
ERR_FAIL_COND_V(!ptr, ERR_OUT_OF_MEMORY);
*(ptr - 1) = 0; //size, currently none
*(ptr - 2) = 1; //refcount

_ptr = (T *)ptr;
_ptr = (T *)ptr;

} else {
void *_ptrnew = (T *)Memory::realloc_static(_ptr, alloc_size, true);
ERR_FAIL_COND_V(!_ptrnew, ERR_OUT_OF_MEMORY);
_ptr = (T *)(_ptrnew);
} else {
void *_ptrnew = (T *)Memory::realloc_static(_ptr, alloc_size, true);
ERR_FAIL_COND_V(!_ptrnew, ERR_OUT_OF_MEMORY);
_ptr = (T *)(_ptrnew);
}
}

// construct the newly created elements
Expand All @@ -298,7 +303,7 @@ Error CowData<T>::resize(int p_size) {

*_get_size() = p_size;

} else if (p_size < size()) {
} else if (p_size < current_size) {

if (!__has_trivial_destructor(T)) {
// deinitialize no longer needed elements
Expand All @@ -308,10 +313,12 @@ Error CowData<T>::resize(int p_size) {
}
}

void *_ptrnew = (T *)Memory::realloc_static(_ptr, alloc_size, true);
ERR_FAIL_COND_V(!_ptrnew, ERR_OUT_OF_MEMORY);
if (alloc_size != current_alloc_size) {
void *_ptrnew = (T *)Memory::realloc_static(_ptr, alloc_size, true);
ERR_FAIL_COND_V(!_ptrnew, ERR_OUT_OF_MEMORY);

_ptr = (T *)(_ptrnew);
_ptr = (T *)(_ptrnew);
}

*_get_size() = p_size;
}
Expand Down