-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add std::allocator compatible memory allocator wrapper
- Loading branch information
Showing
6 changed files
with
301 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Copyright (c) 2011-2021, The DART development contributors | ||
* All rights reserved. | ||
* | ||
* The list of contributors can be found at: | ||
* https://github.com/dartsim/dart/blob/master/LICENSE | ||
* | ||
* This file is provided under the following "BSD-style" License: | ||
* Redistribution and use in source and binary forms, with or | ||
* without modification, are permitted provided that the following | ||
* conditions are met: | ||
* * Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* * Redistributions in binary form must reproduce the above | ||
* copyright notice, this list of conditions and the following | ||
* disclaimer in the documentation and/or other materials provided | ||
* with the distribution. | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND | ||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR | ||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | ||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | ||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | ||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#ifndef DART_COMMON_STLALLOCATOR_HPP_ | ||
#define DART_COMMON_STLALLOCATOR_HPP_ | ||
|
||
#include <memory> | ||
|
||
#include "dart/common/MemoryAllocator.hpp" | ||
|
||
namespace dart::common { | ||
|
||
/// Wrapper class for MemoryAllocator to be compatible with std::allocator | ||
template <typename T> | ||
class StlAllocator : public std::allocator<T> | ||
{ | ||
public: | ||
// Type aliases | ||
using Base = std::allocator<T>; | ||
using value_type = typename std::allocator_traits<Base>::value_type; | ||
using size_type = typename std::allocator_traits<Base>::size_type; | ||
using pointer = typename std::allocator_traits<Base>::pointer; | ||
using const_pointer = typename std::allocator_traits<Base>::const_pointer; | ||
|
||
template <typename U> | ||
struct rebind | ||
{ | ||
using other = StlAllocator<U>; | ||
}; | ||
|
||
/// Default constructor | ||
explicit StlAllocator( | ||
MemoryAllocator& base_allocator = MemoryAllocator::GetDefault()) noexcept; | ||
|
||
/// Copy constructor | ||
StlAllocator(const StlAllocator& other) throw(); | ||
|
||
/// Copy constructor | ||
template <class U> | ||
StlAllocator(const StlAllocator<U>& other) throw(); | ||
|
||
/// Destructor | ||
~StlAllocator() = default; | ||
|
||
/// Allocates n * sizeof(T) bytes of uninitialized storage. | ||
/// | ||
/// @param[in] n: The number of objects to allocate sotrage for. | ||
/// @param[in] hint: Point to a nearby memory location. | ||
/// @return On success, the pointer to the beginning of newly allocated | ||
/// memory. | ||
/// @return On failure, a null pointer | ||
[[nodiscard]] pointer allocate(size_type n, const void* hint = 0); | ||
|
||
/// Deallocates the storage referenced by the pointer @c p, which must be a | ||
/// pointer obtained by an earlier cal to allocate(). | ||
/// | ||
/// @param[in] pointer: Pointer obtained from allocate(). | ||
/// @param[in] n: Number of objects earlier passed to allocate(). | ||
void deallocate(pointer pointer, size_type n); | ||
// TODO(JS): Make this constexpr once migrated to C++20 | ||
|
||
// TODO(JS): Add size_type max_size() const noexcept; | ||
|
||
/// Prints state of the memory allocator | ||
void print(std::ostream& os = std::cout, int indent = 0) const; | ||
|
||
/// Prints state of the memory allocator | ||
template <typename U> | ||
friend std::ostream& operator<<( | ||
std::ostream& os, const StlAllocator<U>& allocator); | ||
|
||
private: | ||
template <typename U> | ||
friend class StlAllocator; | ||
MemoryAllocator& m_base_allocator; | ||
}; | ||
|
||
} // namespace dart::common | ||
|
||
#include "dart/common/detail/StlAllocator-impl.hpp" | ||
|
||
#endif // DART_COMMON_STLALLOCATOR_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Copyright (c) 2011-2021, The DART development contributors | ||
* All rights reserved. | ||
* | ||
* The list of contributors can be found at: | ||
* https://github.com/dartsim/dart/blob/master/LICENSE | ||
* | ||
* This file is provided under the following "BSD-style" License: | ||
* Redistribution and use in source and binary forms, with or | ||
* without modification, are permitted provided that the following | ||
* conditions are met: | ||
* * Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* * Redistributions in binary form must reproduce the above | ||
* copyright notice, this list of conditions and the following | ||
* disclaimer in the documentation and/or other materials provided | ||
* with the distribution. | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND | ||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR | ||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | ||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | ||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | ||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#include "dart/common/Logging.hpp" | ||
#include "dart/common/StlAllocator.hpp" | ||
|
||
namespace dart::common { | ||
|
||
//============================================================================== | ||
template <typename T> | ||
StlAllocator<T>::StlAllocator(MemoryAllocator& base_allocator) noexcept | ||
: m_base_allocator(base_allocator) | ||
{ | ||
// Do nothing | ||
} | ||
|
||
//============================================================================== | ||
template <typename T> | ||
StlAllocator<T>::StlAllocator(const StlAllocator& other) throw() | ||
: std::allocator<T>(other), m_base_allocator(other.m_base_allocator) | ||
{ | ||
// Do nothing | ||
} | ||
|
||
//============================================================================== | ||
template <typename T> | ||
template <class U> | ||
StlAllocator<T>::StlAllocator(const StlAllocator<U>& other) throw() | ||
: std::allocator<T>(other), m_base_allocator(other.m_base_allocator) | ||
{ | ||
// Do nothing | ||
} | ||
|
||
//============================================================================== | ||
template <typename T> | ||
typename StlAllocator<T>::pointer StlAllocator<T>::allocate( | ||
size_type n, const void* hint) | ||
{ | ||
(void)hint; | ||
pointer ptr | ||
= reinterpret_cast<pointer>(m_base_allocator.allocate(n * sizeof(T))); | ||
|
||
// Throw std::bad_alloc to comply 23.10.9.1 | ||
// Reference: https://stackoverflow.com/a/50326956/3122234 | ||
if (!ptr) | ||
{ | ||
throw std::bad_alloc(); | ||
} | ||
|
||
return ptr; | ||
} | ||
|
||
//============================================================================== | ||
template <typename T> | ||
void StlAllocator<T>::deallocate(pointer pointer, size_type n) | ||
{ | ||
m_base_allocator.deallocate(pointer, n * sizeof(T)); | ||
} | ||
|
||
//============================================================================== | ||
template <typename T> | ||
void StlAllocator<T>::print(std::ostream& os, int indent) const | ||
{ | ||
if (indent == 0) | ||
{ | ||
os << "[StlAllocator]\n"; | ||
} | ||
const std::string spaces(indent, ' '); | ||
os << spaces << "base_allocator:\n"; | ||
m_base_allocator.print(os, indent + 2); | ||
} | ||
|
||
//============================================================================== | ||
template <typename T> | ||
std::ostream& operator<<(std::ostream& os, const StlAllocator<T>& allocator) | ||
{ | ||
allocator.print(os); | ||
return os; | ||
} | ||
|
||
} // namespace dart::common |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright (c) 2011-2021, The DART development contributors | ||
* All rights reserved. | ||
* | ||
* The list of contributors can be found at: | ||
* https://github.com/dartsim/dart/blob/master/LICENSE | ||
* | ||
* This file is provided under the following "BSD-style" License: | ||
* Redistribution and use in source and binary forms, with or | ||
* without modification, are permitted provided that the following | ||
* conditions are met: | ||
* * Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* * Redistributions in binary form must reproduce the above | ||
* copyright notice, this list of conditions and the following | ||
* disclaimer in the documentation and/or other materials provided | ||
* with the distribution. | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND | ||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR | ||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | ||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | ||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | ||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#include <dart/common/StlAllocator.hpp> | ||
#include <gtest/gtest.h> | ||
|
||
#include "TestHelpers.hpp" | ||
|
||
using namespace dart; | ||
using namespace common; | ||
|
||
//============================================================================== | ||
TEST(StlAllocatorTest, Basics) | ||
{ | ||
auto a = StlAllocator<int>(); | ||
auto o1 = a.allocate(1); | ||
auto o2 = a.allocate(1); | ||
EXPECT_TRUE(o1 != nullptr); | ||
EXPECT_TRUE(o2 != nullptr); | ||
a.deallocate(o1, 1); | ||
a.deallocate(o2, 1); | ||
a.print(); | ||
} | ||
|
||
//============================================================================== | ||
TEST(StlAllocatorTest, StdVector) | ||
{ | ||
std::vector<int, StlAllocator<int>> vec; | ||
EXPECT_EQ(vec.capacity(), 0); | ||
vec.reserve(1); | ||
EXPECT_EQ(vec.capacity(), 1); | ||
vec.reserve(2); | ||
EXPECT_EQ(vec.capacity(), 2); | ||
vec.get_allocator().print(); | ||
} |