diff --git a/glslang/Include/PoolAlloc.h b/glslang/Include/PoolAlloc.h index 8a9284144a..1c51a08210 100644 --- a/glslang/Include/PoolAlloc.h +++ b/glslang/Include/PoolAlloc.h @@ -84,9 +84,9 @@ class TAllocation { // makes the compiler print warnings about 0 length memsets, // even with the if() protecting them. # ifdef GUARD_BLOCKS - memset(preGuard(), guardBlockBeginVal, guardBlockSize); + memset(preGuard(), guardBlockBeginVal, guardBlockSize()); memset(data(), userDataFill, size); - memset(postGuard(), guardBlockEndVal, guardBlockSize); + memset(postGuard(), guardBlockEndVal, guardBlockSize()); # endif } @@ -100,12 +100,12 @@ class TAllocation { // Return total size needed to accommodate user buffer of 'size', // plus our tracking data. inline static size_t allocationSize(size_t size) { - return size + 2 * guardBlockSize + headerSize(); + return size + 2 * guardBlockSize() + headerSize(); } // Offset from surrounding buffer to get to user data buffer. inline static unsigned char* offsetAllocation(unsigned char* m) { - return m + guardBlockSize + headerSize(); + return m + guardBlockSize() + headerSize(); } private: @@ -113,7 +113,7 @@ class TAllocation { // Find offsets to pre and post guard blocks, and user data buffer unsigned char* preGuard() const { return mem + headerSize(); } - unsigned char* data() const { return preGuard() + guardBlockSize; } + unsigned char* data() const { return preGuard() + guardBlockSize(); } unsigned char* postGuard() const { return data() + size; } size_t size; // size of the user data area @@ -125,15 +125,19 @@ class TAllocation { static inline constexpr unsigned char userDataFill = 0xcd; # ifdef GUARD_BLOCKS - static inline constexpr size_t guardBlockSize = 16; -# else - static inline constexpr size_t guardBlockSize = 0; -# endif - -# ifdef GUARD_BLOCKS - inline static size_t headerSize() { return sizeof(TAllocation); } + inline static constexpr size_t headerSize() { return sizeof(TAllocation); } + inline static constexpr size_t guardBlockSize() { + constexpr size_t minGuardSize = 16; + constexpr size_t alignmentSize = 16; + constexpr size_t guardLayoutSize = + (minGuardSize + sizeof(TAllocation) + alignmentSize - 1) & ~(alignmentSize - 1); + static_assert((guardLayoutSize % alignmentSize) == 0, + "Guard block layout is not 16-byte aligned."); + return guardLayoutSize - sizeof(TAllocation); + } # else - inline static size_t headerSize() { return 0; } + inline static constexpr size_t headerSize() { return 0; } + inline static constexpr size_t guardBlockSize() { return 0; } # endif }; diff --git a/glslang/MachineIndependent/PoolAlloc.cpp b/glslang/MachineIndependent/PoolAlloc.cpp index 93a3b0d12f..1ac9fbb28c 100644 --- a/glslang/MachineIndependent/PoolAlloc.cpp +++ b/glslang/MachineIndependent/PoolAlloc.cpp @@ -148,7 +148,7 @@ void TAllocation::checkGuardBlock(unsigned char*, unsigned char, const char*) co #endif { #ifdef GUARD_BLOCKS - for (size_t x = 0; x < guardBlockSize; x++) { + for (size_t x = 0; x < guardBlockSize(); x++) { if (blockMem[x] != val) { const int maxSize = 80; char assertMsg[maxSize]; @@ -160,7 +160,7 @@ void TAllocation::checkGuardBlock(unsigned char*, unsigned char, const char*) co } } #else - assert(guardBlockSize == 0); + assert(guardBlockSize() == 0); #endif }