Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/utilities/copy.hpp
32285 views
/*1* Copyright (c) 2003, 2018, 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_COPY_HPP25#define SHARE_VM_UTILITIES_COPY_HPP2627#include "runtime/stubRoutines.hpp"2829// Assembly code for platforms that need it.30extern "C" {31void _Copy_conjoint_words(HeapWord* from, HeapWord* to, size_t count);32void _Copy_disjoint_words(HeapWord* from, HeapWord* to, size_t count);3334void _Copy_conjoint_words_atomic(HeapWord* from, HeapWord* to, size_t count);35void _Copy_disjoint_words_atomic(HeapWord* from, HeapWord* to, size_t count);3637void _Copy_aligned_conjoint_words(HeapWord* from, HeapWord* to, size_t count);38void _Copy_aligned_disjoint_words(HeapWord* from, HeapWord* to, size_t count);3940void _Copy_conjoint_bytes(void* from, void* to, size_t count);4142void _Copy_conjoint_bytes_atomic (void* from, void* to, size_t count);43void _Copy_conjoint_jshorts_atomic(jshort* from, jshort* to, size_t count);44void _Copy_conjoint_jints_atomic (jint* from, jint* to, size_t count);45void _Copy_conjoint_jlongs_atomic (jlong* from, jlong* to, size_t count);46void _Copy_conjoint_oops_atomic (oop* from, oop* to, size_t count);4748void _Copy_arrayof_conjoint_bytes (HeapWord* from, HeapWord* to, size_t count);49void _Copy_arrayof_conjoint_jshorts(HeapWord* from, HeapWord* to, size_t count);50void _Copy_arrayof_conjoint_jints (HeapWord* from, HeapWord* to, size_t count);51void _Copy_arrayof_conjoint_jlongs (HeapWord* from, HeapWord* to, size_t count);52void _Copy_arrayof_conjoint_oops (HeapWord* from, HeapWord* to, size_t count);53}5455class Copy : AllStatic {56public:57// Block copy methods have four attributes. We don't define all possibilities.58// alignment: aligned to BytesPerLong59// arrayof: arraycopy operation with both operands aligned on the same60// boundary as the first element of an array of the copy unit.61// This is currently a HeapWord boundary on all platforms, except62// for long and double arrays, which are aligned on an 8-byte63// boundary on all platforms.64// arraycopy operations are implicitly atomic on each array element.65// overlap: disjoint or conjoint.66// copy unit: bytes or words (i.e., HeapWords) or oops (i.e., pointers).67// atomicity: atomic or non-atomic on the copy unit.68//69// Names are constructed thusly:70//71// [ 'aligned_' | 'arrayof_' ]72// ('conjoint_' | 'disjoint_')73// ('words' | 'bytes' | 'jshorts' | 'jints' | 'jlongs' | 'oops')74// [ '_atomic' ]75//76// Except in the arrayof case, whatever the alignment is, we assume we can copy77// whole alignment units. E.g., if BytesPerLong is 2x word alignment, an odd78// count may copy an extra word. In the arrayof case, we are allowed to copy79// only the number of copy units specified.80//81// All callees check count for 0.82//8384// HeapWords8586// Word-aligned words, conjoint, not atomic on each word87static void conjoint_words(HeapWord* from, HeapWord* to, size_t count) {88assert_params_ok(from, to, LogHeapWordSize);89pd_conjoint_words(from, to, count);90}9192// Word-aligned words, disjoint, not atomic on each word93static void disjoint_words(HeapWord* from, HeapWord* to, size_t count) {94assert_params_ok(from, to, LogHeapWordSize);95assert_disjoint(from, to, count);96pd_disjoint_words(from, to, count);97}9899// Word-aligned words, disjoint, atomic on each word100static void disjoint_words_atomic(HeapWord* from, HeapWord* to, size_t count) {101assert_params_ok(from, to, LogHeapWordSize);102assert_disjoint(from, to, count);103pd_disjoint_words_atomic(from, to, count);104}105106// Object-aligned words, conjoint, not atomic on each word107static void aligned_conjoint_words(HeapWord* from, HeapWord* to, size_t count) {108assert_params_aligned(from, to);109pd_aligned_conjoint_words(from, to, count);110}111112// Object-aligned words, disjoint, not atomic on each word113static void aligned_disjoint_words(HeapWord* from, HeapWord* to, size_t count) {114assert_params_aligned(from, to);115assert_disjoint(from, to, count);116pd_aligned_disjoint_words(from, to, count);117}118119// bytes, jshorts, jints, jlongs, oops120121// bytes, conjoint, not atomic on each byte (not that it matters)122static void conjoint_jbytes(void* from, void* to, size_t count) {123pd_conjoint_bytes(from, to, count);124}125126// bytes, conjoint, atomic on each byte (not that it matters)127static void conjoint_jbytes_atomic(void* from, void* to, size_t count) {128pd_conjoint_bytes(from, to, count);129}130131// jshorts, conjoint, atomic on each jshort132static void conjoint_jshorts_atomic(jshort* from, jshort* to, size_t count) {133assert_params_ok(from, to, LogBytesPerShort);134pd_conjoint_jshorts_atomic(from, to, count);135}136137// jints, conjoint, atomic on each jint138static void conjoint_jints_atomic(jint* from, jint* to, size_t count) {139assert_params_ok(from, to, LogBytesPerInt);140pd_conjoint_jints_atomic(from, to, count);141}142143// jlongs, conjoint, atomic on each jlong144static void conjoint_jlongs_atomic(jlong* from, jlong* to, size_t count) {145assert_params_ok(from, to, LogBytesPerLong);146pd_conjoint_jlongs_atomic(from, to, count);147}148149// oops, conjoint, atomic on each oop150static void conjoint_oops_atomic(oop* from, oop* to, size_t count) {151assert_params_ok(from, to, LogBytesPerHeapOop);152pd_conjoint_oops_atomic(from, to, count);153}154155// overloaded for UseCompressedOops156static void conjoint_oops_atomic(narrowOop* from, narrowOop* to, size_t count) {157assert(sizeof(narrowOop) == sizeof(jint), "this cast is wrong");158assert_params_ok(from, to, LogBytesPerInt);159pd_conjoint_jints_atomic((jint*)from, (jint*)to, count);160}161162// Copy a span of memory. If the span is an integral number of aligned163// longs, words, or ints, copy those units atomically.164// The largest atomic transfer unit is 8 bytes, or the largest power165// of two which divides all of from, to, and size, whichever is smaller.166static void conjoint_memory_atomic(void* from, void* to, size_t size);167168// bytes, conjoint array, atomic on each byte (not that it matters)169static void arrayof_conjoint_jbytes(HeapWord* from, HeapWord* to, size_t count) {170pd_arrayof_conjoint_bytes(from, to, count);171}172173// jshorts, conjoint array, atomic on each jshort174static void arrayof_conjoint_jshorts(HeapWord* from, HeapWord* to, size_t count) {175assert_params_ok(from, to, LogBytesPerShort);176pd_arrayof_conjoint_jshorts(from, to, count);177}178179// jints, conjoint array, atomic on each jint180static void arrayof_conjoint_jints(HeapWord* from, HeapWord* to, size_t count) {181assert_params_ok(from, to, LogBytesPerInt);182pd_arrayof_conjoint_jints(from, to, count);183}184185// jlongs, conjoint array, atomic on each jlong186static void arrayof_conjoint_jlongs(HeapWord* from, HeapWord* to, size_t count) {187assert_params_ok(from, to, LogBytesPerLong);188pd_arrayof_conjoint_jlongs(from, to, count);189}190191// oops, conjoint array, atomic on each oop192static void arrayof_conjoint_oops(HeapWord* from, HeapWord* to, size_t count) {193assert_params_ok(from, to, LogBytesPerHeapOop);194pd_arrayof_conjoint_oops(from, to, count);195}196197// Known overlap methods198199// Copy word-aligned words from higher to lower addresses, not atomic on each word200inline static void conjoint_words_to_lower(HeapWord* from, HeapWord* to, size_t byte_count) {201// byte_count is in bytes to check its alignment202assert_params_ok(from, to, LogHeapWordSize);203assert_byte_count_ok(byte_count, HeapWordSize);204205size_t count = (size_t)round_to(byte_count, HeapWordSize) >> LogHeapWordSize;206assert(to <= from || from + count <= to, "do not overwrite source data");207208while (count-- > 0) {209*to++ = *from++;210}211}212213// Copy word-aligned words from lower to higher addresses, not atomic on each word214inline static void conjoint_words_to_higher(HeapWord* from, HeapWord* to, size_t byte_count) {215// byte_count is in bytes to check its alignment216assert_params_ok(from, to, LogHeapWordSize);217assert_byte_count_ok(byte_count, HeapWordSize);218219size_t count = (size_t)round_to(byte_count, HeapWordSize) >> LogHeapWordSize;220assert(from <= to || to + count <= from, "do not overwrite source data");221222from += count - 1;223to += count - 1;224while (count-- > 0) {225*to-- = *from--;226}227}228229/**230* Copy and *unconditionally* byte swap elements231*232* @param src address of source233* @param dst address of destination234* @param byte_count number of bytes to copy235* @param elem_size size of the elements to copy-swap236*/237static void conjoint_swap(address src, address dst, size_t byte_count, size_t elem_size);238239// Fill methods240241// Fill word-aligned words, not atomic on each word242// set_words243static void fill_to_words(HeapWord* to, size_t count, juint value = 0) {244assert_params_ok(to, LogHeapWordSize);245pd_fill_to_words(to, count, value);246}247248static void fill_to_aligned_words(HeapWord* to, size_t count, juint value = 0) {249assert_params_aligned(to);250pd_fill_to_aligned_words(to, count, value);251}252253// Fill bytes254static void fill_to_bytes(void* to, size_t count, jubyte value = 0) {255pd_fill_to_bytes(to, count, value);256}257258// Fill a span of memory. If the span is an integral number of aligned259// longs, words, or ints, store to those units atomically.260// The largest atomic transfer unit is 8 bytes, or the largest power261// of two which divides both to and size, whichever is smaller.262static void fill_to_memory_atomic(void* to, size_t size, jubyte value = 0);263264// Zero-fill methods265266// Zero word-aligned words, not atomic on each word267static void zero_to_words(HeapWord* to, size_t count) {268assert_params_ok(to, LogHeapWordSize);269pd_zero_to_words(to, count);270}271272// Zero bytes273static void zero_to_bytes(void* to, size_t count) {274pd_zero_to_bytes(to, count);275}276277private:278static bool params_disjoint(HeapWord* from, HeapWord* to, size_t count) {279if (from < to) {280return pointer_delta(to, from) >= count;281}282return pointer_delta(from, to) >= count;283}284285// These methods raise a fatal if they detect a problem.286287static void assert_disjoint(HeapWord* from, HeapWord* to, size_t count) {288#ifdef ASSERT289if (!params_disjoint(from, to, count))290basic_fatal("source and dest overlap");291#endif292}293294static void assert_params_ok(void* from, void* to, intptr_t log_align) {295#ifdef ASSERT296if (mask_bits((uintptr_t)from, right_n_bits(log_align)) != 0)297basic_fatal("not aligned");298if (mask_bits((uintptr_t)to, right_n_bits(log_align)) != 0)299basic_fatal("not aligned");300#endif301}302303static void assert_params_ok(HeapWord* to, intptr_t log_align) {304#ifdef ASSERT305if (mask_bits((uintptr_t)to, right_n_bits(log_align)) != 0)306basic_fatal("not word aligned");307#endif308}309static void assert_params_aligned(HeapWord* from, HeapWord* to) {310#ifdef ASSERT311if (mask_bits((uintptr_t)from, BytesPerLong-1) != 0)312basic_fatal("not long aligned");313if (mask_bits((uintptr_t)to, BytesPerLong-1) != 0)314basic_fatal("not long aligned");315#endif316}317318static void assert_params_aligned(HeapWord* to) {319#ifdef ASSERT320if (mask_bits((uintptr_t)to, BytesPerLong-1) != 0)321basic_fatal("not long aligned");322#endif323}324325static void assert_byte_count_ok(size_t byte_count, size_t unit_size) {326#ifdef ASSERT327if ((size_t)round_to(byte_count, unit_size) != byte_count) {328basic_fatal("byte count must be aligned");329}330#endif331}332333// Platform dependent implementations of the above methods.334#ifdef TARGET_ARCH_x86335# include "copy_x86.hpp"336#endif337#ifdef TARGET_ARCH_aarch32338# include "copy_aarch32.hpp"339#endif340#ifdef TARGET_ARCH_aarch64341# include "copy_aarch64.hpp"342#endif343#ifdef TARGET_ARCH_sparc344# include "copy_sparc.hpp"345#endif346#ifdef TARGET_ARCH_zero347# include "copy_zero.hpp"348#endif349#ifdef TARGET_ARCH_arm350# include "copy_arm.hpp"351#endif352#ifdef TARGET_ARCH_ppc353# include "copy_ppc.hpp"354#endif355356};357358#endif // SHARE_VM_UTILITIES_COPY_HPP359360361