1 | /* |
2 | * Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"). |
5 | * You may not use this file except in compliance with the License. |
6 | * A copy of the License is located at |
7 | * |
8 | * http://aws.amazon.com/apache2.0 |
9 | * |
10 | * or in the "license" file accompanying this file. This file is distributed |
11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
12 | * express or implied. See the License for the specific language governing |
13 | * permissions and limitations under the License. |
14 | */ |
15 | |
16 | #include <aws/core/utils/memory/AWSMemory.h> |
17 | |
18 | #include <aws/core/utils/memory/MemorySystemInterface.h> |
19 | #include <aws/common/common.h> |
20 | |
21 | #include <atomic> |
22 | |
23 | using namespace Aws::Utils; |
24 | using namespace Aws::Utils::Memory; |
25 | |
26 | #ifdef USE_AWS_MEMORY_MANAGEMENT |
27 | static MemorySystemInterface* AWSMemorySystem(nullptr); |
28 | #endif // USE_AWS_MEMORY_MANAGEMENT |
29 | |
30 | namespace Aws |
31 | { |
32 | namespace Utils |
33 | { |
34 | namespace Memory |
35 | { |
36 | |
37 | void InitializeAWSMemorySystem(MemorySystemInterface& memorySystem) |
38 | { |
39 | #ifdef USE_AWS_MEMORY_MANAGEMENT |
40 | if(AWSMemorySystem != nullptr) |
41 | { |
42 | AWSMemorySystem->End(); |
43 | } |
44 | |
45 | AWSMemorySystem = &memorySystem; |
46 | AWSMemorySystem->Begin(); |
47 | #else |
48 | AWS_UNREFERENCED_PARAM(memorySystem); |
49 | #endif // USE_AWS_MEMORY_MANAGEMENT |
50 | } |
51 | |
52 | void ShutdownAWSMemorySystem(void) |
53 | { |
54 | #ifdef USE_AWS_MEMORY_MANAGEMENT |
55 | if(AWSMemorySystem != nullptr) |
56 | { |
57 | AWSMemorySystem->End(); |
58 | } |
59 | AWSMemorySystem = nullptr; |
60 | #endif // USE_AWS_MEMORY_MANAGEMENT |
61 | } |
62 | |
63 | MemorySystemInterface* GetMemorySystem() |
64 | { |
65 | #ifdef USE_AWS_MEMORY_MANAGEMENT |
66 | return AWSMemorySystem; |
67 | #else |
68 | return nullptr; |
69 | #endif // USE_AWS_MEMORY_MANAGEMENT |
70 | } |
71 | |
72 | } // namespace Memory |
73 | } // namespace Utils |
74 | |
75 | void* Malloc(const char* allocationTag, size_t allocationSize) |
76 | { |
77 | Aws::Utils::Memory::MemorySystemInterface* memorySystem = Aws::Utils::Memory::GetMemorySystem(); |
78 | |
79 | void* rawMemory = nullptr; |
80 | if(memorySystem != nullptr) |
81 | { |
82 | rawMemory = memorySystem->AllocateMemory(allocationSize, 1, allocationTag); |
83 | } |
84 | else |
85 | { |
86 | rawMemory = malloc(allocationSize); |
87 | } |
88 | |
89 | return rawMemory; |
90 | } |
91 | |
92 | |
93 | void Free(void* memoryPtr) |
94 | { |
95 | if(memoryPtr == nullptr) |
96 | { |
97 | return; |
98 | } |
99 | |
100 | Aws::Utils::Memory::MemorySystemInterface* memorySystem = Aws::Utils::Memory::GetMemorySystem(); |
101 | if(memorySystem != nullptr) |
102 | { |
103 | memorySystem->FreeMemory(memoryPtr); |
104 | } |
105 | else |
106 | { |
107 | free(memoryPtr); |
108 | } |
109 | } |
110 | |
111 | static void* MemAcquire(aws_allocator* allocator, size_t size) |
112 | { |
113 | (void)allocator; // unused; |
114 | return Aws::Malloc("MemAcquire" , size); |
115 | } |
116 | |
117 | static void MemRelease(aws_allocator* allocator, void* ptr) |
118 | { |
119 | (void)allocator; // unused; |
120 | return Aws::Free(ptr); |
121 | } |
122 | |
123 | static aws_allocator create_aws_allocator() |
124 | { |
125 | #if (__GNUC__ == 4) && !defined(__clang__) |
126 | AWS_SUPPRESS_WARNING("-Wmissing-field-initializers" , aws_allocator wrapper{};); |
127 | #else |
128 | aws_allocator wrapper{}; |
129 | #endif |
130 | wrapper.mem_acquire = MemAcquire; |
131 | wrapper.mem_release = MemRelease; |
132 | wrapper.mem_realloc = nullptr; |
133 | return wrapper; |
134 | } |
135 | |
136 | aws_allocator* get_aws_allocator() |
137 | { |
138 | static aws_allocator wrapper = create_aws_allocator(); |
139 | return &wrapper; |
140 | } |
141 | |
142 | } // namespace Aws |
143 | |
144 | |
145 | |