-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathAwsSDKMemoryManager.h
74 lines (61 loc) · 1.91 KB
/
AwsSDKMemoryManager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
#include <atomic>
#include <cstddef>
#include <mutex>
namespace Aws
{
namespace IoTFleetWise
{
/**
* @brief Keep track of how much memory we are using for AWS SDK
*
* Note that this is not an allocator. The methods of this class should be called before calling
* some SDK operation and after the operation succeeds or fails.
*/
class AwsSDKMemoryManager
{
public:
static AwsSDKMemoryManager &getInstance();
/**
* @brief Set the Limit of maximal memory usage
*
* @param size
* @return true if setting succeeds
* @return false if setting fails
*/
bool setLimit( std::size_t size );
/**
* @brief Get the Limit object
*
* @return std::size_t
*/
std::size_t getLimit();
/**
* @brief Reserve a chunk of memory for usage later
*
* @return true if successfully reserved, false if the allocation exceeds the limit
*/
bool reserveMemory( std::size_t bytes );
/**
* @brief Release the memory reservation.
* The behavior is undefined if releaseReservedMemory is called without a matching reserveMemory call with the same
* number of bytes
*
* @param bytes The number of bytes to release that were reserved previously
* @return std::size_t Memory size currently in use plus reserved
*/
std::size_t releaseReservedMemory( std::size_t bytes );
private:
AwsSDKMemoryManager() = default;
/**
* @brief Usage tracking in terms of how much memory is in use - allocated but not yet deallocated
*
*/
std::atomic<std::size_t> mMemoryUsedAndReserved{ 0 };
static constexpr std::size_t MAXIMUM_AWS_SDK_HEAP_MEMORY_BYTES = 10000000;
size_t mMaximumAwsSDKMemorySize = MAXIMUM_AWS_SDK_HEAP_MEMORY_BYTES;
std::mutex mMutex;
};
} // namespace IoTFleetWise
} // namespace Aws