< prev index next >

src/share/vm/memory/padded.hpp

Print this page




  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_MEMORY_PADDED_HPP
  26 #define SHARE_VM_MEMORY_PADDED_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "utilities/globalDefinitions.hpp"
  30 
  31 // Bytes needed to pad type to avoid cache-line sharing; alignment should be the
  32 // expected cache line size (a power of two).  The first addend avoids sharing
  33 // when the start address is not a multiple of alignment; the second maintains
  34 // alignment of starting addresses that happen to be a multiple.
  35 #define PADDING_SIZE(type, alignment)                           \
  36   ((alignment) + align_size_up_(sizeof(type), alignment))
  37 
  38 // Templates to create a subclass padded to avoid cache line sharing.  These are
  39 // effective only when applied to derived-most (leaf) classes.
  40 
  41 // When no args are passed to the base ctor.
  42 template <class T, size_t alignment = DEFAULT_CACHE_LINE_SIZE>
  43 class Padded : public T {
  44  private:
  45   char _pad_buf_[PADDING_SIZE(T, alignment)];
  46 };
  47 
  48 // When either 0 or 1 args may be passed to the base ctor.
  49 template <class T, typename Arg1T, size_t alignment = DEFAULT_CACHE_LINE_SIZE>
  50 class Padded01 : public T {
  51  public:
  52   Padded01(): T() { }
  53   Padded01(Arg1T arg1): T(arg1) { }
  54  private:
  55   char _pad_buf_[PADDING_SIZE(T, alignment)];
  56 };
  57 
  58 // Super class of PaddedEnd when pad_size != 0.
  59 template <class T, size_t pad_size>
  60 class PaddedEndImpl : public T {
  61  private:
  62   char _pad_buf[pad_size];
  63 };
  64 
  65 // Super class of PaddedEnd when pad_size == 0.
  66 template <class T>
  67 class PaddedEndImpl<T, /*pad_size*/ 0> : public T {
  68   // No padding.
  69 };
  70 
  71 #define PADDED_END_SIZE(type, alignment) (align_size_up_(sizeof(type), alignment) - sizeof(type))
  72 
  73 // More memory conservative implementation of Padded. The subclass adds the
  74 // minimal amount of padding needed to make the size of the objects be aligned.
  75 // This will help reducing false sharing,
  76 // if the start address is a multiple of alignment.
  77 template <class T, size_t alignment = DEFAULT_CACHE_LINE_SIZE>
  78 class PaddedEnd : public PaddedEndImpl<T, PADDED_END_SIZE(T, alignment)> {
  79   // C++ doesn't allow zero-length arrays. The padding is put in a
  80   // super class that is specialized for the pad_size == 0 case.
  81 };
  82 
  83 // Similar to PaddedEnd, this macro defines a _pad_buf#id field
  84 // that is (alignment - size) bytes in size. This macro is used
  85 // to add padding in between non-class fields in a class or struct.
  86 #define DEFINE_PAD_MINUS_SIZE(id, alignment, size) \
  87           char _pad_buf##id[(alignment) - (size)]
  88 
  89 // Helper class to create an array of PaddedEnd<T> objects. All elements will
  90 // start at a multiple of alignment and the size will be aligned to alignment.
  91 template <class T, MEMFLAGS flags, size_t alignment = DEFAULT_CACHE_LINE_SIZE>




  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_MEMORY_PADDED_HPP
  26 #define SHARE_VM_MEMORY_PADDED_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "utilities/globalDefinitions.hpp"
  30 
  31 // Bytes needed to pad type to avoid cache-line sharing; alignment should be the
  32 // expected cache line size (a power of two).  The first addend avoids sharing
  33 // when the start address is not a multiple of alignment; the second maintains
  34 // alignment of starting addresses that happen to be a multiple.
  35 #define PADDING_SIZE(type, alignment)                           \
  36   ((alignment) + align_up_(sizeof(type), (alignment)))
  37 
  38 // Templates to create a subclass padded to avoid cache line sharing.  These are
  39 // effective only when applied to derived-most (leaf) classes.
  40 
  41 // When no args are passed to the base ctor.
  42 template <class T, size_t alignment = DEFAULT_CACHE_LINE_SIZE>
  43 class Padded : public T {
  44  private:
  45   char _pad_buf_[PADDING_SIZE(T, alignment)];
  46 };
  47 
  48 // When either 0 or 1 args may be passed to the base ctor.
  49 template <class T, typename Arg1T, size_t alignment = DEFAULT_CACHE_LINE_SIZE>
  50 class Padded01 : public T {
  51  public:
  52   Padded01(): T() { }
  53   Padded01(Arg1T arg1): T(arg1) { }
  54  private:
  55   char _pad_buf_[PADDING_SIZE(T, alignment)];
  56 };
  57 
  58 // Super class of PaddedEnd when pad_size != 0.
  59 template <class T, size_t pad_size>
  60 class PaddedEndImpl : public T {
  61  private:
  62   char _pad_buf[pad_size];
  63 };
  64 
  65 // Super class of PaddedEnd when pad_size == 0.
  66 template <class T>
  67 class PaddedEndImpl<T, /*pad_size*/ 0> : public T {
  68   // No padding.
  69 };
  70 
  71 #define PADDED_END_SIZE(type, alignment) (align_up_(sizeof(type), (alignment)) - sizeof(type))
  72 
  73 // More memory conservative implementation of Padded. The subclass adds the
  74 // minimal amount of padding needed to make the size of the objects be aligned.
  75 // This will help reducing false sharing,
  76 // if the start address is a multiple of alignment.
  77 template <class T, size_t alignment = DEFAULT_CACHE_LINE_SIZE>
  78 class PaddedEnd : public PaddedEndImpl<T, PADDED_END_SIZE(T, alignment)> {
  79   // C++ doesn't allow zero-length arrays. The padding is put in a
  80   // super class that is specialized for the pad_size == 0 case.
  81 };
  82 
  83 // Similar to PaddedEnd, this macro defines a _pad_buf#id field
  84 // that is (alignment - size) bytes in size. This macro is used
  85 // to add padding in between non-class fields in a class or struct.
  86 #define DEFINE_PAD_MINUS_SIZE(id, alignment, size) \
  87           char _pad_buf##id[(alignment) - (size)]
  88 
  89 // Helper class to create an array of PaddedEnd<T> objects. All elements will
  90 // start at a multiple of alignment and the size will be aligned to alignment.
  91 template <class T, MEMFLAGS flags, size_t alignment = DEFAULT_CACHE_LINE_SIZE>


< prev index next >