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