The main goal of these changes is to support checking if some data has been changed over time. This is used by the WIP simulation nodes during baking to detect which attributes have to be stored in every frame because they have changed. By using a combination of a weak user count and a version counter, it is possible to detect that an attribute (or any data controlled by implicit sharing) has not been changed with O(1) memory and time. It's still possible that the data has been changed multiple times and is the same in the end and beginning of course. That wouldn't be detected using this mechanism. The `ImplicitSharingInfo` struct has a new weak user count. A weak reference is one that does not keep the referenced data alive, but makes sure that the `ImplicitSharingInfo` itself is not deleted. If some piece of data has one strong and multiple weak users, it is still mutable. If the strong user count goes down to zero, the referenced data is freed. Remaining weak users can check for this condition using `is_expired`. This is a bit similar to `std::weak_ptr` but there is an important difference: a weak user can not become a strong user while one can create a `shared_ptr` from a `weak_ptr`. This restriction is necessary, because some code might be changing the referenced data assuming that it is the only owner. If another thread suddenly adds a new owner, the data would be shared again and the first thread would not have been allowed to modify the data in the first place. There is also a new integer version counter in `ImplicitSharingInfo`. It is incremented whenever some code wants to modify the referenced data. Obviously, this can only be done when the data is not shared because then it would be immutable. By comparing an old and new version number of the same sharing info, one can check if the data has been modified. One has to keep a weak reference to the sharing info together with the old version number to ensure that the new sharing info is still the same as the old one. Without this, it can happen that the sharing info was freed and a new one was allocated at the same pointer address. Using a strong reference for this purpose does not work, because then the data would never be modified because it's shared.
106 lines
2.9 KiB
C++
106 lines
2.9 KiB
C++
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
|
|
#include <algorithm>
|
|
#include <cstring>
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
#include "BLI_implicit_sharing.hh"
|
|
|
|
namespace blender::implicit_sharing {
|
|
|
|
class MEMFreeImplicitSharing : public ImplicitSharingInfo {
|
|
public:
|
|
void *data;
|
|
|
|
MEMFreeImplicitSharing(void *data) : data(data)
|
|
{
|
|
BLI_assert(data != nullptr);
|
|
}
|
|
|
|
private:
|
|
void delete_self_with_data() override
|
|
{
|
|
MEM_freeN(data);
|
|
MEM_delete(this);
|
|
}
|
|
};
|
|
|
|
const ImplicitSharingInfo *info_for_mem_free(void *data)
|
|
{
|
|
return MEM_new<MEMFreeImplicitSharing>(__func__, data);
|
|
}
|
|
|
|
namespace detail {
|
|
|
|
void *make_trivial_data_mutable_impl(void *old_data,
|
|
const int64_t size,
|
|
const int64_t alignment,
|
|
const ImplicitSharingInfo **sharing_info)
|
|
{
|
|
if (!old_data) {
|
|
BLI_assert(size == 0);
|
|
return nullptr;
|
|
}
|
|
|
|
BLI_assert(*sharing_info != nullptr);
|
|
if ((*sharing_info)->is_mutable()) {
|
|
(*sharing_info)->tag_ensured_mutable();
|
|
}
|
|
else {
|
|
void *new_data = MEM_mallocN_aligned(size, alignment, __func__);
|
|
memcpy(new_data, old_data, size);
|
|
(*sharing_info)->remove_user_and_delete_if_last();
|
|
*sharing_info = info_for_mem_free(new_data);
|
|
return new_data;
|
|
}
|
|
|
|
return old_data;
|
|
}
|
|
|
|
void *resize_trivial_array_impl(void *old_data,
|
|
const int64_t old_size,
|
|
const int64_t new_size,
|
|
const int64_t alignment,
|
|
const ImplicitSharingInfo **sharing_info)
|
|
{
|
|
if (new_size == 0) {
|
|
if (*sharing_info) {
|
|
(*sharing_info)->remove_user_and_delete_if_last();
|
|
*sharing_info = nullptr;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
if (!old_data) {
|
|
BLI_assert(old_size == 0);
|
|
BLI_assert(*sharing_info == nullptr);
|
|
void *new_data = MEM_mallocN_aligned(new_size, alignment, __func__);
|
|
*sharing_info = info_for_mem_free(new_data);
|
|
return new_data;
|
|
}
|
|
|
|
BLI_assert(old_size != 0);
|
|
if ((*sharing_info)->is_mutable()) {
|
|
if (auto *info = const_cast<MEMFreeImplicitSharing *>(
|
|
dynamic_cast<const MEMFreeImplicitSharing *>(*sharing_info))) {
|
|
/* If the array was allocated with the MEM allocator, we can use realloc directly, which
|
|
* could theoretically give better performance if the data can be reused in place. */
|
|
void *new_data = static_cast<int *>(MEM_reallocN(old_data, new_size));
|
|
info->data = new_data;
|
|
(*sharing_info)->tag_ensured_mutable();
|
|
return new_data;
|
|
}
|
|
}
|
|
|
|
void *new_data = MEM_mallocN_aligned(new_size, alignment, __func__);
|
|
memcpy(new_data, old_data, std::min(old_size, new_size));
|
|
(*sharing_info)->remove_user_and_delete_if_last();
|
|
*sharing_info = info_for_mem_free(new_data);
|
|
return new_data;
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
} // namespace blender::implicit_sharing
|