aws-crt-cpp
StlAllocator.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License").
6  * You may not use this file except in compliance with the License.
7  * A copy of the License is located at
8  *
9  * http://aws.amazon.com/apache2.0
10  *
11  * or in the "license" file accompanying this file. This file is distributed
12  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13  * express or implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16 
17 #include <memory>
18 
19 #include <aws/common/common.h>
20 
21 namespace Aws
22 {
23  namespace Crt
24  {
25  using Allocator = aws_allocator;
26  extern Allocator *g_allocator;
27 
28  template <typename T> class StlAllocator : public std::allocator<T>
29  {
30  public:
31  using Base = std::allocator<T>;
32 
33  StlAllocator() noexcept : Base() { m_allocator = g_allocator; }
34 
35  StlAllocator(Allocator *allocator) noexcept : Base() { m_allocator = allocator; }
36 
37  StlAllocator(const StlAllocator<T> &a) noexcept : Base(a) { m_allocator = a.m_allocator; }
38 
39  template <class U> StlAllocator(const StlAllocator<U> &a) noexcept : Base(a)
40  {
41  m_allocator = a.m_allocator;
42  }
43 
45 
46  using size_type = std::size_t;
47 
48  template <typename U> struct rebind
49  {
51  };
52 
53  typename Base::pointer allocate(size_type n, const void *hint = nullptr)
54  {
55  (void)hint;
56  AWS_ASSERT(m_allocator);
57  return reinterpret_cast<typename Base::pointer>(aws_mem_acquire(m_allocator, n * sizeof(T)));
58  }
59 
60  void deallocate(typename Base::pointer p, size_type)
61  {
62  AWS_ASSERT(m_allocator);
63  aws_mem_release(m_allocator, p);
64  }
65 
67  };
68  } // namespace Crt
69 } // namespace Aws
Aws::Crt::Allocator
aws_allocator Allocator
Definition: StlAllocator.h:25
Aws::Crt::StlAllocator::Base
std::allocator< T > Base
Definition: StlAllocator.h:31
Aws::Crt::StlAllocator::allocate
Base::pointer allocate(size_type n, const void *hint=nullptr)
Definition: StlAllocator.h:53
Aws::Crt::StlAllocator::m_allocator
Allocator * m_allocator
Definition: StlAllocator.h:66
Aws::Crt::StlAllocator::deallocate
void deallocate(typename Base::pointer p, size_type)
Definition: StlAllocator.h:60
Aws::Crt::StlAllocator::rebind
Definition: StlAllocator.h:49
Aws::Crt::g_allocator
Allocator * g_allocator
Definition: Api.cpp:28
Aws::Crt::StlAllocator::StlAllocator
StlAllocator(const StlAllocator< T > &a) noexcept
Definition: StlAllocator.h:37
Aws::Crt::StlAllocator::StlAllocator
StlAllocator() noexcept
Definition: StlAllocator.h:33
Aws::n
cJSON * n
Definition: cJSON.cpp:2079
Aws::Crt::StlAllocator::~StlAllocator
~StlAllocator()
Definition: StlAllocator.h:44
Aws
Definition: Api.h:25
Aws::p
cJSON * p
Definition: cJSON.cpp:2080
Aws::Crt::StlAllocator::size_type
std::size_t size_type
Definition: StlAllocator.h:46
Aws::Crt::StlAllocator::rebind::other
StlAllocator< U > other
Definition: StlAllocator.h:50
Aws::a
cJSON * a
Definition: cJSON.cpp:2081
Aws::Crt::StlAllocator
Definition: StlAllocator.h:29
Aws::Crt::StlAllocator::StlAllocator
StlAllocator(Allocator *allocator) noexcept
Definition: StlAllocator.h:35
Aws::Crt::StlAllocator::StlAllocator
StlAllocator(const StlAllocator< U > &a) noexcept
Definition: StlAllocator.h:39