Blender V4.3
blender::ImplicitSharingInfo Class Referenceabstract

#include <BLI_implicit_sharing.hh>

Inherits blender::NonCopyable, and blender::NonMovable.

Inherited by CustomDataLayerImplicitSharing, blender::ImplicitSharedValue< T >, blender::ImplicitSharingMixin, and blender::implicit_sharing::MEMFreeImplicitSharing.

Public Member Functions

virtual ~ImplicitSharingInfo ()
 
bool is_mutable () const
 
bool is_expired () const
 
void add_user () const
 
void add_weak_user () const
 
void tag_ensured_mutable () const
 
int64_t version () const
 
int strong_users () const
 
void remove_user_and_delete_if_last () const
 
void remove_weak_user_and_delete_if_last () const
 

Detailed Description

#ImplicitSharingInfo is the core data structure for implicit sharing in Blender. Implicit sharing is a technique that avoids copying data when it is not necessary. This results in better memory usage and performance. Only read-only data can be shared, because otherwise multiple owners might want to change the data in conflicting ways.

To determine whether data is shared, #ImplicitSharingInfo keeps a user count. If the count is 1, the data only has a single owner and is therefore mutable. If some code wants to modify data that is currently shared, it has to make a copy first. This behavior is also called "copy on write".

In addition to containing the reference count, #ImplicitSharingInfo also knows how to destruct the referenced data. This is important because the code freeing the data in the end might not know how it was allocated (for example, it doesn't know whether an array was allocated using the system or guarded allocator).

#ImplicitSharingInfo can be used in two ways:

  • It can be allocated separately from the referenced data. This is used when the shared data is e.g. a plain data array.
  • It can be embedded into another struct. For that it's best to use #ImplicitSharingMixin.

Definition at line 41 of file BLI_implicit_sharing.hh.

Constructor & Destructor Documentation

◆ ~ImplicitSharingInfo()

virtual blender::ImplicitSharingInfo::~ImplicitSharingInfo ( )
inlinevirtual

Definition at line 66 of file BLI_implicit_sharing.hh.

References BLI_assert.

Member Function Documentation

◆ add_user()

void blender::ImplicitSharingInfo::add_user ( ) const
inline

Call when a the data has a new additional owner.

Definition at line 88 of file BLI_implicit_sharing.hh.

References BLI_assert, and is_expired().

Referenced by blender::bke::GeometrySet::add(), BLO_write_shared(), blender::geometry::gather_realize_tasks_recursive(), and um_arraystore_cd_compact().

◆ add_weak_user()

void blender::ImplicitSharingInfo::add_weak_user ( ) const
inline

Adding a weak owner prevents the #ImplicitSharingInfo from being freed but not the referenced data.

Note
Unlike std::shared_ptr a weak user cannot be turned into a strong user. This is because some code might change the referenced data assuming that there is only one strong user while a new strong user is added by another thread.

Definition at line 102 of file BLI_implicit_sharing.hh.

Referenced by blender::tests::TEST(), and blender::bke::bake::BlobWriteSharing::write_implicitly_shared().

◆ is_expired()

bool blender::ImplicitSharingInfo::is_expired ( ) const
inline

◆ is_mutable()

bool blender::ImplicitSharingInfo::is_mutable ( ) const
inline

Whether the resource can be modified in place because there is only one owner.

Definition at line 73 of file BLI_implicit_sharing.hh.

Referenced by blender::bke::CurveComponent::clear(), blender::bke::GeometryComponentEditData::clear(), blender::bke::GreasePencilComponent::clear(), blender::bke::InstancesComponent::clear(), blender::bke::MeshComponent::clear(), blender::bke::PointCloudComponent::clear(), blender::bke::VolumeComponent::clear(), blender::bke::CurveComponent::ensure_owns_direct_data(), blender::bke::GreasePencilComponent::ensure_owns_direct_data(), blender::bke::MeshComponent::ensure_owns_direct_data(), blender::bke::PointCloudComponent::ensure_owns_direct_data(), blender::bke::VolumeComponent::ensure_owns_direct_data(), blender::bke::CurveComponent::get_for_write(), blender::bke::GreasePencilComponent::get_for_write(), blender::bke::InstancesComponent::get_for_write(), blender::bke::MeshComponent::get_for_write(), blender::bke::PointCloudComponent::get_for_write(), blender::bke::VolumeComponent::get_for_write(), blender::bke::CurveComponent::release(), blender::bke::GreasePencilComponent::release(), blender::bke::InstancesComponent::release(), blender::bke::MeshComponent::release(), blender::bke::PointCloudComponent::release(), blender::bke::VolumeComponent::release(), blender::bke::CurveComponent::replace(), blender::bke::GreasePencilComponent::replace(), blender::bke::InstancesComponent::replace(), blender::bke::MeshComponent::replace(), blender::bke::PointCloudComponent::replace(), blender::bke::VolumeComponent::replace(), tag_ensured_mutable(), and blender::tests::TEST().

◆ remove_user_and_delete_if_last()

void blender::ImplicitSharingInfo::remove_user_and_delete_if_last ( ) const
inline

Call when the data is no longer needed. This might just decrement the user count, or it might also delete the data if this was the last user.

Definition at line 139 of file BLI_implicit_sharing.hh.

References BLI_assert, and remove_weak_user_and_delete_if_last().

Referenced by BKE_mesh_legacy_face_set_to_generic(), blender::bke::bake::load_attributes(), blender::bke::mesh_sculpt_mask_to_generic(), move_face_map_data_to_attributes(), and replace_custom_data_layer_with_named().

◆ remove_weak_user_and_delete_if_last()

void blender::ImplicitSharingInfo::remove_weak_user_and_delete_if_last ( ) const
inline

This might just decrement the weak user count or might delete the data. Should be used in conjunction with add_weak_user.

Definition at line 168 of file BLI_implicit_sharing.hh.

References BLI_assert.

Referenced by remove_user_and_delete_if_last(), and blender::tests::TEST().

◆ strong_users()

int blender::ImplicitSharingInfo::strong_users ( ) const
inline

Definition at line 130 of file BLI_implicit_sharing.hh.

Referenced by BLO_write_shared().

◆ tag_ensured_mutable()

void blender::ImplicitSharingInfo::tag_ensured_mutable ( ) const
inline

Call this when making sure that the referenced data is mutable, which also implies that it is about to be modified. This allows other code to detect whether data has not been changed very efficiently.

Definition at line 112 of file BLI_implicit_sharing.hh.

References BLI_assert, and is_mutable().

◆ version()

int64_t blender::ImplicitSharingInfo::version ( ) const
inline

Get a version number that is increased when the data is modified. It can be used to detect if data has been changed.

Definition at line 125 of file BLI_implicit_sharing.hh.

Referenced by blender::tests::TEST(), and blender::bke::bake::BlobWriteSharing::write_implicitly_shared().


The documentation for this class was generated from the following file: