+void deMemory_selfTest (void)
+{
+ static const struct
+ {
+ size_t numBytes;
+ size_t alignment;
+ } s_alignedAllocCases[] =
+ {
+ { 1, 1 },
+ { 1, 2 },
+ { 1, 256 },
+ { 1, 4096 },
+ { 547389, 1 },
+ { 547389, 2 },
+ { 547389, 256 },
+ { 547389, 4096 },
+ { 52532, 1<<4 },
+ { 52532, 1<<10 },
+ { 52532, 1<<16 },
+ };
+ static const struct
+ {
+ size_t initialSize;
+ size_t newSize;
+ size_t alignment;
+ } s_alignedReallocCases[] =
+ {
+ { 1, 1, 1 },
+ { 1, 1, 2 },
+ { 1, 1, 256 },
+ { 1, 1, 4096 },
+ { 1, 1241, 1 },
+ { 1, 1241, 2 },
+ { 1, 1241, 256 },
+ { 1, 1241, 4096 },
+ { 547389, 234, 1 },
+ { 547389, 234, 2 },
+ { 547389, 234, 256 },
+ { 547389, 234, 4096 },
+ { 52532, 421523, 1<<4 },
+ { 52532, 421523, 1<<10 },
+ { 52532, 421523, 1<<16 },
+ };
+
+ int caseNdx;
+
+ for (caseNdx = 0; caseNdx < DE_LENGTH_OF_ARRAY(s_alignedAllocCases); caseNdx++)
+ {
+ void* const ptr = deAlignedMalloc(s_alignedAllocCases[caseNdx].numBytes, s_alignedAllocCases[caseNdx].alignment);
+
+ DE_TEST_ASSERT(ptr);
+ DE_TEST_ASSERT(deIsAlignedPtr(ptr, s_alignedAllocCases[caseNdx].alignment));
+
+ deMemset(ptr, 0xaa, s_alignedAllocCases[caseNdx].numBytes);
+
+ deAlignedFree(ptr);
+ }
+
+ for (caseNdx = 0; caseNdx < DE_LENGTH_OF_ARRAY(s_alignedReallocCases); caseNdx++)
+ {
+ void* const ptr = deAlignedMalloc(s_alignedReallocCases[caseNdx].initialSize, s_alignedReallocCases[caseNdx].alignment);
+
+ DE_TEST_ASSERT(ptr);
+ DE_TEST_ASSERT(deIsAlignedPtr(ptr, s_alignedReallocCases[caseNdx].alignment));
+
+ deMemset(ptr, 0xaa, s_alignedReallocCases[caseNdx].initialSize);
+
+ {
+ void* const newPtr = deAlignedRealloc(ptr, s_alignedReallocCases[caseNdx].newSize, s_alignedReallocCases[caseNdx].alignment);
+ const size_t numPreserved = s_alignedReallocCases[caseNdx].newSize < s_alignedReallocCases[caseNdx].initialSize
+ ? s_alignedReallocCases[caseNdx].newSize
+ : s_alignedReallocCases[caseNdx].initialSize;
+ size_t off;
+
+ DE_TEST_ASSERT(newPtr);
+ DE_TEST_ASSERT(deIsAlignedPtr(ptr, s_alignedReallocCases[caseNdx].alignment));
+
+ for (off = 0; off < numPreserved; off++)
+ DE_TEST_ASSERT(*((const deUint8*)newPtr + off) == 0xaa);
+
+ deAlignedFree(newPtr);
+ }
+ }
+}
+