Path: blob/master/src/hotspot/share/utilities/copy.hpp
40950 views
/*1* Copyright (c) 2003, 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*22*/2324#ifndef SHARE_UTILITIES_COPY_HPP25#define SHARE_UTILITIES_COPY_HPP2627#include "oops/oopsHierarchy.hpp"28#include "runtime/globals.hpp"29#include "utilities/align.hpp"30#include "utilities/bytes.hpp"31#include "utilities/debug.hpp"32#include "utilities/macros.hpp"3334// Assembly code for platforms that need it.35extern "C" {36void _Copy_conjoint_words(const HeapWord* from, HeapWord* to, size_t count);37void _Copy_disjoint_words(const HeapWord* from, HeapWord* to, size_t count);3839void _Copy_conjoint_words_atomic(const HeapWord* from, HeapWord* to, size_t count);40void _Copy_disjoint_words_atomic(const HeapWord* from, HeapWord* to, size_t count);4142void _Copy_aligned_conjoint_words(const HeapWord* from, HeapWord* to, size_t count);43void _Copy_aligned_disjoint_words(const HeapWord* from, HeapWord* to, size_t count);4445void _Copy_conjoint_bytes(const void* from, void* to, size_t count);4647void _Copy_conjoint_bytes_atomic (const void* from, void* to, size_t count);48void _Copy_conjoint_jshorts_atomic(const jshort* from, jshort* to, size_t count);49void _Copy_conjoint_jints_atomic (const jint* from, jint* to, size_t count);50void _Copy_conjoint_jlongs_atomic (const jlong* from, jlong* to, size_t count);51void _Copy_conjoint_oops_atomic (const oop* from, oop* to, size_t count);5253void _Copy_arrayof_conjoint_bytes (const HeapWord* from, HeapWord* to, size_t count);54void _Copy_arrayof_conjoint_jshorts(const HeapWord* from, HeapWord* to, size_t count);55void _Copy_arrayof_conjoint_jints (const HeapWord* from, HeapWord* to, size_t count);56void _Copy_arrayof_conjoint_jlongs (const HeapWord* from, HeapWord* to, size_t count);57void _Copy_arrayof_conjoint_oops (const HeapWord* from, HeapWord* to, size_t count);58}5960class Copy : AllStatic {61public:62// Block copy methods have four attributes. We don't define all possibilities.63// alignment: aligned to BytesPerLong64// arrayof: arraycopy operation with both operands aligned on the same65// boundary as the first element of an array of the copy unit.66// This is currently a HeapWord boundary on all platforms, except67// for long and double arrays, which are aligned on an 8-byte68// boundary on all platforms.69// arraycopy operations are implicitly atomic on each array element.70// overlap: disjoint or conjoint.71// copy unit: bytes or words (i.e., HeapWords) or oops (i.e., pointers).72// atomicity: atomic or non-atomic on the copy unit.73//74// Names are constructed thusly:75//76// [ 'aligned_' | 'arrayof_' ]77// ('conjoint_' | 'disjoint_')78// ('words' | 'bytes' | 'jshorts' | 'jints' | 'jlongs' | 'oops')79// [ '_atomic' ]80//81// Except in the arrayof case, whatever the alignment is, we assume we can copy82// whole alignment units. E.g., if BytesPerLong is 2x word alignment, an odd83// count may copy an extra word. In the arrayof case, we are allowed to copy84// only the number of copy units specified.85//86// All callees check count for 0.87//8889// HeapWords9091// Word-aligned words, conjoint, not atomic on each word92static void conjoint_words(const HeapWord* from, HeapWord* to, size_t count) {93assert_params_ok(from, to, HeapWordSize);94pd_conjoint_words(from, to, count);95}9697// Word-aligned words, disjoint, not atomic on each word98static void disjoint_words(const HeapWord* from, HeapWord* to, size_t count) {99assert_params_ok(from, to, HeapWordSize);100assert_disjoint(from, to, count);101pd_disjoint_words(from, to, count);102}103104// Word-aligned words, disjoint, atomic on each word105static void disjoint_words_atomic(const HeapWord* from, HeapWord* to, size_t count) {106assert_params_ok(from, to, HeapWordSize);107assert_disjoint(from, to, count);108pd_disjoint_words_atomic(from, to, count);109}110111// Object-aligned words, conjoint, not atomic on each word112static void aligned_conjoint_words(const HeapWord* from, HeapWord* to, size_t count) {113assert_params_aligned(from, to);114pd_aligned_conjoint_words(from, to, count);115}116117// Object-aligned words, disjoint, not atomic on each word118static void aligned_disjoint_words(const HeapWord* from, HeapWord* to, size_t count) {119assert_params_aligned(from, to);120assert_disjoint(from, to, count);121pd_aligned_disjoint_words(from, to, count);122}123124// bytes, jshorts, jints, jlongs, oops125126// bytes, conjoint, not atomic on each byte (not that it matters)127static void conjoint_jbytes(const void* from, void* to, size_t count) {128pd_conjoint_bytes(from, to, count);129}130131// bytes, conjoint, atomic on each byte (not that it matters)132static void conjoint_jbytes_atomic(const void* from, void* to, size_t count) {133pd_conjoint_bytes(from, to, count);134}135136// jshorts, conjoint, atomic on each jshort137static void conjoint_jshorts_atomic(const jshort* from, jshort* to, size_t count) {138assert_params_ok(from, to, BytesPerShort);139pd_conjoint_jshorts_atomic(from, to, count);140}141142// jints, conjoint, atomic on each jint143static void conjoint_jints_atomic(const jint* from, jint* to, size_t count) {144assert_params_ok(from, to, BytesPerInt);145pd_conjoint_jints_atomic(from, to, count);146}147148// jlongs, conjoint, atomic on each jlong149static void conjoint_jlongs_atomic(const jlong* from, jlong* to, size_t count) {150assert_params_ok(from, to, BytesPerLong);151pd_conjoint_jlongs_atomic(from, to, count);152}153154// oops, conjoint, atomic on each oop155static void conjoint_oops_atomic(const oop* from, oop* to, size_t count) {156assert_params_ok(from, to, BytesPerHeapOop);157pd_conjoint_oops_atomic(from, to, count);158}159160// overloaded for UseCompressedOops161static void conjoint_oops_atomic(const narrowOop* from, narrowOop* to, size_t count) {162assert(sizeof(narrowOop) == sizeof(jint), "this cast is wrong");163assert_params_ok(from, to, BytesPerInt);164pd_conjoint_jints_atomic((const jint*)from, (jint*)to, count);165}166167// Copy a span of memory. If the span is an integral number of aligned168// longs, words, or ints, copy those units atomically.169// The largest atomic transfer unit is 8 bytes, or the largest power170// of two which divides all of from, to, and size, whichever is smaller.171static void conjoint_memory_atomic(const void* from, void* to, size_t size);172173// bytes, conjoint array, atomic on each byte (not that it matters)174static void arrayof_conjoint_jbytes(const HeapWord* from, HeapWord* to, size_t count) {175pd_arrayof_conjoint_bytes(from, to, count);176}177178// jshorts, conjoint array, atomic on each jshort179static void arrayof_conjoint_jshorts(const HeapWord* from, HeapWord* to, size_t count) {180assert_params_ok(from, to, BytesPerShort);181pd_arrayof_conjoint_jshorts(from, to, count);182}183184// jints, conjoint array, atomic on each jint185static void arrayof_conjoint_jints(const HeapWord* from, HeapWord* to, size_t count) {186assert_params_ok(from, to, BytesPerInt);187pd_arrayof_conjoint_jints(from, to, count);188}189190// jlongs, conjoint array, atomic on each jlong191static void arrayof_conjoint_jlongs(const HeapWord* from, HeapWord* to, size_t count) {192assert_params_ok(from, to, BytesPerLong);193pd_arrayof_conjoint_jlongs(from, to, count);194}195196// oops, conjoint array, atomic on each oop197static void arrayof_conjoint_oops(const HeapWord* from, HeapWord* to, size_t count) {198assert_params_ok(from, to, BytesPerHeapOop);199pd_arrayof_conjoint_oops(from, to, count);200}201202// Known overlap methods203204// Copy word-aligned words from higher to lower addresses, not atomic on each word205inline static void conjoint_words_to_lower(const HeapWord* from, HeapWord* to, size_t byte_count) {206// byte_count is in bytes to check its alignment207assert_params_ok(from, to, HeapWordSize);208assert_byte_count_ok(byte_count, HeapWordSize);209210size_t count = align_up(byte_count, HeapWordSize) >> LogHeapWordSize;211assert(to <= from || from + count <= to, "do not overwrite source data");212213while (count-- > 0) {214*to++ = *from++;215}216}217218// Copy word-aligned words from lower to higher addresses, not atomic on each word219inline static void conjoint_words_to_higher(const HeapWord* from, HeapWord* to, size_t byte_count) {220// byte_count is in bytes to check its alignment221assert_params_ok(from, to, HeapWordSize);222assert_byte_count_ok(byte_count, HeapWordSize);223224size_t count = align_up(byte_count, HeapWordSize) >> LogHeapWordSize;225assert(from <= to || to + count <= from, "do not overwrite source data");226227from += count - 1;228to += count - 1;229while (count-- > 0) {230*to-- = *from--;231}232}233234/**235* Copy elements236*237* @param src address of source238* @param dst address of destination239* @param byte_count number of bytes to copy240* @param elem_size size of the elements to copy-swap241*/242static void conjoint_copy(const void* src, void* dst, size_t byte_count, size_t elem_size);243244/**245* Copy and *unconditionally* byte swap elements246*247* @param src address of source248* @param dst address of destination249* @param byte_count number of bytes to copy250* @param elem_size size of the elements to copy-swap251*/252static void conjoint_swap(const void* src, void* dst, size_t byte_count, size_t elem_size);253254/**255* Copy and byte swap elements from the specified endian to the native (cpu) endian if needed (if they differ)256*257* @param src address of source258* @param dst address of destination259* @param byte_count number of bytes to copy260* @param elem_size size of the elements to copy-swap261*/262template <Endian::Order endian>263static void conjoint_swap_if_needed(const void* src, void* dst, size_t byte_count, size_t elem_size) {264if (Endian::NATIVE != endian) {265conjoint_swap(src, dst, byte_count, elem_size);266} else {267conjoint_copy(src, dst, byte_count, elem_size);268}269}270271// Fill methods272273// Fill word-aligned words, not atomic on each word274// set_words275static void fill_to_words(HeapWord* to, size_t count, juint value = 0) {276assert_params_ok(to, HeapWordSize);277pd_fill_to_words(to, count, value);278}279280static void fill_to_aligned_words(HeapWord* to, size_t count, juint value = 0) {281assert_params_aligned(to);282pd_fill_to_aligned_words(to, count, value);283}284285// Fill bytes286static void fill_to_bytes(void* to, size_t count, jubyte value = 0) {287pd_fill_to_bytes(to, count, value);288}289290// Fill a span of memory. If the span is an integral number of aligned291// longs, words, or ints, store to those units atomically.292// The largest atomic transfer unit is 8 bytes, or the largest power293// of two which divides both to and size, whichever is smaller.294static void fill_to_memory_atomic(void* to, size_t size, jubyte value = 0);295296// Zero-fill methods297298// Zero word-aligned words, not atomic on each word299static void zero_to_words(HeapWord* to, size_t count) {300assert_params_ok(to, HeapWordSize);301pd_zero_to_words(to, count);302}303304// Zero bytes305static void zero_to_bytes(void* to, size_t count) {306pd_zero_to_bytes(to, count);307}308309private:310static bool params_disjoint(const HeapWord* from, HeapWord* to, size_t count) {311if (from < to) {312return pointer_delta(to, from) >= count;313}314return pointer_delta(from, to) >= count;315}316317// These methods raise a fatal if they detect a problem.318319static void assert_disjoint(const HeapWord* from, HeapWord* to, size_t count) {320assert(params_disjoint(from, to, count), "source and dest overlap");321}322323static void assert_params_ok(const void* from, void* to, intptr_t alignment) {324assert(is_aligned(from, alignment), "must be aligned: " INTPTR_FORMAT, p2i(from));325assert(is_aligned(to, alignment), "must be aligned: " INTPTR_FORMAT, p2i(to));326}327328static void assert_params_ok(HeapWord* to, intptr_t alignment) {329assert(is_aligned(to, alignment), "must be aligned: " INTPTR_FORMAT, p2i(to));330}331332static void assert_params_aligned(const HeapWord* from, HeapWord* to) {333assert(is_aligned(from, BytesPerLong), "must be aligned: " INTPTR_FORMAT, p2i(from));334assert(is_aligned(to, BytesPerLong), "must be aligned: " INTPTR_FORMAT, p2i(to));335}336337static void assert_params_aligned(HeapWord* to) {338assert(is_aligned(to, BytesPerLong), "must be aligned: " INTPTR_FORMAT, p2i(to));339}340341static void assert_byte_count_ok(size_t byte_count, size_t unit_size) {342assert(is_aligned(byte_count, unit_size), "byte count must be aligned");343}344345// Platform dependent implementations of the above methods.346#include CPU_HEADER(copy)347348};349350#endif // SHARE_UTILITIES_COPY_HPP351352353