1
1

Cleanup: use const for implicit sharing info

Generally, one does not know if the sharing info is currently shared
and should therefore be const. Better keep it const almost all the
time and only remove the constness when absolutely necessary
and the code has checked that it is valid.
This commit is contained in:
2023-04-20 23:31:32 +02:00
parent 491f098edf
commit 8e69b41bdf
6 changed files with 21 additions and 18 deletions

View File

@@ -55,7 +55,7 @@ struct BasisCache {
class CurvesGeometryRuntime {
public:
/** Implicit sharing user count for #CurvesGeometry::curve_offsets. */
ImplicitSharingInfo *curve_offsets_sharing_info = nullptr;
const ImplicitSharingInfo *curve_offsets_sharing_info = nullptr;
/**
* The cached number of curves with each type. Unlike other caches here, this is not computed

View File

@@ -98,7 +98,7 @@ struct MeshRuntime {
std::mutex render_mutex;
/** Implicit sharing user count for #Mesh::poly_offset_indices. */
ImplicitSharingInfoHandle *poly_offsets_sharing_info;
const ImplicitSharingInfoHandle *poly_offsets_sharing_info;
/**
* A cache of bounds shared between data-blocks with unchanged positions. When changing positions

View File

@@ -2376,9 +2376,9 @@ class CustomDataLayerImplicitSharing : public ImplicitSharingInfo {
};
/** Create a #ImplicitSharingInfo that takes ownership of the data. */
static ImplicitSharingInfo *make_implicit_sharing_info_for_layer(const eCustomDataType type,
const void *data,
const int totelem)
static const ImplicitSharingInfo *make_implicit_sharing_info_for_layer(const eCustomDataType type,
const void *data,
const int totelem)
{
return MEM_new<CustomDataLayerImplicitSharing>(__func__, data, totelem, type);
}

View File

@@ -110,11 +110,11 @@ void *resize_trivial_array_impl(void *old_data,
int64_t old_size,
int64_t new_size,
int64_t alignment,
ImplicitSharingInfo **sharing_info);
const ImplicitSharingInfo **sharing_info);
void *make_trivial_data_mutable_impl(void *old_data,
int64_t size,
int64_t alignment,
ImplicitSharingInfo **sharing_info);
const ImplicitSharingInfo **sharing_info);
} // namespace detail
@@ -124,9 +124,9 @@ void *make_trivial_data_mutable_impl(void *old_data,
*/
template<typename T>
void copy_shared_pointer(T *src_ptr,
ImplicitSharingInfo *src_sharing_info,
const ImplicitSharingInfo *src_sharing_info,
T **r_dst_ptr,
ImplicitSharingInfo **r_dst_sharing_info)
const ImplicitSharingInfo **r_dst_sharing_info)
{
*r_dst_ptr = src_ptr;
*r_dst_sharing_info = src_sharing_info;
@@ -139,7 +139,7 @@ void copy_shared_pointer(T *src_ptr,
/**
* Remove this reference to the shared data and remove dangling pointers.
*/
template<typename T> void free_shared_data(T **data, ImplicitSharingInfo **sharing_info)
template<typename T> void free_shared_data(T **data, const ImplicitSharingInfo **sharing_info)
{
if (*sharing_info) {
BLI_assert(*data != nullptr);
@@ -153,13 +153,15 @@ template<typename T> void free_shared_data(T **data, ImplicitSharingInfo **shari
* Create an implicit sharing object that takes ownership of the data, allowing it to be shared.
* When it is no longer used, the data is freed with #MEM_freeN, so it must be a trivial type.
*/
ImplicitSharingInfo *info_for_mem_free(void *data);
const ImplicitSharingInfo *info_for_mem_free(void *data);
/**
* Make data mutable (single-user) if it is shared. For trivially-copyable data only.
*/
template<typename T>
void make_trivial_data_mutable(T **data, ImplicitSharingInfo **sharing_info, const int64_t size)
void make_trivial_data_mutable(T **data,
const ImplicitSharingInfo **sharing_info,
const int64_t size)
{
*data = static_cast<T *>(
detail::make_trivial_data_mutable_impl(*data, sizeof(T) * size, alignof(T), sharing_info));
@@ -171,7 +173,7 @@ void make_trivial_data_mutable(T **data, ImplicitSharingInfo **sharing_info, con
*/
template<typename T>
void resize_trivial_array(T **data,
ImplicitSharingInfo **sharing_info,
const ImplicitSharingInfo **sharing_info,
int64_t old_size,
int64_t new_size)
{

View File

@@ -26,7 +26,7 @@ class MEMFreeImplicitSharing : public ImplicitSharingInfo {
}
};
ImplicitSharingInfo *info_for_mem_free(void *data)
const ImplicitSharingInfo *info_for_mem_free(void *data)
{
return MEM_new<MEMFreeImplicitSharing>(__func__, data);
}
@@ -36,7 +36,7 @@ namespace detail {
void *make_trivial_data_mutable_impl(void *old_data,
const int64_t size,
const int64_t alignment,
ImplicitSharingInfo **sharing_info)
const ImplicitSharingInfo **sharing_info)
{
if (!old_data) {
BLI_assert(size == 0);
@@ -59,7 +59,7 @@ void *resize_trivial_array_impl(void *old_data,
const int64_t old_size,
const int64_t new_size,
const int64_t alignment,
ImplicitSharingInfo **sharing_info)
const ImplicitSharingInfo **sharing_info)
{
if (new_size == 0) {
if (*sharing_info) {
@@ -79,7 +79,8 @@ void *resize_trivial_array_impl(void *old_data,
BLI_assert(old_size != 0);
if ((*sharing_info)->is_mutable()) {
if (auto *info = dynamic_cast<MEMFreeImplicitSharing *>(*sharing_info)) {
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));

View File

@@ -154,7 +154,7 @@ struct SculptUndoNodeGeometry {
CustomData ldata;
CustomData pdata;
int *poly_offset_indices;
blender::ImplicitSharingInfo *poly_offsets_sharing_info;
const blender::ImplicitSharingInfo *poly_offsets_sharing_info;
int totvert;
int totedge;
int totloop;