Path: blob/master/src/hotspot/share/memory/padded.inline.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_INLINE_HPP25#define SHARE_MEMORY_PADDED_INLINE_HPP2627#include "memory/padded.hpp"2829#include "memory/allocation.inline.hpp"30#include "utilities/align.hpp"31#include "utilities/debug.hpp"32#include "utilities/globalDefinitions.hpp"3334// Creates an aligned padded array.35// The memory can't be deleted since the raw memory chunk is not returned.36template <class T, MEMFLAGS flags, size_t alignment>37PaddedEnd<T>* PaddedArray<T, flags, alignment>::create_unfreeable(uint length) {38// Check that the PaddedEnd class works as intended.39STATIC_ASSERT(is_aligned(sizeof(PaddedEnd<T>), alignment));4041// Allocate a chunk of memory large enough to allow for some alignment.42void* chunk = AllocateHeap(length * sizeof(PaddedEnd<T, alignment>) + alignment, flags);4344// Make the initial alignment.45PaddedEnd<T>* aligned_padded_array = (PaddedEnd<T>*)align_up(chunk, alignment);4647// Call the default constructor for each element.48for (uint i = 0; i < length; i++) {49::new (&aligned_padded_array[i]) T();50}5152return aligned_padded_array;53}5455template <class T, MEMFLAGS flags, size_t alignment>56T** Padded2DArray<T, flags, alignment>::create_unfreeable(uint rows, uint columns, size_t* allocation_size) {57// Calculate and align the size of the first dimension's table.58size_t table_size = align_up(rows * sizeof(T*), alignment);59// The size of the separate rows.60size_t row_size = align_up(columns * sizeof(T), alignment);61// Total size consists of the indirection table plus the rows.62size_t total_size = table_size + rows * row_size + alignment;6364// Allocate a chunk of memory large enough to allow alignment of the chunk.65void* chunk = MmapArrayAllocator<uint8_t>::allocate(total_size, flags);66// Clear the allocated memory.67// Align the chunk of memory.68T** result = (T**)align_up(chunk, alignment);69void* data_start = (void*)((uintptr_t)result + table_size);7071// Fill in the row table.72for (size_t i = 0; i < rows; i++) {73result[i] = (T*)((uintptr_t)data_start + i * row_size);74}7576if (allocation_size != NULL) {77*allocation_size = total_size;78}7980return result;81}8283template <class T, MEMFLAGS flags, size_t alignment>84T* PaddedPrimitiveArray<T, flags, alignment>::create_unfreeable(size_t length) {85void* temp;86return create(length, &temp);87}8889template <class T, MEMFLAGS flags, size_t alignment>90T* PaddedPrimitiveArray<T, flags, alignment>::create(size_t length, void** alloc_base) {91// Allocate a chunk of memory large enough to allow for some alignment.92void* chunk = AllocateHeap(length * sizeof(T) + alignment, flags);9394memset(chunk, 0, length * sizeof(T) + alignment);9596*alloc_base = chunk;97return (T*)align_up(chunk, alignment);98}99100#endif // SHARE_MEMORY_PADDED_INLINE_HPP101102103