|
| 1 | +/* |
| 2 | + * Copyright (c) 2011-2021, 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 | + |
| 33 | +#ifndef DART_COMMON_STLALLOCATOR_HPP_ |
| 34 | +#define DART_COMMON_STLALLOCATOR_HPP_ |
| 35 | + |
| 36 | +#include <memory> |
| 37 | + |
| 38 | +#include "dart/common/MemoryAllocator.hpp" |
| 39 | + |
| 40 | +namespace dart::common { |
| 41 | + |
| 42 | +/// Wrapper class for MemoryAllocator to be compatible with std::allocator |
| 43 | +template <typename T> |
| 44 | +class StlAllocator : public std::allocator<T> |
| 45 | +{ |
| 46 | +public: |
| 47 | + // Type aliases |
| 48 | + using Base = std::allocator<T>; |
| 49 | + using value_type = typename std::allocator_traits<Base>::value_type; |
| 50 | + using size_type = typename std::allocator_traits<Base>::size_type; |
| 51 | + using pointer = typename std::allocator_traits<Base>::pointer; |
| 52 | + using const_pointer = typename std::allocator_traits<Base>::const_pointer; |
| 53 | + |
| 54 | + template <typename U> |
| 55 | + struct rebind |
| 56 | + { |
| 57 | + using other = StlAllocator<U>; |
| 58 | + }; |
| 59 | + |
| 60 | + /// Default constructor |
| 61 | + explicit StlAllocator( |
| 62 | + MemoryAllocator& base_allocator = MemoryAllocator::GetDefault()) noexcept; |
| 63 | + |
| 64 | + /// Copy constructor |
| 65 | + StlAllocator(const StlAllocator& other) throw(); |
| 66 | + |
| 67 | + /// Copy constructor |
| 68 | + template <class U> |
| 69 | + StlAllocator(const StlAllocator<U>& other) throw(); |
| 70 | + |
| 71 | + /// Destructor |
| 72 | + ~StlAllocator() = default; |
| 73 | + |
| 74 | + /// Allocates n * sizeof(T) bytes of uninitialized storage. |
| 75 | + /// |
| 76 | + /// @param[in] n: The number of objects to allocate sotrage for. |
| 77 | + /// @param[in] hint: Point to a nearby memory location. |
| 78 | + /// @return On success, the pointer to the beginning of newly allocated |
| 79 | + /// memory. |
| 80 | + /// @return On failure, a null pointer |
| 81 | + [[nodiscard]] pointer allocate(size_type n, const void* hint = 0); |
| 82 | + |
| 83 | + /// Deallocates the storage referenced by the pointer @c p, which must be a |
| 84 | + /// pointer obtained by an earlier cal to allocate(). |
| 85 | + /// |
| 86 | + /// @param[in] pointer: Pointer obtained from allocate(). |
| 87 | + /// @param[in] n: Number of objects earlier passed to allocate(). |
| 88 | + void deallocate(pointer pointer, size_type n); |
| 89 | + // TODO(JS): Make this constexpr once migrated to C++20 |
| 90 | + |
| 91 | + // TODO(JS): Add size_type max_size() const noexcept; |
| 92 | + |
| 93 | + /// Prints state of the memory allocator |
| 94 | + void print(std::ostream& os = std::cout, int indent = 0) const; |
| 95 | + |
| 96 | + /// Prints state of the memory allocator |
| 97 | + template <typename U> |
| 98 | + friend std::ostream& operator<<( |
| 99 | + std::ostream& os, const StlAllocator<U>& allocator); |
| 100 | + |
| 101 | +private: |
| 102 | + template <typename U> |
| 103 | + friend class StlAllocator; |
| 104 | + MemoryAllocator& m_base_allocator; |
| 105 | +}; |
| 106 | + |
| 107 | +} // namespace dart::common |
| 108 | + |
| 109 | +#include "dart/common/detail/StlAllocator-impl.hpp" |
| 110 | + |
| 111 | +#endif // DART_COMMON_STLALLOCATOR_HPP_ |
0 commit comments