From 09063a36329ee372a214f62d57a8902f42842e44 Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Sat, 6 Jan 2024 01:47:39 +0100 Subject: [PATCH] Cleanup: Remove some indirect includes in common headers The idea is to avoid mistakenly depending on indirect includes, and avoid compile time overhead from unnecessary header parsing. Pull Request: https://projects.blender.org/blender/blender/pulls/116664 --- source/blender/animrig/intern/keyframing.cc | 1 + .../blender/asset_system/AS_asset_library.hh | 1 + .../blendthumb/src/blendthumb_extract.cc | 1 + source/blender/blenkernel/BKE_attribute.hh | 1 + .../blender/blenkernel/intern/file_handler.cc | 2 ++ source/blender/blenlib/BLI_allocator.hh | 6 ++-- source/blender/blenlib/BLI_any.hh | 9 ++++-- source/blender/blenlib/BLI_assert.h | 2 ++ .../blender/blenlib/BLI_bit_group_vector.hh | 2 +- source/blender/blenlib/BLI_bit_span.hh | 1 - source/blender/blenlib/BLI_bit_span_ops.hh | 1 + source/blender/blenlib/BLI_bit_vector.hh | 2 -- source/blender/blenlib/BLI_hash.hh | 1 - source/blender/blenlib/BLI_hash_tables.hh | 30 ++----------------- .../blender/blenlib/BLI_implicit_sharing.hh | 3 +- source/blender/blenlib/BLI_index_mask.hh | 1 + source/blender/blenlib/BLI_index_range.hh | 3 +- .../blender/blenlib/BLI_linear_allocator.hh | 2 +- .../blender/blenlib/BLI_probing_strategies.hh | 2 ++ source/blender/blenlib/BLI_span.hh | 1 - source/blender/blenlib/BLI_utildefines.h | 26 ++++++++++++++++ source/blender/blenlib/BLI_vector.hh | 5 ---- .../blender/blenlib/intern/BLI_index_range.cc | 2 +- .../tests/BLI_linear_allocator_test.cc | 2 +- .../editors/interface/interface_layout.cc | 1 + .../editors/interface/views/grid_view.cc | 1 + source/blender/editors/mask/mask_select.cc | 1 + .../io/common/IO_dupli_persistent_id.hh | 4 +-- .../io/stl/tests/stl_importer_tests.cc | 1 + .../blender/nodes/shader/node_shader_util.hh | 1 - source/blender/render/intern/initrender.cc | 1 + 31 files changed, 61 insertions(+), 56 deletions(-) diff --git a/source/blender/animrig/intern/keyframing.cc b/source/blender/animrig/intern/keyframing.cc index de86f61d97f..1a19e2a9bac 100644 --- a/source/blender/animrig/intern/keyframing.cc +++ b/source/blender/animrig/intern/keyframing.cc @@ -28,6 +28,7 @@ #include "BKE_report.h" #include "BLI_dynstr.h" +#include "BLI_math_base.h" #include "BLI_utildefines.h" #include "BLT_translation.h" diff --git a/source/blender/asset_system/AS_asset_library.hh b/source/blender/asset_system/AS_asset_library.hh index df175d46755..81c77c12bbc 100644 --- a/source/blender/asset_system/AS_asset_library.hh +++ b/source/blender/asset_system/AS_asset_library.hh @@ -8,6 +8,7 @@ #pragma once +#include #include #include "AS_asset_catalog.hh" diff --git a/source/blender/blendthumb/src/blendthumb_extract.cc b/source/blender/blendthumb/src/blendthumb_extract.cc index aed6e0585b3..30903dfacc5 100644 --- a/source/blender/blendthumb/src/blendthumb_extract.cc +++ b/source/blender/blendthumb/src/blendthumb_extract.cc @@ -9,6 +9,7 @@ * but does not write it to a file. */ +#include #include #include "BLI_alloca.h" diff --git a/source/blender/blenkernel/BKE_attribute.hh b/source/blender/blenkernel/BKE_attribute.hh index dc1e2236cbf..d144e9d5c0e 100644 --- a/source/blender/blenkernel/BKE_attribute.hh +++ b/source/blender/blenkernel/BKE_attribute.hh @@ -4,6 +4,7 @@ #pragma once +#include #include #include "BLI_function_ref.hh" diff --git a/source/blender/blenkernel/intern/file_handler.cc b/source/blender/blenkernel/intern/file_handler.cc index 9f1b4a7ed71..f6c966e483c 100644 --- a/source/blender/blenkernel/intern/file_handler.cc +++ b/source/blender/blenkernel/intern/file_handler.cc @@ -2,6 +2,8 @@ * * SPDX-License-Identifier: GPL-2.0-or-later */ +#include + #include "BKE_file_handler.hh" #include "BLI_string.h" diff --git a/source/blender/blenlib/BLI_allocator.hh b/source/blender/blenlib/BLI_allocator.hh index 35cdf4d30da..17b0756f43e 100644 --- a/source/blender/blenlib/BLI_allocator.hh +++ b/source/blender/blenlib/BLI_allocator.hh @@ -25,12 +25,10 @@ * need. More complexity can be added when it seems necessary. */ -#include -#include +#include #include "MEM_guardedalloc.h" -#include "BLI_math_base.h" #include "BLI_utildefines.h" namespace blender { @@ -67,7 +65,7 @@ class RawAllocator { public: void *allocate(size_t size, size_t alignment, const char * /*name*/) { - BLI_assert(is_power_of_2_i(int(alignment))); + BLI_assert(is_power_of_2(int(alignment))); void *ptr = malloc(size + alignment + sizeof(MemHead)); void *used_ptr = reinterpret_cast( uintptr_t(POINTER_OFFSET(ptr, alignment + sizeof(MemHead))) & ~(uintptr_t(alignment) - 1)); diff --git a/source/blender/blenlib/BLI_any.hh b/source/blender/blenlib/BLI_any.hh index f5746aa40eb..eafe62807d6 100644 --- a/source/blender/blenlib/BLI_any.hh +++ b/source/blender/blenlib/BLI_any.hh @@ -15,7 +15,6 @@ */ #include -#include #include #include "BLI_memory_utils.hh" @@ -160,7 +159,9 @@ class Any { info_->copy_construct(&buffer_, &other.buffer_); } else { - memcpy(&buffer_, &other.buffer_, RealInlineBufferCapacity); + std::copy_n(static_cast(other.buffer_.ptr()), + RealInlineBufferCapacity, + static_cast(buffer_.ptr())); } } } @@ -176,7 +177,9 @@ class Any { info_->move_construct(&buffer_, &other.buffer_); } else { - memcpy(&buffer_, &other.buffer_, RealInlineBufferCapacity); + std::copy_n(static_cast(other.buffer_.ptr()), + RealInlineBufferCapacity, + static_cast(buffer_.ptr())); } } } diff --git a/source/blender/blenlib/BLI_assert.h b/source/blender/blenlib/BLI_assert.h index a26594fdf0c..ea8e14bf086 100644 --- a/source/blender/blenlib/BLI_assert.h +++ b/source/blender/blenlib/BLI_assert.h @@ -13,6 +13,8 @@ * - #BLI_STATIC_ASSERT_ALIGN */ +#include + #ifdef __cplusplus extern "C" { #endif diff --git a/source/blender/blenlib/BLI_bit_group_vector.hh b/source/blender/blenlib/BLI_bit_group_vector.hh index c6ca966394c..86e093213f9 100644 --- a/source/blender/blenlib/BLI_bit_group_vector.hh +++ b/source/blender/blenlib/BLI_bit_group_vector.hh @@ -33,7 +33,7 @@ class BitGroupVector { { if (group_size < 64) { /* Align to next power of two so that a single group never spans across two ints. */ - return int64_t(power_of_2_max_u(uint32_t(group_size))); + return power_of_2_max(group_size); } /* Align to multiple of BitsPerInt. */ return (group_size + BitsPerInt - 1) & ~(BitsPerInt - 1); diff --git a/source/blender/blenlib/BLI_bit_span.hh b/source/blender/blenlib/BLI_bit_span.hh index c464db9ffed..4fc20c8e814 100644 --- a/source/blender/blenlib/BLI_bit_span.hh +++ b/source/blender/blenlib/BLI_bit_span.hh @@ -8,7 +8,6 @@ #include "BLI_bit_ref.hh" #include "BLI_index_range.hh" -#include "BLI_math_bits.h" #include "BLI_memory_utils.hh" namespace blender::bits { diff --git a/source/blender/blenlib/BLI_bit_span_ops.hh b/source/blender/blenlib/BLI_bit_span_ops.hh index 4036236b627..f74ca38cb81 100644 --- a/source/blender/blenlib/BLI_bit_span_ops.hh +++ b/source/blender/blenlib/BLI_bit_span_ops.hh @@ -5,6 +5,7 @@ #pragma once #include "BLI_bit_span.hh" +#include "BLI_math_bits.h" namespace blender::bits { diff --git a/source/blender/blenlib/BLI_bit_vector.hh b/source/blender/blenlib/BLI_bit_vector.hh index 59df72924a6..b574910c781 100644 --- a/source/blender/blenlib/BLI_bit_vector.hh +++ b/source/blender/blenlib/BLI_bit_vector.hh @@ -37,8 +37,6 @@ * - `BLI_bitmap` should only be used in C code that can not use `blender::BitVector`. */ -#include - #include "BLI_allocator.hh" #include "BLI_bit_span.hh" #include "BLI_span.hh" diff --git a/source/blender/blenlib/BLI_hash.hh b/source/blender/blenlib/BLI_hash.hh index af88d5c5061..d9df5a600fc 100644 --- a/source/blender/blenlib/BLI_hash.hh +++ b/source/blender/blenlib/BLI_hash.hh @@ -61,7 +61,6 @@ * }; */ -#include #include #include #include diff --git a/source/blender/blenlib/BLI_hash_tables.hh b/source/blender/blenlib/BLI_hash_tables.hh index 978358670d0..4bfe657b67b 100644 --- a/source/blender/blenlib/BLI_hash_tables.hh +++ b/source/blender/blenlib/BLI_hash_tables.hh @@ -11,9 +11,7 @@ */ #include -#include -#include "BLI_allocator.hh" #include "BLI_memory_utils.hh" #include "BLI_utildefines.h" #include "BLI_vector.hh" @@ -26,30 +24,6 @@ namespace blender { * Those should eventually be de-duplicated with functions in BLI_math_base.h. * \{ */ -inline constexpr int64_t is_power_of_2_constexpr(const int64_t x) -{ - BLI_assert(x >= 0); - return (x & (x - 1)) == 0; -} - -inline constexpr int64_t log2_floor_constexpr(const int64_t x) -{ - BLI_assert(x >= 0); - return x <= 1 ? 0 : 1 + log2_floor_constexpr(x >> 1); -} - -inline constexpr int64_t log2_ceil_constexpr(const int64_t x) -{ - BLI_assert(x >= 0); - return (is_power_of_2_constexpr(int(x))) ? log2_floor_constexpr(x) : log2_floor_constexpr(x) + 1; -} - -inline constexpr int64_t power_of_2_max_constexpr(const int64_t x) -{ - BLI_assert(x >= 0); - return 1ll << log2_ceil_constexpr(x); -} - template inline constexpr IntT ceil_division(const IntT x, const IntT y) { BLI_assert(x >= 0); @@ -83,7 +57,7 @@ inline constexpr int64_t total_slot_amount_for_usable_slots( const int64_t max_load_factor_numerator, const int64_t max_load_factor_denominator) { - return power_of_2_max_constexpr(ceil_division_by_fraction( + return power_of_2_max(ceil_division_by_fraction( min_usable_slots, max_load_factor_numerator, max_load_factor_denominator)); } @@ -115,7 +89,7 @@ class LoadFactor { int64_t *r_total_slots, int64_t *r_usable_slots) const { - BLI_assert(is_power_of_2_i(int(min_total_slots))); + BLI_assert(is_power_of_2(int(min_total_slots))); int64_t total_slots = this->compute_total_slots(min_usable_slots, numerator_, denominator_); total_slots = std::max(total_slots, min_total_slots); diff --git a/source/blender/blenlib/BLI_implicit_sharing.hh b/source/blender/blenlib/BLI_implicit_sharing.hh index 072ac6e3fd8..42a76dcbcc7 100644 --- a/source/blender/blenlib/BLI_implicit_sharing.hh +++ b/source/blender/blenlib/BLI_implicit_sharing.hh @@ -10,8 +10,7 @@ #include -#include "BLI_compiler_attrs.h" -#include "BLI_utildefines.h" +#include "BLI_assert.h" #include "BLI_utility_mixins.hh" namespace blender { diff --git a/source/blender/blenlib/BLI_index_mask.hh b/source/blender/blenlib/BLI_index_mask.hh index fc0d48ce960..9e132a23f83 100644 --- a/source/blender/blenlib/BLI_index_mask.hh +++ b/source/blender/blenlib/BLI_index_mask.hh @@ -5,6 +5,7 @@ #pragma once #include +#include #include #include diff --git a/source/blender/blenlib/BLI_index_range.hh b/source/blender/blenlib/BLI_index_range.hh index 755848a6d0e..316dda821a1 100644 --- a/source/blender/blenlib/BLI_index_range.hh +++ b/source/blender/blenlib/BLI_index_range.hh @@ -38,10 +38,9 @@ */ #include -#include #include -#include "BLI_utildefines.h" +#include "BLI_assert.h" namespace blender { diff --git a/source/blender/blenlib/BLI_linear_allocator.hh b/source/blender/blenlib/BLI_linear_allocator.hh index ed62791614b..36151998e8c 100644 --- a/source/blender/blenlib/BLI_linear_allocator.hh +++ b/source/blender/blenlib/BLI_linear_allocator.hh @@ -65,7 +65,7 @@ template class LinearAllocator : NonCopya { BLI_assert(size >= 0); BLI_assert(alignment >= 1); - BLI_assert(is_power_of_2_i(alignment)); + BLI_assert(is_power_of_2(alignment)); const uintptr_t alignment_mask = alignment - 1; const uintptr_t potential_allocation_begin = (current_begin_ + alignment_mask) & diff --git a/source/blender/blenlib/BLI_probing_strategies.hh b/source/blender/blenlib/BLI_probing_strategies.hh index 088d8635123..0096e5021c6 100644 --- a/source/blender/blenlib/BLI_probing_strategies.hh +++ b/source/blender/blenlib/BLI_probing_strategies.hh @@ -43,6 +43,8 @@ * probing might work best. */ +#include + #include "BLI_sys_types.h" namespace blender { diff --git a/source/blender/blenlib/BLI_span.hh b/source/blender/blenlib/BLI_span.hh index 6146d87b866..058dcfa60b0 100644 --- a/source/blender/blenlib/BLI_span.hh +++ b/source/blender/blenlib/BLI_span.hh @@ -58,7 +58,6 @@ #include #include -#include #include #include "BLI_index_range.hh" diff --git a/source/blender/blenlib/BLI_utildefines.h b/source/blender/blenlib/BLI_utildefines.h index dd7557accd8..25ed1e27fec 100644 --- a/source/blender/blenlib/BLI_utildefines.h +++ b/source/blender/blenlib/BLI_utildefines.h @@ -254,6 +254,32 @@ extern "C" { */ #define DECIMAL_DIGITS_BOUND(t) (241 * sizeof(t) / 100 + 1) +#ifdef __cplusplus +inline constexpr int64_t is_power_of_2(const int64_t x) +{ + BLI_assert(x >= 0); + return (x & (x - 1)) == 0; +} + +inline constexpr int64_t log2_floor(const int64_t x) +{ + BLI_assert(x >= 0); + return x <= 1 ? 0 : 1 + log2_floor(x >> 1); +} + +inline constexpr int64_t log2_ceil(const int64_t x) +{ + BLI_assert(x >= 0); + return (is_power_of_2(int(x))) ? log2_floor(x) : log2_floor(x) + 1; +} + +inline constexpr int64_t power_of_2_max(const int64_t x) +{ + BLI_assert(x >= 0); + return 1ll << log2_ceil(x); +} +#endif + /** \} */ /* -------------------------------------------------------------------- */ diff --git a/source/blender/blenlib/BLI_vector.hh b/source/blender/blenlib/BLI_vector.hh index cd9c9fb5643..13e7cadb8d7 100644 --- a/source/blender/blenlib/BLI_vector.hh +++ b/source/blender/blenlib/BLI_vector.hh @@ -25,9 +25,6 @@ */ #include -#include -#include -#include #include "BLI_allocator.hh" #include "BLI_index_range.hh" @@ -35,8 +32,6 @@ #include "BLI_span.hh" #include "BLI_utildefines.h" -#include "MEM_guardedalloc.h" - namespace blender { namespace internal { diff --git a/source/blender/blenlib/intern/BLI_index_range.cc b/source/blender/blenlib/intern/BLI_index_range.cc index 3cb769491f3..df2c56f1c0e 100644 --- a/source/blender/blenlib/intern/BLI_index_range.cc +++ b/source/blender/blenlib/intern/BLI_index_range.cc @@ -14,7 +14,7 @@ namespace blender { AlignedIndexRanges split_index_range_by_alignment(const IndexRange range, const int64_t alignment) { - BLI_assert(is_power_of_2_i(alignment)); + BLI_assert(is_power_of_2(alignment)); const int64_t mask = alignment - 1; AlignedIndexRanges aligned_ranges; diff --git a/source/blender/blenlib/tests/BLI_linear_allocator_test.cc b/source/blender/blenlib/tests/BLI_linear_allocator_test.cc index 1f0a58e772b..8701fb301d5 100644 --- a/source/blender/blenlib/tests/BLI_linear_allocator_test.cc +++ b/source/blender/blenlib/tests/BLI_linear_allocator_test.cc @@ -11,7 +11,7 @@ namespace blender::tests { static bool is_aligned(void *ptr, uint alignment) { - BLI_assert(is_power_of_2_i(int(alignment))); + BLI_assert(is_power_of_2(int(alignment))); return (POINTER_AS_UINT(ptr) & (alignment - 1)) == 0; } diff --git a/source/blender/editors/interface/interface_layout.cc b/source/blender/editors/interface/interface_layout.cc index 86b6206098d..9fd4bb6fae0 100644 --- a/source/blender/editors/interface/interface_layout.cc +++ b/source/blender/editors/interface/interface_layout.cc @@ -20,6 +20,7 @@ #include "BLI_array.hh" #include "BLI_dynstr.h" #include "BLI_listbase.h" +#include "BLI_math_base.h" #include "BLI_rect.h" #include "BLI_string.h" #include "BLI_utildefines.h" diff --git a/source/blender/editors/interface/views/grid_view.cc b/source/blender/editors/interface/views/grid_view.cc index 0de81bdfaae..0773d4d1a18 100644 --- a/source/blender/editors/interface/views/grid_view.cc +++ b/source/blender/editors/interface/views/grid_view.cc @@ -6,6 +6,7 @@ * \ingroup edinterface */ +#include #include #include diff --git a/source/blender/editors/mask/mask_select.cc b/source/blender/editors/mask/mask_select.cc index 34f5d2aee31..d9a564b449f 100644 --- a/source/blender/editors/mask/mask_select.cc +++ b/source/blender/editors/mask/mask_select.cc @@ -10,6 +10,7 @@ #include "BLI_lasso_2d.h" #include "BLI_listbase.h" +#include "BLI_math_base.h" #include "BLI_rect.h" #include "BLI_utildefines.h" diff --git a/source/blender/io/common/IO_dupli_persistent_id.hh b/source/blender/io/common/IO_dupli_persistent_id.hh index 482a98bc751..cf57f6498be 100644 --- a/source/blender/io/common/IO_dupli_persistent_id.hh +++ b/source/blender/io/common/IO_dupli_persistent_id.hh @@ -8,8 +8,8 @@ #include "DNA_object_types.h" /* For MAX_DUPLI_RECUR */ #include -#include -#include +#include +#include namespace blender::io { diff --git a/source/blender/io/stl/tests/stl_importer_tests.cc b/source/blender/io/stl/tests/stl_importer_tests.cc index 27772feb478..b8682f1e59b 100644 --- a/source/blender/io/stl/tests/stl_importer_tests.cc +++ b/source/blender/io/stl/tests/stl_importer_tests.cc @@ -7,6 +7,7 @@ #include "BKE_mesh.hh" #include "BKE_object.hh" +#include "BLI_math_base.h" #include "BLI_math_vector_types.hh" #include "BLI_string.h" diff --git a/source/blender/nodes/shader/node_shader_util.hh b/source/blender/nodes/shader/node_shader_util.hh index a7041dccdfa..d3693a06f52 100644 --- a/source/blender/nodes/shader/node_shader_util.hh +++ b/source/blender/nodes/shader/node_shader_util.hh @@ -10,7 +10,6 @@ #include #include -#include #include "BKE_node.hh" diff --git a/source/blender/render/intern/initrender.cc b/source/blender/render/intern/initrender.cc index b32b3f0ad6a..3abb219f154 100644 --- a/source/blender/render/intern/initrender.cc +++ b/source/blender/render/intern/initrender.cc @@ -17,6 +17,7 @@ #include "BLI_blenlib.h" #include "BLI_ghash.h" +#include "BLI_math_base.h" #include "BLI_math_matrix.h" #include "BLI_utildefines.h"