aoqi@0: /* aoqi@0: * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_MEMORY_PADDED_HPP aoqi@0: #define SHARE_VM_MEMORY_PADDED_HPP aoqi@0: aoqi@0: #include "memory/allocation.hpp" aoqi@0: #include "utilities/globalDefinitions.hpp" aoqi@0: aoqi@0: // Bytes needed to pad type to avoid cache-line sharing; alignment should be the aoqi@0: // expected cache line size (a power of two). The first addend avoids sharing aoqi@0: // when the start address is not a multiple of alignment; the second maintains aoqi@0: // alignment of starting addresses that happen to be a multiple. aoqi@0: #define PADDING_SIZE(type, alignment) \ aoqi@0: ((alignment) + align_size_up_(sizeof(type), alignment)) aoqi@0: aoqi@0: // Templates to create a subclass padded to avoid cache line sharing. These are aoqi@0: // effective only when applied to derived-most (leaf) classes. aoqi@0: aoqi@0: // When no args are passed to the base ctor. aoqi@0: template aoqi@0: class Padded : public T { aoqi@0: private: aoqi@0: char _pad_buf_[PADDING_SIZE(T, alignment)]; aoqi@0: }; aoqi@0: aoqi@0: // When either 0 or 1 args may be passed to the base ctor. aoqi@0: template aoqi@0: class Padded01 : public T { aoqi@0: public: aoqi@0: Padded01(): T() { } aoqi@0: Padded01(Arg1T arg1): T(arg1) { } aoqi@0: private: aoqi@0: char _pad_buf_[PADDING_SIZE(T, alignment)]; aoqi@0: }; aoqi@0: aoqi@0: // Super class of PaddedEnd when pad_size != 0. aoqi@0: template aoqi@0: class PaddedEndImpl : public T { aoqi@0: private: aoqi@0: char _pad_buf[pad_size]; aoqi@0: }; aoqi@0: aoqi@0: // Super class of PaddedEnd when pad_size == 0. aoqi@0: template aoqi@0: class PaddedEndImpl : public T { aoqi@0: // No padding. aoqi@0: }; aoqi@0: aoqi@0: #define PADDED_END_SIZE(type, alignment) (align_size_up_(sizeof(type), alignment) - sizeof(type)) aoqi@0: aoqi@0: // More memory conservative implementation of Padded. The subclass adds the aoqi@0: // minimal amount of padding needed to make the size of the objects be aligned. aoqi@0: // This will help reducing false sharing, aoqi@0: // if the start address is a multiple of alignment. aoqi@0: template aoqi@0: class PaddedEnd : public PaddedEndImpl { aoqi@0: // C++ don't allow zero-length arrays. The padding is put in a aoqi@0: // super class that is specialized for the pad_size == 0 case. aoqi@0: }; aoqi@0: aoqi@0: // Helper class to create an array of PaddedEnd objects. All elements will aoqi@0: // start at a multiple of alignment and the size will be aligned to alignment. aoqi@0: template aoqi@0: class PaddedArray { aoqi@0: public: aoqi@0: // Creates an aligned padded array. aoqi@0: // The memory can't be deleted since the raw memory chunk is not returned. aoqi@0: static PaddedEnd* create_unfreeable(uint length); aoqi@0: }; aoqi@0: aoqi@0: // Helper class to create an array of references to arrays of primitive types aoqi@0: // Both the array of references and the data arrays are aligned to the given aoqi@0: // alignment. The allocated memory is zero-filled. aoqi@0: template aoqi@0: class Padded2DArray { aoqi@0: public: aoqi@0: // Creates an aligned padded 2D array. aoqi@0: // The memory cannot be deleted since the raw memory chunk is not returned. aoqi@0: static T** create_unfreeable(uint rows, uint columns, size_t* allocation_size = NULL); aoqi@0: }; aoqi@0: aoqi@0: // Helper class to create an array of T objects. The array as a whole will aoqi@0: // start at a multiple of alignment and its size will be aligned to alignment. aoqi@0: template aoqi@0: class PaddedPrimitiveArray { aoqi@0: public: aoqi@0: static T* create_unfreeable(size_t length); aoqi@0: }; aoqi@0: aoqi@0: #endif // SHARE_VM_MEMORY_PADDED_HPP