Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/utilities/align.hpp
32285 views
/*1* Copyright (c) 1997, 2017, 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_VM_UTILITIES_ALIGN_HPP25#define SHARE_VM_UTILITIES_ALIGN_HPP2627#include "utilities/globalDefinitions.hpp"2829// Signed variants of alignment helpers. There are two versions of each, a macro30// for use in places like enum definitions that require compile-time constant31// expressions and a function for all other places so as to get type checking.3233// Using '(what) & ~align_mask(alignment)' to align 'what' down is broken when34// 'alignment' is an unsigned int and 'what' is a wider type. The & operation35// will widen the inverted mask, and not sign extend it, leading to a mask with36// zeros in the most significant bits. The use of align_mask_widened() solves37// this problem.38#define align_mask(alignment) ((alignment) - 1)39#define widen_to_type_of(what, type_carrier) (true ? (what) : (type_carrier))40#define align_mask_widened(alignment, type_carrier) widen_to_type_of(align_mask(alignment), (type_carrier))4142#define align_down_(size, alignment) ((size) & ~align_mask_widened((alignment), (size)))4344#define align_up_(size, alignment) (align_down_((size) + align_mask(alignment), (alignment)))4546#define is_aligned_(size, alignment) (((size) & align_mask(alignment)) == 0)4748// Temporary declaration until this file has been restructured.49template <typename T>50bool is_power_of_2_t(T x) {51return (x != T(0)) && ((x & (x - 1)) == T(0));52}5354// Helpers to align sizes and check for alignment5556template <typename T, typename A>57inline T align_up(T size, A alignment) {58assert(is_power_of_2_t(alignment), "must be a power of 2");5960T ret = align_up_(size, alignment);61assert(is_aligned_(ret, alignment), "must be aligned");6263return ret;64}6566template <typename T, typename A>67inline T align_down(T size, A alignment) {68assert(is_power_of_2_t(alignment), "must be a power of 2");6970T ret = align_down_(size, alignment);71assert(is_aligned_(ret, alignment), "must be aligned");7273return ret;74}7576template <typename T, typename A>77inline bool is_aligned(T size, A alignment) {78assert(is_power_of_2_t(alignment), "must be a power of 2");7980return is_aligned_(size, alignment);81}8283// Align down with a lower bound. If the aligning results in 0, return 'alignment'.84template <typename T, typename A>85inline T align_down_bounded(T size, A alignment) {86A aligned_size = align_down(size, alignment);87return aligned_size > 0 ? aligned_size : alignment;88}8990// Helpers to align pointers and check for alignment.9192template <typename T, typename A>93inline T* align_up(T* ptr, A alignment) {94return (T*)align_up((uintptr_t)ptr, alignment);95}9697template <typename T, typename A>98inline T* align_down(T* ptr, A alignment) {99return (T*)align_down((uintptr_t)ptr, alignment);100}101102template <typename T, typename A>103inline bool is_aligned(T* ptr, A alignment) {104return is_aligned((uintptr_t)ptr, alignment);105}106107// Align metaspace objects by rounding up to natural word boundary108template <typename T>109inline T align_metadata_size(T size) {110return align_up(size, 1);111}112113// Align objects in the Java Heap by rounding up their size, in HeapWord units.114template <typename T>115inline T align_object_size(T word_size) {116return align_up(word_size, MinObjAlignment);117}118119inline bool is_object_aligned(size_t word_size) {120return is_aligned(word_size, MinObjAlignment);121}122123inline bool is_object_aligned(const void* addr) {124return is_aligned(addr, MinObjAlignmentInBytes);125}126127// Pad out certain offsets to jlong alignment, in HeapWord units.128template <typename T>129inline T align_object_offset(T offset) {130return align_up(offset, HeapWordsPerLong);131}132133// Clamp an address to be within a specific page134// 1. If addr is on the page it is returned as is135// 2. If addr is above the page_address the start of the *next* page will be returned136// 3. Otherwise, if addr is below the page_address the start of the page will be returned137template <typename T>138inline T* clamp_address_in_page(T* addr, T* page_address, size_t page_size) {139if (align_down(addr, page_size) == align_down(page_address, page_size)) {140// address is in the specified page, just return it as is141return addr;142} else if (addr > page_address) {143// address is above specified page, return start of next page144return align_down(page_address, page_size) + page_size;145} else {146// address is below specified page, return start of page147return align_down(page_address, page_size);148}149}150151#endif // SHARE_VM_UTILITIES_ALIGN_HPP152153154