1 /**
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  * SPDX-License-Identifier: Apache-2.0.
4  */
5 
6 #include <aws/core/utils/memory/AWSMemory.h>
7 
8 #include <aws/core/utils/memory/MemorySystemInterface.h>
9 #include <aws/common/common.h>
10 
11 #include <atomic>
12 
13 using namespace Aws::Utils;
14 using namespace Aws::Utils::Memory;
15 
16 #ifdef USE_AWS_MEMORY_MANAGEMENT
17   static MemorySystemInterface* AWSMemorySystem(nullptr);
18 #endif // USE_AWS_MEMORY_MANAGEMENT
19 
20 namespace Aws
21 {
22 namespace Utils
23 {
24 namespace Memory
25 {
26 
InitializeAWSMemorySystem(MemorySystemInterface & memorySystem)27 void InitializeAWSMemorySystem(MemorySystemInterface& memorySystem)
28 {
29     #ifdef USE_AWS_MEMORY_MANAGEMENT
30         if(AWSMemorySystem != nullptr)
31         {
32             AWSMemorySystem->End();
33         }
34 
35         AWSMemorySystem = &memorySystem;
36         AWSMemorySystem->Begin();
37     #else
38         AWS_UNREFERENCED_PARAM(memorySystem);
39     #endif // USE_AWS_MEMORY_MANAGEMENT
40 }
41 
ShutdownAWSMemorySystem(void)42 void ShutdownAWSMemorySystem(void)
43 {
44     #ifdef USE_AWS_MEMORY_MANAGEMENT
45         if(AWSMemorySystem != nullptr)
46         {
47             AWSMemorySystem->End();
48         }
49         AWSMemorySystem = nullptr;
50     #endif // USE_AWS_MEMORY_MANAGEMENT
51 }
52 
GetMemorySystem()53 MemorySystemInterface* GetMemorySystem()
54 {
55     #ifdef USE_AWS_MEMORY_MANAGEMENT
56         return AWSMemorySystem;
57     #else
58         return nullptr;
59     #endif // USE_AWS_MEMORY_MANAGEMENT
60 }
61 
62 } // namespace Memory
63 } // namespace Utils
64 
Malloc(const char * allocationTag,size_t allocationSize)65 void* Malloc(const char* allocationTag, size_t allocationSize)
66 {
67     Aws::Utils::Memory::MemorySystemInterface* memorySystem = Aws::Utils::Memory::GetMemorySystem();
68 
69     void* rawMemory = nullptr;
70     if(memorySystem != nullptr)
71     {
72         rawMemory = memorySystem->AllocateMemory(allocationSize, 1, allocationTag);
73     }
74     else
75     {
76         rawMemory = malloc(allocationSize);
77     }
78 
79     return rawMemory;
80 }
81 
82 
Free(void * memoryPtr)83 void Free(void* memoryPtr)
84 {
85     if(memoryPtr == nullptr)
86     {
87         return;
88     }
89 
90     Aws::Utils::Memory::MemorySystemInterface* memorySystem = Aws::Utils::Memory::GetMemorySystem();
91     if(memorySystem != nullptr)
92     {
93         memorySystem->FreeMemory(memoryPtr);
94     }
95     else
96     {
97         free(memoryPtr);
98     }
99 }
100 
MemAcquire(aws_allocator * allocator,size_t size)101 static void* MemAcquire(aws_allocator* allocator, size_t size)
102 {
103     (void)allocator; // unused;
104     return Aws::Malloc("MemAcquire", size);
105 }
106 
MemRelease(aws_allocator * allocator,void * ptr)107 static void MemRelease(aws_allocator* allocator, void* ptr)
108 {
109     (void)allocator; // unused;
110     return Aws::Free(ptr);
111 }
112 
create_aws_allocator()113 static aws_allocator create_aws_allocator()
114 {
115 #if (__GNUC__ == 4) && !defined(__clang__)
116     AWS_SUPPRESS_WARNING("-Wmissing-field-initializers", aws_allocator wrapper{};);
117 #else
118     aws_allocator wrapper{};
119 #endif
120     wrapper.mem_acquire = MemAcquire;
121     wrapper.mem_release = MemRelease;
122     wrapper.mem_realloc = nullptr;
123     return wrapper;
124 }
125 
get_aws_allocator()126 aws_allocator* get_aws_allocator()
127 {
128     static aws_allocator wrapper = create_aws_allocator();
129     return &wrapper;
130 }
131 
132 } // namespace Aws
133 
134 
135