@@ -84,9 +84,9 @@ class TAllocation {
8484 // makes the compiler print warnings about 0 length memsets,
8585 // even with the if() protecting them.
8686# ifdef GUARD_BLOCKS
87- memset (preGuard (), guardBlockBeginVal, guardBlockSize);
87+ memset (preGuard (), guardBlockBeginVal, guardBlockSize () );
8888 memset (data (), userDataFill, size);
89- memset (postGuard (), guardBlockEndVal, guardBlockSize);
89+ memset (postGuard (), guardBlockEndVal, guardBlockSize () );
9090# endif
9191 }
9292
@@ -100,20 +100,20 @@ class TAllocation {
100100 // Return total size needed to accommodate user buffer of 'size',
101101 // plus our tracking data.
102102 inline static size_t allocationSize (size_t size) {
103- return size + 2 * guardBlockSize + headerSize ();
103+ return size + 2 * guardBlockSize () + headerSize ();
104104 }
105105
106106 // Offset from surrounding buffer to get to user data buffer.
107107 inline static unsigned char * offsetAllocation (unsigned char * m) {
108- return m + guardBlockSize + headerSize ();
108+ return m + guardBlockSize () + headerSize ();
109109 }
110110
111111private:
112112 void checkGuardBlock (unsigned char * blockMem, unsigned char val, const char * locText) const ;
113113
114114 // Find offsets to pre and post guard blocks, and user data buffer
115115 unsigned char * preGuard () const { return mem + headerSize (); }
116- unsigned char * data () const { return preGuard () + guardBlockSize; }
116+ unsigned char * data () const { return preGuard () + guardBlockSize () ; }
117117 unsigned char * postGuard () const { return data () + size; }
118118
119119 size_t size; // size of the user data area
@@ -125,15 +125,19 @@ class TAllocation {
125125 static inline constexpr unsigned char userDataFill = 0xcd ;
126126
127127# ifdef GUARD_BLOCKS
128- static inline constexpr size_t guardBlockSize = 16 ;
129- # else
130- static inline constexpr size_t guardBlockSize = 0 ;
131- # endif
132-
133- # ifdef GUARD_BLOCKS
134- inline static size_t headerSize () { return sizeof (TAllocation); }
128+ inline static constexpr size_t headerSize () { return sizeof (TAllocation); }
129+ inline static constexpr size_t guardBlockSize () {
130+ constexpr size_t minGuardSize = 16 ;
131+ constexpr size_t alignmentSize = 16 ;
132+ constexpr size_t guardLayoutSize =
133+ (minGuardSize + sizeof (TAllocation) + alignmentSize - 1 ) & ~(alignmentSize - 1 );
134+ static_assert ((guardLayoutSize % alignmentSize) == 0 ,
135+ " Guard block layout is not 16-byte aligned." );
136+ return guardLayoutSize - sizeof (TAllocation);
137+ }
135138# else
136- inline static size_t headerSize () { return 0 ; }
139+ inline static constexpr size_t headerSize () { return 0 ; }
140+ inline static constexpr size_t guardBlockSize () { return 0 ; }
137141# endif
138142};
139143
0 commit comments