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