Skip to content

SingletonPtr: const and alignment #8354

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 2 commits into from
Oct 17, 2018
Merged

Conversation

kjbracey
Copy link
Contributor

@kjbracey kjbracey commented Oct 9, 2018

Description

  • Make SingletonPtr methods const
  • Strengthen alignment of embedded object

Follow-up from discussion in #8001

Pull request type

[ ] Fix
[X] Refactor
[ ] Target update
[ ] Functionality change
[ ] Breaking change

Make get() and operators * and -> of SingletonPtr const - they are
logically const and thread-safe, despite the construction on first call.
This construction is "invisible" to the caller of those methods.
mutable T *_ptr;
#if __cplusplus >= 201103L
// Align data appropriately
alignas(T) mutable char data[sizeof(T)];
Copy link
Member

Choose a reason for hiding this comment

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

Wrong name, should be _data.
Note that std::aligned_storage<Size, Alignement> may be used but it is actually more verbose and less readable than the current expression: mutable typename std::aligned_storage<sizeof(T), alignof(T)>::type _data; 🤦‍♂️

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good spot. Also, I'm aware that this may break anyway when a C++11 switch gets thrown. It fails on ARMC5 which sets __cplusplus high enough, but does not implement alignas

I considered std::aligned_storage, but afaict it may well not work as well - you're only passing it size, not the actual type, so it has presumably has to assume that any type >= 8 bytes might need 8-byte alignment. Whereas alignas(type) can check if there are any uint64_ts or whatever in there.

I do wonder if I'm missing something, because std::aligned_storage just seems pretty redundant when you have alignas. Maybe it came first.

Be more cautious about alignment - align the data within a SingletonPtr
to 8 bytes rather than 4. This could increase padding overhead by up
to 8 bytes, sadly, but we may need this alignment for correct operation.

Conditional check added for C++11 - if in use we can get correct minimal
alignment by using alignas(T).
@cmonr
Copy link
Contributor

cmonr commented Oct 16, 2018

@pan- Was that the only withholding you had?

Copy link
Member

@pan- pan- left a comment

Choose a reason for hiding this comment

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

@cmonr Yes as it was preventing compilation in C++11 mode.
Approved.

@cmonr
Copy link
Contributor

cmonr commented Oct 16, 2018

Waiting on a review by @ARMmbed/mbed-os-core

@cmonr
Copy link
Contributor

cmonr commented Oct 17, 2018

/morph build

@mbed-ci
Copy link

mbed-ci commented Oct 17, 2018

Build : SUCCESS

Build number : 3375
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/8354/

Triggering tests

/morph test
/morph export-build
/morph mbed2-build

@mbed-ci
Copy link

mbed-ci commented Oct 17, 2018

@mbed-ci
Copy link

mbed-ci commented Oct 17, 2018

@cmonr cmonr merged commit 7085d16 into ARMmbed:master Oct 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants