1 /*
   2  * Copyright (c) 2003, 2008, 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  */
  24 
  25 // Assembly code for platforms that need it.
  26 extern "C" {
  27   void _Copy_conjoint_words(HeapWord* from, HeapWord* to, size_t count);
  28   void _Copy_disjoint_words(HeapWord* from, HeapWord* to, size_t count);
  29 
  30   void _Copy_conjoint_words_atomic(HeapWord* from, HeapWord* to, size_t count);
  31   void _Copy_disjoint_words_atomic(HeapWord* from, HeapWord* to, size_t count);
  32 
  33   void _Copy_aligned_conjoint_words(HeapWord* from, HeapWord* to, size_t count);
  34   void _Copy_aligned_disjoint_words(HeapWord* from, HeapWord* to, size_t count);
  35 
  36   void _Copy_conjoint_bytes(void* from, void* to, size_t count);
  37 
  38   void _Copy_conjoint_bytes_atomic  (void*   from, void*   to, size_t count);
  39   void _Copy_conjoint_jshorts_atomic(jshort* from, jshort* to, size_t count);
  40   void _Copy_conjoint_jints_atomic  (jint*   from, jint*   to, size_t count);
  41   void _Copy_conjoint_jlongs_atomic (jlong*  from, jlong*  to, size_t count);
  42   void _Copy_conjoint_oops_atomic   (oop*    from, oop*    to, size_t count);
  43 
  44   void _Copy_arrayof_conjoint_bytes  (HeapWord* from, HeapWord* to, size_t count);
  45   void _Copy_arrayof_conjoint_jshorts(HeapWord* from, HeapWord* to, size_t count);
  46   void _Copy_arrayof_conjoint_jints  (HeapWord* from, HeapWord* to, size_t count);
  47   void _Copy_arrayof_conjoint_jlongs (HeapWord* from, HeapWord* to, size_t count);
  48   void _Copy_arrayof_conjoint_oops   (HeapWord* from, HeapWord* to, size_t count);
  49 }
  50 
  51 class Copy : AllStatic {
  52  public:
  53   // Block copy methods have four attributes.  We don't define all possibilities.
  54   //   alignment: aligned to BytesPerLong
  55   //   arrayof:   arraycopy operation with both operands aligned on the same
  56   //              boundary as the first element of an array of the copy unit.
  57   //              This is currently a HeapWord boundary on all platforms, except
  58   //              for long and double arrays, which are aligned on an 8-byte
  59   //              boundary on all platforms.
  60   //              arraycopy operations are implicitly atomic on each array element.
  61   //   overlap:   disjoint or conjoint.
  62   //   copy unit: bytes or words (i.e., HeapWords) or oops (i.e., pointers).
  63   //   atomicity: atomic or non-atomic on the copy unit.
  64   //
  65   // Names are constructed thusly:
  66   //
  67   //     [ 'aligned_' | 'arrayof_' ]
  68   //     ('conjoint_' | 'disjoint_')
  69   //     ('words' | 'bytes' | 'jshorts' | 'jints' | 'jlongs' | 'oops')
  70   //     [ '_atomic' ]
  71   //
  72   // Except in the arrayof case, whatever the alignment is, we assume we can copy
  73   // whole alignment units.  E.g., if BytesPerLong is 2x word alignment, an odd
  74   // count may copy an extra word.  In the arrayof case, we are allowed to copy
  75   // only the number of copy units specified.
  76 
  77   // HeapWords
  78 
  79   // Word-aligned words,    conjoint, not atomic on each word
  80   static void conjoint_words(HeapWord* from, HeapWord* to, size_t count) {
  81     assert_params_ok(from, to, LogHeapWordSize);
  82     pd_conjoint_words(from, to, count);
  83   }
  84 
  85   // Word-aligned words,    disjoint, not atomic on each word
  86   static void disjoint_words(HeapWord* from, HeapWord* to, size_t count) {
  87     assert_params_ok(from, to, LogHeapWordSize);
  88     assert_disjoint(from, to, count);
  89     pd_disjoint_words(from, to, count);
  90   }
  91 
  92   // Word-aligned words,    disjoint, atomic on each word
  93   static void disjoint_words_atomic(HeapWord* from, HeapWord* to, size_t count) {
  94     assert_params_ok(from, to, LogHeapWordSize);
  95     assert_disjoint(from, to, count);
  96     pd_disjoint_words_atomic(from, to, count);
  97   }
  98 
  99   // Object-aligned words,  conjoint, not atomic on each word
 100   static void aligned_conjoint_words(HeapWord* from, HeapWord* to, size_t count) {
 101     assert_params_aligned(from, to);
 102     assert_non_zero(count);
 103     pd_aligned_conjoint_words(from, to, count);
 104   }
 105 
 106   // Object-aligned words,  disjoint, not atomic on each word
 107   static void aligned_disjoint_words(HeapWord* from, HeapWord* to, size_t count) {
 108     assert_params_aligned(from, to);
 109     assert_disjoint(from, to, count);
 110     assert_non_zero(count);
 111     pd_aligned_disjoint_words(from, to, count);
 112   }
 113 
 114   // bytes, jshorts, jints, jlongs, oops
 115 
 116   // bytes,                 conjoint, not atomic on each byte (not that it matters)
 117   static void conjoint_bytes(void* from, void* to, size_t count) {
 118     assert_non_zero(count);
 119     pd_conjoint_bytes(from, to, count);
 120   }
 121 
 122   // bytes,                 conjoint, atomic on each byte (not that it matters)
 123   static void conjoint_bytes_atomic(void* from, void* to, size_t count) {
 124     assert_non_zero(count);
 125     pd_conjoint_bytes(from, to, count);
 126   }
 127 
 128   // jshorts,               conjoint, atomic on each jshort
 129   static void conjoint_jshorts_atomic(jshort* from, jshort* to, size_t count) {
 130     assert_params_ok(from, to, LogBytesPerShort);
 131     assert_non_zero(count);
 132     pd_conjoint_jshorts_atomic(from, to, count);
 133   }
 134 
 135   // jints,                 conjoint, atomic on each jint
 136   static void conjoint_jints_atomic(jint* from, jint* to, size_t count) {
 137     assert_params_ok(from, to, LogBytesPerInt);
 138     assert_non_zero(count);
 139     pd_conjoint_jints_atomic(from, to, count);
 140   }
 141 
 142   // jlongs,                conjoint, atomic on each jlong
 143   static void conjoint_jlongs_atomic(jlong* from, jlong* to, size_t count) {
 144     assert_params_ok(from, to, LogBytesPerLong);
 145     assert_non_zero(count);
 146     pd_conjoint_jlongs_atomic(from, to, count);
 147   }
 148 
 149   // oops,                  conjoint, atomic on each oop
 150   static void conjoint_oops_atomic(oop* from, oop* to, size_t count) {
 151     assert_params_ok(from, to, LogBytesPerHeapOop);
 152     assert_non_zero(count);
 153     pd_conjoint_oops_atomic(from, to, count);
 154   }
 155 
 156   // overloaded for UseCompressedOops
 157   static void conjoint_oops_atomic(narrowOop* from, narrowOop* to, size_t count) {
 158     assert(sizeof(narrowOop) == sizeof(jint), "this cast is wrong");
 159     assert_params_ok(from, to, LogBytesPerInt);
 160     assert_non_zero(count);
 161     pd_conjoint_jints_atomic((jint*)from, (jint*)to, count);
 162   }
 163 
 164   // Copy a span of memory.  If the span is an integral number of aligned
 165   // longs, words, or ints, copy those units atomically.
 166   // The largest atomic transfer unit is 8 bytes, or the largest power
 167   // of two which divides all of from, to, and size, whichever is smaller.
 168   static void conjoint_memory_atomic(void* from, void* to, size_t size);
 169 
 170   // bytes,                 conjoint array, atomic on each byte (not that it matters)
 171   static void arrayof_conjoint_bytes(HeapWord* from, HeapWord* to, size_t count) {
 172     assert_non_zero(count);
 173     pd_arrayof_conjoint_bytes(from, to, count);
 174   }
 175 
 176   // jshorts,               conjoint array, atomic on each jshort
 177   static void arrayof_conjoint_jshorts(HeapWord* from, HeapWord* to, size_t count) {
 178     assert_params_ok(from, to, LogBytesPerShort);
 179     assert_non_zero(count);
 180     pd_arrayof_conjoint_jshorts(from, to, count);
 181   }
 182 
 183   // jints,                 conjoint array, atomic on each jint
 184   static void arrayof_conjoint_jints(HeapWord* from, HeapWord* to, size_t count) {
 185     assert_params_ok(from, to, LogBytesPerInt);
 186     assert_non_zero(count);
 187     pd_arrayof_conjoint_jints(from, to, count);
 188   }
 189 
 190   // jlongs,                conjoint array, atomic on each jlong
 191   static void arrayof_conjoint_jlongs(HeapWord* from, HeapWord* to, size_t count) {
 192     assert_params_ok(from, to, LogBytesPerLong);
 193     assert_non_zero(count);
 194     pd_arrayof_conjoint_jlongs(from, to, count);
 195   }
 196 
 197   // oops,                  conjoint array, atomic on each oop
 198   static void arrayof_conjoint_oops(HeapWord* from, HeapWord* to, size_t count) {
 199     assert_params_ok(from, to, LogBytesPerHeapOop);
 200     assert_non_zero(count);
 201     pd_arrayof_conjoint_oops(from, to, count);
 202   }
 203 
 204   // Known overlap methods
 205 
 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);
 211 
 212     size_t count = (size_t)round_to(byte_count, HeapWordSize) >> LogHeapWordSize;
 213     assert(to <= from || from + count <= to, "do not overwrite source data");
 214 
 215     while (count-- > 0) {
 216       *to++ = *from++;
 217     }
 218   }
 219 
 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);
 225 
 226     size_t count = (size_t)round_to(byte_count, HeapWordSize) >> LogHeapWordSize;
 227     assert(from <= to || to + count <= from, "do not overwrite source data");
 228 
 229     from += count - 1;
 230     to   += count - 1;
 231     while (count-- > 0) {
 232       *to-- = *from--;
 233     }
 234   }
 235 
 236   // Fill methods
 237 
 238   // Fill word-aligned words, not atomic on each word
 239   // set_words
 240   static void fill_to_words(HeapWord* to, size_t count, juint value = 0) {
 241     assert_params_ok(to, LogHeapWordSize);
 242     pd_fill_to_words(to, count, value);
 243   }
 244 
 245   static void fill_to_aligned_words(HeapWord* to, size_t count, juint value = 0) {
 246     assert_params_aligned(to);
 247     pd_fill_to_aligned_words(to, count, value);
 248   }
 249 
 250   // Fill bytes
 251   static void fill_to_bytes(void* to, size_t count, jubyte value = 0) {
 252     pd_fill_to_bytes(to, count, value);
 253   }
 254 
 255   // Fill a span of memory.  If the span is an integral number of aligned
 256   // longs, words, or ints, store to those units atomically.
 257   // The largest atomic transfer unit is 8 bytes, or the largest power
 258   // of two which divides both to and size, whichever is smaller.
 259   static void fill_to_memory_atomic(void* to, size_t size, jubyte value = 0);
 260 
 261   // Zero-fill methods
 262 
 263   // Zero word-aligned words, not atomic on each word
 264   static void zero_to_words(HeapWord* to, size_t count) {
 265     assert_params_ok(to, LogHeapWordSize);
 266     pd_zero_to_words(to, count);
 267   }
 268 
 269   // Zero bytes
 270   static void zero_to_bytes(void* to, size_t count) {
 271     pd_zero_to_bytes(to, count);
 272   }
 273 
 274  private:
 275   static bool params_disjoint(HeapWord* from, HeapWord* to, size_t count) {
 276     if (from < to) {
 277       return pointer_delta(to, from) >= count;
 278     }
 279     return pointer_delta(from, to) >= count;
 280   }
 281 
 282   // These methods raise a fatal if they detect a problem.
 283 
 284   static void assert_disjoint(HeapWord* from, HeapWord* to, size_t count) {
 285 #ifdef ASSERT
 286     if (!params_disjoint(from, to, count))
 287       basic_fatal("source and dest overlap");
 288 #endif
 289   }
 290 
 291   static void assert_params_ok(void* from, void* to, intptr_t log_align) {
 292 #ifdef ASSERT
 293     if (mask_bits((uintptr_t)from, right_n_bits(log_align)) != 0)
 294       basic_fatal("not aligned");
 295     if (mask_bits((uintptr_t)to, right_n_bits(log_align)) != 0)
 296       basic_fatal("not aligned");
 297 #endif
 298   }
 299 
 300   static void assert_params_ok(HeapWord* to, intptr_t log_align) {
 301 #ifdef ASSERT
 302     if (mask_bits((uintptr_t)to, right_n_bits(log_align)) != 0)
 303       basic_fatal("not word aligned");
 304 #endif
 305   }
 306   static void assert_params_aligned(HeapWord* from, HeapWord* to) {
 307 #ifdef ASSERT
 308     if (mask_bits((uintptr_t)from, BytesPerLong-1) != 0)
 309       basic_fatal("not long aligned");
 310     if (mask_bits((uintptr_t)to, BytesPerLong-1) != 0)
 311       basic_fatal("not long aligned");
 312 #endif
 313   }
 314 
 315   static void assert_params_aligned(HeapWord* to) {
 316 #ifdef ASSERT
 317     if (mask_bits((uintptr_t)to, BytesPerLong-1) != 0)
 318       basic_fatal("not long aligned");
 319 #endif
 320   }
 321 
 322   static void assert_non_zero(size_t count) {
 323 #ifdef ASSERT
 324     if (count == 0) {
 325       basic_fatal("count must be non-zero");
 326     }
 327 #endif
 328   }
 329 
 330   static void assert_byte_count_ok(size_t byte_count, size_t unit_size) {
 331 #ifdef ASSERT
 332     if ((size_t)round_to(byte_count, unit_size) != byte_count) {
 333       basic_fatal("byte count must be aligned");
 334     }
 335 #endif
 336   }
 337 
 338   // Platform dependent implementations of the above methods.
 339   #include "incls/_copy_pd.hpp.incl"
 340 };