AWS SDK for C++

AWS SDK for C++ Version 1.11.843

Loading...
Searching...
No Matches
AWSAllocator.h
1
6#pragma once
7
8#include <aws/core/Core_EXPORTS.h>
9
10#include <aws/core/SDKConfig.h>
11#include <aws/core/utils/memory/AWSMemory.h>
12#include <aws/core/utils/memory/MemorySystemInterface.h>
13#include <aws/crt/StlAllocator.h>
14
15#include <memory>
16#include <cstdlib>
17
18namespace Aws
19{
20#ifdef USE_AWS_MEMORY_MANAGEMENT
29 template< typename T > using CrtAllocator = Aws::Crt::StlAllocator<T>;
30
35 template <typename T>
36 class Allocator : public std::allocator<T>
37 {
38 public:
39
40 typedef std::allocator<T> Base;
41
42 Allocator() throw() :
43 Base()
44 {}
45
46 Allocator(const Allocator<T>& a) throw() :
47 Base(a)
48 {}
49
50 template <class U>
51 Allocator(const Allocator<U>& a) throw() :
52 Base(a)
53 {}
54
55 ~Allocator() throw() {}
56
57 typedef std::size_t size_type;
58
59 template<typename U>
60 struct rebind
61 {
62 typedef Allocator<U> other;
63 };
64
65 using RawPointer = typename std::allocator_traits<std::allocator<T>>::pointer;
66
67 RawPointer allocate(size_type n, const void *hint = nullptr)
68 {
69 AWS_UNREFERENCED_PARAM(hint);
70
71 return reinterpret_cast<RawPointer>(Malloc("AWSSTL", n * sizeof(T)));
72 }
73
74 void deallocate(RawPointer p, size_type n)
75 {
76 AWS_UNREFERENCED_PARAM(n);
77
78 Free(p);
79 }
80
81 };
82
83#ifdef __ANDROID__
84#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
85 template< typename T >
86 bool operator ==(const Allocator< T >& lhs, const Allocator< T >& rhs)
87 {
88 AWS_UNREFERENCED_PARAM(lhs);
89 AWS_UNREFERENCED_PARAM(rhs);
90
91 return false;
92 }
93#endif // _GLIBCXX_FULLY_DYNAMIC_STRING == 0
94#endif // __ANDROID__
95
96#else
97
98 template< typename T > using Allocator = std::allocator<T>;
99
100#endif // USE_AWS_MEMORY_MANAGEMENT
105 template<typename T, typename ...ArgTypes>
106 std::shared_ptr<T> MakeShared(const char* allocationTag, ArgTypes&&... args)
107 {
108#ifdef USE_AWS_MEMORY_MANAGEMENT
110 // Was InitAPI forgotten or ShutdownAPI already called or Aws:: class used as static?
111 // TODO: enforce to non-conditional assert
112 AWS_ASSERT(memorySystem && "Memory system is not initialized.");
113 AWS_UNREFERENCED_PARAM(memorySystem);
114#endif
115 AWS_UNREFERENCED_PARAM(allocationTag);
116
117 return std::allocate_shared<T, Aws::Allocator<T>>(Aws::Allocator<T>(), std::forward<ArgTypes>(args)...);
118 }
119
120
121} // namespace Aws
AWS_CORE_API MemorySystemInterface * GetMemorySystem()
std::shared_ptr< T > MakeShared(const char *allocationTag, ArgTypes &&... args)
std::allocator< T > Allocator
AWS_CORE_API void * Malloc(const char *allocationTag, size_t allocationSize)
AWS_CORE_API void Free(void *memoryPtr)