Skip to content

Rework meshopt_Allocator callback storage #896

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 17, 2025
Merged

Rework meshopt_Allocator callback storage #896

merged 3 commits into from
Jun 17, 2025

Conversation

zeux
Copy link
Owner

@zeux zeux commented Jun 16, 2025

Using templated class static allowed us to avoid function calls even in
unoptimized builds, but results in conflicts with Clang's C++20 module
implementation when meshoptimizer.h is included in global module
fragment. Instead, we now use a function static to store this state and
require a function call to get access to it.

Additionally, while allocator callbacks were deduplicated by the linker
within an individual module, when meshoptimizer was compiled as a
shared library the library code and the calling code would get
separate configuration, and allocator overrides would not affect temporary
allocations done by meshopt_IndexAdapter. To fix that, this PR introduces
a separate configuration option, MESHOPTIMIZER_ALLOC_EXPORT,
which can be used to declare the allocator storage function in the library
once.

Fixes #895.

This contribution is sponsored by Valve.

zeux added 3 commits June 16, 2025 12:40
Using templated class static allowed us to avoid function calls even in
unoptimized builds, but results in conflicts with Clang's C++20 module
implementation when meshoptimizer.h is included in global module
fragment.

Instead, we now use a function static to store this state and require a
function call to get access to it. The function static should, similarly
to the previous code, be instantiated in translation units that use it
and deduplicated by linker.
Before this change (and irrespectively of the previous change to
callback storage), allocator callbacks were deduplicated by the linker
within an individual module, but when meshoptimizer was compiled as a
shared library, the library code and the calling code would get
separate configuration.

This meant that meshopt_setAllocator would only affect the allocation
behavior of the library internals, but would not affect the temporary
allocations done by meshopt_IndexAdapter. To fix that, we need to make
sure the storage function can be implemented in allocator.cpp and
exported.
This ensures that the allocator overrides work throughout the library
usage both for inlined APIs via IndexAdapter and for internal library
allocations, when shared library builds are used.
@zeux zeux merged commit 3beccf6 into master Jun 17, 2025
13 checks passed
@zeux zeux deleted the allocator branch June 17, 2025 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can't use <meshoptimizer.h> header in C++ global module fragment
1 participant