src/share/vm/memory/padded.hpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/memory/padded.hpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,112 @@
     1.4 +/*
     1.5 + * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#ifndef SHARE_VM_MEMORY_PADDED_HPP
    1.29 +#define SHARE_VM_MEMORY_PADDED_HPP
    1.30 +
    1.31 +#include "memory/allocation.hpp"
    1.32 +#include "utilities/globalDefinitions.hpp"
    1.33 +
    1.34 +// Bytes needed to pad type to avoid cache-line sharing; alignment should be the
    1.35 +// expected cache line size (a power of two).  The first addend avoids sharing
    1.36 +// when the start address is not a multiple of alignment; the second maintains
    1.37 +// alignment of starting addresses that happen to be a multiple.
    1.38 +#define PADDING_SIZE(type, alignment)                           \
    1.39 +  ((alignment) + align_size_up_(sizeof(type), alignment))
    1.40 +
    1.41 +// Templates to create a subclass padded to avoid cache line sharing.  These are
    1.42 +// effective only when applied to derived-most (leaf) classes.
    1.43 +
    1.44 +// When no args are passed to the base ctor.
    1.45 +template <class T, size_t alignment = DEFAULT_CACHE_LINE_SIZE>
    1.46 +class Padded : public T {
    1.47 + private:
    1.48 +  char _pad_buf_[PADDING_SIZE(T, alignment)];
    1.49 +};
    1.50 +
    1.51 +// When either 0 or 1 args may be passed to the base ctor.
    1.52 +template <class T, typename Arg1T, size_t alignment = DEFAULT_CACHE_LINE_SIZE>
    1.53 +class Padded01 : public T {
    1.54 + public:
    1.55 +  Padded01(): T() { }
    1.56 +  Padded01(Arg1T arg1): T(arg1) { }
    1.57 + private:
    1.58 +  char _pad_buf_[PADDING_SIZE(T, alignment)];
    1.59 +};
    1.60 +
    1.61 +// Super class of PaddedEnd when pad_size != 0.
    1.62 +template <class T, size_t pad_size>
    1.63 +class PaddedEndImpl : public T {
    1.64 + private:
    1.65 +  char _pad_buf[pad_size];
    1.66 +};
    1.67 +
    1.68 +// Super class of PaddedEnd when pad_size == 0.
    1.69 +template <class T>
    1.70 +class PaddedEndImpl<T, /*pad_size*/ 0> : public T {
    1.71 +  // No padding.
    1.72 +};
    1.73 +
    1.74 +#define PADDED_END_SIZE(type, alignment) (align_size_up_(sizeof(type), alignment) - sizeof(type))
    1.75 +
    1.76 +// More memory conservative implementation of Padded. The subclass adds the
    1.77 +// minimal amount of padding needed to make the size of the objects be aligned.
    1.78 +// This will help reducing false sharing,
    1.79 +// if the start address is a multiple of alignment.
    1.80 +template <class T, size_t alignment = DEFAULT_CACHE_LINE_SIZE>
    1.81 +class PaddedEnd : public PaddedEndImpl<T, PADDED_END_SIZE(T, alignment)> {
    1.82 +  // C++ don't allow zero-length arrays. The padding is put in a
    1.83 +  // super class that is specialized for the pad_size == 0 case.
    1.84 +};
    1.85 +
    1.86 +// Helper class to create an array of PaddedEnd<T> objects. All elements will
    1.87 +// start at a multiple of alignment and the size will be aligned to alignment.
    1.88 +template <class T, MEMFLAGS flags, size_t alignment = DEFAULT_CACHE_LINE_SIZE>
    1.89 +class PaddedArray {
    1.90 + public:
    1.91 +  // Creates an aligned padded array.
    1.92 +  // The memory can't be deleted since the raw memory chunk is not returned.
    1.93 +  static PaddedEnd<T>* create_unfreeable(uint length);
    1.94 +};
    1.95 +
    1.96 +// Helper class to create an array of references to arrays of primitive types
    1.97 +// Both the array of references and the data arrays are aligned to the given
    1.98 +// alignment. The allocated memory is zero-filled.
    1.99 +template <class T, MEMFLAGS flags, size_t alignment = DEFAULT_CACHE_LINE_SIZE>
   1.100 +class Padded2DArray {
   1.101 + public:
   1.102 +  // Creates an aligned padded 2D array.
   1.103 +  // The memory cannot be deleted since the raw memory chunk is not returned.
   1.104 +  static T** create_unfreeable(uint rows, uint columns, size_t* allocation_size = NULL);
   1.105 +};
   1.106 +
   1.107 +// Helper class to create an array of T objects. The array as a whole will
   1.108 +// start at a multiple of alignment and its size will be aligned to alignment.
   1.109 +template <class T, MEMFLAGS flags, size_t alignment = DEFAULT_CACHE_LINE_SIZE>
   1.110 +class PaddedPrimitiveArray {
   1.111 + public:
   1.112 +  static T* create_unfreeable(size_t length);
   1.113 +};
   1.114 +
   1.115 +#endif // SHARE_VM_MEMORY_PADDED_HPP

mercurial