DART 6.13.2
Loading...
Searching...
No Matches
StlAllocator-impl.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011-2022, The DART development contributors
3 * All rights reserved.
4 *
5 * The list of contributors can be found at:
6 * https://github.com/dartsim/dart/blob/master/LICENSE
7 *
8 * This file is provided under the following "BSD-style" License:
9 * Redistribution and use in source and binary forms, with or
10 * without modification, are permitted provided that the following
11 * conditions are met:
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
35
36namespace dart::common {
37
38//==============================================================================
39template <typename T>
41 : mBaseAllocator(baseAllocator)
42{
43 // Do nothing
44}
45
46//==============================================================================
47template <typename T>
49 : std::allocator<T>(other), mBaseAllocator(other.mBaseAllocator)
50{
51 // Do nothing
52}
53
54//==============================================================================
55template <typename T>
56template <class U>
58 : std::allocator<T>(other), mBaseAllocator(other.mBaseAllocator)
59{
60 // Do nothing
61}
62
63//==============================================================================
64template <typename T>
66 size_type n, const void* hint)
67{
68 (void)hint;
69 pointer ptr
70 = reinterpret_cast<pointer>(mBaseAllocator.allocate(n * sizeof(T)));
71
72 // Throw std::bad_alloc to comply 23.10.9.1
73 // Reference: https://stackoverflow.com/a/50326956/3122234
74 if (!ptr)
75 {
76 throw std::bad_alloc();
77 }
78
79 return ptr;
80}
81
82//==============================================================================
83template <typename T>
85{
86 mBaseAllocator.deallocate(pointer, n * sizeof(T));
87}
88
89//==============================================================================
90template <typename T>
91void StlAllocator<T>::print(std::ostream& os, int indent) const
92{
93 if (indent == 0)
94 {
95 os << "[StlAllocator]\n";
96 }
97 const std::string spaces(indent, ' ');
98 os << spaces << "base_allocator:\n";
99 mBaseAllocator.print(os, indent + 2);
100}
101
102//==============================================================================
103template <typename T>
104std::ostream& operator<<(std::ostream& os, const StlAllocator<T>& allocator)
105{
106 allocator.print(os);
107 return os;
108}
109
110} // namespace dart::common
Base class for std::allocator compatible allocators.
Definition MemoryAllocator.hpp:46
Wrapper class for MemoryAllocator to be compatible with std::allocator.
Definition StlAllocator.hpp:45
void print(std::ostream &os=std::cout, int indent=0) const
Prints state of the memory allocator.
Definition StlAllocator-impl.hpp:91
friend class StlAllocator
Definition StlAllocator.hpp:103
typename std::allocator_traits< Base >::size_type size_type
Definition StlAllocator.hpp:50
typename std::allocator_traits< Base >::pointer pointer
Definition StlAllocator.hpp:51
void deallocate(pointer pointer, size_type n)
Deallocates the storage referenced by the pointer p, which must be a pointer obtained by an earlier c...
Definition StlAllocator-impl.hpp:84
pointer allocate(size_type n, const void *hint=0)
Allocates n * sizeof(T) bytes of uninitialized storage.
Definition StlAllocator-impl.hpp:65
Definition Aspect.cpp:42
std::ostream & operator<<(std::ostream &os, const StlAllocator< T > &allocator)
Definition StlAllocator-impl.hpp:104