Skip to content

Commit

Permalink
Overhaul memory smart pointer (#149)
Browse files Browse the repository at this point in the history
* Add smart pointer wrapper utility

Add a new file "smart_ptr.h" to simplify the use of Boost intrusive pointers. This includes template aliases for reference-counted smart pointers and their base class, enhancing memory management in the project. Additionally, update the CMakeLists.txt to include the new header file.

* Switch to custom smart pointers.

Replaced Boost intrusive pointers with the custom Rc smart pointers across multiple headers for consistency and potential performance improvements. This change impacts device handling, algebra context, basis, and linear operator functionalities.

* Replace boost smart pointers with custom smart pointers

Refactored the code to use the internal `Rc` smart pointers instead of `boost::intrusive_ptr`. This change improves maintainability and reduces dependency on external libraries. Included necessary header files for the new smart pointers.

* Replace Boost intrusive_ref_counter with mem::RcBase

Transition from Boost's intrusive_ref_counter to mem::RcBase for better memory management consistency across the codebase. This adjustment affects the DeviceHandle, ContextBase, and BasisInterface classes.
  • Loading branch information
inakleinbottle authored Nov 10, 2024
1 parent 1e1447c commit 49b682f
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 30 deletions.
7 changes: 3 additions & 4 deletions algebra/include/roughpy/algebra/basis.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@

#include "algebra_fwd.h"

#include <boost/smart_ptr/intrusive_ptr.hpp>
#include <boost/smart_ptr/intrusive_ref_counter.hpp>
#include <roughpy/core/hash.h>
#include <roughpy/core/smart_ptr.h>

#include <roughpy/core/traits.h>

Expand All @@ -47,7 +46,7 @@ class BasisImplementation;

template <typename Derived, typename KeyType = rpy::key_type>
class BasisInterface
: public boost::intrusive_ref_counter<BasisInterface<KeyType>>
: public mem::RcBase<BasisInterface<KeyType>>
{
public:
using key_type = KeyType;
Expand Down Expand Up @@ -165,7 +164,7 @@ class Basis : public PrimaryInterface::mixin_t
"Primary template must be an instance of BasisInterface"
);

boost::intrusive_ptr<const basis_interface> p_impl;
Rc<const basis_interface> p_impl;

public:
using key_type = typename basis_interface::key_type;
Expand Down
2 changes: 1 addition & 1 deletion algebra/include/roughpy/algebra/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ struct VectorConstructionData {
VectorType vector_type = VectorType::Sparse;
};

class ROUGHPY_ALGEBRA_EXPORT ContextBase : public boost::intrusive_ref_counter<ContextBase>
class ROUGHPY_ALGEBRA_EXPORT ContextBase : public mem::RcBase<ContextBase>
{
deg_t m_width;
deg_t m_depth;
Expand Down
7 changes: 3 additions & 4 deletions algebra/include/roughpy/algebra/context_fwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@

#include "algebra_fwd.h"

#include <boost/smart_ptr/intrusive_ptr.hpp>
#include <boost/smart_ptr/intrusive_ref_counter.hpp>

#include <roughpy/core/slice.h>
#include <roughpy/core/smart_ptr.h>

#include <roughpy/platform/serialization.h>

Expand Down Expand Up @@ -76,8 +75,8 @@ class ContextBase;

class Context;

using base_context_pointer = boost::intrusive_ptr<const ContextBase>;
using context_pointer = boost::intrusive_ptr<const Context>;
using base_context_pointer = Rc<const ContextBase>;
using context_pointer = Rc<const Context>;

struct BasicContextSpec {
string stype_id;
Expand Down
3 changes: 2 additions & 1 deletion algebra/include/roughpy/algebra/linear_operator.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

#include <roughpy/core/macros.h>
#include <roughpy/core/traits.h>
#include <roughpy/core/smart_ptr.h>

namespace rpy {
namespace algebra {
Expand All @@ -56,7 +57,7 @@ template <typename Argument, typename Result>
class LinearOperator
{
using interface_type = LinearOperatorInterface<Argument, Result>;
boost::intrusive_ptr<interface_type> p_impl;
Rc<interface_type> p_impl;

public:
Result operator()(const Argument& arg) const;
Expand Down
12 changes: 4 additions & 8 deletions algebra/src/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,25 +372,21 @@ UnspecifiedAlgebraType Context::adjoint_to_left_multiply_by(

void rpy::algebra::intrusive_ptr_add_ref(const ContextBase* ptr) noexcept
{
using counter_t = boost::
intrusive_ref_counter<ContextBase, boost::thread_safe_counter>;
using counter_t = mem::RcBase<ContextBase, boost::thread_safe_counter>;
intrusive_ptr_add_ref(static_cast<const counter_t*>(ptr));
}
void rpy::algebra::intrusive_ptr_release(const ContextBase* ptr) noexcept
{
using counter_t = boost::
intrusive_ref_counter<ContextBase, boost::thread_safe_counter>;
using counter_t = mem::RcBase<ContextBase, boost::thread_safe_counter>;
intrusive_ptr_release(static_cast<const counter_t*>(ptr));
}
void rpy::algebra::intrusive_ptr_add_ref(const Context* ptr) noexcept
{
using counter_t = boost::
intrusive_ref_counter<ContextBase, boost::thread_safe_counter>;
using counter_t = mem::RcBase<ContextBase, boost::thread_safe_counter>;
intrusive_ptr_add_ref(static_cast<const counter_t*>(ptr));
}
void rpy::algebra::intrusive_ptr_release(const Context* ptr) noexcept
{
using counter_t = boost::
intrusive_ref_counter<ContextBase, boost::thread_safe_counter>;
using counter_t = mem::RcBase<ContextBase, boost::thread_safe_counter>;
intrusive_ptr_release(static_cast<const counter_t*>(ptr));
}
1 change: 1 addition & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ add_library(RoughPy_Core INTERFACE
include/roughpy/core/macros.h
include/roughpy/core/helpers.h
include/roughpy/core/slice.h
include/roughpy/core/smart_ptr.h
)
add_library(RoughPy::Core ALIAS RoughPy_Core)

Expand Down
32 changes: 32 additions & 0 deletions core/include/roughpy/core/smart_ptr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// Created by sammorley on 10/11/24.
//

#ifndef ROUGHPY_CORE_SMART_PTR_H
#define ROUGHPY_CORE_SMART_PTR_H

#include <boost/smart_ptr/intrusive_ptr.hpp>
#include <boost/smart_ptr/intrusive_ref_counter.hpp>

namespace rpy {


namespace mem {

template <typename T>
using Rc = boost::intrusive_ptr<T>;

template <typename T, typename Policy=boost::thread_safe_counter>
using RcBase = boost::intrusive_ref_counter<T, Policy>;



}


using mem::Rc;


}

#endif //ROUGHPY_CORE_SMART_PTR_H
6 changes: 4 additions & 2 deletions platform/include/roughpy/platform/devices/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
#define ROUGHPY_DEVICE_CORE_H_

#include <roughpy/core/macros.h>
#include <roughpy/core/smart_ptr.h>
#include <roughpy/core/traits.h>
#include <roughpy/core/types.h>

#include <roughpy/platform/serialization.h>
#include <roughpy/platform/errors.h>

Expand Down Expand Up @@ -303,8 +305,8 @@ class MemoryView;
class QueueInterface;
class Queue;

using Device = boost::intrusive_ptr<const DeviceHandle>;
using HostDevice = boost::intrusive_ptr<const HostDeviceHandle>;
using Device = Rc<const DeviceHandle>;
using HostDevice = Rc<const HostDeviceHandle>;

ROUGHPY_PLATFORM_EXPORT HostDevice get_host_device();
ROUGHPY_PLATFORM_EXPORT Device get_default_device();
Expand Down
2 changes: 1 addition & 1 deletion platform/include/roughpy/platform/devices/device_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ struct ExtensionSourceAndOptions {
*
*/
class ROUGHPY_PLATFORM_EXPORT DeviceHandle
: public boost::intrusive_ref_counter<DeviceHandle>
: public mem::RcBase<DeviceHandle>
{
mutable std::recursive_mutex m_lock;
mutable std::unordered_map<string, Kernel> m_kernel_cache;
Expand Down
6 changes: 2 additions & 4 deletions platform/src/devices/device_handle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,13 @@ void rpy::devices::intrusive_ptr_add_ref(
const rpy::devices::DeviceHandle* device
) noexcept
{
using counter_t = boost::
intrusive_ref_counter<DeviceHandle, boost::thread_safe_counter>;
using counter_t = mem::RcBase<DeviceHandle, boost::thread_safe_counter>;
intrusive_ptr_add_ref(static_cast<const counter_t*>(device));
}
void rpy::devices::intrusive_ptr_release(
const rpy::devices::DeviceHandle* device
) noexcept
{
using counter_t = boost::
intrusive_ref_counter<DeviceHandle, boost::thread_safe_counter>;
using counter_t = mem::RcBase<DeviceHandle, boost::thread_safe_counter>;
intrusive_ptr_release(static_cast<const counter_t*>(device));
}
5 changes: 3 additions & 2 deletions platform/src/devices/host/host_decls.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@
#define ROUGHPY_DEVICE_SRC_CPU_CPU_DECLS_H_

#include <roughpy/core/macros.h>
#include <roughpy/core/smart_ptr.h>
#include <roughpy/core/types.h>

#include <boost/smart_ptr/intrusive_ptr.hpp>


namespace rpy {
namespace devices {
Expand All @@ -46,7 +47,7 @@ class CPUEvent;
class CPUKernel;
class CPUQueue;

using CPUDevice = boost::intrusive_ptr<const CPUDeviceHandle>;
using CPUDevice = Rc<const CPUDeviceHandle>;

}// namespace device
}// namespace rpy
Expand Down
3 changes: 2 additions & 1 deletion platform/src/devices/host/host_device_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "host_device_impl.h"

#include <roughpy/core/alloc.h>
#include <roughpy/core/smart_ptr.h>

#include "devices/buffer.h"
#include "devices/event.h"
Expand Down Expand Up @@ -190,7 +191,7 @@ CPUDeviceHandle::~CPUDeviceHandle() = default;

CPUDevice CPUDeviceHandle::get()
{
static boost::intrusive_ptr<CPUDeviceHandle> device(new CPUDeviceHandle);
static Rc<CPUDeviceHandle> device(new CPUDeviceHandle);
return device;
}

Expand Down
4 changes: 2 additions & 2 deletions platform/src/devices/opencl/ocl_decls.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
#define ROUGHPY_DEVICE_SRC_OPENCL_OCL_DECLS_H_

#include <roughpy/core/macros.h>
#include <roughpy/core/smart_ptr.h>
#include <roughpy/core/types.h>

#include <boost/smart_ptr/intrusive_ptr.hpp>

namespace rpy {
namespace devices {
Expand All @@ -46,7 +46,7 @@ class OCLEvent;
class OCLKernel;
class OCLQueue;

using OCLDevice = boost::intrusive_ptr<const OCLDeviceHandle>;
using OCLDevice = Rc<const OCLDeviceHandle>;

}// namespace devices
}// namespace rpy
Expand Down

0 comments on commit 49b682f

Please sign in to comment.