Path: blob/master/src/hotspot/share/gc/z/zAttachedArray.inline.hpp
40961 views
/*1* Copyright (c) 2019, 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*/2223#ifndef SHARE_GC_Z_ZATTACHEDARRAY_INLINE_HPP24#define SHARE_GC_Z_ZATTACHEDARRAY_INLINE_HPP2526#include "gc/z/zAttachedArray.hpp"2728#include "memory/allocation.hpp"29#include "utilities/align.hpp"3031template <typename ObjectT, typename ArrayT>32inline size_t ZAttachedArray<ObjectT, ArrayT>::object_size() {33return align_up(sizeof(ObjectT), sizeof(ArrayT));34}3536template <typename ObjectT, typename ArrayT>37inline size_t ZAttachedArray<ObjectT, ArrayT>::array_size(size_t length) {38return sizeof(ArrayT) * length;39}4041template <typename ObjectT, typename ArrayT>42template <typename Allocator>43inline void* ZAttachedArray<ObjectT, ArrayT>::alloc(Allocator* allocator, size_t length) {44// Allocate memory for object and array45const size_t size = object_size() + array_size(length);46void* const addr = allocator->alloc(size);4748// Placement new array49void* const array_addr = reinterpret_cast<char*>(addr) + object_size();50::new (array_addr) ArrayT[length];5152// Return pointer to object53return addr;54}5556template <typename ObjectT, typename ArrayT>57inline void* ZAttachedArray<ObjectT, ArrayT>::alloc(size_t length) {58struct Allocator {59void* alloc(size_t size) const {60return AllocateHeap(size, mtGC);61}62} allocator;63return alloc(&allocator, length);64}6566template <typename ObjectT, typename ArrayT>67inline void ZAttachedArray<ObjectT, ArrayT>::free(ObjectT* obj) {68FreeHeap(obj);69}7071template <typename ObjectT, typename ArrayT>72inline ZAttachedArray<ObjectT, ArrayT>::ZAttachedArray(size_t length) :73_length(length) {}7475template <typename ObjectT, typename ArrayT>76inline size_t ZAttachedArray<ObjectT, ArrayT>::length() const {77return _length;78}7980template <typename ObjectT, typename ArrayT>81inline ArrayT* ZAttachedArray<ObjectT, ArrayT>::operator()(const ObjectT* obj) const {82return reinterpret_cast<ArrayT*>(reinterpret_cast<uintptr_t>(obj) + object_size());83}8485#endif // SHARE_GC_Z_ZATTACHEDARRAY_INLINE_HPP868788