Commit Graph

375 Commits

Author SHA1 Message Date
Brad Smith
a50768616f Build: fix OpenBSD error in guardedalloc
Pull Request: https://projects.blender.org/blender/blender/pulls/108252
2023-05-25 15:21:48 +02:00
Brad Smith
322dab936f Build: fixes for OpenBSD
Pull Request: https://projects.blender.org/blender/blender/pulls/107666
2023-05-09 13:19:16 +02:00
Sergey Sharybin
a12a8a71bb Remove "All Rights Reserved" from Blender Foundation copyright code
The goal is to solve confusion of the "All rights reserved" for licensing
code under an open-source license.

The phrase "All rights reserved" comes from a historical convention that
required this phrase for the copyright protection to apply. This convention
is no longer relevant.

However, even though the phrase has no meaning in establishing the copyright
it has not lost meaning in terms of licensing.

This change makes it so code under the Blender Foundation copyright does
not use "all rights reserved". This is also how the GPL license itself
states how to apply it to the source code:

    <one line to give the program's name and a brief idea of what it does.>
    Copyright (C) <year>  <name of author>

    This program is free software ...

This change does not change copyright notice in cases when the copyright
is dual (BF and an author), or just an author of the code. It also does
mot change copyright which is inherited from NaN Holding BV as it needs
some further investigation about what is the proper way to handle it.
2023-03-30 10:51:59 +02: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
Campbell Barton
e39b358a2f Guarded allocator: print memory-in-use as a size_t, not uint
There is no reason to cast to a smaller value which could overflow.
2023-03-23 14:20:14 +11:00
Germano Cavalcante
f27d6b9640 MSVC: lower C4100 warning level from 4 to 3
The C4100 warning is related to unused formal parameters in functions.

Enabling it better aligns with "-Wunused-parameter" option in other
compilers.

While suppressing it with `__pragma(warning(suppress:4100))` is not the
same as using `__attribute__((__unused__))` in GCC or Clang, it is
still preferable to use it over completely hiding the warning.

This ensures consistent warning behavior across compilers and improves
code quality by addressing unused function parameters.

(Note that some warnings in Windows-specific code have already been
silenced in 7fcb262dfd2f48a73f9cf794944ff677c36e3783)

Pull Request: https://projects.blender.org/blender/blender/pulls/105534
2023-03-09 16:05:48 +01:00
Germano Cavalcante
a755e6e63f Revert "MSVC: lower C4100 warning level from 4 to 3"
This reverts commit db4e7616f3590c04484fe8a015b6a02b7a1ede44.

Caused many issues when compiling mantaflow.
2023-02-14 15:36:35 -03:00
Germano Cavalcante
db4e7616f3 MSVC: lower C4100 warning level from 4 to 3
This better aligns with OSX/Linux warnings.

Although `__pragma(warning(suppress:4100))` is not the same as
`__attribute__((__unused__))` in gcc (which only affects the attribute
instead of the line), it still seems to be better to use it than to
hide the warning entirely.
2023-02-14 14:38:18 -03: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
Campbell Barton
66dee44088 CMake: quiet references to undeclared variable warnings
These warnings can reveal errors in logic, so quiet them by checking
if the features are enabled before using variables or by assigning
empty strings in some cases.

- Check CMAKE_THREAD_LIBS_INIT is set before use as CMake docs
  note that this may be left unset if it's not needed.
- Remove BOOST/OPENVDB/VULKAN references when disable.
- Define INC_SYS even when empty.
- Remove PNG_INC from freetype (not defined anywhere).
2023-01-19 17:10:42 +11:00
Jacques Lucke
ae7163007d Allocator: improve protection against false sharing
On Apple Silicon, the cache line size is 128 bytes.
2023-01-06 13:04:49 +01:00
Campbell Barton
14fc02f91d Cleanup: spelling in comments 2023-01-06 14:00:36 +11:00
Jacques Lucke
862d08bb97 Fix: use-after-free when threadlocal is destructed after static variable
This issue was introduced in rB78f28b55d39288926634d0cc.

The fix is to use a `std::shared_ptr` to ensure that the `Global` will live
long enough until all `Local` objects are destructed.
2023-01-05 14:38:34 +01:00
Jacques Lucke
78f28b55d3 Allocator: improve multi-threaded allocation performance
Both, the guarded and lockfree allocator, are keeping track of current
and peak memory usage. Even the lockfree allocator used to use a
global atomic variable for the memory usage. When multiple threads
use the allocator at the same time, this variable is highly contended.
This can result in significant slowdowns as presented in D16862.

While specific cases could always be optimized by reducing the number
of allocations, having this synchronization point in functions used by
almost every part of Blender is not great.

The solution is use thread-local memory counters which are only added
together when the memory usage is actually requested. For more details
see in-code comments and D16862.

Differential Revision: https://developer.blender.org/D16862
2023-01-04 14:55:46 +01:00
Sergey Sharybin
09b9e1e95e Fix T102225: Crash opening menus
Seems to be introduced by 99e5024e97f.

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
889b3385b5 Cleanup: spelling in comments 2022-11-02 10:18:19 +11:00
Campbell Barton
b9e4d930b3 Cleanup: use 'u' prefixed unsigned types 2022-10-12 10:39:12 +11:00
Campbell Barton
3961d3493b Cleanup: use 'u' prefixed integer types for brevity in C code
This also simplifies using function style casts when moving to C++.
2022-09-26 11:33:22 +10:00
Campbell Barton
15f3cf7f8f Cleanup: replace C-style casts with functional casts for numeric types
Some changes missed from f68cfd6bb078482c4a779a6e26a56e2734edb5b8.
2022-09-26 09:53:49 +10:00
Campbell Barton
f68cfd6bb0 Cleanup: replace C-style casts with functional casts for numeric types 2022-09-25 20:17:08 +10:00
Brecht Van Lommel
0ffd288fab Build: fix gtest build flags affecting actual library
Switch to target_ functions to avoid this.
2022-09-18 11:26:34 +02: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
listout
f197b1a1f1 CMake: support building with musl libc
Instead of using macros like GLIBC we can use the CMake build
systems internal functions to check if some header or functions are
present on the running system's libc.

Add ./build_files/cmake/have_features.cmake to add checks for
platform features which can be used to set defines for source
files that require them.

Reviewed By: campbellbarton

Ref D15696
2022-08-18 08:12:56 +10: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
49babc7caa Cleanup: early exit MEM_lockfree_freeN when called with NULL pointer
Simplify logic for freeing a NULL pointer. While no null-pointer
de-reference was performed, this wasn't as so obvious as the pointer
was passed to MEM_lockfree_allocN_len before checking for NULL.

NOTE: T99744 claimed the a NULL pointer free was a vulnerability,
while I can't see evidence for this - exiting early makes it clearer
the memory isn't accessed.

*Details*

- Add MEMHEAD_LEN macro, avoids redundant NULL check.
- Use "UNLIKELY(..)" hint's for error cases
  (freeing NULL pointer and checking if `leak_detector_has_run`).
2022-07-16 17:23:42 +10:00
Campbell Barton
61a7e5be18 Cleanup: '*' prefix C-comment blocks 2022-06-01 15:38:48 +10:00
Campbell Barton
c93f3b4596 Cleanup: spelling in comments 2022-05-17 15:34:02 +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
fe55789b6f File headers: manually convert files to use SPDX headers
Also add BSD-2-Clause to SPDX license list.
2022-02-11 15:15:49 +11: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 aa363ec2ae9382c052f024284dcdb77ac495c177.

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
Aaron Carlisle
c1279768a7 Cleanup: Clang-Tidy modernize-redundant-void-arg 2021-12-08 00:31:20 -05:00
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
74f45ed9c5 Cleanup: spelling in comments 2021-10-03 12:13:29 +11:00
Campbell Barton
1ef275963d Cleanup: use C++ style comments for disabled code 2021-08-12 14:34:41 +10:00
Campbell Barton
828c66f393 Cleanup: spelling in comments 2021-07-26 12:32:42 +10:00
Jesse Yurkovich
dc679f6247 Cleanup: Use C99 format string for the SIZET_FORMAT macro
All platforms support the proper format string and it's already used in
various other places.

Differential Revision: https://developer.blender.org/D11460
2021-07-12 20:32:37 -07:00
Campbell Barton
f1e4903854 Cleanup: full sentences in comments, improve comment formatting 2021-06-26 21:50:48 +10: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
Jeroen Bakker
715c746296 Cleanup: clang-tidy errors. 2021-03-24 11:00:31 +01: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