Commit Graph

447 Commits

Author SHA1 Message Date
Jesse Yurkovich
f60c528c48 Cleanup: Fix one-definition-rule violations for various structs
This fixes most "One Definition Rule" violations inside blender proper
resulting from duplicate structures of the same name. The fixes were
made similar to that of !135491. See also #120444 for how this has come
up in the past.

These were found by using the following compile options:
-flto=4 -Werror=odr -Werror=lto-type-mismatch -Werror=strict-aliasing

Note: There are still various ODR issues remaining that require
more / different fixes than what was done here.

Pull Request: https://projects.blender.org/blender/blender/pulls/136371
2025-04-04 21:05:16 +02:00
Bastien Montagne
2900cfa50a MEM_guardedalloc: Add template 'type-safe' versions of MEM_mallocN.
Same thing as for `MEM_callocN<T>` and `MEM_freeN<T>` in dd168a35c5,
allows to reduce type verbosity, and increase type safety.
2025-03-05 19:35:50 +01:00
Bastien Montagne
dd168a35c5 Refactor: Replace MEM_cnew with a type-aware template version of MEM_callocN.
The general idea is to keep the 'old', C-style MEM_callocN signature, and slowly
replace most of its usages with the new, C++-style type-safer template version.

* `MEM_cnew<T>` allocation version is renamed to `MEM_callocN<T>`.
* `MEM_cnew_array<T>` allocation version is renamed to `MEM_calloc_arrayN<T>`.
* `MEM_cnew<T>` duplicate version is renamed to `MEM_dupallocN<T>`.

Similar templates type-safe version of `MEM_mallocN` will be added soon
as well.

Following discussions in !134452.

NOTE: For now static type checking in `MEM_callocN` and related are slightly
different for Windows MSVC. This compiler seems to consider structs using the
`DNA_DEFINE_CXX_METHODS` macro as non-trivial (likely because their default
copy constructors are deleted). So using checks on trivially
constructible/destructible instead on this compiler/system.

Pull Request: https://projects.blender.org/blender/blender/pulls/134771
2025-03-05 16:35:09 +01:00
Bastien Montagne
e0829f9e55 Cleanup: Update comment in MEM_freeN<T> about MSVC and triviality check. 2025-02-21 10:37:13 +01:00
Bastien Montagne
718d4ffe9f Add some type-check to MEM_freeN<T> with MSVC.
Using `DNA_DEFINE_CXX_METHODS` in DNA structs make them non-trivially
copyable for MSVC, so use a narrower check that should still catch most
issues.

Pull Request: https://projects.blender.org/blender/blender/pulls/134875
2025-02-21 10:30:29 +01:00
Bastien Montagne
318ae49f1e Cleanup: Remove void * handling from MEM_freen<T>.
Followup to 48e26c3afe, and discussions in !134771 about keeping
'C-style' and 'C++ template type-safe style' implementations of our
guardedalloc separated. And it makes `MEM_freeN<T>` code simpler.

Also skip type-checking in `MEM_freeN<T>` only with MSVC, as clang-cl on
windows-arm64 does work fine with DNA structs using
`DNA_DEFINE_CXX_METHODS`.

Pull Request: https://projects.blender.org/blender/blender/pulls/134861
2025-02-20 16:42:22 +01:00
Bastien Montagne
48e26c3afe MEM_guardedalloc: Refactor to add more type-safety.
The main goal of these changes are to improve static (i.e. build-time)
checks on whether a given data can be allocated and freed with `malloc`
and `free` (C-style), or requires proper C++-style construction and
destruction (`new` and `delete`).

* Add new `MEM_malloc_arrayN_aligned` API.
* Make `MEM_freeN` a template function in C++, which does static assert on
  type triviality.
* Add `MEM_SAFE_DELETE`, similar to `MEM_SAFE_FREE` but calling
  `MEM_delete`.

The changes to `MEM_freeN` was painful and useful, as it allowed to fix a bunch
of invalid calls in existing codebase already.

