Making allocator use the MallocSchema so we can take full advantage of ASan
Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com>monroegm-disable-blank-issue-2
parent
a087fc06a9
commit
89e1b6db25
@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright (c) Contributors to the Open 3D Engine Project.
|
||||
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <CryCommon/LegacyAllocator.h>
|
||||
|
||||
namespace AZ
|
||||
{
|
||||
LegacyAllocator::pointer_type LegacyAllocator::Allocate(size_type byteSize, size_type alignment, int flags, const char* name, const char* fileName, int lineNum, unsigned int suppressStackRecord)
|
||||
{
|
||||
if (alignment == 0)
|
||||
{
|
||||
// Some STL containers, like std::vector, are assuming a specific minimum alignment. seems to have a requirement
|
||||
// Take a look at _Allocate_manually_vector_aligned in xmemory0
|
||||
alignment = sizeof(void*) * 2;
|
||||
}
|
||||
|
||||
pointer_type ptr = m_schema->Allocate(byteSize, alignment, flags, name, fileName, lineNum, suppressStackRecord);
|
||||
AZ_PROFILE_MEMORY_ALLOC_EX(AZ::Debug::ProfileCategory::MemoryReserved, fileName, lineNum, ptr, byteSize, name ? name : GetName());
|
||||
AZ_MEMORY_PROFILE(ProfileAllocation(ptr, byteSize, alignment, name, fileName, lineNum, suppressStackRecord));
|
||||
AZ_Assert(ptr || byteSize == 0, "OOM - Failed to allocate %zu bytes from LegacyAllocator", byteSize);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
// DeAllocate with file/line, to track when allocs were freed from Cry
|
||||
void LegacyAllocator::DeAllocate(pointer_type ptr, [[maybe_unused]] const char* file, [[maybe_unused]] const int line, size_type byteSize, size_type alignment)
|
||||
{
|
||||
AZ_PROFILE_MEMORY_FREE_EX(AZ::Debug::ProfileCategory::MemoryReserved, file, line, ptr);
|
||||
AZ_MEMORY_PROFILE(ProfileDeallocation(ptr, byteSize, alignment, nullptr));
|
||||
m_schema->DeAllocate(ptr, byteSize, alignment);
|
||||
}
|
||||
|
||||
// Realloc with file/line, because Cry uses realloc(nullptr) and realloc(ptr, 0) to mimic malloc/free
|
||||
LegacyAllocator::pointer_type LegacyAllocator::ReAllocate(pointer_type ptr, size_type newSize, size_type newAlignment, [[maybe_unused]] const char* file, [[maybe_unused]] const int line)
|
||||
{
|
||||
if (newAlignment == 0)
|
||||
{
|
||||
// Some STL containers, like std::vector, are assuming a specific minimum alignment. seems to have a requirement
|
||||
// Take a look at _Allocate_manually_vector_aligned in xmemory0
|
||||
newAlignment = sizeof(void*) * 2;
|
||||
}
|
||||
|
||||
AZ_MEMORY_PROFILE(ProfileReallocationBegin(ptr, newSize));
|
||||
AZ_PROFILE_MEMORY_FREE_EX(AZ::Debug::ProfileCategory::MemoryReserved, file, line, ptr);
|
||||
pointer_type newPtr = m_schema->ReAllocate(ptr, newSize, newAlignment);
|
||||
AZ_PROFILE_MEMORY_ALLOC_EX(AZ::Debug::ProfileCategory::MemoryReserved, file, line, newPtr, newSize, "LegacyAllocator Realloc");
|
||||
AZ_MEMORY_PROFILE(ProfileReallocationEnd(ptr, newPtr, newSize, newAlignment));
|
||||
AZ_Assert(newPtr || newSize == 0, "OOM - Failed to reallocate %zu bytes from LegacyAllocator", newSize);
|
||||
return newPtr;
|
||||
}
|
||||
|
||||
void LegacyAllocator::DeAllocate(pointer_type ptr, size_type byteSize, size_type alignment)
|
||||
{
|
||||
AZ_MEMORY_PROFILE(ProfileDeallocation(ptr, 0, 0, nullptr));
|
||||
Base::DeAllocate(ptr, byteSize, alignment);
|
||||
}
|
||||
|
||||
LegacyAllocator::pointer_type LegacyAllocator::ReAllocate(pointer_type ptr, size_type newSize, size_type newAlignment)
|
||||
{
|
||||
if (newAlignment == 0)
|
||||
{
|
||||
// Some STL containers, like std::vector, are assuming a specific minimum alignment. seems to have a requirement
|
||||
// Take a look at _Allocate_manually_vector_aligned in xmemory0
|
||||
newAlignment = sizeof(void*) * 2;
|
||||
}
|
||||
|
||||
AZ_MEMORY_PROFILE(ProfileReallocationBegin(ptr, newSize));
|
||||
pointer_type newPtr = Base::ReAllocate(ptr, newSize, newAlignment);
|
||||
AZ_MEMORY_PROFILE(ProfileReallocationEnd(ptr, newPtr, newSize, newAlignment));
|
||||
AZ_Assert(newPtr || newSize == 0, "OOM - Failed to reallocate %zu bytes from LegacyAllocator", newSize);
|
||||
return newPtr;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue