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: #include "memory/allocation.inline.hpp" stefank@5515: #include "memory/padded.hpp" stefank@5515: #include "utilities/debug.hpp" stefank@5515: #include "utilities/globalDefinitions.hpp" stefank@5515: 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: template stefank@5515: PaddedEnd* PaddedArray::create_unfreeable(uint length) { stefank@5515: // Check that the PaddedEnd class works as intended. stefank@5515: STATIC_ASSERT(is_size_aligned_(sizeof(PaddedEnd), alignment)); stefank@5515: stefank@5515: // Allocate a chunk of memory large enough to allow for some alignment. stefank@5515: void* chunk = AllocateHeap(length * sizeof(PaddedEnd) + alignment, flags); stefank@5515: stefank@5515: // Make the initial alignment. stefank@5515: PaddedEnd* aligned_padded_array = (PaddedEnd*)align_pointer_up(chunk, alignment); stefank@5515: stefank@5515: // Call the default constructor for each element. stefank@5515: for (uint i = 0; i < length; i++) { stefank@5515: ::new (&aligned_padded_array[i]) T(); stefank@5515: } stefank@5515: stefank@5515: return aligned_padded_array; stefank@5515: } tschatzl@6403: tschatzl@6403: template tschatzl@6403: T** Padded2DArray::create_unfreeable(uint rows, uint columns, size_t* allocation_size) { tschatzl@6403: // Calculate and align the size of the first dimension's table. tschatzl@6403: size_t table_size = align_size_up_(rows * sizeof(T*), alignment); tschatzl@6403: // The size of the separate rows. tschatzl@6403: size_t row_size = align_size_up_(columns * sizeof(T), alignment); tschatzl@6403: // Total size consists of the indirection table plus the rows. tschatzl@6403: size_t total_size = table_size + rows * row_size + alignment; tschatzl@6403: tschatzl@6403: // Allocate a chunk of memory large enough to allow alignment of the chunk. tschatzl@6403: void* chunk = AllocateHeap(total_size, flags); tschatzl@6403: // Clear the allocated memory. tschatzl@6403: memset(chunk, 0, total_size); tschatzl@6403: // Align the chunk of memory. tschatzl@6403: T** result = (T**)align_pointer_up(chunk, alignment); tschatzl@6403: void* data_start = (void*)((uintptr_t)result + table_size); tschatzl@6403: tschatzl@6403: // Fill in the row table. tschatzl@6403: for (size_t i = 0; i < rows; i++) { tschatzl@6403: result[i] = (T*)((uintptr_t)data_start + i * row_size); tschatzl@6403: } tschatzl@6403: tschatzl@6403: if (allocation_size != NULL) { tschatzl@6403: *allocation_size = total_size; tschatzl@6403: } tschatzl@6403: tschatzl@6403: return result; tschatzl@6403: } tschatzl@6409: tschatzl@6409: template tschatzl@6409: T* PaddedPrimitiveArray::create_unfreeable(size_t length) { tschatzl@6409: // Allocate a chunk of memory large enough to allow for some alignment. tschatzl@6409: void* chunk = AllocateHeap(length * sizeof(T) + alignment, flags); tschatzl@6409: tschatzl@6409: memset(chunk, 0, length * sizeof(T) + alignment); tschatzl@6409: tschatzl@6409: return (T*)align_pointer_up(chunk, alignment); tschatzl@6409: }