align.hpp revision 13249:a2753984d2c1
1207753Smm/*
2207753Smm * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
3207753Smm * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4207753Smm *
5207753Smm * This code is free software; you can redistribute it and/or modify it
6207753Smm * under the terms of the GNU General Public License version 2 only, as
7207753Smm * published by the Free Software Foundation.
8207753Smm *
9207753Smm * This code is distributed in the hope that it will be useful, but WITHOUT
10207753Smm * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11207753Smm * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12207753Smm * version 2 for more details (a copy is included in the LICENSE file that
13207753Smm * accompanied this code).
14207753Smm *
15207753Smm * You should have received a copy of the GNU General Public License version
16207753Smm * 2 along with this work; if not, write to the Free Software Foundation,
17207753Smm * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18207753Smm *
19207753Smm * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20207753Smm * or visit www.oracle.com if you need additional information or have any
21207753Smm * questions.
22207753Smm *
23207753Smm */
24207753Smm
25207753Smm#ifndef SHARE_VM_UTILITIES_ALIGN_HPP
26207753Smm#define SHARE_VM_UTILITIES_ALIGN_HPP
27207753Smm
28207753Smm#include "utilities/globalDefinitions.hpp"
29207753Smm
30207753Smm// Signed variants of alignment helpers.  There are two versions of each, a macro
31207753Smm// for use in places like enum definitions that require compile-time constant
32207753Smm// expressions and a function for all other places so as to get type checking.
33207753Smm
34207753Smm// Using '(what) & ~align_mask(alignment)' to align 'what' down is broken when
35207753Smm// 'alignment' is an unsigned int and 'what' is a wider type. The & operation
36207753Smm// will widen the inverted mask, and not sign extend it, leading to a mask with
37207753Smm// zeros in the most significant bits. The use of align_mask_widened() solves
38207753Smm// this problem.
39207753Smm#define align_mask(alignment) ((alignment) - 1)
40207753Smm#define widen_to_type_of(what, type_carrier) (true ? (what) : (type_carrier))
41207753Smm#define align_mask_widened(alignment, type_carrier) widen_to_type_of(align_mask(alignment), (type_carrier))
42207753Smm
43207753Smm#define align_down_(size, alignment) ((size) & ~align_mask_widened((alignment), (size)))
44207753Smm
45207753Smm#define align_up_(size, alignment) (align_down_((size) + align_mask(alignment), (alignment)))
46207753Smm
47207753Smm#define is_aligned_(size, alignment) ((size) == (align_up_((size), (alignment))))
48207753Smm
49207753Smm// Temporary declaration until this file has been restructured.
50207753Smmtemplate <typename T>
51207753Smmbool is_power_of_2_t(T x) {
52207753Smm  return (x != T(0)) && ((x & (x - 1)) == T(0));
53207753Smm}
54207753Smm
55207753Smm// Helpers to align sizes and check for alignment
56207753Smm
57207753Smmtemplate <typename T, typename A>
58207753Smminline T align_up(T size, A alignment) {
59207753Smm  assert(is_power_of_2_t(alignment), "must be a power of 2: " UINT64_FORMAT, (uint64_t)alignment);
60207753Smm
61207753Smm  T ret = align_up_(size, alignment);
62207753Smm  assert(is_aligned_(ret, alignment), "must be aligned: " UINT64_FORMAT, (uint64_t)ret);
63207753Smm
64207753Smm  return ret;
65207753Smm}
66207753Smm
67207753Smmtemplate <typename T, typename A>
68207753Smminline T align_down(T size, A alignment) {
69207753Smm  assert(is_power_of_2_t(alignment), "must be a power of 2: " UINT64_FORMAT, (uint64_t)alignment);
70207753Smm
71207753Smm  T ret = align_down_(size, alignment);
72207753Smm  assert(is_aligned_(ret, alignment), "must be aligned: " UINT64_FORMAT, (uint64_t)ret);
73207753Smm
74207753Smm  return ret;
75207753Smm}
76207753Smm
77207753Smmtemplate <typename T, typename A>
78207753Smminline bool is_aligned(T size, A alignment) {
79207753Smm  assert(is_power_of_2_t(alignment), "must be a power of 2: " UINT64_FORMAT, (uint64_t)alignment);
80207753Smm
81207753Smm  return is_aligned_(size, alignment);
82207753Smm}
83207753Smm
84207753Smm// Align down with a lower bound. If the aligning results in 0, return 'alignment'.
85207753Smmtemplate <typename T, typename A>
86207753Smminline T align_down_bounded(T size, A alignment) {
87207753Smm  A aligned_size = align_down(size, alignment);
88207753Smm  return aligned_size > 0 ? aligned_size : alignment;
89207753Smm}
90207753Smm
91207753Smm// Helpers to align pointers and check for alignment.
92207753Smm
93207753Smmtemplate <typename T, typename A>
94207753Smminline T* align_up(T* ptr, A alignment) {
95207753Smm  return (T*)align_up((uintptr_t)ptr, alignment);
96207753Smm}
97207753Smm
98207753Smmtemplate <typename T, typename A>
99207753Smminline T* align_down(T* ptr, A alignment) {
100207753Smm  return (T*)align_down((uintptr_t)ptr, alignment);
101207753Smm}
102207753Smm
103207753Smmtemplate <typename T, typename A>
104207753Smminline bool is_aligned(T* ptr, A alignment) {
105207753Smm  return is_aligned((uintptr_t)ptr, alignment);
106207753Smm}
107207753Smm
108207753Smm// Align metaspace objects by rounding up to natural word boundary
109207753Smmtemplate <typename T>
110207753Smminline T align_metadata_size(T size) {
111207753Smm  return align_up(size, 1);
112207753Smm}
113207753Smm
114207753Smm// Align objects in the Java Heap by rounding up their size, in HeapWord units.
115207753Smmtemplate <typename T>
116207753Smminline T align_object_size(T word_size) {
117207753Smm  return align_up(word_size, MinObjAlignment);
118207753Smm}
119207753Smm
120207753Smminline bool is_object_aligned(size_t word_size) {
121207753Smm  return is_aligned(word_size, MinObjAlignment);
122207753Smm}
123207753Smm
124207753Smminline bool is_object_aligned(const void* addr) {
125207753Smm  return is_aligned(addr, MinObjAlignmentInBytes);
126207753Smm}
127207753Smm
128207753Smm// Pad out certain offsets to jlong alignment, in HeapWord units.
129207753Smmtemplate <typename T>
130207753Smminline T align_object_offset(T offset) {
131207753Smm  return align_up(offset, HeapWordsPerLong);
132207753Smm}
133207753Smm
134207753Smm// Clamp an address to be within a specific page
135207753Smm// 1. If addr is on the page it is returned as is
136207753Smm// 2. If addr is above the page_address the start of the *next* page will be returned
137207753Smm// 3. Otherwise, if addr is below the page_address the start of the page will be returned
138207753Smmtemplate <typename T>
139207753Smminline T* clamp_address_in_page(T* addr, T* page_address, size_t page_size) {
140207753Smm  if (align_down(addr, page_size) == align_down(page_address, page_size)) {
141207753Smm    // address is in the specified page, just return it as is
142207753Smm    return addr;
143207753Smm  } else if (addr > page_address) {
144207753Smm    // address is above specified page, return start of next page
145207753Smm    return align_down(page_address, page_size) + page_size;
146207753Smm  } else {
147207753Smm    // address is below specified page, return start of page
148207753Smm    return align_down(page_address, page_size);
149207753Smm  }
150207753Smm}
151207753Smm
152207753Smm#endif // SHARE_VM_UTILITIES_ALIGN_HPP
153207753Smm