From 371ddb84e5113e75846c43fe0455c06124da369b Mon Sep 17 00:00:00 2001 From: godeffroy Date: Thu, 17 Sep 2020 17:55:45 +0200 Subject: [PATCH] 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). --- native_client/ctcdecode/path_trie.h | 4 ++-- .../third_party/object_pool/README.mozilla | 1 + .../ctcdecode/third_party/object_pool/object_pool.h | 13 +++++++------ .../ctcdecode/third_party/object_pool/unique_ptr.h | 12 ++++++------ 4 files changed, 16 insertions(+), 14 deletions(-) create mode 100644 native_client/ctcdecode/third_party/object_pool/README.mozilla diff --git a/native_client/ctcdecode/path_trie.h b/native_client/ctcdecode/path_trie.h index cab1b4ab..93a09437 100644 --- a/native_client/ctcdecode/path_trie.h +++ b/native_client/ctcdecode/path_trie.h @@ -17,7 +17,7 @@ template struct TreeNode { TreeNode* parent; - std::vector, godefv::memory::object_pool_deleter_t> >> children; + std::vector, godefv::object_pool_deleter_t> >> children; DataT data; @@ -115,7 +115,7 @@ private: // TreeNode implementation template TreeNode* add_child(TreeNode* tree_node, ChildDataT&& data) { - static thread_local godefv::memory::object_pool_t> tree_node_pool; + static thread_local godefv::object_pool_t> tree_node_pool; tree_node->children.push_back(tree_node_pool.make_unique(tree_node, std::forward(data))); return tree_node->children.back().get(); } diff --git a/native_client/ctcdecode/third_party/object_pool/README.mozilla b/native_client/ctcdecode/third_party/object_pool/README.mozilla new file mode 100644 index 00000000..2db63215 --- /dev/null +++ b/native_client/ctcdecode/third_party/object_pool/README.mozilla @@ -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. diff --git a/native_client/ctcdecode/third_party/object_pool/object_pool.h b/native_client/ctcdecode/third_party/object_pool/object_pool.h index e91a4f51..3a1e2810 100755 --- a/native_client/ctcdecode/third_party/object_pool/object_pool.h +++ b/native_client/ctcdecode/third_party/object_pool/object_pool.h @@ -6,12 +6,13 @@ #include #include -namespace godefv{ namespace memory{ +namespace godefv{ +// Forward declaration template 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 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_ptr)); @@ -60,8 +61,8 @@ public: using object_unique_ptr_t = std::unique_ptr; //!< The type returned by the object pool. object_pool_t(Allocator const& 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. - 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 */ diff --git a/native_client/ctcdecode/third_party/object_pool/unique_ptr.h b/native_client/ctcdecode/third_party/object_pool/unique_ptr.h index b1f00950..44f21cb8 100755 --- a/native_client/ctcdecode/third_party/object_pool/unique_ptr.h +++ b/native_client/ctcdecode/third_party/object_pool/unique_ptr.h @@ -3,10 +3,11 @@ #include -namespace godefv{ namespace memory{ +namespace godefv{ //! A deleter to deallocate memory which have been allocated by the given allocator. -template struct allocator_deleter_t +template +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 unique_ptr_t : public std::unique_ptr> +template> +struct unique_ptr_t : public std::unique_ptr> { using base_t = std::unique_ptr>; -public: unique_ptr_t(Allocator allocator = Allocator{}) : base_t{ allocator.allocate(1), allocator_deleter_t{ allocator } } {} }; -}} // namespace godefv::memory +} // namespace godefv #endif // GODEFV_MEMORY_ALLOCATED_UNIQUE_PTR_H