Skip to content

Commit

Permalink
chore: Renames & comments in allocation policies
Browse files Browse the repository at this point in the history
  • Loading branch information
gavv committed Jul 8, 2024
1 parent 6839b2c commit 1ad2399
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 34 deletions.
10 changes: 5 additions & 5 deletions src/internal_modules/roc_core/allocation_policy.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <class T>
CustomAllocation(void (*destroy_func)(T*))
FuncAllocation(void (*destroy_func)(T*))
: destroy_func_((DestroyFunc)destroy_func) {
if (!destroy_func_) {
roc_panic("allocation policy: null function");
Expand All @@ -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 <class T> void destroy(T&) {
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/internal_modules/roc_core/buffer_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ typedef SharedPtr<BufferView> BufferViewPtr;
//! (i.e. if there are still slices referring to it).
//!
//! @see Buffer, Slice.
class BufferView : public RefCounted<BufferView, ManualAllocation> {
class BufferView : public RefCounted<BufferView, NoopAllocation> {
public:
//! Initialize view referring to memory region.
//! Memory should remain valid until view in destroyed.
Expand Down
2 changes: 1 addition & 1 deletion src/internal_modules/roc_core/hashmap_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 6 additions & 4 deletions src/internal_modules/roc_core/noop_arena.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

#include "roc_core/noop_arena.h"
#include "roc_core/panic.h"

namespace roc {
namespace core {
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/internal_modules/roc_core/ref_counted.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ class RefCounted : public NonCopyable<RefCounted<T, AllocationPolicy> >,
}

//! Initialize.
explicit RefCounted(const AllocationPolicy& policy)
: AllocationPolicy(policy) {
explicit RefCounted(const AllocationPolicy& alloc)
: AllocationPolicy(alloc) {
}

//! Get reference counter.
Expand Down
27 changes: 20 additions & 7 deletions src/internal_modules/roc_core/scoped_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 T, class AllocationPolicy = ArenaAllocation>
class ScopedPtr : public NonCopyable<> {
public:
Expand All @@ -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.
Expand All @@ -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));
}
}

Expand Down Expand Up @@ -108,6 +109,18 @@ class ScopedPtr : public NonCopyable<> {
Optional<AllocationPolicy> policy_;
};

//! Equality check.
template <class T1, class T2, class P>
inline bool operator==(const ScopedPtr<T1, P>& a, const ScopedPtr<T2, P>& b) {
return a.get() == b.get();
}

//! Equality check.
template <class T1, class T2, class P>
inline bool operator!=(const ScopedPtr<T1, P>& a, const ScopedPtr<T2, P>& b) {
return a.get() != b.get();
}

} // namespace core
} // namespace roc

Expand Down
28 changes: 20 additions & 8 deletions src/internal_modules/roc_core/shared_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 T, template <class TT> class OwnershipPolicy = RefCountedOwnership>
class SharedPtr {
public:
Expand Down Expand Up @@ -57,7 +58,7 @@ class SharedPtr {
//! Create shared pointer from shared pointer of another type.
//! @remarks
//! - This overload hits SharedPtr(SharedPtr<ConvertibleToT>).
//! - 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 <class TT>
SharedPtr(const SharedPtr<TT, OwnershipPolicy>& other)
: ptr_(other.get()) {
Expand Down Expand Up @@ -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_;
Expand Down Expand Up @@ -132,14 +144,14 @@ class SharedPtr {
};

//! Equality check.
template <class T1, class T2>
inline bool operator==(const SharedPtr<T1>& a, const SharedPtr<T2>& b) {
template <class T1, class T2, template <class TT> class P>
inline bool operator==(const SharedPtr<T1, P>& a, const SharedPtr<T2, P>& b) {
return a.get() == b.get();
}

//! Equality check.
template <class T1, class T2>
inline bool operator!=(const SharedPtr<T1>& a, const SharedPtr<T2>& b) {
template <class T1, class T2, template <class TT> class P>
inline bool operator!=(const SharedPtr<T1, P>& a, const SharedPtr<T2, P>& b) {
return a.get() != b.get();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace netio {
//! @note
//! - Methods are called from the network loop thread.
//! - Methods should not block.
class IConnHandler : public core::RefCounted<IConnHandler, core::ManualAllocation> {
class IConnHandler : public core::RefCounted<IConnHandler, core::NoopAllocation> {
public:
virtual ~IConnHandler();

Expand Down
4 changes: 2 additions & 2 deletions src/internal_modules/roc_node/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ struct ContextConfig {
};

//! Node context.
class Context : public core::RefCounted<Context, core::ManualAllocation> {
class Context : public core::RefCounted<Context, core::NoopAllocation> {
public:
//! Initialize.
explicit Context(const ContextConfig& config, core::IArena& arena);
Context(const ContextConfig& config, core::IArena& arena);

//! Deinitialize.
~Context();
Expand Down
2 changes: 1 addition & 1 deletion src/tools/roc_copy/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ int main(int argc, char** argv) {
return code;
}

core::ScopedPtr<gengetopt_args_info, core::CustomAllocation> args_holder(
core::ScopedPtr<gengetopt_args_info, core::FuncAllocation> args_holder(
&args, &cmdline_parser_free);

core::Logger::instance().set_verbosity(args.verbose_given);
Expand Down
2 changes: 1 addition & 1 deletion src/tools/roc_recv/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ int main(int argc, char** argv) {
return code;
}

core::ScopedPtr<gengetopt_args_info, core::CustomAllocation> args_holder(
core::ScopedPtr<gengetopt_args_info, core::FuncAllocation> args_holder(
&args, &cmdline_parser_free);

core::Logger::instance().set_verbosity(args.verbose_given);
Expand Down
2 changes: 1 addition & 1 deletion src/tools/roc_send/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ int main(int argc, char** argv) {
return code;
}

core::ScopedPtr<gengetopt_args_info, core::CustomAllocation> args_holder(
core::ScopedPtr<gengetopt_args_info, core::FuncAllocation> args_holder(
&args, &cmdline_parser_free);

core::Logger::instance().set_verbosity(args.verbose_given);
Expand Down

0 comments on commit 1ad2399

Please sign in to comment.