From 42e61d3a6e069d1768fe356aa4689691c34dbbda Mon Sep 17 00:00:00 2001 From: Eugene Kozlov Date: Sun, 20 Oct 2024 18:55:54 +0400 Subject: [PATCH] Restore Android SDK 21 support. --- BuildTools/Android/tests/build.gradle | 2 +- Common/src/DefaultRawMemoryAllocator.cpp | 33 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/BuildTools/Android/tests/build.gradle b/BuildTools/Android/tests/build.gradle index 7e47ed99e..6d6ae069a 100644 --- a/BuildTools/Android/tests/build.gradle +++ b/BuildTools/Android/tests/build.gradle @@ -9,7 +9,7 @@ android { defaultConfig { applicationId 'DE.Android.Tests' targetSdkVersion 32 - minSdkVersion 28 + minSdkVersion 21 ndk { abiFilters = [] diff --git a/Common/src/DefaultRawMemoryAllocator.cpp b/Common/src/DefaultRawMemoryAllocator.cpp index 84b62ac46..2cd9a125c 100644 --- a/Common/src/DefaultRawMemoryAllocator.cpp +++ b/Common/src/DefaultRawMemoryAllocator.cpp @@ -39,6 +39,35 @@ namespace Diligent { +namespace +{ + +void* AllocateAlignedFallback(size_t Size, size_t Alignment) +{ + constexpr size_t PointerSize = sizeof(void*); + const size_t AdjustedAlignment = (std::max)(Alignment, PointerSize); + + void* Pointer = malloc(Size + AdjustedAlignment + PointerSize); + void* AlignedPointer = AlignUp(reinterpret_cast(reinterpret_cast(Pointer) + PointerSize), AdjustedAlignment); + + void** StoredPointer = reinterpret_cast(AlignedPointer) - 1; + VERIFY_EXPR(StoredPointer >= Pointer); + *StoredPointer = Pointer; + + return AlignedPointer; +} + +void FreeAlignedFallback(void* Ptr) +{ + if (Ptr != nullptr) + { + void* OriginalPointer = *(reinterpret_cast(Ptr) - 1); + free(OriginalPointer); + } +} + +} // namespace + DefaultRawMemoryAllocator::DefaultRawMemoryAllocator() { } @@ -66,6 +95,8 @@ void* DefaultRawMemoryAllocator::AllocateAligned(size_t Size, size_t Alignment, return _aligned_malloc_dbg(Size, Alignment, dbgFileName, dbgLineNumber); #elif defined(_MSC_VER) || defined(__MINGW64__) || defined(__MINGW32__) return _aligned_malloc(Size, Alignment); +#elif PLATFORM_ANDROID && __ANDROID_API__ < 28 + return AllocateAlignedFallback(Size, Alignment); #else return aligned_alloc(Alignment, Size); #endif @@ -75,6 +106,8 @@ void DefaultRawMemoryAllocator::FreeAligned(void* Ptr) { #if defined(_MSC_VER) || defined(__MINGW64__) || defined(__MINGW32__) _aligned_free(Ptr); +#elif PLATFORM_ANDROID && __ANDROID_API__ < 28 + FreeAlignedFallback(Ptr); #else free(Ptr); #endif