Path: blob/master/src/hotspot/share/memory/padded.hpp
40949 views
/*1* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_MEMORY_PADDED_HPP25#define SHARE_MEMORY_PADDED_HPP2627#include "memory/allocation.hpp"28#include "utilities/align.hpp"29#include "utilities/globalDefinitions.hpp"3031// Bytes needed to pad type to avoid cache-line sharing; alignment should be the32// expected cache line size (a power of two). The first addend avoids sharing33// when the start address is not a multiple of alignment; the second maintains34// alignment of starting addresses that happen to be a multiple.35#define PADDING_SIZE(type, alignment) \36((alignment) + align_up(sizeof(type), (alignment)))3738// Templates to create a subclass padded to avoid cache line sharing. These are39// effective only when applied to derived-most (leaf) classes.4041// When no args are passed to the base ctor.42template <class T, size_t alignment = DEFAULT_CACHE_LINE_SIZE>43class Padded : public T {44private:45char _pad_buf_[PADDING_SIZE(T, alignment)];46};4748// When either 0 or 1 args may be passed to the base ctor.49template <class T, typename Arg1T, size_t alignment = DEFAULT_CACHE_LINE_SIZE>50class Padded01 : public T {51public:52Padded01(): T() { }53Padded01(Arg1T arg1): T(arg1) { }54private:55char _pad_buf_[PADDING_SIZE(T, alignment)];56};5758// Super class of PaddedEnd when pad_size != 0.59template <class T, size_t pad_size>60class PaddedEndImpl : public T {61private:62char _pad_buf[pad_size];63};6465// Super class of PaddedEnd when pad_size == 0.66template <class T>67class PaddedEndImpl<T, /*pad_size*/ 0> : public T {68// No padding.69};7071#define PADDED_END_SIZE(type, alignment) (align_up(sizeof(type), (alignment)) - sizeof(type))7273// More memory conservative implementation of Padded. The subclass adds the74// 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.77template <class T, size_t alignment = DEFAULT_CACHE_LINE_SIZE>78class PaddedEnd : public PaddedEndImpl<T, PADDED_END_SIZE(T, alignment)> {79// C++ doesn't allow zero-length arrays. The padding is put in a80// super class that is specialized for the pad_size == 0 case.81};8283// Similar to PaddedEnd, this macro defines a _pad_buf#id field84// that is (alignment - size) bytes in size. This macro is used85// to add padding in between non-class fields in a class or struct.86#define DEFINE_PAD_MINUS_SIZE(id, alignment, size) \87char _pad_buf##id[(alignment) - (size)]8889// Helper class to create an array of PaddedEnd<T> objects. All elements will90// start at a multiple of alignment and the size will be aligned to alignment.91template <class T, MEMFLAGS flags, size_t alignment = DEFAULT_CACHE_LINE_SIZE>92class PaddedArray {93public:94// Creates an aligned padded array.95// The memory can't be deleted since the raw memory chunk is not returned.96static PaddedEnd<T>* create_unfreeable(uint length);97};9899// Helper class to create an array of references to arrays of primitive types100// Both the array of references and the data arrays are aligned to the given101// alignment. The allocated memory is zero-filled.102template <class T, MEMFLAGS flags, size_t alignment = DEFAULT_CACHE_LINE_SIZE>103class Padded2DArray {104public:105// Creates an aligned padded 2D array.106// The memory cannot be deleted since the raw memory chunk is not returned.107// Always uses mmap to reserve memory. Only the first few pages with the index to108// the rows are touched. Allocation size should be "large" to cover page overhead.109static T** create_unfreeable(uint rows, uint columns, size_t* allocation_size = NULL);110};111112// Helper class to create an array of T objects. The array as a whole will113// start at a multiple of alignment and its size will be aligned to alignment.114template <class T, MEMFLAGS flags, size_t alignment = DEFAULT_CACHE_LINE_SIZE>115class PaddedPrimitiveArray {116public:117static T* create_unfreeable(size_t length);118static T* create(size_t length, void** alloc_base);119};120121#endif // SHARE_MEMORY_PADDED_HPP122123124