Skip to content
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

Use system allocator for containers on macOS (bypassing Unity's allocator) #2900

Merged
merged 5 commits into from Mar 7, 2021

Conversation

rnlahaye
Copy link
Contributor

@rnlahaye rnlahaye commented Mar 1, 2021

First part of a two-part fix for #2899.

This change causes STL containers (std::vector and the like) to use the native system allocator on macOS instead of the Unity-provided allocator.

It does this by defining an allocator which avoids using of new and delete, and then creating aliases to std::vector and friends in the principia::std namespace using the new allocator. The header file containing this change is included via compiler argument, so this change is almost entirely transparent to the rest of the codebase (I had to add full qualification in two places).

There are many intentional uses of the c99 designator initialization
syntax in the codebase.
This is necessary for the macOS alloc code to compile.
The change in error_analysis_test.cpp is necessary because a
non-Principia function is called which returns a ::std::vector.
I'm not sure why the friend std::pair in discrete_trajectory.cpp
needs full qualification.
Specifically, force include magic/malloc_allocator.hpp on macOS
builds via clang argument.
@pleroy
Copy link
Member

pleroy commented Mar 2, 2021

FWIW, this PR generally looks good. We'll do a more thorough review over the week-end. Expect nits, but nothing earth-shattering.

@@ -87,6 +87,7 @@ SHARED_ARGS := \
-Wall -Wpedantic \
-Wno-char-subscripts \
-Wno-gnu-anonymous-struct \
-Wno-c99-extensions \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just out of curiosity, which warning does this suppress?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

./astronomy/orbital_elements_body.hpp:165:10: warning: designated initializers
      are a C99 feature [-Wc99-extensions]
        {.t = time,
         ^~~~~~~~~
./astronomy/orbital_elements_body.hpp:166:10: warning: designated initializers
      are a C99 feature [-Wc99-extensions]
         .a = *osculating_elements.semimajor_axis,
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./astronomy/orbital_elements_body.hpp:167:10: warning: designated initializers
      are a C99 feature [-Wc99-extensions]
         .h = e * Sin(ϖ),
         ^~~~~~~~~~~~~~~

…and so on.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right, these are C++20 whereas the flags say 1z=17.

@@ -0,0 +1,110 @@
// This file contains two things:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Among other things, the magic directly is going to interact poorly with VS. I would organize things differently here:

  1. A file base/malloc_allocator.hpp that declares and defines the class MallocAllocator in namespace principia::base. This one is not magic in any way, and should be included in the usual manner.
  2. A file base/macos_allocator_replacement.hpp that has all the stuff declared in principia::std. This is the one that is magically included. For safety, it should have preprocessor directives that ensure that it's never pulled for other platforms:
#if !OS_MACOSX
#error Only include for macOS
#endif 
... all the rest ...

You'll need to include base/macros.hpp in that second file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

magic/malloc_allocator.hpp Outdated Show resolved Hide resolved
Split principia::std into a separate file.
Disabled principia::std for non-macOS builds.
@eggrobin eggrobin added the LGTM label Mar 7, 2021
@pleroy pleroy merged commit 134ba85 into mockingbirdnest:master Mar 7, 2021
@eggrobin eggrobin mentioned this pull request Mar 10, 2021
@pleroy pleroy added this to the Goldbach milestone Mar 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants