diff --git a/Source/Memory/AuHeapInternal.cpp b/Source/Memory/AuHeapInternal.cpp index fdd09545..4a2b8ce7 100644 --- a/Source/Memory/AuHeapInternal.cpp +++ b/Source/Memory/AuHeapInternal.cpp @@ -200,16 +200,40 @@ namespace Aurora::Memory void *InternalHeap::_ZRealloc(void *pBuffer, Types::size_t uLength) { - auto prevLength = GetHeapSize(pBuffer); - auto alloc = this->_ZAlloc(uLength); - if (!alloc) + if (pBuffer) + { + if (!uLength) + { + this->_Free(pBuffer); + return nullptr; + } + + auto uLengthOld = this->GetChunkSize(pBuffer); + + if (uLengthOld >= uLength) + { + return pBuffer; + } + + if (auto pNext = this->_ZAlloc(uLength)) + { + AuMemcpy(pNext, pBuffer, AuMin(uLengthOld, uLength)); + this->_Free(pBuffer); + return pNext; + } + else + { + return nullptr; + } + } + else if (uLength) + { + return this->_ZAlloc(uLength); + } + else { return nullptr; } - - AuMemcpy(alloc, pBuffer, AuMin(prevLength, uLength)); - this->_Free(pBuffer); - return alloc; } void *InternalHeap::_ZRealloc(void *pBuffer, Types::size_t uLength, Types::size_t uAlign) @@ -220,16 +244,40 @@ namespace Aurora::Memory void *InternalHeap::_FRealloc(void *pBuffer, Types::size_t uLength) { - auto prevLength = GetHeapSize(pBuffer); - auto alloc = this->_FAlloc(uLength); - if (!alloc) + if (pBuffer) + { + if (!uLength) + { + this->_Free(pBuffer); + return nullptr; + } + + auto uLengthOld = this->GetChunkSize(pBuffer); + + if (uLengthOld >= uLength) + { + return pBuffer; + } + + if (auto pNext = this->_FAlloc(uLength)) + { + AuMemcpy(pNext, pBuffer, AuMin(uLengthOld, uLength)); + this->_Free(pBuffer); + return pNext; + } + else + { + return nullptr; + } + } + else if (uLength) + { + return this->_FAlloc(uLength); + } + else { return nullptr; } - - AuMemcpy(alloc, pBuffer, AuMin(prevLength, uLength)); - this->_Free(pBuffer); - return alloc; } void *InternalHeap::_FRealloc(void *pBuffer, Types::size_t uLength, Types::size_t uAlign)