Path: blob/master/src/hotspot/share/utilities/copy.cpp
40949 views
/*1* Copyright (c) 2006, 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#include "precompiled.hpp"25#include "utilities/copy.hpp"26#include "runtime/sharedRuntime.hpp"27#include "utilities/align.hpp"28#include "utilities/copy.hpp"293031// Copy bytes; larger units are filled atomically if everything is aligned.32void Copy::conjoint_memory_atomic(const void* from, void* to, size_t size) {33uintptr_t bits = (uintptr_t) from | (uintptr_t) to | (uintptr_t) size;3435// (Note: We could improve performance by ignoring the low bits of size,36// and putting a short cleanup loop after each bulk copy loop.37// There are plenty of other ways to make this faster also,38// and it's a slippery slope. For now, let's keep this code simple39// since the simplicity helps clarify the atomicity semantics of40// this operation. There are also CPU-specific assembly versions41// which may or may not want to include such optimizations.)4243if (bits % sizeof(jlong) == 0) {44Copy::conjoint_jlongs_atomic((const jlong*) from, (jlong*) to, size / sizeof(jlong));45} else if (bits % sizeof(jint) == 0) {46Copy::conjoint_jints_atomic((const jint*) from, (jint*) to, size / sizeof(jint));47} else if (bits % sizeof(jshort) == 0) {48Copy::conjoint_jshorts_atomic((const jshort*) from, (jshort*) to, size / sizeof(jshort));49} else {50// Not aligned, so no need to be atomic.51Copy::conjoint_jbytes((const void*) from, (void*) to, size);52}53}5455class CopySwap : AllStatic {56public:57/**58* Copy and optionally byte swap elements59*60* <swap> - true if elements should be byte swapped61*62* @param src address of source63* @param dst address of destination64* @param byte_count number of bytes to copy65* @param elem_size size of the elements to copy-swap66*/67template<bool swap>68static void conjoint_swap_if_needed(const void* src, void* dst, size_t byte_count, size_t elem_size) {69assert(src != NULL, "address must not be NULL");70assert(dst != NULL, "address must not be NULL");71assert(elem_size == 2 || elem_size == 4 || elem_size == 8,72"incorrect element size: " SIZE_FORMAT, elem_size);73assert(is_aligned(byte_count, elem_size),74"byte_count " SIZE_FORMAT " must be multiple of element size " SIZE_FORMAT, byte_count, elem_size);7576address src_end = (address)src + byte_count;7778if (dst <= src || dst >= src_end) {79do_conjoint_swap<RIGHT,swap>(src, dst, byte_count, elem_size);80} else {81do_conjoint_swap<LEFT,swap>(src, dst, byte_count, elem_size);82}83}8485private:86/**87* Byte swap a 16-bit value88*/89static uint16_t byte_swap(uint16_t x) {90return (x << 8) | (x >> 8);91}9293/**94* Byte swap a 32-bit value95*/96static uint32_t byte_swap(uint32_t x) {97uint16_t lo = (uint16_t)x;98uint16_t hi = (uint16_t)(x >> 16);99100return ((uint32_t)byte_swap(lo) << 16) | (uint32_t)byte_swap(hi);101}102103/**104* Byte swap a 64-bit value105*/106static uint64_t byte_swap(uint64_t x) {107uint32_t lo = (uint32_t)x;108uint32_t hi = (uint32_t)(x >> 32);109110return ((uint64_t)byte_swap(lo) << 32) | (uint64_t)byte_swap(hi);111}112113enum CopyDirection {114RIGHT, // lower -> higher address115LEFT // higher -> lower address116};117118/**119* Copy and byte swap elements120*121* <T> - type of element to copy122* <D> - copy direction123* <is_src_aligned> - true if src argument is aligned to element size124* <is_dst_aligned> - true if dst argument is aligned to element size125*126* @param src address of source127* @param dst address of destination128* @param byte_count number of bytes to copy129*/130template <typename T, CopyDirection D, bool swap, bool is_src_aligned, bool is_dst_aligned>131static void do_conjoint_swap(const void* src, void* dst, size_t byte_count) {132const char* cur_src;133char* cur_dst;134135switch (D) {136case RIGHT:137cur_src = (const char*)src;138cur_dst = (char*)dst;139break;140case LEFT:141cur_src = (const char*)src + byte_count - sizeof(T);142cur_dst = (char*)dst + byte_count - sizeof(T);143break;144}145146for (size_t i = 0; i < byte_count / sizeof(T); i++) {147T tmp;148149if (is_src_aligned) {150tmp = *(T*)cur_src;151} else {152memcpy(&tmp, cur_src, sizeof(T));153}154155if (swap) {156tmp = byte_swap(tmp);157}158159if (is_dst_aligned) {160*(T*)cur_dst = tmp;161} else {162memcpy(cur_dst, &tmp, sizeof(T));163}164165switch (D) {166case RIGHT:167cur_src += sizeof(T);168cur_dst += sizeof(T);169break;170case LEFT:171cur_src -= sizeof(T);172cur_dst -= sizeof(T);173break;174}175}176}177178/**179* Copy and byte swap elements180*181* <T> - type of element to copy182* <D> - copy direction183* <swap> - true if elements should be byte swapped184*185* @param src address of source186* @param dst address of destination187* @param byte_count number of bytes to copy188*/189template <typename T, CopyDirection direction, bool swap>190static void do_conjoint_swap(const void* src, void* dst, size_t byte_count) {191if (is_aligned(src, sizeof(T))) {192if (is_aligned(dst, sizeof(T))) {193do_conjoint_swap<T,direction,swap,true,true>(src, dst, byte_count);194} else {195do_conjoint_swap<T,direction,swap,true,false>(src, dst, byte_count);196}197} else {198if (is_aligned(dst, sizeof(T))) {199do_conjoint_swap<T,direction,swap,false,true>(src, dst, byte_count);200} else {201do_conjoint_swap<T,direction,swap,false,false>(src, dst, byte_count);202}203}204}205206207/**208* Copy and byte swap elements209*210* <D> - copy direction211* <swap> - true if elements should be byte swapped212*213* @param src address of source214* @param dst address of destination215* @param byte_count number of bytes to copy216* @param elem_size size of the elements to copy-swap217*/218template <CopyDirection D, bool swap>219static void do_conjoint_swap(const void* src, void* dst, size_t byte_count, size_t elem_size) {220switch (elem_size) {221case 2: do_conjoint_swap<uint16_t,D,swap>(src, dst, byte_count); break;222case 4: do_conjoint_swap<uint32_t,D,swap>(src, dst, byte_count); break;223case 8: do_conjoint_swap<uint64_t,D,swap>(src, dst, byte_count); break;224default: guarantee(false, "do_conjoint_swap: Invalid elem_size " SIZE_FORMAT "\n", elem_size);225}226}227};228229void Copy::conjoint_copy(const void* src, void* dst, size_t byte_count, size_t elem_size) {230CopySwap::conjoint_swap_if_needed<false>(src, dst, byte_count, elem_size);231}232233void Copy::conjoint_swap(const void* src, void* dst, size_t byte_count, size_t elem_size) {234CopySwap::conjoint_swap_if_needed<true>(src, dst, byte_count, elem_size);235}236237// Fill bytes; larger units are filled atomically if everything is aligned.238void Copy::fill_to_memory_atomic(void* to, size_t size, jubyte value) {239address dst = (address) to;240uintptr_t bits = (uintptr_t) to | (uintptr_t) size;241if (bits % sizeof(jlong) == 0) {242jlong fill = (julong)( (jubyte)value ); // zero-extend243if (fill != 0) {244fill += fill << 8;245fill += fill << 16;246fill += fill << 32;247}248//Copy::fill_to_jlongs_atomic((jlong*) dst, size / sizeof(jlong));249for (uintptr_t off = 0; off < size; off += sizeof(jlong)) {250*(jlong*)(dst + off) = fill;251}252} else if (bits % sizeof(jint) == 0) {253jint fill = (juint)( (jubyte)value ); // zero-extend254if (fill != 0) {255fill += fill << 8;256fill += fill << 16;257}258//Copy::fill_to_jints_atomic((jint*) dst, size / sizeof(jint));259for (uintptr_t off = 0; off < size; off += sizeof(jint)) {260*(jint*)(dst + off) = fill;261}262} else if (bits % sizeof(jshort) == 0) {263jshort fill = (jushort)( (jubyte)value ); // zero-extend264fill += fill << 8;265//Copy::fill_to_jshorts_atomic((jshort*) dst, size / sizeof(jshort));266for (uintptr_t off = 0; off < size; off += sizeof(jshort)) {267*(jshort*)(dst + off) = fill;268}269} else {270// Not aligned, so no need to be atomic.271Copy::fill_to_bytes(dst, size, value);272}273}274275276