src/share/vm/memory/padded.hpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 6876
710a3c8b516e
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_MEMORY_PADDED_HPP
aoqi@0 26 #define SHARE_VM_MEMORY_PADDED_HPP
aoqi@0 27
aoqi@0 28 #include "memory/allocation.hpp"
aoqi@0 29 #include "utilities/globalDefinitions.hpp"
aoqi@0 30
aoqi@0 31 // Bytes needed to pad type to avoid cache-line sharing; alignment should be the
aoqi@0 32 // expected cache line size (a power of two). The first addend avoids sharing
aoqi@0 33 // when the start address is not a multiple of alignment; the second maintains
aoqi@0 34 // alignment of starting addresses that happen to be a multiple.
aoqi@0 35 #define PADDING_SIZE(type, alignment) \
aoqi@0 36 ((alignment) + align_size_up_(sizeof(type), alignment))
aoqi@0 37
aoqi@0 38 // Templates to create a subclass padded to avoid cache line sharing. These are
aoqi@0 39 // effective only when applied to derived-most (leaf) classes.
aoqi@0 40
aoqi@0 41 // When no args are passed to the base ctor.
aoqi@0 42 template <class T, size_t alignment = DEFAULT_CACHE_LINE_SIZE>
aoqi@0 43 class Padded : public T {
aoqi@0 44 private:
aoqi@0 45 char _pad_buf_[PADDING_SIZE(T, alignment)];
aoqi@0 46 };
aoqi@0 47
aoqi@0 48 // When either 0 or 1 args may be passed to the base ctor.
aoqi@0 49 template <class T, typename Arg1T, size_t alignment = DEFAULT_CACHE_LINE_SIZE>
aoqi@0 50 class Padded01 : public T {
aoqi@0 51 public:
aoqi@0 52 Padded01(): T() { }
aoqi@0 53 Padded01(Arg1T arg1): T(arg1) { }
aoqi@0 54 private:
aoqi@0 55 char _pad_buf_[PADDING_SIZE(T, alignment)];
aoqi@0 56 };
aoqi@0 57
aoqi@0 58 // Super class of PaddedEnd when pad_size != 0.
aoqi@0 59 template <class T, size_t pad_size>
aoqi@0 60 class PaddedEndImpl : public T {
aoqi@0 61 private:
aoqi@0 62 char _pad_buf[pad_size];
aoqi@0 63 };
aoqi@0 64
aoqi@0 65 // Super class of PaddedEnd when pad_size == 0.
aoqi@0 66 template <class T>
aoqi@0 67 class PaddedEndImpl<T, /*pad_size*/ 0> : public T {
aoqi@0 68 // No padding.
aoqi@0 69 };
aoqi@0 70
aoqi@0 71 #define PADDED_END_SIZE(type, alignment) (align_size_up_(sizeof(type), alignment) - sizeof(type))
aoqi@0 72
aoqi@0 73 // More memory conservative implementation of Padded. The subclass adds the
aoqi@0 74 // minimal amount of padding needed to make the size of the objects be aligned.
aoqi@0 75 // This will help reducing false sharing,
aoqi@0 76 // if the start address is a multiple of alignment.
aoqi@0 77 template <class T, size_t alignment = DEFAULT_CACHE_LINE_SIZE>
aoqi@0 78 class PaddedEnd : public PaddedEndImpl<T, PADDED_END_SIZE(T, alignment)> {
aoqi@0 79 // C++ don't allow zero-length arrays. The padding is put in a
aoqi@0 80 // super class that is specialized for the pad_size == 0 case.
aoqi@0 81 };
aoqi@0 82
aoqi@0 83 // Helper class to create an array of PaddedEnd<T> objects. All elements will
aoqi@0 84 // start at a multiple of alignment and the size will be aligned to alignment.
aoqi@0 85 template <class T, MEMFLAGS flags, size_t alignment = DEFAULT_CACHE_LINE_SIZE>
aoqi@0 86 class PaddedArray {
aoqi@0 87 public:
aoqi@0 88 // Creates an aligned padded array.
aoqi@0 89 // The memory can't be deleted since the raw memory chunk is not returned.
aoqi@0 90 static PaddedEnd<T>* create_unfreeable(uint length);
aoqi@0 91 };
aoqi@0 92
aoqi@0 93 // Helper class to create an array of references to arrays of primitive types
aoqi@0 94 // Both the array of references and the data arrays are aligned to the given
aoqi@0 95 // alignment. The allocated memory is zero-filled.
aoqi@0 96 template <class T, MEMFLAGS flags, size_t alignment = DEFAULT_CACHE_LINE_SIZE>
aoqi@0 97 class Padded2DArray {
aoqi@0 98 public:
aoqi@0 99 // Creates an aligned padded 2D array.
aoqi@0 100 // The memory cannot be deleted since the raw memory chunk is not returned.
aoqi@0 101 static T** create_unfreeable(uint rows, uint columns, size_t* allocation_size = NULL);
aoqi@0 102 };
aoqi@0 103
aoqi@0 104 // Helper class to create an array of T objects. The array as a whole will
aoqi@0 105 // start at a multiple of alignment and its size will be aligned to alignment.
aoqi@0 106 template <class T, MEMFLAGS flags, size_t alignment = DEFAULT_CACHE_LINE_SIZE>
aoqi@0 107 class PaddedPrimitiveArray {
aoqi@0 108 public:
aoqi@0 109 static T* create_unfreeable(size_t length);
aoqi@0 110 };
aoqi@0 111
aoqi@0 112 #endif // SHARE_VM_MEMORY_PADDED_HPP

mercurial