Commit Graph

115 Commits

Author SHA1 Message Date
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
Campbell Barton b7f3e0d84e Cleanup: spelling & punctuation in comments
Also remove some unhelpful/redundant comments.
2023-09-14 13:25:24 +10:00
Jacques Lucke d1aeb1c3b4 Cleanup: add static assert for detect bad usage of MEM_SAFE_FREE
Without this, there is not compilation error when doing e.g.:
```
int a;
MEM_SAFE_FREE(a);
```
2023-07-28 13:50:02 +02:00
Jacques Lucke 3e6025c1b1 Cleanup: move some files to c++
Also see #103343.

Pull Request: https://projects.blender.org/blender/blender/pulls/110529
2023-07-27 13:10:42 +02:00
Campbell Barton 65f99397ec License headers: use SPDX-FileCopyrightText in all sources 2023-06-15 13:35:34 +10:00
Sergey Sharybin d32d787f5f Clang-Format: Allow empty functions to be single-line
For example

```
OIIOOutputDriver::~OIIOOutputDriver()
{
}
```

becomes

```
OIIOOutputDriver::~OIIOOutputDriver() {}
```

Saves quite some vertical space, which is especially handy for
constructors.

Pull Request: https://projects.blender.org/blender/blender/pulls/105594
2023-03-29 16:50:54 +02:00
Julian Eisel 4126284e46 Allocator: Fail building when trying to MEM_delete a void pointer
`MEM_delete()` is designed for type safe destruction and freeing, void
pointers make that impossible.
Was reviewing a patch that was trying to free a C-style custom data
pointer this way. Apparently MSVC compiles this just fine, other
compilers error out. Make sure this is a build error on all platforms
with a useful message.
2023-02-14 12:48:26 +01:00
Sergey Sharybin 09b9e1e95e Fix T102225: Crash opening menus
Seems to be introduced by 99e5024e97.

The crash is caused by the difference in the expected alignment
of the `uiPopupMenu` which is 16 bytes and the actual alignment
returned by the `MEM_mallocN()` which is 8 bytes due to the memory
head.

Now made it so that `MEM_new()` can be used for types with any
alignment.

Differential Revision: https://developer.blender.org/D16375
2022-11-03 14:32:04 +01:00
Campbell Barton c29d63aa5e Cleanup: spelling in comments 2022-08-30 16:22:49 +10:00
Brecht Van Lommel ff27457240 Allocator: add MEM_cnew_array
This is a more C++ friendly version MEM_calloc_arrayN, like MEM_cnew is for
MEM_callocN. For cases where data structures are still C and Vector or Array
don't work.
2022-08-29 19:45:58 +02:00
Julian Eisel 28e0cc424d Guarded allocator: Function to rename block for debugging
We have plenty of sorta generic functions, that allocate memory with
some generic name for debugging. When such a function is called and the
memory leaks, it may be unclear which call to it allocated the unfreed
memory (and thus which execution path leads to the leak).

The added function is only available if `NDEBUG` is not defined.

Differential Revision: https://developer.blender.org/D15605

Reviewed by: Sergey Sharybin, Bastien Montagne
2022-08-04 11:26:57 +02:00
Campbell Barton 61a7e5be18 Cleanup: '*' prefix C-comment blocks 2022-06-01 15:38:48 +10:00
Sergey Sharybin 42e13ea4bf Cleanup: Deprecated field access in outliner_duplicate
Solved by introducing introducing a variant of MEM_cnew which behaves
as a copy-constructor for a trivial types.

Alternative approach would be to surround DNA structs with clang/gcc
diagnostics push/modify/pop so that implicitly defined constructors
and copy operators are allowed to access deprecated fields.

The downside of the DNA approach is that it will require some way to
easily apply diagnostics modifications to many structs, which is not
possible currently.

The newly added MEM_cnew has other good usecases, so is easiest to
use this route, at least for now.

Differential Revision: https://developer.blender.org/D14356
2022-03-16 12:38:23 +01:00
Campbell Barton c434782e3a File headers: SPDX License migration
Use a shorter/simpler license convention, stops the header taking so
much space.

Follow the SPDX license specification: https://spdx.org/licenses

- C/C++/objc/objc++
- Python
- Shell Scripts
- CMake, GNUmakefile

While most of the source tree has been included

- `./extern/` was left out.
- `./intern/cycles` & `./intern/atomic` are also excluded because they
  use different header conventions.

doc/license/SPDX-license-identifiers.txt has been added to list SPDX all
used identifiers.

See P2788 for the script that automated these edits.

Reviewed By: brecht, mont29, sergey

Ref D14069
2022-02-11 09:14:36 +11:00
Julian Eisel 4aeb6add6e Guarded allocator: Document non-obvious initialization with MEM_new()
Initialization with `MEM_new()` depends a lot on the initialization rules
of C++, which are not obvious. Calling it with no arguments to be passed
to the constructor may cause the resulting object to be implicitly 0
initialized (or parts of it). This can have an impact on performance
sensitive code, so it's something to document.

Alternatively we could enforce default initialization (as opposed to the
value initalization that happens now), but this could cause
uninitialized memory in a way that isn't obvious either. This is a
possible source of bugs, so Jacques and I decided against it.
2022-02-04 16:57:46 +01:00
Campbell Barton ac3324f197 Cleanup: spelling in comments 2022-01-31 10:51:33 +11:00
Campbell Barton c77f837598 Revert "Cleanup: remove declaration for removed function"
This reverts commit aa363ec2ae.

The function still exists, this commit caused a warning with Clang
So keep MEM_printmemlist_pydict.
2022-01-11 18:16:00 +11:00
Campbell Barton aa363ec2ae Cleanup: remove declaration for removed function 2022-01-06 13:54:51 +11:00
Jacques Lucke 594438ef0d Allocator: add missing include
The placement-new operator requires `#include <new>`.
It is used in `MEM_new`.
2021-12-27 15:41:56 +01:00
Jacques Lucke a3ad5abf2f Allocator: simplify using guarded allocator in C++ code
Using the `MEM_*` API from C++ code was a bit annoying:
* When converting C to C++ code, one often has to add a type cast on
  returned `void *`. That leads to having the same type name three times
  in the same line. This patch reduces the amount to two and removes the
  `sizeof(...)` from the line.
* The existing alternative of using `OBJECT_GUARDED_NEW` looks a out
  of place compared to other allocation methods. Sometimes
  `MEM_CXX_CLASS_ALLOC_FUNCS` can be used when structs are defined
  in C++ code. It doesn't look great but it's definitely better. The downside
  is that it makes the name of the allocation less useful. That's because
  the same name is used for all allocations of a type, independend of
  where it is allocated.

This patch introduces three new functions: `MEM_new`, `MEM_cnew` and
`MEM_delete`. These cover the majority of use cases (array allocation is
not covered).

The `OBJECT_GUARDED_*` macros are removed because they are not
needed anymore.

Differential Revision: https://developer.blender.org/D13502
2021-12-17 15:42:28 +01:00
Campbell Barton f6fd3a84c2 Cleanup: reorganize doxygen modules
- Nest compositor pages under the compositor module
- Nest GUI, DNA/RNA & externformats modules under Blender.
- Remove modules from intern which no longer exist.
- Add intern modules (atomic, eigen, glew-mx, libc_compat, locale,
  numaapi, rigidbody, sky, utfconv).
- Use 'intern_' prefix for intern modules since some of the modules
  use generic terms such as locale & atomic.
2021-12-14 20:56:11 +11:00
Sybren A. Stüvel bee7a56687 Cleanup: document that `MEM_dupallocN` is NULL-safe
Add comment explaining `MEM_dupallocN` is NULL-safe, in that it returns
NULL when it receives a NULL pointer. This is currently true for both
implementations of the function (`MEM_lockfree_dupallocN` and
`MEM_guarded_dupallocN`), and will be expected of other implementations
as well.

No functional changes.
2021-11-16 17:11:45 +01:00
Campbell Barton 4b9ff3cd42 Cleanup: comment blocks, trailing space in comments 2021-06-24 15:59:34 +10:00
Campbell Barton bae66609b4 Cleanup: use our own code style for doxy-gen comment blocks 2021-04-09 19:00:04 +10:00
Campbell Barton 865025c343 Cleanup: remove stdio.h header from MEM_guardedalloc.h
This was included for `FILE *` which isn't used in the header.

Ref D10799
2021-03-24 19:34:44 +11:00
Campbell Barton bb6765f28f Cleanup: spelling 2021-03-18 09:36:44 +11:00
Campbell Barton b2a6e2abdb Cleanup: remove extra in trailing asterisk
Comment blocks not conforming to convention.
2021-01-20 16:14:00 +11:00
Campbell Barton 25266caa45 Cleanup: spelling 2020-11-20 11:39:22 +11:00
Sergey Sharybin 4a8cf9d182 Guarded allocator: Add explicit switch to the lockfree implementation
Previously the only way to use lockfree implementation was to start
executable and never switch to guarded allocator.

Surely, it is not possible to switch implementation once any allocation
did happen, but some tests are desired to test lock-free implementation
of the allocator. Those tests did not operate properly because the main
entry point of tests are forcing guarded allocator to help catching
bugs.