It also highlighted a fair amount of places where it is called to free incomplete
type pointers, which is likely a sign of badly designed code (there should
rather be an API to destroy and free these data then, if the data type is not fully
publicly exposed). For now, these are 'worked around' by explicitly casting the
freed pointers to `void *` in these cases - which also makes them easy to search for.
Some of these will be addressed separately (see blender/blender!134765).

Finally, MSVC seems to consider structs defining new/delete operators (e.g. by
using the `MEM_CXX_CLASS_ALLOC_FUNCS` macro) as non-trivial. This does not
seem to follow the definition of type triviality, so for now static type checking in
`MEM_freeN` has been disabled for Windows. We'll likely have to do the same
with type-safe `MEM_[cm]allocN` API being worked on in blender/blender!134771

Based on ideas from Brecht in blender/blender!134452

Pull Request: https://projects.blender.org/blender/blender/pulls/134463
2025-02-20 10:37:10 +01:00
Julian Eisel
152c6c54e4 Cleanup: Improve API comment for MEM_new()
194e233d86 caused a discussion in the chat about the initialization
behavior of `MEM_new()`, and agreement was to not rely on
zero-initialization ever. Noted this in the API comment now.

Some people found the existing comment useful but it still left some
questions. Tried to clarify that now.

This is a crucial memory management function, it's important to have
behavior documented well, even if a full explanation is out-of-scope.
Also added another link in case people want to check more details.

Pull Request: https://projects.blender.org/blender/blender/pulls/134577
2025-02-17 16:18:04 +01:00
Jacques Lucke
64a9260921 Core: remove WITH_CXX_GUARDEDALLOC option
This implements the proposal from #124512. For that it contains the following
changes:
* Remove the global override of `new`/`delete` when `WITH_CXX_GUARDEDALLOC` was
  enabled.
* Always use `MEM_CXX_CLASS_ALLOC_FUNCS` where it is currently used. This used
  to be guarded by `WITH_CXX_GUARDEDALLOC` in some but not all cases. This means
  that a few classes which didn't use our guarded allocator by default before,
  are now using it.

Pull Request: https://projects.blender.org/blender/blender/pulls/130181
2024-11-13 13:39:49 +01:00
Campbell Barton
0fc27c8d81 Cleanup: spelling in comments 2024-09-20 13:14:57 +10:00
Bastien Montagne
16eff5af62 Cleanup: Update documentation of MEM module API.
Make it clearer that C++-style MEM_new/MEM_delete and
C-style MEM_cnew/MEM_malloc/etc./MEM_freeN calls should never be mixed
on the same data.
2024-09-03 13:00:21 +02:00
Campbell Barton
5cb29528e6 Cleanup: spelling in comments 2024-08-26 11:50:15 +10:00
Campbell Barton
da94978cc4 Cleanup: format 2024-08-15 21:26:12 +10:00
Campbell Barton
b5e0b59736 Cleanup: remove space around identifiers in C-style comments 2024-08-15 20:46:00 +10:00
Bastien Montagne
63016ad965 MEM management: Add data storage only destructed after memleak detection.
Add a new API to store data that is guaranteed to not be freed
before the memleak detector has run.

This will be used in next commit by the readfile code to improve
reporting on leaks from blendfile readingi process.

This is done by a two-layer approach:

A new templated `MEM_construct_leak_detection_data` allows to
create any type of data. Its ownership and lifetime are handled
internally, and guaranteed to not be destroyed before the memleak
detector has run.

Add a new template-based 'allocation string storage' system to
`intern/memutil`. This uses the new `Guardedalloc Persistent Storage`
system to store all 'complex' allocation messages, that cannot be
defined as literals.

Internally, the storage is done through an owning reference (a
`shared_ptr`) of the created data into a mutex-protected static
vector.

`MEM_init_memleak_detection` code ensures that this static storage
is created before the memleak detection data, so that it is destructed
after the memleak detector has ran.

The main container (`AllocStringStorageContainer`) is wrapping a
map of `{string -> AllocStringStorage<key_type, hash_type>}`.
The key is a storage identifier.

Each storage is also a map wrapped into a simple templated API
class (`AllocStringStorage`), where the values are the alloc strings,
and the keys type is defined by the user code.

