From 199d1667ff1d0558f631fb21774bda9f8eace368 Mon Sep 17 00:00:00 2001 From: Kevin Bracey Date: Mon, 10 Sep 2018 11:00:08 +0300 Subject: [PATCH 1/2] Make SingletonPtr methods const 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. --- platform/SingletonPtr.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/platform/SingletonPtr.h b/platform/SingletonPtr.h index fd3112e590..57fc76b154 100644 --- a/platform/SingletonPtr.h +++ b/platform/SingletonPtr.h @@ -88,7 +88,7 @@ struct SingletonPtr { * @returns * A pointer to the singleton */ - T *get() + T *get() const { if (NULL == _ptr) { singleton_lock(); @@ -108,7 +108,7 @@ struct SingletonPtr { * @returns * A pointer to the singleton */ - T *operator->() + T *operator->() const { return get(); } @@ -118,15 +118,15 @@ struct SingletonPtr { * @returns * A reference to the singleton */ - T &operator*() + T &operator*() const { return *get(); } // This is zero initialized when in global scope - T *_ptr; + mutable T *_ptr; // Force data to be 4 byte aligned - uint32_t _data[(sizeof(T) + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; + mutable uint32_t _data[(sizeof(T) + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; }; #endif From e7815c64ad8878dad3bf4db425b59c3f466a6bc5 Mon Sep 17 00:00:00 2001 From: Kevin Bracey Date: Mon, 10 Sep 2018 11:10:41 +0300 Subject: [PATCH 2/2] Align SingletonPtr data to 8 bytes, or use C++11 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). --- platform/SingletonPtr.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/platform/SingletonPtr.h b/platform/SingletonPtr.h index 57fc76b154..595d9807ca 100644 --- a/platform/SingletonPtr.h +++ b/platform/SingletonPtr.h @@ -125,8 +125,13 @@ struct SingletonPtr { // This is zero initialized when in global scope mutable T *_ptr; - // Force data to be 4 byte aligned - mutable uint32_t _data[(sizeof(T) + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; +#if __cplusplus >= 201103L + // Align data appropriately + alignas(T) mutable char _data[sizeof(T)]; +#else + // Force data to be 8 byte aligned + mutable uint64_t _data[(sizeof(T) + sizeof(uint64_t) - 1) / sizeof(uint64_t)]; +#endif }; #endif