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

Revert "Use sizeof(char) in place for sizeof(void) (#515)" #516

Merged
merged 1 commit into from
May 10, 2021

Conversation

hidmic
Copy link
Contributor

@hidmic hidmic commented May 5, 2021

This reverts commit c3769dd.

I was wrong. void allocators shouldn't behave like char allocators. See ros2/rclcpp#1657.

This reverts commit c3769dd.

Signed-off-by: Michel Hidalgo <michel@ekumenlabs.com>
@hidmic
Copy link
Contributor Author

hidmic commented May 6, 2021

@ros-pull-request-builder retest this please

@hidmic
Copy link
Contributor Author

hidmic commented May 7, 2021

CI up to demo_nodes_cpp:

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

Copy link
Contributor

@clalancette clalancette left a comment

Choose a reason for hiding this comment

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

I may not be qualified to review this, but I'm trying to reconcile a couple of different statements made around this area:

  • Do not attempt to use void allocators for memory allocation. rclcpp#1657 (comment) says "it doesn't cover custom void allocators (which shouldn't have allocate nor deallocate methods to begin with)."
  • https://en.cppreference.com/w/cpp/memory/allocator says "The explicit specialization for void lacks the member typedefs reference, const_reference, size_type and difference_type. This specialization declares no member functions. " (which agrees with the above statement)
  • This file (allocator_tutorial.cpp) declares struct MyAllocator, which by default has typename T = void, and also has implementations of allocate and deallocate. Doesn't that directly contradict the above two statements?

@hidmic
Copy link
Contributor Author

hidmic commented May 10, 2021

This file (allocator_tutorial.cpp) declares struct MyAllocator, which by default has typename T = void, and also has implementations of allocate and deallocate. Doesn't that directly contradict the above two statements?

Exactly. It shouldn't have them. Alas, many Allocator implementations (the one in allocator_tutorial.cpp, the one in rclcpp tests, tlsf_cpp's, libcxx's) do not go through the hassle of partial specialization when value_type is void. Instead, they roll with it knowing that (a) no downstream code should attempt to allocate or deallocate using a void allocator, and (b) sizeof(void) is ill-formed anyways and it won't compile (unless you're using gcc with GNU extensions, which do allow it). You can still use such void allocators so long as allocate never gets addressed (because a C++ compiler will not compile its definition if it knows it's not being used i.e. it will partially instantiate the template).

@clalancette
Copy link
Contributor

OK, thanks, that makes some sense. Just be totally sure I understand, then, the problem here is not really that MyAllocator has an allocate and deallocate method, its that it should never be allowed to be instantiated with a void type. We pretty much know that doesn't happen, otherwise it wouldn't compile on clang/MSVC. So is the right thing to do here to totally remove the default template type as void? Wouldn't that still work, but get rid of the false impression that this works for void?

@hidmic
Copy link
Contributor Author

hidmic commented May 10, 2021

Just be totally sure I understand, then, the problem here is not really that MyAllocator has an allocate and deallocate method, its that it should never be allowed to be instantiated with a void type. We pretty much know that doesn't happen, otherwise it wouldn't compile on clang/MSVC.

It is being instantiated with a void type, but only to pass around type erased allocators (which user code rebinds), not to allocate memory.

@hidmic
Copy link
Contributor Author

hidmic commented May 10, 2021

Alright, thanks for the reviews ! Going in.

@hidmic hidmic merged commit 1c0d891 into master May 10, 2021
@delete-merged-branch delete-merged-branch bot deleted the hidmic/revert-515 branch May 10, 2021 18:26
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.

None yet

3 participants