Skip to content

Commit

Permalink
Add std::allocator compatible memory allocator wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
jslee02 committed Dec 23, 2021
1 parent 09935e5 commit 25f8d84
Show file tree
Hide file tree
Showing 6 changed files with 301 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Added Castable class: [#1634](https://github.com/dartsim/dart/pull/1634)
* Added spdlog support as underlying logging framework: [#1633](https://github.com/dartsim/dart/pull/1633)
* Added MemoryAllocator and CAllocator: [#1636](https://github.com/dartsim/dart/pull/1636)
* Added std::allocator compatible memory allocator wrapper: [#1637](https://github.com/dartsim/dart/pull/1637)

* Dynamics

Expand Down
19 changes: 14 additions & 5 deletions dart/common/CAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "dart/common/CAllocator.hpp"

#include "dart/common/Console.hpp"
#include "dart/common/Logging.hpp"

namespace dart::common {
Expand All @@ -55,9 +56,15 @@ CAllocator::~CAllocator()
void* pointer = it.first;
size_t size = it.second;
total_size += size;
DART_FATAL("Found memory leak of {} bytes at {}!", size, pointer);
dterr << "Found memory leak of " << size << " bytes at " << pointer
<< "\n";
// TODO(JS): Change to DART_FATAL once the issue of calling spdlog in
// destructor is resolved.
}
DART_FATAL("Found potential memory leak of total {} bytes!", total_size);
dterr << "Found potential memory leak of total " << total_size
<< " bytes!\n";
// TODO(JS): Change to DART_FATAL once the issue of calling spdlog in
// destructor is resolved.
}
#endif
}
Expand Down Expand Up @@ -99,12 +106,14 @@ void CAllocator::deallocate(void* pointer, size_t size)
if (size != allocated_size)
{
DART_FATAL(
"Cannot deallocated memory {} because the deallocating size {} is "
"different from the allocated size {}.",
"Attempting to deallocate memory at {} of {} bytes that is different "
"from the allocated size {}, which is a critical bug. Deallocating "
"{} bytes.",
pointer,
size,
allocated_size,
allocated_size);
return;
size = allocated_size;
}
m_size -= size;
m_map_pointer_to_size.erase(it);
Expand Down
111 changes: 111 additions & 0 deletions dart/common/StlAllocator.hpp
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_
110 changes: 110 additions & 0 deletions dart/common/detail/StlAllocator-impl.hpp
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
1 change: 1 addition & 0 deletions unittests/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dart_add_test("unit" test_Optimizer)
dart_add_test("unit" test_Random)
dart_add_test("unit" test_ScrewJoint)
dart_add_test("unit" test_Signal)
dart_add_test("unit" test_StlAllocator)
dart_add_test("unit" test_Subscriptions)
dart_add_test("unit" test_TriMesh)
dart_add_test("unit" test_Uri)
Expand Down
64 changes: 64 additions & 0 deletions unittests/unit/test_StlAllocator.cpp
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();
}

0 comments on commit 25f8d84

Please sign in to comment.