Pull Request: https://projects.blender.org/blender/blender/pulls/125320
2024-07-29 11:47:04 +02:00
Campbell Barton
068bb052b2 Cleanup: use const variables in mallocn reporting 2024-07-23 15:58:21 +10:00
Weizhen Huang
204407ca11 Cleanup: make format 2024-07-08 16:22:51 +02:00
Campbell Barton
67bb5b25b1 Cleanup: add printf function attributes, quiet GCC warnings 2024-07-08 22:32:53 +10:00
Bastien Montagne
4195e9e1a7 MEM_guarded: Improve error reporting usefulness.
The main change from this commit is the usage of ASAN poisoning (if
available) to trigger an ASAN report on the erroring memory block.

The main benefit is the report of the allocation backtrace of that
faulty memory block.

Pull Request: https://projects.blender.org/blender/blender/pulls/124231
2024-07-08 12:10:37 +02:00
Bastien Montagne
06be295946 Add detection of mismatches usages of MEM_new/MEM_freeN.
This commit will error (and abort if enabled) when trying to call
`MEM_freeN` (and related `MEM_dupallocN`, `MEM_reallocN` and
`MEM_recallocN` functions) with a pointer created the C++ way (i.e.
through `MEM_new`, or the guardedalloc-overloaded `new` operator).

To do so, it adds internal use only implementations for `malloc_alligned`
and `free`, which take an extra parameter indicating whether they are
dealing with data created/deleted the 'C++ way' (using `new`/`delete`
and similar).

The cpp-created data are flagged with the new
`MEMHEAD_FLAG_FROM_CPP_NEW`, either in the lower two-bytes len value for
lockfree allocator, or as a new flag member of the guarded allocator
header data.

The public `MEM_new`/`MEM_delete` template functions, and the
guardedalloc-overloaded versions of `new`/`delete` operators are updated
accordingly.

These changes have been successfully tested both with and without
`WITH_CXX_GUARDEDALLOC`.

NOTE: A lot of mismatches have already been fixed in `main` before merging
this change. There are likely some less easy to trigger ones still in our
codebase though.

Pull Request: https://projects.blender.org/blender/blender/pulls/123740
2024-07-03 17:23:03 +02:00
Bastien Montagne
4afb48acad Cleanup: Move internal guardedalloc headers to C++.
Pull Request: https://projects.blender.org/blender/blender/pulls/124106
2024-07-03 16:04:18 +02:00
Bastien Montagne
32ece7d604 Fix (unreported) 'copy' version of MEM_cnew not handling alignment.
Almost certainly not an issue in current codebase (this 'copy' version
of `MEM_cnew` does not seem much used in the first place), but better be
consistent with the 'allocating' version.

Pull Request: https://projects.blender.org/blender/blender/pulls/123445
2024-06-20 11:00:34 +02:00
Bastien Montagne
461ee89b61 Cleanup: Sync 'alignment' checks of guarded allocator with lockfree one.
Sync a bit better the checks on the alignment value between
`MEM_lockfree_mallocN_aligned` and `MEM_guarded_mallocN_aligned`.

The only significant change, in `MEM_guarded_mallocN_aligned`, is the
usage of `ALIGNED_MALLOC_MINIMUM_ALIGNMENT` instead of 'magic value' `8`.
This should not have any effect on 64bits platforms, but on 32bits ones
the minimum alignment would be reduced from `8` to `4` now.

NOTE: we could also consider making these checks part of a utils
function, instead of duplicating them in the codebase.
2024-06-20 11:00:33 +02:00
Campbell Barton
fd589fdca4 Cleanup: various non functional C++ changes 2024-04-20 13:46:14 +10:00
Campbell Barton
cf37424203 Cleanup: use const pointers, quiet cppcheck unreadVariable warning 2024-04-17 11:36:38 +10:00
Campbell Barton
20b0805213 Cleanup: use const pointers where possible 2024-04-16 12:27:47 +10:00
Campbell Barton
90f1d1cc61 Cleanup: use doxygen formatting for guarded-allocator 2024-04-16 12:27:13 +10:00
Jacques Lucke
00648411ea MEM: support overaligned types in MEM_cnew_array
This is similar to the recent change for `MEM_cnew`: 60a3d85b88.
A new function called `MEM_calloc_arrayN_aligned` is added that is used by both `cnew` functions now.

Pull Request: https://projects.blender.org/blender/blender/pulls/120632
2024-04-15 19:11:06 +02:00
Jacques Lucke
e3b8d45c93 Cleanup: move mallocn to c++
Required for a cleaner solution for #120632.
2024-04-15 13:02:17 +02:00
dupoxy
1079f0918e Cleanup: use function style cast in MAKE_ID macro
Ref: !120630
2024-04-14 23:08:45 +10:00
Campbell Barton
a70d6d79dd Cleanup: various non-functional C++ changes 2024-04-14 12:24:17 +10:00
Clément Foucault
d1d90ecbec Cleanup: MEM: Remove outdated comment 2024-04-12 18:01:26 +02:00
Clément Foucault
60a3d85b88 MEM: Make MEM_cnew return aligned memory
Now made it so that `MEM_cnew()` can be used for types with any
alignment.

Fixes #120407.

Pull Request: https://projects.blender.org/blender/blender/pulls/120569
2024-04-12 17:52:27 +02:00
Sergey Sharybin
a4b36cd0d5 Guarded allocator: Ensure alignment and size of MemHead
Ensure that the MemHead and MemHeadAligned are such that memory
allocation followed with the head offset keeps the allocation
aligned to at least MEM_MIN_CPP_ALIGNMENT.

Pull Request: https://projects.blender.org/blender/blender/pulls/120582
2024-04-12 17:22:46 +02:00
Sergey Sharybin
fac451e513 Cleanup: Convert guarded allocator to C++
Pull Request: https://projects.blender.org/blender/blender/pulls/120577
2024-04-12 16:44:49 +02:00
Campbell Barton
09ee8d97e6 Cleanup: use C-style comments for descriptive text 2024-04-11 17:44:27 +10:00
Campbell Barton
52ce8d408f Cleanup: use const arguments & variables 2024-04-04 10:55:10 +11:00
Jacques Lucke
8ea425d95d Fix #118402: enforce expected minimum alignment in MEM_CXX_CLASS_ALLOC_FUNCS
This `operator new` added in ecc3e78d78
are only called if the alignment is greater than `__STDCPP_DEFAULT_NEW_ALIGNMENT__`.
This is generally 8 or 16 depending on the platform. `MEM_mallocN` does
guarantee 16 byte alignment currently (in fact it's usually not 16 byte aligned
because of `MemHead`). Now `MEM_mallocN_aligned` is used with the default
alignment, even if we don't know that the type does not require it.

An alternative would be to pass the alignment to `MEM_CXX_CLASS_ALLOC_FUNCS`,
but that would be more intrusive.

Pull Request: https://projects.blender.org/blender/blender/pulls/118568
2024-02-21 18:14:11 +01:00
Jacques Lucke
ecc3e78d78 Fix #118402: support overaligned types in MEM_CXX_CLASS_ALLOC_FUNCS
Previously, the alignment of structs that use `MEM_CXX_CLASS_ALLOC_FUNCS`
were not taken into account when doing the allocation. This can cause some data
to be mis-aligned and leads to crashes when cpu instructions or code expect the
data to be aligned.

The fix is to provide an overload of `operator new` that accepts the alignment as parameter.

More info: https://en.cppreference.com/w/cpp/language/new (search for `align_val_t`).

Pull Request: https://projects.blender.org/blender/blender/pulls/118526
2024-02-20 18:44:26 +01:00
Hans Goudey
da6b45f9b8 Cleanup: Make format 2024-01-16 10:56:55 -05:00
Eugene Kuznetsov
10dfa07e36 Linux: Use huge pages in jemalloc to speed up allocations
Enable huge pages for jemalloc. This requests the Linux kernel to use
huge (2 MB) pages for large allocations. This has the effect of speeding
up first accesses to those allocations, and possibly also speeds up future
accesses by reducing TLB faults.

By default, 4 KB pages are used unless the user enables huge pages through
a kernel parameter or an obscure sysfs setting.

For Cycles benchmarks, this gives about a 5% CPU rendering performance
improvement. It likely also improves performance in other areas of Blender.

Pull Request: https://projects.blender.org/blender/blender/pulls/116663
2024-01-16 16:37:40 +01:00
Brecht Van Lommel
364beee159 Tests: add option to build one binary per GTest file
Bundling many tests in a single binary reduces build time and disk space
usage, but is less convenient for running individual tests command line
as filter flags need to be used.

This adds WITH_TESTS_SINGLE_BINARY to generate one executable file per
source file. Note that enabling this option requires a significant amount
of disk space.

Due to refactoring, the resulting ctest names are a bit different than
before. The number of tests is also a bit different depending if this
option is used, as one uses gtests discovery and the other is organized
purely by filename, which isn't always 1:1.

Co-authored-by: Sergey Sharybin <sergey@blender.org>
Pull Request: https://projects.blender.org/blender/blender/pulls/114604
2024-01-03 18:35:50 +01:00
Brecht Van Lommel
f63accd3b6 Cleanup: move CMake test utility functions into testing.cmake
Combining functions from macros.cmake and Modules/GTestTesting.cmake.
It was unusual to have Blender specific code in the Modules folder.

Pull Request: https://projects.blender.org/blender/blender/pulls/116719
2024-01-03 14:49:11 +01:00
Brecht Van Lommel
d377ef2543 Clang Format: bump to version 17
Along with the 4.1 libraries upgrade, we are bumping the clang-format
version from 8-12 to 17. This affects quite a few files.

If not already the case, you may consider pointing your IDE to the
clang-format binary bundled with the Blender precompiled libraries.
2024-01-03 13:38:14 +01:00
Brecht Van Lommel
4ce14a639f Revert "Cleanup: move CMake test utility functions into testing.cmake"
This breaks execution of some Windows tests.

This reverts commit 4190a61020.
2024-01-02 19:06:39 +01:00
Brecht Van Lommel
4190a61020 Cleanup: move CMake test utility functions into testing.cmake
Combining functions from macros.cmake and Modules/GTestTesting.cmake.
It was unusual to have Blender specific code in the Modules folder.
2024-01-02 15:34:52 +01:00
Campbell Barton
9898602e9d Cleanup: clarify #ifndef checks in trailing #endif comments 2023-12-07 10:38:54 +11:00
Campbell Barton
20fd012adb Valgrind: suppress warnings with MemPool & MEM_* trailing padding
Suppress false positive Valgrind warnings which flooded the output.

- BLI_mempool alloc/free & iteration.
- Set alignment padding bytes at the end of MEM_* allocations
  as "defined" since this causes many false positive warnings
  in blend file writing and MEMFILE comparisons.
- Set MEM_* allocations as undefined when `--debug-memory`
  is passed in to account for debug initialization.
- Initialize pad bytes in TextLine allocations.
2023-12-05 17:12:32 +11:00
Campbell Barton
d4f1a9b0ad Build: resolve build error on MSVC
Address error introduced by [0].

[0]: 6b967287c9
2023-09-28 14:23:11 +10:00
Campbell Barton
6b967287c9 MEM_guarded_alloc: restore execinfo.h back-trace on Linux/Apple
Recently [0] replaced back-traces from `execinfo.h` with ASAN's
`__asan_describe_address` since the linking options to hide symbols
cause the stack-traces only to include addresses (without functions).
Although using ASAN makes sense when enabled, in my tests the
stack-traces are sometimes empty. Using CMAKE_BUILD_TYPE=RelWithDebInfo
for e.g. wasn't showing a stack-trace for the leak fixed in [1].

A utility is now included to conveniently expand the addresses from
these stack traces (`tools/utils/addr2line_backtrace.py`).

Restore support for the execinfo stack-trace reporting, used when ASAN
is disabled.

[0]: 2e79ca3205
[1]: a9f0d19197
2023-09-28 12:45:04 +10:00