Skip to content

Commit 47030e9

Browse files
authored
Essentially fixed release build test crash on Mac because of alignment.
1 parent 0611eb7 commit 47030e9

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

aws-cpp-sdk-text-to-speech-tests/RunTests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@
1616
#include <aws/external/gtest.h>
1717
#include <aws/core/Aws.h>
1818
#include <aws/testing/platform/PlatformTesting.h>
19-
//#include <aws/testing/MemoryTesting.h>
19+
#include <aws/testing/MemoryTesting.h>
2020

2121
int main(int argc, char** argv)
2222
{
2323
Aws::SDKOptions options;
2424
options.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Trace;
25-
//AWS_BEGIN_MEMORY_TEST_EX(options, 1024, 128);
25+
AWS_BEGIN_MEMORY_TEST_EX(options, 1024, 128);
2626
Aws::Testing::InitPlatformTest(options);
2727

2828
Aws::InitAPI(options);
2929
::testing::InitGoogleTest(&argc, argv);
3030
int exitCode = RUN_ALL_TESTS();
3131
Aws::ShutdownAPI(options);
3232

33-
///AWS_END_MEMORY_TEST_EX;
33+
AWS_END_MEMORY_TEST_EX;
3434
Aws::Testing::ShutdownPlatformTest(options);
3535
return exitCode;
3636
}

testing-resources/source/MemoryTesting.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
#include <chrono>
2424
#include <thread>
2525
#include <cstdlib>
26+
#include <cstddef>
27+
#if defined(_MSC_VER) && _MSC_VER < 1900
28+
#define alignof __alignof
29+
#endif
2630

2731
BaseTestMemorySystem::BaseTestMemorySystem() :
2832
m_currentBytesAllocated(0),
@@ -47,11 +51,19 @@ void* BaseTestMemorySystem::AllocateMemory(std::size_t blockSize, std::size_t al
4751
m_maxBytesAllocated = (std::max)(m_maxBytesAllocated, m_currentBytesAllocated);
4852
m_totalBytesAllocated += blockSize;
4953

50-
char* rawMemory = reinterpret_cast<char*>(malloc(blockSize + sizeof(std::size_t)));
54+
// Note: malloc will always return an address aligned with alignof(std::max_align_t);
55+
// This alignment value is not always equals to sizeof(std::size_t). But one thing we can make sure is that
56+
// alignof(std::max_align_t) is always multiple of sizeof(std::size_t).
57+
// On some platforms, in place construction requires memory address must be aligned with alignof(std::max_align_t).
58+
// E.g on Mac, x86_64, sizeof(std::size_t) equals 8. but alignof(std::max_align_t) equals 16. std::function requires aligned memory address.
59+
// To record the malloc size and keep returned address align with 16, instead of malloc extra 8 bytes,
60+
// we end up with malloc extra 16 bytes.
61+
62+
char* rawMemory = reinterpret_cast<char*>(malloc(blockSize + alignof(std::max_align_t)));
5163
std::size_t *pointerToSize = reinterpret_cast<std::size_t*>(reinterpret_cast<void*>(rawMemory));
5264
*pointerToSize = blockSize;
5365

54-
return reinterpret_cast<void*>(rawMemory + sizeof(std::size_t));
66+
return reinterpret_cast<void*>(rawMemory + alignof(std::max_align_t));
5567
}
5668

5769
void BaseTestMemorySystem::FreeMemory(void* memoryPtr)
@@ -62,8 +74,7 @@ void BaseTestMemorySystem::FreeMemory(void* memoryPtr)
6274
--m_currentOutstandingAllocations;
6375
}
6476

65-
std::size_t *pointerToSize = reinterpret_cast<std::size_t*>(memoryPtr);
66-
--pointerToSize;
77+
std::size_t *pointerToSize = reinterpret_cast<std::size_t*>(reinterpret_cast<char*>(memoryPtr) - alignof(std::max_align_t));
6778
std::size_t blockSize = *pointerToSize;
6879

6980
ASSERT_GE(m_currentBytesAllocated, blockSize);

0 commit comments

Comments
 (0)