AWS IoT Device SDK C++ v2: crt/aws-crt-cpp/include/aws/crt/StlAllocator.h Source File

AWS IoT Device SDK C++ v2 1.14.0 AWS IoT Device SDK C++ v2

StlAllocator.h
Go to the documentation of this file.
1#pragma once
7#include <memory>
8
9#include <aws/common/common.h>
10#include <aws/crt/Exports.h>
11#include <type_traits>
12
13namespace Aws
14{
15 namespace Crt
16 {
17 using Allocator = aws_allocator;
18 extern AWS_CRT_CPP_API Allocator *g_allocator;
19
24 template <typename T> class StlAllocator : public std::allocator<T>
25 {
26 public:
27 using Base = std::allocator<T>;
28
29 StlAllocator() noexcept : Base() { m_allocator = g_allocator; }
30
31 StlAllocator(Allocator *allocator) noexcept : Base() { m_allocator = allocator; }
32
33 StlAllocator(const StlAllocator<T> &a) noexcept : Base(a) { m_allocator = a.m_allocator; }
34
35 template <class U> StlAllocator(const StlAllocator<U> &a) noexcept : Base(a)
36 {
37 m_allocator = a.m_allocator;
38 }
39
40 ~StlAllocator() {}
41
42 using size_type = std::size_t;
43
44 template <typename U> struct rebind
45 {
46 typedef StlAllocator<U> other;
47 };
48
49 using RawPointer = typename std::allocator_traits<std::allocator<T>>::pointer;
50
51 RawPointer allocate(size_type n, const void *hint = nullptr)
52 {
53 (void)hint;
54 AWS_ASSERT(m_allocator);
55 return static_cast<RawPointer>(aws_mem_acquire(m_allocator, n * sizeof(T)));
56 }
57
58 void deallocate(RawPointer p, size_type)
59 {
60 AWS_ASSERT(m_allocator);
61 aws_mem_release(m_allocator, p);
62 }
63
64 Allocator *m_allocator;
65 };
66 } // namespace Crt
67} // namespace Aws
Aws::Crt::StlAllocator
Definition: StlAllocator.h:25
Aws::Crt::StlAllocator::deallocate
void deallocate(RawPointer p, size_type)
Definition: StlAllocator.h:58
Aws::Crt::StlAllocator::RawPointer
typename std::allocator_traits< std::allocator< T > >::pointer RawPointer
Definition: StlAllocator.h:49
Aws::Crt::StlAllocator::allocate
RawPointer allocate(size_type n, const void *hint=nullptr)
Definition: StlAllocator.h:51
Aws::Crt::StlAllocator::size_type
std::size_t size_type
Definition: StlAllocator.h:42
Aws::Crt::StlAllocator::m_allocator
Allocator * m_allocator
Definition: StlAllocator.h:64
Aws::Crt::StlAllocator::StlAllocator
StlAllocator(Allocator *allocator) noexcept
Definition: StlAllocator.h:31
Aws::Crt::StlAllocator::StlAllocator
StlAllocator() noexcept
Definition: StlAllocator.h:29
Aws::Crt::StlAllocator::StlAllocator
StlAllocator(const StlAllocator< U > &a) noexcept
Definition: StlAllocator.h:35
Aws::Crt::StlAllocator::~StlAllocator
~StlAllocator()
Definition: StlAllocator.h:40
Aws::Crt::StlAllocator::Base
std::allocator< T > Base
Definition: StlAllocator.h:27
Aws::Crt::StlAllocator::StlAllocator
StlAllocator(const StlAllocator< T > &a) noexcept
Definition: StlAllocator.h:33
Exports.h
AWS_CRT_CPP_API
#define AWS_CRT_CPP_API
Definition: Exports.h:37
Aws::Crt::Allocator
aws_allocator Allocator
Definition: StlAllocator.h:17
Aws::Crt::g_allocator
AWS_CRT_CPP_API Allocator * g_allocator
Definition: Api.cpp:21
Aws
Definition: Api.h:17
Aws::n
cJSON * n
Definition: cJSON.cpp:2560
Aws::a
cJSON * a
Definition: cJSON.cpp:2562
Aws::p
cJSON * p
Definition: cJSON.cpp:2561
Aws::Crt::StlAllocator::rebind
Definition: StlAllocator.h:45
Aws::Crt::StlAllocator::rebind::other
StlAllocator< U > other
Definition: StlAllocator.h:46