AWS IoT Device SDK C++ v2  1.34.0
AWS IoT Device SDK C++ v2
StlAllocator.h
Go to the documentation of this file.
1 #pragma once
2 
7 #include <aws/crt/Allocator.h>
8 
9 #include <memory>
10 #include <type_traits>
11 
12 namespace Aws
13 {
14  namespace Crt
15  {
20  template <typename T> class StlAllocator : public std::allocator<T>
21  {
22  public:
23  using Base = std::allocator<T>;
24 
25  StlAllocator() noexcept : Base() { m_allocator = ApiAllocator(); }
26 
27  StlAllocator(Allocator *allocator) noexcept : Base() { m_allocator = allocator; }
28 
29  StlAllocator(const StlAllocator<T> &a) noexcept : Base(a) { m_allocator = a.m_allocator; }
30 
31  template <class U> StlAllocator(const StlAllocator<U> &a) noexcept : Base(a)
32  {
33  m_allocator = a.m_allocator;
34  }
35 
37 
38  using size_type = std::size_t;
39 
40  template <typename U> struct rebind
41  {
43  };
44 
45  using RawPointer = typename std::allocator_traits<std::allocator<T>>::pointer;
46 
47  RawPointer allocate(size_type n, const void *hint = nullptr)
48  {
49  (void)hint;
50  AWS_ASSERT(m_allocator);
51  return static_cast<RawPointer>(aws_mem_acquire(m_allocator, n * sizeof(T)));
52  }
53 
54 #if _LIBCPP_STD_VER > 20
55  std::allocation_result<T *> allocate_at_least(size_type n) { return {allocate(n), n}; }
56 #endif
57 
59  {
60  AWS_ASSERT(m_allocator);
61  aws_mem_release(m_allocator, p);
62  }
63 
65  };
66  } // namespace Crt
67 } // namespace Aws
Aws::Crt::StlAllocator::StlAllocator
StlAllocator(Allocator *allocator) noexcept
Definition: StlAllocator.h:27
Aws::Crt::StlAllocator::StlAllocator
StlAllocator(const StlAllocator< U > &a) noexcept
Definition: StlAllocator.h:31
Aws::Crt::StlAllocator::StlAllocator
StlAllocator() noexcept
Definition: StlAllocator.h:25
Aws::Crt::StlAllocator::rebind
Definition: StlAllocator.h:41
Aws::Crt::ApiAllocator
AWS_CRT_CPP_API Allocator * ApiAllocator() noexcept
Definition: Allocator.cpp:24
Aws::Crt::StlAllocator::rebind::other
StlAllocator< U > other
Definition: StlAllocator.h:42
Aws::Crt::StlAllocator::deallocate
void deallocate(RawPointer p, size_type)
Definition: StlAllocator.h:58
Aws::Crt::StlAllocator::size_type
std::size_t size_type
Definition: StlAllocator.h:38
Aws::Crt::StlAllocator::RawPointer
typename std::allocator_traits< std::allocator< T > >::pointer RawPointer
Definition: StlAllocator.h:45
Allocator.h
Aws::Crt::StlAllocator::Base
std::allocator< T > Base
Definition: StlAllocator.h:23
Aws::Crt::StlAllocator::allocate
RawPointer allocate(size_type n, const void *hint=nullptr)
Definition: StlAllocator.h:47
Aws
Definition: Allocator.h:11
Aws::Crt::StlAllocator::StlAllocator
StlAllocator(const StlAllocator< T > &a) noexcept
Definition: StlAllocator.h:29
Aws::Crt::Allocator
aws_allocator Allocator
Definition: Allocator.h:14
Aws::Crt::StlAllocator::m_allocator
Allocator * m_allocator
Definition: StlAllocator.h:64
Aws::Crt::StlAllocator::~StlAllocator
~StlAllocator()
Definition: StlAllocator.h:36
Aws::Crt::StlAllocator
Definition: StlAllocator.h:21