diff --git a/.cspell.json b/.cspell.json index c24d14b1715..ea7689601ab 100644 --- a/.cspell.json +++ b/.cspell.json @@ -11,7 +11,7 @@ "Bodyless", "HTTPGET", "ratelimiter", "Ratelimiter", "STDMETHODCALLTYPE", "CANTSAVE", "OLECHAR", "DISPID", "UNKNOWNNAME", "DISPPARAMS", "XMLHTTP", "comptr", "Metadataservice", "Streamfn", "HWAVEOUT", "matdesc", "Presigner", "xindex", "errortype", "waveout", "WAVEOUTCAPSA", "ALLOWSYNC", "WAVEHDR", "MMSYSERR", - "WAVEFORMATEX", "Unprepare", "DDISABLE_IMDSV1", "SENDREQUEST", "threadpool", + "WAVEFORMATEX", "Unprepare", "DDISABLE_IMDSV1", "SENDREQUEST", "threadpool", "stdlib", "ALLOC", // AWS general "Arns", "AMZN", "amzn", "Paulo", "Ningxia", "ISOB", "isob", "AWSXML", "IMDSV", "AWSSTL", // AWS Signature @@ -39,7 +39,7 @@ // MSVC "msvc", "MSFT", "LPDWORD", "DATAW", "mkgmtime", "vscprintf", "wtoi", "msxml", "runtimeobject", "winhttp", "Wininet", "HINTERNET", "ADDREQ", "LPCSTR", "MAKELANGID", "SUBLANG", "WSADATA", "Initate", "ioctlsocket", - "dupenv", "USERPROFILE", "subblock", "LANGANDCODEPAGE", + "dupenv", "USERPROFILE", "subblock", "LANGANDCODEPAGE", "CPPUNWIND", // Crypto "decryptor", "encryptor", "NTSTATUS", "PBYTE", "PUCHAR", "noconf", "HAMC", "PBCRYPT", "BCRYPT", "libcrypto", "AWSLC", "CBCCTS", "tweaklen", "taglen", "blockcipher", "AESGCM", "compated", "outdata", "Decrypto", "GCMAAD", diff --git a/src/aws-cpp-sdk-core/CMakeLists.txt b/src/aws-cpp-sdk-core/CMakeLists.txt index b1881deadfa..c0f30e2974e 100644 --- a/src/aws-cpp-sdk-core/CMakeLists.txt +++ b/src/aws-cpp-sdk-core/CMakeLists.txt @@ -534,6 +534,13 @@ if(MSVC) endif(MSVC) +check_cxx_source_compiles(" + #include + int main() { + void* ptr = aligned_alloc(24, 64); + return 0; + }" AWS_HAS_ALIGNED_ALLOC) + add_library(${PROJECT_NAME} ${AWS_NATIVE_SDK_SRC}) add_library(AWS::${PROJECT_NAME} ALIAS ${PROJECT_NAME}) @@ -561,6 +568,10 @@ if (CURL_HAS_TLS_PROXY) target_compile_definitions(${PROJECT_NAME} PRIVATE "CURL_HAS_TLS_PROXY") endif() +if (AWS_HAS_ALIGNED_ALLOC) + target_compile_definitions(${PROJECT_NAME} PRIVATE "AWS_HAS_ALIGNED_ALLOC") +endif() + set(Core_INCLUDES "${CMAKE_CURRENT_SOURCE_DIR}/include/") if(PLATFORM_CUSTOM) diff --git a/src/aws-cpp-sdk-core/include/aws/core/utils/memory/AWSMemory.h b/src/aws-cpp-sdk-core/include/aws/core/utils/memory/AWSMemory.h index 125b4e05a53..16960f0a465 100644 --- a/src/aws-cpp-sdk-core/include/aws/core/utils/memory/AWSMemory.h +++ b/src/aws-cpp-sdk-core/include/aws/core/utils/memory/AWSMemory.h @@ -38,6 +38,11 @@ namespace Aws */ AWS_CORE_API MemorySystemInterface* GetMemorySystem(); + /** + * Get the pointer to the SDK default memory system + */ + AWS_CORE_API MemorySystemInterface& GetDefaultMemorySystem(); + } // namespace Memory } // namespace Utils diff --git a/src/aws-cpp-sdk-core/source/Aws.cpp b/src/aws-cpp-sdk-core/source/Aws.cpp index 686d67d4e6f..9042176df8a 100644 --- a/src/aws-cpp-sdk-core/source/Aws.cpp +++ b/src/aws-cpp-sdk-core/source/Aws.cpp @@ -45,6 +45,10 @@ namespace Aws { Aws::Utils::Memory::InitializeAWSMemorySystem(*options.memoryManagementOptions.memoryManager); } + else + { + Aws::Utils::Memory::InitializeAWSMemorySystem(Utils::Memory::GetDefaultMemorySystem()); + } #endif // USE_AWS_MEMORY_MANAGEMENT Aws::Client::CoreErrorsMapper::InitCoreErrorsMapper(); if(options.loggingOptions.logLevel != Aws::Utils::Logging::LogLevel::Off) diff --git a/src/aws-cpp-sdk-core/source/utils/memory/AWSMemory.cpp b/src/aws-cpp-sdk-core/source/utils/memory/AWSMemory.cpp index 21627e98010..074450f4e2d 100644 --- a/src/aws-cpp-sdk-core/source/utils/memory/AWSMemory.cpp +++ b/src/aws-cpp-sdk-core/source/utils/memory/AWSMemory.cpp @@ -59,6 +59,71 @@ MemorySystemInterface* GetMemorySystem() #endif // USE_AWS_MEMORY_MANAGEMENT } +#if defined(__cpp_exceptions) || defined(_CPPUNWIND) || defined(__EXCEPTIONS) +#define AWS_HAS_EXCEPTIONS +#endif + +/** + * A default memory allocator + * It is used in case of custom memory management SDK build + * and no custom allocator provided by application. + */ +class AwsDefaultMemorySystem : public MemorySystemInterface +{ +public: +#if defined(AWS_HAS_EXCEPTIONS) + static std::bad_alloc s_OOM; +#endif + static AwsDefaultMemorySystem s_defMemSystem; + + virtual ~AwsDefaultMemorySystem() = default; + + void Begin() override + { + } + + void End() override + { + } + + void* AllocateMemory(std::size_t blockSize, std::size_t alignment, const char *allocationTag = nullptr) override + { + AWS_UNREFERENCED_PARAM(allocationTag); + void *ret; + +#if defined(AWS_HAS_ALIGNED_ALLOC) + if (alignment == 1) { + ret = malloc(blockSize); + } else { + ret = aligned_alloc(alignment, blockSize); + } +#else + AWS_UNREFERENCED_PARAM(alignment); + ret = malloc(blockSize); +#endif + if (ret == nullptr) { +#if defined(AWS_HAS_EXCEPTIONS) + throw s_OOM; +#endif + } + return ret; + } + + void FreeMemory(void* memoryPtr) override + { + free(memoryPtr); + } +}; +#if defined(AWS_HAS_EXCEPTIONS) +std::bad_alloc AwsDefaultMemorySystem::s_OOM; +#endif +AwsDefaultMemorySystem AwsDefaultMemorySystem::s_defMemSystem; + +MemorySystemInterface& GetDefaultMemorySystem() +{ + return AwsDefaultMemorySystem::s_defMemSystem; +} + } // namespace Memory } // namespace Utils @@ -67,7 +132,7 @@ void* Malloc(const char* allocationTag, size_t allocationSize) Aws::Utils::Memory::MemorySystemInterface* memorySystem = Aws::Utils::Memory::GetMemorySystem(); #ifdef USE_AWS_MEMORY_MANAGEMENT // Was InitAPI forgotten or ShutdownAPI already called or Aws:: class used as static? - // TODO: enforce to non-conditional assert AWS_ASSERT + // TODO: enforce to non-conditional assert AWS_ASSERT(memorySystem && "Memory system is not initialized."); #endif diff --git a/tests/aws-cpp-sdk-eventbridge-tests/EventBridgeTests.cpp b/tests/aws-cpp-sdk-eventbridge-tests/EventBridgeTests.cpp index cd0892f4143..b6596b6426c 100644 --- a/tests/aws-cpp-sdk-eventbridge-tests/EventBridgeTests.cpp +++ b/tests/aws-cpp-sdk-eventbridge-tests/EventBridgeTests.cpp @@ -137,12 +137,12 @@ struct EventBridgeEndpointTestCase const char* expectedEndpointId; const char* expectedException; - using ExpectedHeaders = Aws::Vector>; + using ExpectedHeaders = std::vector>; ExpectedHeaders expectedHeaders; }; static const std::pair X_AMZ_HEADER = {"x-amz-region-set", "*"}; -static const Aws::Vector TEST_CASES = { +static const std::vector TEST_CASES = { {"us-east-1", false, false, nullptr, "events.us-east-1.amazonaws.com", nullptr, {}}, {"us-east-1", false, false, "abc123.456def", "abc123.456def.endpoint.events.amazonaws.com", nullptr, {X_AMZ_HEADER}}, {"us-east-1", false, false, "badactor.aws?foo=bar", nullptr, "EndpointId must be a valid host label.", {}},