AWS SDK for C++  0.14.3
AWS SDK for C++
AWSAllocator.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010-2016 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 #pragma once
17 
18 #include <aws/core/Core_EXPORTS.h>
19 
20 #include <aws/core/SDKConfig.h>
23 
24 #include <memory>
25 #include <cstdlib>
26 
27 namespace Aws
28 {
29 #ifdef USE_AWS_MEMORY_MANAGEMENT
30 
33  template <typename T>
34  class Allocator : public std::allocator<T>
35  {
36  public:
37 
38  typedef std::allocator<T> Base;
39 
40  Allocator() throw() :
41  Base()
42  {}
43 
44  Allocator(const Allocator<T>& a) throw() :
45  Base(a)
46  {}
47 
48  template <class U>
49  Allocator(const Allocator<U>& a) throw() :
50  Base(a)
51  {}
52 
53  ~Allocator() throw() {}
54 
55  typedef std::size_t size_type;
56 
57  template<typename U>
58  struct rebind
59  {
60  typedef Allocator<U> other;
61  };
62 
63  typename Base::pointer allocate(size_type n, const void *hint = nullptr)
64  {
66 
67  return reinterpret_cast<typename Base::pointer>(Malloc("AWSSTL", n * sizeof(T)));
68  }
69 
70  void deallocate(typename Base::pointer p, size_type n)
71  {
73 
74  Free(p);
75  }
76 
77  };
78 
79 #ifdef __ANDROID__
80 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
81  template< typename T >
82  bool operator ==(const Allocator< T >& lhs, const Allocator< T >& rhs)
83  {
86 
87  return false;
88  }
89 #endif // _GLIBCXX_FULLY_DYNAMIC_STRING == 0
90 #endif // __ANDROID__
91 
92 #else
93 
94  template< typename T > using Allocator = std::allocator<T>;
95 
96 #endif // USE_AWS_MEMORY_MANAGEMENT
97 
101  template<typename T, typename ...ArgTypes>
102  std::shared_ptr<T> MakeShared(const char* allocationTag, ArgTypes&&... args)
103  {
104  AWS_UNREFERENCED_PARAM(allocationTag);
105 
106  return std::allocate_shared<T, Aws::Allocator<T>>(Aws::Allocator<T>(), std::forward<ArgTypes>(args)...);
107  }
108 
109 
110 } // namespace Aws
std::allocator< T > Allocator
Definition: AWSAllocator.h:94
#define AWS_UNREFERENCED_PARAM(x)
AWS_CORE_API void Free(void *memoryPtr)
AWS_CORE_API void * Malloc(const char *allocationTag, size_t allocationSize)
JSON (JavaScript Object Notation).
std::shared_ptr< T > MakeShared(const char *allocationTag, ArgTypes &&...args)
Definition: AWSAllocator.h:102