Skip to content

Commit

Permalink
Add PoolAllocator class (#1645)
Browse files Browse the repository at this point in the history
  • Loading branch information
jslee02 authored Jan 6, 2022
1 parent 4041649 commit 5ef8c1f
Show file tree
Hide file tree
Showing 9 changed files with 684 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,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 custom memory allocators: [#1636](https://github.com/dartsim/dart/pull/1636), [#1637](https://github.com/dartsim/dart/pull/1637), [#1639](https://github.com/dartsim/dart/pull/1639)
* Added custom memory allocators: [#1636](https://github.com/dartsim/dart/pull/1636), [#1637](https://github.com/dartsim/dart/pull/1637), [#1639](https://github.com/dartsim/dart/pull/1639), [#1645](https://github.com/dartsim/dart/pull/1645)
* Added Stopwatch class to replace Timer: [#1638](https://github.com/dartsim/dart/pull/1638)

* Dynamics
Expand Down
28 changes: 27 additions & 1 deletion dart/common/CAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

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

namespace dart::common {

Expand Down Expand Up @@ -97,7 +98,7 @@ void* CAllocator::allocate(size_t size) noexcept
void CAllocator::deallocate(void* pointer, size_t size)
{
(void)size;
#ifndef NDEBUG
#ifndef NDEBUG // debug
std::lock_guard<std::mutex> lock(mMutex);
auto it = mMapPointerToSize.find(pointer);
if (it != mMapPointerToSize.end())
Expand Down Expand Up @@ -132,6 +133,31 @@ void CAllocator::deallocate(void* pointer, size_t size)
std::free(pointer);
}

#ifndef NDEBUG
//==============================================================================
bool CAllocator::isAllocated(void* pointer, size_t size) const noexcept
{
std::lock_guard<std::mutex> lock(mMutex);

const auto it = mMapPointerToSize.find(pointer);
if (it == mMapPointerToSize.end())
return false;

const auto& allocatedSize = it->second;
if (size != allocatedSize)
return false;

return true;
}

//==============================================================================
bool CAllocator::isEmpty() const noexcept
{
std::lock_guard<std::mutex> lock(mMutex);
return mMapPointerToSize.empty();
}
#endif

//==============================================================================
void CAllocator::print(std::ostream& os, int indent) const
{
Expand Down
9 changes: 9 additions & 0 deletions dart/common/CAllocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ class CAllocator : public MemoryAllocator
// Documentation inherited
void deallocate(void* pointer, size_t size) override;

#ifndef NDEBUG
// Documentation inherited
[[nodiscard]] bool isAllocated(void* pointer, size_t size) const
noexcept override;

// Documentation inherited
[[nodiscard]] bool isEmpty() const noexcept override;
#endif

// Documentation inherited
void print(std::ostream& os = std::cout, int indent = 0) const override;

Expand Down
25 changes: 25 additions & 0 deletions dart/common/FreeListAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,31 @@ void FreeListAllocator::deallocate(void* pointer, size_t size)
mFreeBlock = curr;
}

#ifndef NDEBUG
//==============================================================================
bool FreeListAllocator::isAllocated(void* pointer, size_t size) const noexcept
{
std::lock_guard<std::mutex> lock(mMutex);

const auto it = mMapPointerToSize.find(pointer);
if (it == mMapPointerToSize.end())
return false;

const auto& allocatedSize = it->second;
if (size != allocatedSize)
return false;

return true;
}

//==============================================================================
bool FreeListAllocator::isEmpty() const noexcept
{
std::lock_guard<std::mutex> lock(mMutex);
return mMapPointerToSize.empty();
}
#endif

//==============================================================================
bool FreeListAllocator::allocateMemoryBlock(size_t sizeToAllocate)
{
Expand Down
9 changes: 9 additions & 0 deletions dart/common/FreeListAllocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ class FreeListAllocator : public MemoryAllocator
// Documentation inherited
void deallocate(void* pointer, size_t size) override;

#ifndef NDEBUG
// Documentation inherited
[[nodiscard]] bool isAllocated(void* pointer, size_t size) const
noexcept override;

// Documentation inherited
[[nodiscard]] bool isEmpty() const noexcept override;
#endif

// Documentation inherited
void print(std::ostream& os = std::cout, int indent = 0) const override;

Expand Down
10 changes: 10 additions & 0 deletions dart/common/MemoryAllocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ class MemoryAllocator : public Castable<MemoryAllocator>
template <typename T>
void destroy(T* object) noexcept;

#ifndef NDEBUG
/// Returns true if a pointer is allocated by this allocator.
[[nodiscard]] virtual bool isAllocated(void* pointer, size_t size) const
noexcept
= 0;

/// Returns true if there is no memory allocated by this allocator.
[[nodiscard]] virtual bool isEmpty() const noexcept = 0;
#endif

/// Prints state of the memory allocator
virtual void print(std::ostream& os = std::cout, int indent = 0) const;

Expand Down
Loading

0 comments on commit 5ef8c1f

Please sign in to comment.