src/share/vm/utilities/copy.hpp

Thu, 04 Apr 2019 17:56:29 +0800

author
aoqi
date
Thu, 04 Apr 2019 17:56:29 +0800
changeset 9572
624a0741915c
parent 9562
dee6a1ce4a0c
parent 6876
710a3c8b516e
permissions
-rw-r--r--

Merge

     1 /*
     2  * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  *
    23  */
    25 /*
    26  * This file has been modified by Loongson Technology in 2015. These
    27  * modifications are Copyright (c) 2015 Loongson Technology, and are made
    28  * available on the same license terms set forth above.
    29  */
    31 #ifndef SHARE_VM_UTILITIES_COPY_HPP
    32 #define SHARE_VM_UTILITIES_COPY_HPP
    34 #include "runtime/stubRoutines.hpp"
    36 // Assembly code for platforms that need it.
    37 extern "C" {
    38   void _Copy_conjoint_words(HeapWord* from, HeapWord* to, size_t count);
    39   void _Copy_disjoint_words(HeapWord* from, HeapWord* to, size_t count);
    41   void _Copy_conjoint_words_atomic(HeapWord* from, HeapWord* to, size_t count);
    42   void _Copy_disjoint_words_atomic(HeapWord* from, HeapWord* to, size_t count);
    44   void _Copy_aligned_conjoint_words(HeapWord* from, HeapWord* to, size_t count);
    45   void _Copy_aligned_disjoint_words(HeapWord* from, HeapWord* to, size_t count);
    47   void _Copy_conjoint_bytes(void* from, void* to, size_t count);
    49   void _Copy_conjoint_bytes_atomic  (void*   from, void*   to, size_t count);
    50   void _Copy_conjoint_jshorts_atomic(jshort* from, jshort* to, size_t count);
    51   void _Copy_conjoint_jints_atomic  (jint*   from, jint*   to, size_t count);
    52   void _Copy_conjoint_jlongs_atomic (jlong*  from, jlong*  to, size_t count);
    53   void _Copy_conjoint_oops_atomic   (oop*    from, oop*    to, size_t count);
    55   void _Copy_arrayof_conjoint_bytes  (HeapWord* from, HeapWord* to, size_t count);
    56   void _Copy_arrayof_conjoint_jshorts(HeapWord* from, HeapWord* to, size_t count);
    57   void _Copy_arrayof_conjoint_jints  (HeapWord* from, HeapWord* to, size_t count);
    58   void _Copy_arrayof_conjoint_jlongs (HeapWord* from, HeapWord* to, size_t count);
    59   void _Copy_arrayof_conjoint_oops   (HeapWord* from, HeapWord* to, size_t count);
    60 }
    62 class Copy : AllStatic {
    63  public:
    64   // Block copy methods have four attributes.  We don't define all possibilities.
    65   //   alignment: aligned to BytesPerLong
    66   //   arrayof:   arraycopy operation with both operands aligned on the same
    67   //              boundary as the first element of an array of the copy unit.
    68   //              This is currently a HeapWord boundary on all platforms, except
    69   //              for long and double arrays, which are aligned on an 8-byte
    70   //              boundary on all platforms.
    71   //              arraycopy operations are implicitly atomic on each array element.
    72   //   overlap:   disjoint or conjoint.
    73   //   copy unit: bytes or words (i.e., HeapWords) or oops (i.e., pointers).
    74   //   atomicity: atomic or non-atomic on the copy unit.
    75   //
    76   // Names are constructed thusly:
    77   //
    78   //     [ 'aligned_' | 'arrayof_' ]
    79   //     ('conjoint_' | 'disjoint_')
    80   //     ('words' | 'bytes' | 'jshorts' | 'jints' | 'jlongs' | 'oops')
    81   //     [ '_atomic' ]
    82   //
    83   // Except in the arrayof case, whatever the alignment is, we assume we can copy
    84   // whole alignment units.  E.g., if BytesPerLong is 2x word alignment, an odd
    85   // count may copy an extra word.  In the arrayof case, we are allowed to copy
    86   // only the number of copy units specified.
    87   //
    88   // All callees check count for 0.
    89   //
    91   // HeapWords
    93   // Word-aligned words,    conjoint, not atomic on each word
    94   static void conjoint_words(HeapWord* from, HeapWord* to, size_t count) {
    95     assert_params_ok(from, to, LogHeapWordSize);
    96     pd_conjoint_words(from, to, count);
    97   }
    99   // Word-aligned words,    disjoint, not atomic on each word
   100   static void disjoint_words(HeapWord* from, HeapWord* to, size_t count) {
   101     assert_params_ok(from, to, LogHeapWordSize);
   102     assert_disjoint(from, to, count);
   103     pd_disjoint_words(from, to, count);
   104   }
   106   // Word-aligned words,    disjoint, atomic on each word
   107   static void disjoint_words_atomic(HeapWord* from, HeapWord* to, size_t count) {
   108     assert_params_ok(from, to, LogHeapWordSize);
   109     assert_disjoint(from, to, count);
   110     pd_disjoint_words_atomic(from, to, count);
   111   }
   113   // Object-aligned words,  conjoint, not atomic on each word
   114   static void aligned_conjoint_words(HeapWord* from, HeapWord* to, size_t count) {
   115     assert_params_aligned(from, to);
   116     pd_aligned_conjoint_words(from, to, count);
   117   }
   119   // Object-aligned words,  disjoint, not atomic on each word
   120   static void aligned_disjoint_words(HeapWord* from, HeapWord* to, size_t count) {
   121     assert_params_aligned(from, to);
   122     assert_disjoint(from, to, count);
   123     pd_aligned_disjoint_words(from, to, count);
   124   }
   126   // bytes, jshorts, jints, jlongs, oops
   128   // bytes,                 conjoint, not atomic on each byte (not that it matters)
   129   static void conjoint_jbytes(void* from, void* to, size_t count) {
   130     pd_conjoint_bytes(from, to, count);
   131   }
   133   // bytes,                 conjoint, atomic on each byte (not that it matters)
   134   static void conjoint_jbytes_atomic(void* from, void* to, size_t count) {
   135     pd_conjoint_bytes(from, to, count);
   136   }
   138   // jshorts,               conjoint, atomic on each jshort
   139   static void conjoint_jshorts_atomic(jshort* from, jshort* to, size_t count) {
   140     assert_params_ok(from, to, LogBytesPerShort);
   141     pd_conjoint_jshorts_atomic(from, to, count);
   142   }
   144   // jints,                 conjoint, atomic on each jint
   145   static void conjoint_jints_atomic(jint* from, jint* to, size_t count) {
   146     assert_params_ok(from, to, LogBytesPerInt);
   147     pd_conjoint_jints_atomic(from, to, count);
   148   }
   150   // jlongs,                conjoint, atomic on each jlong
   151   static void conjoint_jlongs_atomic(jlong* from, jlong* to, size_t count) {
   152     assert_params_ok(from, to, LogBytesPerLong);
   153     pd_conjoint_jlongs_atomic(from, to, count);
   154   }
   156   // oops,                  conjoint, atomic on each oop
   157   static void conjoint_oops_atomic(oop* from, oop* to, size_t count) {
   158     assert_params_ok(from, to, LogBytesPerHeapOop);
   159     pd_conjoint_oops_atomic(from, to, count);
   160   }
   162   // overloaded for UseCompressedOops
   163   static void conjoint_oops_atomic(narrowOop* from, narrowOop* to, size_t count) {
   164     assert(sizeof(narrowOop) == sizeof(jint), "this cast is wrong");
   165     assert_params_ok(from, to, LogBytesPerInt);
   166     pd_conjoint_jints_atomic((jint*)from, (jint*)to, count);
   167   }
   169   // Copy a span of memory.  If the span is an integral number of aligned
   170   // longs, words, or ints, copy those units atomically.
   171   // The largest atomic transfer unit is 8 bytes, or the largest power
   172   // of two which divides all of from, to, and size, whichever is smaller.
   173   static void conjoint_memory_atomic(void* from, void* to, size_t size);
   175   // bytes,                 conjoint array, atomic on each byte (not that it matters)
   176   static void arrayof_conjoint_jbytes(HeapWord* from, HeapWord* to, size_t count) {
   177     pd_arrayof_conjoint_bytes(from, to, count);
   178   }
   180   // jshorts,               conjoint array, atomic on each jshort
   181   static void arrayof_conjoint_jshorts(HeapWord* from, HeapWord* to, size_t count) {
   182     assert_params_ok(from, to, LogBytesPerShort);
   183     pd_arrayof_conjoint_jshorts(from, to, count);
   184   }
   186   // jints,                 conjoint array, atomic on each jint
   187   static void arrayof_conjoint_jints(HeapWord* from, HeapWord* to, size_t count) {
   188     assert_params_ok(from, to, LogBytesPerInt);
   189     pd_arrayof_conjoint_jints(from, to, count);
   190   }
   192   // jlongs,                conjoint array, atomic on each jlong
   193   static void arrayof_conjoint_jlongs(HeapWord* from, HeapWord* to, size_t count) {
   194     assert_params_ok(from, to, LogBytesPerLong);
   195     pd_arrayof_conjoint_jlongs(from, to, count);
   196   }
   198   // oops,                  conjoint array, atomic on each oop
   199   static void arrayof_conjoint_oops(HeapWord* from, HeapWord* to, size_t count) {
   200     assert_params_ok(from, to, LogBytesPerHeapOop);
   201     pd_arrayof_conjoint_oops(from, to, count);
   202   }
   204   // Known overlap methods
   206   // Copy word-aligned words from higher to lower addresses, not atomic on each word
   207   inline static void conjoint_words_to_lower(HeapWord* from, HeapWord* to, size_t byte_count) {
   208     // byte_count is in bytes to check its alignment
   209     assert_params_ok(from, to, LogHeapWordSize);
   210     assert_byte_count_ok(byte_count, HeapWordSize);
   212     size_t count = (size_t)round_to(byte_count, HeapWordSize) >> LogHeapWordSize;
   213     assert(to <= from || from + count <= to, "do not overwrite source data");
   215     while (count-- > 0) {
   216       *to++ = *from++;
   217     }
   218   }
   220   // Copy word-aligned words from lower to higher addresses, not atomic on each word
   221   inline static void conjoint_words_to_higher(HeapWord* from, HeapWord* to, size_t byte_count) {
   222     // byte_count is in bytes to check its alignment
   223     assert_params_ok(from, to, LogHeapWordSize);
   224     assert_byte_count_ok(byte_count, HeapWordSize);
   226     size_t count = (size_t)round_to(byte_count, HeapWordSize) >> LogHeapWordSize;
   227     assert(from <= to || to + count <= from, "do not overwrite source data");
   229     from += count - 1;
   230     to   += count - 1;
   231     while (count-- > 0) {
   232       *to-- = *from--;
   233     }
   234   }
   236   /**
   237    * Copy and *unconditionally* byte swap elements
   238    *
   239    * @param src address of source
   240    * @param dst address of destination
   241    * @param byte_count number of bytes to copy
   242    * @param elem_size size of the elements to copy-swap
   243    */
   244   static void conjoint_swap(address src, address dst, size_t byte_count, size_t elem_size);
   246   // Fill methods
   248   // Fill word-aligned words, not atomic on each word
   249   // set_words
   250   static void fill_to_words(HeapWord* to, size_t count, juint value = 0) {
   251     assert_params_ok(to, LogHeapWordSize);
   252     pd_fill_to_words(to, count, value);
   253   }
   255   static void fill_to_aligned_words(HeapWord* to, size_t count, juint value = 0) {
   256     assert_params_aligned(to);
   257     pd_fill_to_aligned_words(to, count, value);
   258   }
   260   // Fill bytes
   261   static void fill_to_bytes(void* to, size_t count, jubyte value = 0) {
   262     pd_fill_to_bytes(to, count, value);
   263   }
   265   // Fill a span of memory.  If the span is an integral number of aligned
   266   // longs, words, or ints, store to those units atomically.
   267   // The largest atomic transfer unit is 8 bytes, or the largest power
   268   // of two which divides both to and size, whichever is smaller.
   269   static void fill_to_memory_atomic(void* to, size_t size, jubyte value = 0);
   271   // Zero-fill methods
   273   // Zero word-aligned words, not atomic on each word
   274   static void zero_to_words(HeapWord* to, size_t count) {
   275     assert_params_ok(to, LogHeapWordSize);
   276     pd_zero_to_words(to, count);
   277   }
   279   // Zero bytes
   280   static void zero_to_bytes(void* to, size_t count) {
   281     pd_zero_to_bytes(to, count);
   282   }
   284  private:
   285   static bool params_disjoint(HeapWord* from, HeapWord* to, size_t count) {
   286     if (from < to) {
   287       return pointer_delta(to, from) >= count;
   288     }
   289     return pointer_delta(from, to) >= count;
   290   }
   292   // These methods raise a fatal if they detect a problem.
   294   static void assert_disjoint(HeapWord* from, HeapWord* to, size_t count) {
   295 #ifdef ASSERT
   296     if (!params_disjoint(from, to, count))
   297       basic_fatal("source and dest overlap");
   298 #endif
   299   }
   301   static void assert_params_ok(void* from, void* to, intptr_t log_align) {
   302 #ifdef ASSERT
   303     if (mask_bits((uintptr_t)from, right_n_bits(log_align)) != 0)
   304       basic_fatal("not aligned");
   305     if (mask_bits((uintptr_t)to, right_n_bits(log_align)) != 0)
   306       basic_fatal("not aligned");
   307 #endif
   308   }
   310   static void assert_params_ok(HeapWord* to, intptr_t log_align) {
   311 #ifdef ASSERT
   312     if (mask_bits((uintptr_t)to, right_n_bits(log_align)) != 0)
   313       basic_fatal("not word aligned");
   314 #endif
   315   }
   316   static void assert_params_aligned(HeapWord* from, HeapWord* to) {
   317 #ifdef ASSERT
   318     if (mask_bits((uintptr_t)from, BytesPerLong-1) != 0)
   319       basic_fatal("not long aligned");
   320     if (mask_bits((uintptr_t)to, BytesPerLong-1) != 0)
   321       basic_fatal("not long aligned");
   322 #endif
   323   }
   325   static void assert_params_aligned(HeapWord* to) {
   326 #ifdef ASSERT
   327     if (mask_bits((uintptr_t)to, BytesPerLong-1) != 0)
   328       basic_fatal("not long aligned");
   329 #endif
   330   }
   332   static void assert_byte_count_ok(size_t byte_count, size_t unit_size) {
   333 #ifdef ASSERT
   334     if ((size_t)round_to(byte_count, unit_size) != byte_count) {
   335       basic_fatal("byte count must be aligned");
   336     }
   337 #endif
   338   }
   341  // SAPJVM AS 2011-09-20. Template for atomic copy.
   342   template <class T> static void copy_conjoint_atomic(T* from, T* to, size_t count)
   343   {
   344     if (from > to) {
   345       while (count-- > 0) {
   346         // Copy forwards
   347         *to++ = *from++;
   348       }
   349     } else {
   350       from += count - 1;
   351       to   += count - 1;
   352       while (count-- > 0) {
   353         // Copy backwards
   354         *to-- = *from--;
   355       }
   356     }
   357   }
   361   // Platform dependent implementations of the above methods.
   362 #ifdef TARGET_ARCH_x86
   363 # include "copy_x86.hpp"
   364 #endif
   365 #ifdef TARGET_ARCH_sparc
   366 # include "copy_sparc.hpp"
   367 #endif
   368 #ifdef TARGET_ARCH_zero
   369 # include "copy_zero.hpp"
   370 #endif
   371 #ifdef TARGET_ARCH_arm
   372 # include "copy_arm.hpp"
   373 #endif
   374 #ifdef TARGET_ARCH_ppc
   375 # include "copy_ppc.hpp"
   376 #endif
   377 #ifdef TARGET_ARCH_mips
   378 # include "copy_mips.hpp"
   379 #endif
   382 };
   384 #endif // SHARE_VM_UTILITIES_COPY_HPP

mercurial