Skip to content

Commit

Permalink
Add default memory system for custom mem management
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyRyabinin committed Apr 25, 2024
1 parent a9fcd93 commit 581789a
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 5 deletions.
4 changes: 2 additions & 2 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down
11 changes: 11 additions & 0 deletions src/aws-cpp-sdk-core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,13 @@ if(MSVC)

endif(MSVC)

check_cxx_source_compiles("
#include <stdlib.h>
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})

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions src/aws-cpp-sdk-core/source/Aws.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
67 changes: 66 additions & 1 deletion src/aws-cpp-sdk-core/source/utils/memory/AWSMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions tests/aws-cpp-sdk-eventbridge-tests/EventBridgeTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ struct EventBridgeEndpointTestCase

const char* expectedEndpointId;
const char* expectedException;
using ExpectedHeaders = Aws::Vector<std::pair<const char*, const char*>>;
using ExpectedHeaders = std::vector<std::pair<const char*, const char*>>;
ExpectedHeaders expectedHeaders;
};

static const std::pair<const char*, const char*> X_AMZ_HEADER = {"x-amz-region-set", "*"};
static const Aws::Vector<EventBridgeEndpointTestCase> TEST_CASES = {
static const std::vector<EventBridgeEndpointTestCase> 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.", {}},
Expand Down

0 comments on commit 581789a

Please sign in to comment.