//------------------------------------------------------------------------------ /* This file is part of Beast: https://github.com/vinniefalco/Beast Copyright 2013, Vinnie Falco Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ //============================================================================== #ifndef BEAST_CACHELINE_BEASTHEADER #define BEAST_CACHELINE_BEASTHEADER #include "beast_MemoryAlignment.h" // Allows turning off of all padding, // e.g. for memory-constrained systems or testing. // #define GLOBAL_PADDING_ENABLED 0 namespace CacheLine { #if GLOBAL_PADDING_ENABLED // Pads an object so that it starts on a cache line boundary. // template class Aligned { public: ~Aligned () { ptr ()->~T (); } Aligned () { new (ptr ()) T; } template explicit Aligned (const T1& t1) { new (ptr ()) T (t1); } template Aligned (const T1& t1, const T2& t2) { new (ptr ()) T (t1, t2); } template Aligned (const T1& t1, const T2& t2, const T3& t3) { new (ptr ()) T (t1, t2, t3); } template Aligned (const T1& t1, const T2& t2, const T3& t3, const T4& t4) { new (ptr ()) T (t1, t2, t3, t4); } template Aligned (const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5) { new (ptr ()) T (t1, t2, t3, t4, t5); } template Aligned (const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6) { new (ptr ()) T (t1, t2, t3, t4, t5, t6); } template < class T1, class T2, class T3, class T4, class T5, class T6, class T7 > Aligned (const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7) { new (ptr ()) T (t1, t2, t3, t4, t5, t6, t7); } template < class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8 > Aligned (const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7, const T8& t8) { new (ptr ()) T (t1, t2, t3, t4, t5, t6, t7, t8); } void operator= (T const& other) { *ptr () = other; } inline T& operator* () noexcept { return *ptr (); } inline T* operator-> () noexcept { return ptr (); } inline operator T& () noexcept { return *ptr (); } inline operator T* () noexcept { return ptr (); } inline const T& operator* () const noexcept { return *ptr (); } inline const T* operator-> () const noexcept { return ptr (); } inline operator const T& () const noexcept { return *ptr (); } inline operator const T* () const noexcept { return ptr (); } private: inline T* ptr () noexcept { return (T*) ((uintptr_t (m_storage) + Memory::cacheLineAlignMask) & ~Memory::cacheLineAlignMask); /* return reinterpret_cast (Memory::pointerAdjustedForAlignment ( m_storage, Memory::cacheLineBytes)); */ } char m_storage [ (sizeof (T) + Memory::cacheLineAlignMask) & ~Memory::cacheLineAlignMask]; }; // Holds an object padded it to completely fill a CPU cache line. // The caller must ensure that this object starts at the beginning // of a cache line. // template class Padded { public: Padded () { } template explicit Padded (const T1& t1) : m_t (t1) { } template Padded (const T1& t1, const T2& t2) : m_t (t1, t2) { } template Padded (const T1& t1, const T2& t2, const T3& t3) : m_t (t1, t2, t3) { } template Padded (const T1& t1, const T2& t2, const T3& t3, const T4& t4) : m_t (t1, t2, t3, t4) { } template Padded (const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5) : m_t (t1, t2, t3, t4, t5) { } template Padded (const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6) : m_t (t1, t2, t3, t4, t5, t6) { } template < class T1, class T2, class T3, class T4, class T5, class T6, class T7 > Padded (const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7) : m_t (t1, t2, t3, t4, t5, t6, t7) { } template < class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8 > Padded (const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7, const T8& t8) : m_t (t1, t2, t3, t4, t5, t6, t7, t8) { } void operator= (const T& other) { m_t = other; } T& operator* () noexcept { return m_t; } T* operator-> () noexcept { return &m_t; } operator T& () noexcept { return m_t; } operator T* () noexcept { return &m_t; } const T& operator* () const noexcept { return m_t; } const T* operator-> () const noexcept { return &m_t; } operator const T& () const noexcept { return m_t; } operator const T* () const noexcept { return &m_t; } private: T m_t; char pad [Memory::cacheLineAlignBytes - sizeof (T)]; }; #else template class Aligned { public: Aligned () { } template explicit Aligned (const T1& t1) : m_t (t1) { } template Aligned (const T1& t1, const T2& t2) : m_t (t1, t2) { } template Aligned (const T1& t1, const T2& t2, const T3& t3) : m_t (t1, t2, t3) { } template Aligned (const T1& t1, const T2& t2, const T3& t3, const T4& t4) : m_t (t1, t2, t3, t4) { } template Aligned (const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5) : m_t (t1, t2, t3, t4, t5) { } template Aligned (const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6) : m_t (t1, t2, t3, t4, t5, t6) { } template < class T1, class T2, class T3, class T4, class T5, class T6, class T7 > Aligned (const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7) : m_t (t1, t2, t3, t4, t5, t6, t7) { } template < class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8 > Aligned (const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7, const T8& t8) : m_t (t1, t2, t3, t4, t5, t6, t7, t8) { } void operator= (const T& other) { m_t = other; } T& operator* () noexcept { return m_t; } T* operator-> () noexcept { return &m_t; } operator T& () noexcept { return m_t; } operator T* () noexcept { return &m_t; } const T& operator* () const noexcept { return m_t; } const T* operator-> () const noexcept { return &m_t; } operator const T& () const noexcept { return m_t; } operator const T* () const noexcept { return &m_t; } private: T m_t; }; template class Padded { public: Padded () { } template explicit Padded (const T1& t1) : m_t (t1) { } template Padded (const T1& t1, const T2& t2) : m_t (t1, t2) { } template Padded (const T1& t1, const T2& t2, const T3& t3) : m_t (t1, t2, t3) { } template Padded (const T1& t1, const T2& t2, const T3& t3, const T4& t4) : m_t (t1, t2, t3, t4) { } template Padded (const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5) : m_t (t1, t2, t3, t4, t5) { } template Padded (const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6) : m_t (t1, t2, t3, t4, t5, t6) { } template < class T1, class T2, class T3, class T4, class T5, class T6, class T7 > Padded (const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7) : m_t (t1, t2, t3, t4, t5, t6, t7) { } template < class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8 > Padded (const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7, const T8& t8) : m_t (t1, t2, t3, t4, t5, t6, t7, t8) { } void operator= (const T& other) { m_t = other; } T& operator* () noexcept { return m_t; } T* operator-> () noexcept { return &m_t; } operator T& () noexcept { return m_t; } operator T* () noexcept { return &m_t; } const T& operator* () const noexcept { return m_t; } const T* operator-> () const noexcept { return &m_t; } operator const T& () const noexcept { return m_t; } operator const T* () const noexcept { return &m_t; } private: T m_t; }; #endif // // Used to remove padding without changing code // template class Unpadded { public: Unpadded () { } template explicit Unpadded (const T1& t1) : m_t (t1) { } template Unpadded (const T1& t1, const T2& t2) : m_t (t1, t2) { } template Unpadded (const T1& t1, const T2& t2, const T3& t3) : m_t (t1, t2, t3) { } template Unpadded (const T1& t1, const T2& t2, const T3& t3, const T4& t4) : m_t (t1, t2, t3, t4) { } template Unpadded (const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5) : m_t (t1, t2, t3, t4, t5) { } template Unpadded (const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6) : m_t (t1, t2, t3, t4, t5, t6) { } template < class T1, class T2, class T3, class T4, class T5, class T6, class T7 > Unpadded (const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7) : m_t (t1, t2, t3, t4, t5, t6, t7) { } template < class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8 > Unpadded (const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7, const T8& t8) : m_t (t1, t2, t3, t4, t5, t6, t7, t8) { } void operator= (const T& other) { m_t = other; } T& operator* () { return m_t; } T* operator-> () { return &m_t; } operator T& () { return m_t; } operator T* () { return &m_t; } const T& operator* () const { return m_t; } const T* operator-> () const { return &m_t; } operator const T& () const { return m_t; } operator const T* () const { return &m_t; } private: T m_t; }; } #endif