This change makes it possible for those tests to ensure they do operate
on lock-free implementation.

There is no functional changes here, preparing boilerplate for an
upcoming work on the allocator tests themselves.
2020-11-19 16:17:48 +01:00
Campbell Barton 14b2a35c8b Cleanup: use parenthesis for if statements in macros 2020-09-19 16:28:17 +10:00
Campbell Barton 62f2925e43 Fix T80774: (correction) keep MEM_init_memleak_detection call early 2020-09-15 13:32:00 +10:00
Campbell Barton bedc68a838 Cleanup: consistent syntax for doxygen parameters
Also use back-slash instead of '@'.
2020-09-08 09:18:59 +10:00
Jacques Lucke 8a9912eaf8 Tests: fail automated tests on memory leaks and other internal errors
This adds a new `--debug-exit-on-error` flag. When it is set, Blender
will abort with a non-zero exit code when there are internal errors.
Currently, "internal errors" includes memory leaks detected by
guardedalloc and error/fatal log entries in clog.

The new flag is passed to Blender in various places where automated
tests are run. Furthermore, the `--debug-memory` flag is used in tests,
because that makes the verbose output more useful, when dealing
with memory leaks.

Reviewers: brecht, sergey

Differential Revision: https://developer.blender.org/D8665
2020-08-26 22:02:02 +02:00
Campbell Barton 901ee66ea1 Cleanup: use term init instead of initialize/initialise
The abbreviation 'init' is brief, unambiguous and already used
in thousands of places, also initialize is often accidentally
written with British spelling.
2020-08-01 13:51:05 +10:00
Jacques Lucke 236ca8fbe8 Allocator: make leak detection work with static variables
When definining static variables that own memory, you should
use the "construct on first use" idiom. Otherwise, you'll get
a warning when Blender exits.

More details are provided in D8354.

Differential Revision: https://developer.blender.org/D8354
2020-07-24 12:26:11 +02:00
Jacques Lucke 02cd159539 Cleanup: add comment explaining operator delete 2020-07-07 12:31:25 +02:00
Jacques Lucke 5761cb9ee2 Guarded Allocator: add missing operator delete
This resolves warning C4291 on windows.
2020-07-07 11:52:45 +02:00
Sergey Sharybin 468adfa4fd Guarded allocator: Override placement new operator
Allows to in-place construct objects which are using guarded allocator.
2020-07-03 12:30:20 +02:00
Campbell Barton 074929d1c5 MEM_guardedalloc: allow freeing const arrays with MEM_SAFE_FREE
'const' arrays couldn't use this macro with GNUC.
2020-06-24 22:13:27 +10:00
Brecht Van Lommel 183ba284f2 Cleanup: make guarded memory allocation always thread safe
Previously this would be enabled when threads were used, but threads are now
basically always in use so there is no point. Further, this is only needed for
guarded allocation with --debug-memory which is not performance critical.
2020-05-20 01:03:05 +02:00
Brecht Van Lommel 120e9924c1 Cleanup: remove legacy mmap memory allocation for 32 bit
This helped to go beyond the 4GB limit, but is no longer relevant for 64 bit.
2020-05-20 00:57:41 +02:00
Dalai Felinto 2d1cce8331 Cleanup: `make format` after SortedIncludes change 2020-03-19 09:33:58 +01:00
Brecht Van Lommel c76a8f65b7 Memory: add OBJECT_GUARDED_SAFE_DELETE like MEM_SAFE_FREE 2020-01-27 12:22:01 +01:00
Brecht Van Lommel 1107af1abb Fix OBJECT_GUARDED_FREE compiler error when type is in namespace 2020-01-27 12:22:01 +01:00
Campbell Barton 177a0ca131 Cleanup: comments (long lines) in various intern/ libs 2019-05-01 21:00:56 +10:00
Campbell Barton 108045faa0 ClangFormat: format '#if 0' code in intern/ 2019-04-17 08:17:13 +02:00
Campbell Barton e12c08e8d1 ClangFormat: apply to source, most of intern
Apply clang format as proposed in T53211.

For details on usage and instructions for migrating branches
without conflicts, see:

https://wiki.blender.org/wiki/Tools/ClangFormat
2019-04-17 06:21:24 +02:00
Campbell Barton de13d0a80c doxygen: add newline after \file
While \file doesn't need an argument, it can't have another doxy
command after it.
2019-02-18 08:22:12 +11:00
Campbell Barton eef4077f18 Cleanup: remove redundant doxygen \file argument
Move \ingroup onto same line to be more compact and
make it clear the file is in the group.
2019-02-06 15:45:22 +11:00