Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] Isolate the intrusive reference counting #5149

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Sofa/framework/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ set(HEADER_FILES
${SRC_ROOT}/ExecParams.h
${SRC_ROOT}/fwd.h
${SRC_ROOT}/init.h
${SRC_ROOT}/IntrusiveObject.h
${SRC_ROOT}/Mapping.h
${SRC_ROOT}/Mapping.inl
${SRC_ROOT}/MappingHelper.h
Expand Down Expand Up @@ -217,6 +218,7 @@ set(SOURCE_FILES
${SRC_ROOT}/DataTrackerCallback.cpp
${SRC_ROOT}/DataTrackerFunctor.cpp
${SRC_ROOT}/ExecParams.cpp
${SRC_ROOT}/IntrusiveObject.cpp
${SRC_ROOT}/fwd.cpp
${SRC_ROOT}/init.cpp
${SRC_ROOT}/Mapping.cpp
Expand Down
38 changes: 38 additions & 0 deletions Sofa/framework/Core/src/sofa/core/IntrusiveObject.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: [email protected] *
******************************************************************************/
#include <sofa/core/IntrusiveObject.h>

namespace sofa::core
{
void IntrusiveObject::addRef()
{
++ref_counter;
}

void IntrusiveObject::release()
{
if (ref_counter.fetch_sub(1) == 1)
{
delete this;
}
}
}
55 changes: 55 additions & 0 deletions Sofa/framework/Core/src/sofa/core/IntrusiveObject.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: [email protected] *
******************************************************************************/
#pragma once
#include <sofa/core/config.h>
#include <atomic>

namespace sofa::core
{

/**
* The `IntrusiveObject` class implements an internal reference counting mechanism
* to manage its lifetime. It is intended to work with intrusive smart pointers like
* `boost::intrusive_ptr`.
*/
class SOFA_CORE_API IntrusiveObject
{
std::atomic<int> ref_counter { 0 };

void addRef();
void release();

friend inline void intrusive_ptr_add_ref(IntrusiveObject* p)
{
p->addRef();
}

friend inline void intrusive_ptr_release(IntrusiveObject* p)
{
p->release();
}

protected:
virtual ~IntrusiveObject() = default;
};

}
15 changes: 1 addition & 14 deletions Sofa/framework/Core/src/sofa/core/objectmodel/Base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ using std::string;
static const std::string unnamed_label=std::string("unnamed");

Base::Base()
: ref_counter(0)
, name(initData(&name,unnamed_label,"name","object name"))
: name(initData(&name,unnamed_label,"name","object name"))
, f_printLog(initData(&f_printLog, false, "printLog", "if true, emits extra messages at runtime."))
, f_tags(initData( &f_tags, "tags", "list of the subsets the object belongs to"))
, f_bbox(initData( &f_bbox, "bbox", "this object bounding box"))
Expand All @@ -78,18 +77,6 @@ Base::~Base()
{
}

void Base::addRef()
{
++ref_counter;
}

void Base::release()
{
if (ref_counter.fetch_sub(1) == 1)
{
delete this;
}
}


void Base::addUpdateCallback(const std::string& name,
Expand Down
18 changes: 2 additions & 16 deletions Sofa/framework/Core/src/sofa/core/objectmodel/Base.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
#include <sofa/core/sptr.h>

#include <deque>
#include <atomic>

#include <sofa/core/objectmodel/ComponentState.h>
#include <sofa/core/DataTracker.h>
#include <sofa/core/DataTrackerCallback.h>
#include <sofa/core/IntrusiveObject.h>
#include <sofa/type/fwd.h>

#define SOFA_BASE_CAST_IMPLEMENTATION(CLASSNAME) \
Expand All @@ -55,7 +55,7 @@ namespace sofa::core::objectmodel
* All classes deriving from Base should use the SOFA_CLASS macro within their declaration (see BaseClass.h).
*
*/
class SOFA_CORE_API Base
class SOFA_CORE_API Base : public IntrusiveObject
{
public:
typedef Base* Ptr;
Expand Down Expand Up @@ -86,20 +86,6 @@ class SOFA_CORE_API Base
Base(const Base& b);
Base& operator=(const Base& b);

std::atomic<int> ref_counter;
void addRef();
void release();

friend inline void intrusive_ptr_add_ref(Base* p)
{
p->addRef();
}

friend inline void intrusive_ptr_release(Base* p)
{
p->release();
}

protected:
std::map<std::string, sofa::core::DataTrackerCallback> m_internalEngine;

Expand Down
73 changes: 73 additions & 0 deletions Sofa/framework/Core/test/IntrusiveObject_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: [email protected] *
******************************************************************************/
#include <sofa/core/IntrusiveObject.h>
#include <gtest/gtest.h>
#include <sofa/core/sptr.h>


class DummyIntrusiveObject : public sofa::core::IntrusiveObject
{
public:
DummyIntrusiveObject() = default;
explicit DummyIntrusiveObject(const std::function<void()>& _destructorCallback)
: destructorCallback(_destructorCallback) {}

~DummyIntrusiveObject() override
{
destructorCallback();
}

private:
std::function<void()> destructorCallback;
};



TEST(IntrusiveObject, IsDestructorCalled)
{
std::size_t nbTimesDestructorCalled = 0;
{
sofa::core::sptr<DummyIntrusiveObject> dummy(new DummyIntrusiveObject([&nbTimesDestructorCalled]()
{
nbTimesDestructorCalled++;
}));
}
EXPECT_EQ(1, nbTimesDestructorCalled);
}


TEST(IntrusiveObject, Copy)
{
std::size_t nbTimesDestructorCalled = 0;
{
sofa::core::sptr<DummyIntrusiveObject> dummy0;
{
sofa::core::sptr<DummyIntrusiveObject> dummy(new DummyIntrusiveObject([&nbTimesDestructorCalled]()
{
nbTimesDestructorCalled++;
}));

dummy0 = dummy;
}
}
EXPECT_EQ(1, nbTimesDestructorCalled);
}
Loading