src/hotspot/share/utilities/align.hpp
author mr
Tue, 29 Oct 2019 13:52:04 -0700
changeset 58850 f4290bf1cc21
parent 53244 9807daeb47c4
permissions -rw-r--r--
8233137: runtime/ErrorHandling/VeryEarlyAssertTest.java fails after 8232080 Reviewed-by: stuefe, iignatyev, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46625
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
     1
/*
53244
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 47216
diff changeset
     2
 * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
46625
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
     4
 *
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
     7
 * published by the Free Software Foundation.
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
     8
 *
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    13
 * accompanied this code).
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    14
 *
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    18
 *
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    21
 * questions.
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    22
 *
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    23
 */
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    24
53244
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 47216
diff changeset
    25
#ifndef SHARE_UTILITIES_ALIGN_HPP
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 47216
diff changeset
    26
#define SHARE_UTILITIES_ALIGN_HPP
46625
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    27
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    28
#include "utilities/globalDefinitions.hpp"
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    29
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    30
// Signed variants of alignment helpers.  There are two versions of each, a macro
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    31
// for use in places like enum definitions that require compile-time constant
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    32
// expressions and a function for all other places so as to get type checking.
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    33
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    34
// Using '(what) & ~align_mask(alignment)' to align 'what' down is broken when
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    35
// 'alignment' is an unsigned int and 'what' is a wider type. The & operation
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    36
// will widen the inverted mask, and not sign extend it, leading to a mask with
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    37
// zeros in the most significant bits. The use of align_mask_widened() solves
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    38
// this problem.
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    39
#define align_mask(alignment) ((alignment) - 1)
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    40
#define widen_to_type_of(what, type_carrier) (true ? (what) : (type_carrier))
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    41
#define align_mask_widened(alignment, type_carrier) widen_to_type_of(align_mask(alignment), (type_carrier))
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    42
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    43
#define align_down_(size, alignment) ((size) & ~align_mask_widened((alignment), (size)))
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    44
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    45
#define align_up_(size, alignment) (align_down_((size) + align_mask(alignment), (alignment)))
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    46
46642
ec1179442657 8183570: Rework is_aligned_ to avoid multiple evaluation of the size expression
stefank
parents: 46625
diff changeset
    47
#define is_aligned_(size, alignment) (((size) & align_mask(alignment)) == 0)
46625
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    48
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    49
// Temporary declaration until this file has been restructured.
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    50
template <typename T>
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    51
bool is_power_of_2_t(T x) {
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    52
  return (x != T(0)) && ((x & (x - 1)) == T(0));
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    53
}
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    54
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    55
// Helpers to align sizes and check for alignment
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    56
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    57
template <typename T, typename A>
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    58
inline T align_up(T size, A alignment) {
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    59
  assert(is_power_of_2_t(alignment), "must be a power of 2: " UINT64_FORMAT, (uint64_t)alignment);
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    60
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    61
  T ret = align_up_(size, alignment);
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    62
  assert(is_aligned_(ret, alignment), "must be aligned: " UINT64_FORMAT, (uint64_t)ret);
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    63
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    64
  return ret;
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    65
}
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    66
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    67
template <typename T, typename A>
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    68
inline T align_down(T size, A alignment) {
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    69
  assert(is_power_of_2_t(alignment), "must be a power of 2: " UINT64_FORMAT, (uint64_t)alignment);
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    70
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    71
  T ret = align_down_(size, alignment);
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    72
  assert(is_aligned_(ret, alignment), "must be aligned: " UINT64_FORMAT, (uint64_t)ret);
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    73
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    74
  return ret;
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    75
}
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    76
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    77
template <typename T, typename A>
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    78
inline bool is_aligned(T size, A alignment) {
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    79
  assert(is_power_of_2_t(alignment), "must be a power of 2: " UINT64_FORMAT, (uint64_t)alignment);
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    80
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    81
  return is_aligned_(size, alignment);
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    82
}
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    83
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    84
// Align down with a lower bound. If the aligning results in 0, return 'alignment'.
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    85
template <typename T, typename A>
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    86
inline T align_down_bounded(T size, A alignment) {
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    87
  A aligned_size = align_down(size, alignment);
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    88
  return aligned_size > 0 ? aligned_size : alignment;
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    89
}
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    90
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    91
// Helpers to align pointers and check for alignment.
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    92
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    93
template <typename T, typename A>
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    94
inline T* align_up(T* ptr, A alignment) {
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    95
  return (T*)align_up((uintptr_t)ptr, alignment);
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    96
}
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    97
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    98
template <typename T, typename A>
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
    99
inline T* align_down(T* ptr, A alignment) {
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   100
  return (T*)align_down((uintptr_t)ptr, alignment);
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   101
}
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   102
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   103
template <typename T, typename A>
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   104
inline bool is_aligned(T* ptr, A alignment) {
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   105
  return is_aligned((uintptr_t)ptr, alignment);
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   106
}
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   107
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   108
// Align metaspace objects by rounding up to natural word boundary
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   109
template <typename T>
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   110
inline T align_metadata_size(T size) {
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   111
  return align_up(size, 1);
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   112
}
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   113
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   114
// Align objects in the Java Heap by rounding up their size, in HeapWord units.
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   115
template <typename T>
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   116
inline T align_object_size(T word_size) {
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   117
  return align_up(word_size, MinObjAlignment);
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   118
}
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   119
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   120
inline bool is_object_aligned(size_t word_size) {
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   121
  return is_aligned(word_size, MinObjAlignment);
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   122
}
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   123
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   124
inline bool is_object_aligned(const void* addr) {
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   125
  return is_aligned(addr, MinObjAlignmentInBytes);
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   126
}
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   127
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   128
// Pad out certain offsets to jlong alignment, in HeapWord units.
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   129
template <typename T>
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   130
inline T align_object_offset(T offset) {
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   131
  return align_up(offset, HeapWordsPerLong);
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   132
}
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   133
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   134
// Clamp an address to be within a specific page
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   135
// 1. If addr is on the page it is returned as is
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   136
// 2. If addr is above the page_address the start of the *next* page will be returned
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   137
// 3. Otherwise, if addr is below the page_address the start of the page will be returned
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   138
template <typename T>
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   139
inline T* clamp_address_in_page(T* addr, T* page_address, size_t page_size) {
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   140
  if (align_down(addr, page_size) == align_down(page_address, page_size)) {
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   141
    // address is in the specified page, just return it as is
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   142
    return addr;
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   143
  } else if (addr > page_address) {
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   144
    // address is above specified page, return start of next page
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   145
    return align_down(page_address, page_size) + page_size;
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   146
  } else {
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   147
    // address is below specified page, return start of page
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   148
    return align_down(page_address, page_size);
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   149
  }
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   150
}
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents:
diff changeset
   151
53244
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 47216
diff changeset
   152
#endif // SHARE_UTILITIES_ALIGN_HPP