PR #3279 - Added README.mozilla to tell where the object pool code is from and updated the object pool code from this origin (minor update).

This commit is contained in:
godeffroy 2020-09-17 17:55:45 +02:00
parent 5bf5124366
commit 371ddb84e5
4 changed files with 16 additions and 14 deletions

View File

@ -17,7 +17,7 @@
template<class DataT>
struct TreeNode {
TreeNode<DataT>* parent;
std::vector<std::unique_ptr< TreeNode<DataT>, godefv::memory::object_pool_deleter_t<TreeNode<DataT>> >> children;
std::vector<std::unique_ptr< TreeNode<DataT>, godefv::object_pool_deleter_t<TreeNode<DataT>> >> children;
DataT data;
@ -115,7 +115,7 @@ private:
// TreeNode implementation
template<class NodeDataT, class ChildDataT>
TreeNode<NodeDataT>* add_child(TreeNode<NodeDataT>* tree_node, ChildDataT&& data) {
static thread_local godefv::memory::object_pool_t<TreeNode<NodeDataT>> tree_node_pool;
static thread_local godefv::object_pool_t<TreeNode<NodeDataT>> tree_node_pool;
tree_node->children.push_back(tree_node_pool.make_unique(tree_node, std::forward<ChildDataT>(data)));
return tree_node->children.back().get();
}

View File

@ -0,0 +1 @@
This code was imported from https://github.com/godefv/memory on September 17th 2020, commit 5ff1af8ee09ced04990b4863b2c02a8d07f4356a. It's licensed under "CC0 1.0 Universal" license.

View File

@ -6,12 +6,13 @@
#include <vector>
#include <array>
namespace godefv{ namespace memory{
namespace godefv{
// Forward declaration
template<class Object, template<class T> class Allocator = std::allocator, std::size_t ChunkSize = 1024>
class object_pool_t;
//! Custom deleter to recycle the deleted pointers.
//! Custom deleter to recycle the deleted pointers of the object_pool_t.
template<class Object, template<class T> class Allocator = std::allocator, std::size_t ChunkSize = 1024>
struct object_pool_deleter_t{
private:
@ -21,7 +22,6 @@ public:
object_pool_ptr(input_object_pool_ptr)
{}
//! When a pointer provided by the ObjectPool is deleted, its memory is converted to an object slot to be recycled.
void operator()(Object* object_ptr)
{
object_pool_ptr->delete_object(object_ptr);
@ -48,6 +48,7 @@ class object_pool_t{
object_slot_t* free_object_slots_begin;
object_slot_t* free_object_slots_end;
//! When a pointer provided by the ObjectPool is deleted, its memory is converted to an object slot to be recycled.
void delete_object(Object* object_ptr){
object_ptr->~Object();
recycled_object_slots.push_back(reinterpret_cast<object_slot_t*>(object_ptr));
@ -60,8 +61,8 @@ public:
using object_unique_ptr_t = std::unique_ptr<object_t, deleter_t>; //!< The type returned by the object pool.
object_pool_t(Allocator<chunk_t> const& allocator = Allocator<chunk_t>{}) :
free_object_slots_begin{ free_object_slots_end }, // At the begining, set the 2 iterators at the same value to simulate a full pool.
chunk_allocator{ allocator }
chunk_allocator{ allocator },
free_object_slots_begin{ free_object_slots_end } // At the begining, set the 2 iterators at the same value to simulate a full pool.
{}
//! Returns a unique pointer to an object_t using an unused object slot from the object pool.
@ -102,6 +103,6 @@ public:
}
};
}} /* namespace godefv::memory */
} /* namespace godefv */
#endif /* GODEFV_MEMORY_OBJECT_POOL_H */

View File

@ -3,10 +3,11 @@
#include <memory>
namespace godefv{ namespace memory{
namespace godefv{
//! A deleter to deallocate memory which have been allocated by the given allocator.
template <class Allocator> struct allocator_deleter_t
template<class Allocator>
struct allocator_deleter_t
{
allocator_deleter_t(Allocator const& allocator) :
mAllocator{ allocator }
@ -23,17 +24,16 @@ private:
//! A smart pointer like std::unique_ptr but templated on an allocator instead of a deleter.
//! The deleter is deduced from the given allocator.
template <class T, class Allocator = std::allocator<T>>
class unique_ptr_t : public std::unique_ptr<T, allocator_deleter_t<Allocator>>
template<class T, class Allocator = std::allocator<T>>
struct unique_ptr_t : public std::unique_ptr<T, allocator_deleter_t<Allocator>>
{
using base_t = std::unique_ptr<T, allocator_deleter_t<Allocator>>;
public:
unique_ptr_t(Allocator allocator = Allocator{}) :
base_t{ allocator.allocate(1), allocator_deleter_t<Allocator>{ allocator } }
{}
};
}} // namespace godefv::memory
} // namespace godefv
#endif // GODEFV_MEMORY_ALLOCATED_UNIQUE_PTR_H