From 1ad239928227b54cac9a416ca953ff23672a07f3 Mon Sep 17 00:00:00 2001 From: Victor Gaydov Date: Mon, 8 Jul 2024 20:44:07 +0400 Subject: [PATCH] chore: Renames & comments in allocation policies --- .../roc_core/allocation_policy.h | 10 +++---- src/internal_modules/roc_core/buffer_view.h | 2 +- .../roc_core/hashmap_impl.cpp | 2 +- src/internal_modules/roc_core/noop_arena.cpp | 10 ++++--- src/internal_modules/roc_core/ref_counted.h | 4 +-- src/internal_modules/roc_core/scoped_ptr.h | 27 +++++++++++++----- src/internal_modules/roc_core/shared_ptr.h | 28 +++++++++++++------ .../target_libuv/roc_netio/iconn_handler.h | 2 +- src/internal_modules/roc_node/context.h | 4 +-- src/tools/roc_copy/main.cpp | 2 +- src/tools/roc_recv/main.cpp | 2 +- src/tools/roc_send/main.cpp | 2 +- 12 files changed, 61 insertions(+), 34 deletions(-) diff --git a/src/internal_modules/roc_core/allocation_policy.h b/src/internal_modules/roc_core/allocation_policy.h index 658bcedff..fc0fc896a 100644 --- a/src/internal_modules/roc_core/allocation_policy.h +++ b/src/internal_modules/roc_core/allocation_policy.h @@ -66,13 +66,13 @@ class PoolAllocation { }; //! Allocation policy for objects with custom deallocation function. -class CustomAllocation { +class FuncAllocation { typedef void (*DestroyFunc)(void*); public: //! Initialize. template - CustomAllocation(void (*destroy_func)(T*)) + FuncAllocation(void (*destroy_func)(T*)) : destroy_func_((DestroyFunc)destroy_func) { if (!destroy_func_) { roc_panic("allocation policy: null function"); @@ -88,12 +88,12 @@ class CustomAllocation { DestroyFunc destroy_func_; }; -//! Allocation policy for objects that does not have automatic deallocation. -class ManualAllocation { +//! Allocation policy for objects that don't have automatic deallocation. +class NoopAllocation { public: //! No-op. //! When SharedPtr or ScopedPtr "destroys" object, nothing happens. - //! The user is responsible for destroying it manually. + //! The user is responsible for destroying object. template void destroy(T&) { } }; diff --git a/src/internal_modules/roc_core/buffer_view.h b/src/internal_modules/roc_core/buffer_view.h index 86d96e36f..581e62ccb 100644 --- a/src/internal_modules/roc_core/buffer_view.h +++ b/src/internal_modules/roc_core/buffer_view.h @@ -43,7 +43,7 @@ typedef SharedPtr BufferViewPtr; //! (i.e. if there are still slices referring to it). //! //! @see Buffer, Slice. -class BufferView : public RefCounted { +class BufferView : public RefCounted { public: //! Initialize view referring to memory region. //! Memory should remain valid until view in destroyed. diff --git a/src/internal_modules/roc_core/hashmap_impl.cpp b/src/internal_modules/roc_core/hashmap_impl.cpp index 0b0956dea..fde5d3a38 100644 --- a/src/internal_modules/roc_core/hashmap_impl.cpp +++ b/src/internal_modules/roc_core/hashmap_impl.cpp @@ -388,7 +388,7 @@ void HashmapImpl::migrate_node_(HashmapData* node) { } size_t HashmapImpl::get_next_bucket_size_(size_t current_count) { - // rougtly doubling sequence of prime numbers, used as bucket counts + // roughly doubling sequence of prime numbers, used as bucket counts static const size_t prime_counts[] = { 5, 11, 23, 53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593, 49157, 98317, 196613, 393241, 786433, diff --git a/src/internal_modules/roc_core/noop_arena.cpp b/src/internal_modules/roc_core/noop_arena.cpp index 318658bd0..eb7210433 100644 --- a/src/internal_modules/roc_core/noop_arena.cpp +++ b/src/internal_modules/roc_core/noop_arena.cpp @@ -7,6 +7,7 @@ */ #include "roc_core/noop_arena.h" +#include "roc_core/panic.h" namespace roc { namespace core { @@ -15,15 +16,16 @@ void* NoopArenaImpl::allocate(size_t size) { return NULL; } -void NoopArenaImpl::deallocate(void*) { +void NoopArenaImpl::deallocate(void* ptr) { + roc_panic("noop arena: unexpected call"); } -size_t NoopArenaImpl::compute_allocated_size(size_t) const { +size_t NoopArenaImpl::compute_allocated_size(size_t size) const { return 0; } -size_t NoopArenaImpl::allocated_size(void*) const { - return 0; +size_t NoopArenaImpl::allocated_size(void* ptr) const { + roc_panic("noop arena: unexpected call"); } } // namespace core diff --git a/src/internal_modules/roc_core/ref_counted.h b/src/internal_modules/roc_core/ref_counted.h index c351bd7da..df4ac40ca 100644 --- a/src/internal_modules/roc_core/ref_counted.h +++ b/src/internal_modules/roc_core/ref_counted.h @@ -45,8 +45,8 @@ class RefCounted : public NonCopyable >, } //! Initialize. - explicit RefCounted(const AllocationPolicy& policy) - : AllocationPolicy(policy) { + explicit RefCounted(const AllocationPolicy& alloc) + : AllocationPolicy(alloc) { } //! Get reference counter. diff --git a/src/internal_modules/roc_core/scoped_ptr.h b/src/internal_modules/roc_core/scoped_ptr.h index 5a35956f9..abad477cb 100644 --- a/src/internal_modules/roc_core/scoped_ptr.h +++ b/src/internal_modules/roc_core/scoped_ptr.h @@ -22,13 +22,13 @@ namespace roc { namespace core { -//! Unique ownrship pointer. +//! Lexical scoped ownership pointer. //! //! @tparam T defines pointee type. It may be const. //! @tparam AllocationPolicy defies (de)allocation policy. //! -//! When ScopedPtr is destroyed or reset, it invokes AllocationPolicy::destroy() -//! to destroy the owned object. +//! ScopedPtr holds an instance of AllocationPolicy. When ScopedPtr is +//! destroyed or reset, it invokes AllocationPolicy::destroy(). template class ScopedPtr : public NonCopyable<> { public: @@ -38,9 +38,9 @@ class ScopedPtr : public NonCopyable<> { } //! Initialize from a raw pointer. - ScopedPtr(T* ptr, const AllocationPolicy& policy) + ScopedPtr(T* ptr, const AllocationPolicy& alloc) : ptr_(ptr) { - policy_.reset(new (policy_) AllocationPolicy(policy)); + policy_.reset(new (policy_) AllocationPolicy(alloc)); } //! Destroy object. @@ -58,12 +58,13 @@ class ScopedPtr : public NonCopyable<> { } //! Reset pointer to a new value. - void reset(T* new_ptr, const AllocationPolicy& new_policy) { + //! @p alloc defines an argument for AllocationPolicy constructor. + void reset(T* new_ptr, const AllocationPolicy& new_alloc) { if (new_ptr != ptr_) { reset(); ptr_ = new_ptr; - policy_.reset(new (policy_) AllocationPolicy(new_policy)); + policy_.reset(new (policy_) AllocationPolicy(new_alloc)); } } @@ -108,6 +109,18 @@ class ScopedPtr : public NonCopyable<> { Optional policy_; }; +//! Equality check. +template +inline bool operator==(const ScopedPtr& a, const ScopedPtr& b) { + return a.get() == b.get(); +} + +//! Equality check. +template +inline bool operator!=(const ScopedPtr& a, const ScopedPtr& b) { + return a.get() != b.get(); +} + } // namespace core } // namespace roc diff --git a/src/internal_modules/roc_core/shared_ptr.h b/src/internal_modules/roc_core/shared_ptr.h index e62160636..791fac7ef 100644 --- a/src/internal_modules/roc_core/shared_ptr.h +++ b/src/internal_modules/roc_core/shared_ptr.h @@ -23,11 +23,12 @@ namespace core { //! //! @tparam T defines pointee type. It may be const. //! -//! @tparam OwnershipPolicy defines ownwership policy. It provides methods to +//! @tparam OwnershipPolicy defines ownership policy. It provides methods to //! increase and decrease the reference counter embedded into object. //! -//! If RefCountedOwnership is used, T should inherit RefCounted. A template -//! parameter of RefCounted defines its (de)allocation policy. +//! If RefCountedOwnership is used, T should inherit RefCounted. RefCounted +//! has a template parameter AllocationPolicy, and inherits from it. +//! When reference counter reaches zero, it invokes AllocationPolicy::destroy(). template class OwnershipPolicy = RefCountedOwnership> class SharedPtr { public: @@ -57,7 +58,7 @@ class SharedPtr { //! Create shared pointer from shared pointer of another type. //! @remarks //! - This overload hits SharedPtr(SharedPtr). - //! - This doesn't work as a copy constructor since it's template. + //! - It doesn't work as a copy constructor since it's template. template SharedPtr(const SharedPtr& other) : ptr_(other.get()) { @@ -89,6 +90,17 @@ class SharedPtr { } } + //! Get underlying pointer and pass ownership to the caller. + T* release() { + T* ret = ptr_; + if (ret == NULL) { + roc_panic("shared ptr: attempting to release a null pointer"); + } + + ptr_ = NULL; + return ret; + } + //! Get underlying pointer. T* get() const { return ptr_; @@ -132,14 +144,14 @@ class SharedPtr { }; //! Equality check. -template -inline bool operator==(const SharedPtr& a, const SharedPtr& b) { +template class P> +inline bool operator==(const SharedPtr& a, const SharedPtr& b) { return a.get() == b.get(); } //! Equality check. -template -inline bool operator!=(const SharedPtr& a, const SharedPtr& b) { +template class P> +inline bool operator!=(const SharedPtr& a, const SharedPtr& b) { return a.get() != b.get(); } diff --git a/src/internal_modules/roc_netio/target_libuv/roc_netio/iconn_handler.h b/src/internal_modules/roc_netio/target_libuv/roc_netio/iconn_handler.h index 97c3510e7..0deab4207 100644 --- a/src/internal_modules/roc_netio/target_libuv/roc_netio/iconn_handler.h +++ b/src/internal_modules/roc_netio/target_libuv/roc_netio/iconn_handler.h @@ -57,7 +57,7 @@ namespace netio { //! @note //! - Methods are called from the network loop thread. //! - Methods should not block. -class IConnHandler : public core::RefCounted { +class IConnHandler : public core::RefCounted { public: virtual ~IConnHandler(); diff --git a/src/internal_modules/roc_node/context.h b/src/internal_modules/roc_node/context.h index c931aa173..665d45010 100644 --- a/src/internal_modules/roc_node/context.h +++ b/src/internal_modules/roc_node/context.h @@ -41,10 +41,10 @@ struct ContextConfig { }; //! Node context. -class Context : public core::RefCounted { +class Context : public core::RefCounted { public: //! Initialize. - explicit Context(const ContextConfig& config, core::IArena& arena); + Context(const ContextConfig& config, core::IArena& arena); //! Deinitialize. ~Context(); diff --git a/src/tools/roc_copy/main.cpp b/src/tools/roc_copy/main.cpp index 813edf4af..57463a972 100644 --- a/src/tools/roc_copy/main.cpp +++ b/src/tools/roc_copy/main.cpp @@ -37,7 +37,7 @@ int main(int argc, char** argv) { return code; } - core::ScopedPtr args_holder( + core::ScopedPtr args_holder( &args, &cmdline_parser_free); core::Logger::instance().set_verbosity(args.verbose_given); diff --git a/src/tools/roc_recv/main.cpp b/src/tools/roc_recv/main.cpp index ef07333be..b43527565 100644 --- a/src/tools/roc_recv/main.cpp +++ b/src/tools/roc_recv/main.cpp @@ -46,7 +46,7 @@ int main(int argc, char** argv) { return code; } - core::ScopedPtr args_holder( + core::ScopedPtr args_holder( &args, &cmdline_parser_free); core::Logger::instance().set_verbosity(args.verbose_given); diff --git a/src/tools/roc_send/main.cpp b/src/tools/roc_send/main.cpp index 28b96b324..83d16f8b7 100644 --- a/src/tools/roc_send/main.cpp +++ b/src/tools/roc_send/main.cpp @@ -45,7 +45,7 @@ int main(int argc, char** argv) { return code; } - core::ScopedPtr args_holder( + core::ScopedPtr args_holder( &args, &cmdline_parser_free); core::Logger::instance().set_verbosity(args.verbose_given);