From 8622f9ccf3523620902560c0e4d9ca203b3e5359 Mon Sep 17 00:00:00 2001 From: James Chen Date: Mon, 17 Jun 2024 18:21:58 +0800 Subject: [PATCH] fixed memory leak on x86 android devices. --- native/cocos/base/Macros.h | 4 ++++ native/cocos/renderer/gfx-base/GFXDef-common.h | 12 +++++++++--- native/cocos/renderer/gfx-base/GFXDef.cpp | 1 + 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/native/cocos/base/Macros.h b/native/cocos/base/Macros.h index 36fcbc497cd..6b2765cf422 100644 --- a/native/cocos/base/Macros.h +++ b/native/cocos/base/Macros.h @@ -409,3 +409,7 @@ It should work same as apples CFSwapInt32LittleToHost(..) #define CC_FORCE_INLINE inline #endif #endif + +// Macro definition to check if a type has padding +#define CC_CHECK_STRUCT_NO_PADDING(T) \ + static_assert(std::has_unique_object_representations_v, "ERROR: Type has padding bytes!") diff --git a/native/cocos/renderer/gfx-base/GFXDef-common.h b/native/cocos/renderer/gfx-base/GFXDef-common.h index 84926c05202..e979be6aa8c 100644 --- a/native/cocos/renderer/gfx-base/GFXDef-common.h +++ b/native/cocos/renderer/gfx-base/GFXDef-common.h @@ -1291,7 +1291,9 @@ struct ALIGNAS(8) ColorAttachment { LoadOp loadOp{LoadOp::CLEAR}; StoreOp storeOp{StoreOp::STORE}; GeneralBarrier *barrier{nullptr}; - +#if CC_CPU_ARCH == CC_CPU_ARCH_32 + uint32_t _padding{0}; +#endif EXPOSE_COPY_FN(ColorAttachment) }; @@ -1305,7 +1307,9 @@ struct ALIGNAS(8) DepthStencilAttachment { LoadOp stencilLoadOp{LoadOp::CLEAR}; StoreOp stencilStoreOp{StoreOp::STORE}; GeneralBarrier *barrier{nullptr}; - +#if CC_CPU_ARCH == CC_CPU_ARCH_32 + uint32_t _padding{0}; +#endif EXPOSE_COPY_FN(DepthStencilAttachment) }; @@ -1401,7 +1405,9 @@ struct ALIGNAS(8) BufferBarrierInfo { Queue *srcQueue{nullptr}; // @ts-nullable Queue *dstQueue{nullptr}; // @ts-nullable - +#if CC_CPU_ARCH == CC_CPU_ARCH_32 + uint32_t _padding{0}; +#endif EXPOSE_COPY_FN(BufferBarrierInfo) }; using BufferBarrierInfoList = ccstd::vector; diff --git a/native/cocos/renderer/gfx-base/GFXDef.cpp b/native/cocos/renderer/gfx-base/GFXDef.cpp index 986c28a1815..f3ed22f35a1 100644 --- a/native/cocos/renderer/gfx-base/GFXDef.cpp +++ b/native/cocos/renderer/gfx-base/GFXDef.cpp @@ -36,6 +36,7 @@ namespace gfx { // T must have no implicit padding template ccstd::hash_t quickHashTrivialStruct(const T *info, size_t count = 1) { + CC_CHECK_STRUCT_NO_PADDING(T); static_assert(std::is_trivially_copyable::value && sizeof(T) % 8 == 0, "T must be 8 bytes aligned and trivially copyable"); return ccstd::hash_range(reinterpret_cast(info), reinterpret_cast(info + count)); }