1 /*
  2  * Copyright (c) 2003, 2025, 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 #ifndef SHARE_UTILITIES_COPY_HPP
 26 #define SHARE_UTILITIES_COPY_HPP
 27 
 28 #include "oops/oopsHierarchy.hpp"
 29 #include "runtime/atomicAccess.hpp"
 30 #include "runtime/globals.hpp"
 31 #include "utilities/align.hpp"
 32 #include "utilities/bytes.hpp"
 33 #include "utilities/debug.hpp"
 34 #include "utilities/macros.hpp"
 35 
 36 // Assembly code for platforms that need it.
 37 extern "C" {
 38   void _Copy_conjoint_words(const HeapWord* from, HeapWord* to, size_t count);
 39   void _Copy_disjoint_words(const HeapWord* from, HeapWord* to, size_t count);
 40 
 41   void _Copy_conjoint_jshorts_atomic(const jshort* from, jshort* to, size_t count);
 42   void _Copy_conjoint_jints_atomic  (const jint*   from, jint*   to, size_t count);
 43   void _Copy_conjoint_jlongs_atomic (const jlong*  from, jlong*  to, size_t count);
 44 
 45   void _Copy_arrayof_conjoint_bytes  (const HeapWord* from, HeapWord* to, size_t count);
 46   void _Copy_arrayof_conjoint_jshorts(const HeapWord* from, HeapWord* to, size_t count);
 47   void _Copy_arrayof_conjoint_jints  (const HeapWord* from, HeapWord* to, size_t count);
 48   void _Copy_arrayof_conjoint_jlongs (const 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   // All callees check count for 0.
 78   //
 79 
 80   // HeapWords
 81 
 82   // Word-aligned words,    conjoint, not atomic on each word
 83   static void conjoint_words(const HeapWord* from, HeapWord* to, size_t count) {
 84     assert_params_ok(from, to, HeapWordSize);
 85     pd_conjoint_words(from, to, count);
 86   }
 87 
 88   // Word-aligned words,    disjoint, not atomic on each word
 89   static void disjoint_words(const HeapWord* from, HeapWord* to, size_t count) {
 90     assert_params_ok(from, to, HeapWordSize);
 91     assert_disjoint(from, to, count);
 92     pd_disjoint_words(from, to, count);
 93   }
 94 
 95   // Word-aligned words,    disjoint, atomic on each word
 96   static void disjoint_words_atomic(const HeapWord* from, HeapWord* to, size_t count) {
 97     assert_params_ok(from, to, HeapWordSize);
 98     assert_disjoint(from, to, count);
 99     pd_disjoint_words_atomic(from, to, count);
100   }
101 
102   // Object-aligned words,  conjoint, not atomic on each word
103   static void aligned_conjoint_words(const HeapWord* from, HeapWord* to, size_t count) {
104     assert_params_aligned(from, to);
105     pd_aligned_conjoint_words(from, to, count);
106   }
107 
108   // Object-aligned words,  disjoint, not atomic on each word
109   static void aligned_disjoint_words(const HeapWord* from, HeapWord* to, size_t count) {
110     assert_params_aligned(from, to);
111     assert_disjoint(from, to, count);
112     pd_aligned_disjoint_words(from, to, count);
113   }
114 
115   // bytes, jshorts, jints, jlongs, oops
116 
117   // bytes,                 conjoint, not atomic on each byte (not that it matters)
118   static void conjoint_jbytes(const void* from, void* to, size_t 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_jbytes_atomic(const void* from, void* to, size_t count) {
124     pd_conjoint_bytes(from, to, count);
125   }
126 
127   // jshorts,               conjoint, atomic on each jshort
128   static void conjoint_jshorts_atomic(const jshort* from, jshort* to, size_t count) {
129     assert_params_ok(from, to, BytesPerShort);
130     pd_conjoint_jshorts_atomic(from, to, count);
131   }
132 
133   // jints,                 conjoint, atomic on each jint
134   static void conjoint_jints_atomic(const jint* from, jint* to, size_t count) {
135     assert_params_ok(from, to, BytesPerInt);
136     pd_conjoint_jints_atomic(from, to, count);
137   }
138 
139   // jlongs,                conjoint, atomic on each jlong
140   static void conjoint_jlongs_atomic(const jlong* from, jlong* to, size_t count) {
141     assert_params_ok(from, to, BytesPerLong);
142     pd_conjoint_jlongs_atomic(from, to, count);
143   }
144 
145   // oops,                  conjoint, atomic on each oop
146   static void conjoint_oops_atomic(const oop* from, oop* to, size_t count) {
147     assert_params_ok(from, to, BytesPerHeapOop);
148     pd_conjoint_oops_atomic(from, to, count);
149   }
150 
151   // overloaded for UseCompressedOops
152   static void conjoint_oops_atomic(const narrowOop* from, narrowOop* to, size_t count) {
153     assert(sizeof(narrowOop) == sizeof(jint), "this cast is wrong");
154     assert_params_ok(from, to, BytesPerInt);
155     pd_conjoint_jints_atomic((const jint*)from, (jint*)to, count);
156   }
157 
158   // Copy a span of memory.  If the span is an integral number of aligned
159   // longs, words, or ints, copy those units atomically.
160   // The largest atomic transfer unit is 8 bytes, or the largest power
161   // of two which divides all of from, to, and size, whichever is smaller.
162   static void conjoint_memory_atomic(const void* from, void* to, size_t size);
163 
164   static void copy_value_content(const void* from, void* to, size_t size);
165   static void clear_value_content(void* to, size_t size);
166 
167   // bytes,                 conjoint array, atomic on each byte (not that it matters)
168   static void arrayof_conjoint_jbytes(const HeapWord* from, HeapWord* to, size_t count) {
169     pd_arrayof_conjoint_bytes(from, to, count);
170   }
171 
172   // jshorts,               conjoint array, atomic on each jshort
173   static void arrayof_conjoint_jshorts(const HeapWord* from, HeapWord* to, size_t count) {
174     assert_params_ok(from, to, BytesPerShort);
175     pd_arrayof_conjoint_jshorts(from, to, count);
176   }
177 
178   // jints,                 conjoint array, atomic on each jint
179   static void arrayof_conjoint_jints(const HeapWord* from, HeapWord* to, size_t count) {
180     assert_params_ok(from, to, BytesPerInt);
181     pd_arrayof_conjoint_jints(from, to, count);
182   }
183 
184   // jlongs,                conjoint array, atomic on each jlong
185   static void arrayof_conjoint_jlongs(const HeapWord* from, HeapWord* to, size_t count) {
186     assert_params_ok(from, to, BytesPerLong);
187     pd_arrayof_conjoint_jlongs(from, to, count);
188   }
189 
190   // oops,                  conjoint array, atomic on each oop
191   static void arrayof_conjoint_oops(const HeapWord* from, HeapWord* to, size_t count) {
192     assert_params_ok(from, to, BytesPerHeapOop);
193     pd_arrayof_conjoint_oops(from, to, count);
194   }
195 
196   // Known overlap methods
197 
198   // Copy word-aligned words from higher to lower addresses, not atomic on each word
199   inline static void conjoint_words_to_lower(const HeapWord* from, HeapWord* to, size_t byte_count) {
200     // byte_count is in bytes to check its alignment
201     assert_params_ok(from, to, HeapWordSize);
202     assert_byte_count_ok(byte_count, HeapWordSize);
203 
204     size_t count = align_up(byte_count, HeapWordSize) >> LogHeapWordSize;
205     assert(to <= from || from + count <= to, "do not overwrite source data");
206 
207     while (count-- > 0) {
208       *to++ = *from++;
209     }
210   }
211 
212   // Copy word-aligned words from lower to higher addresses, not atomic on each word
213   inline static void conjoint_words_to_higher(const HeapWord* from, HeapWord* to, size_t byte_count) {
214     // byte_count is in bytes to check its alignment
215     assert_params_ok(from, to, HeapWordSize);
216     assert_byte_count_ok(byte_count, HeapWordSize);
217     if (byte_count == 0) return;
218 
219     size_t count = align_up(byte_count, HeapWordSize) >> LogHeapWordSize;
220     assert(from <= to || to + count <= from, "do not overwrite source data");
221 
222     from += count - 1;
223     to   += count - 1;
224     while (count-- > 0) {
225       *to-- = *from--;
226     }
227   }
228 
229   /**
230    * Copy elements
231    *
232    * @param src address of source
233    * @param dst address of destination
234    * @param byte_count number of bytes to copy
235    * @param elem_size size of the elements to copy-swap
236    */
237   static void conjoint_copy(const void* src, void* dst, size_t byte_count, size_t elem_size);
238 
239   /**
240    * Copy and *unconditionally* byte swap elements
241    *
242    * @param src address of source
243    * @param dst address of destination
244    * @param byte_count number of bytes to copy
245    * @param elem_size size of the elements to copy-swap
246    */
247   static void conjoint_swap(const void* src, void* dst, size_t byte_count, size_t elem_size);
248 
249   /**
250    * Copy and byte swap elements from the specified endian to the native (cpu) endian if needed (if they differ)
251    *
252    * @param src address of source
253    * @param dst address of destination
254    * @param byte_count number of bytes to copy
255    * @param elem_size size of the elements to copy-swap
256    */
257   template <Endian::Order endian>
258   static void conjoint_swap_if_needed(const void* src, void* dst, size_t byte_count, size_t elem_size) {
259     if (Endian::NATIVE != endian) {
260       conjoint_swap(src, dst, byte_count, elem_size);
261     } else {
262       conjoint_copy(src, dst, byte_count, elem_size);
263     }
264   }
265 
266   // Fill methods
267 
268   // Fill word-aligned words, not atomic on each word
269   // set_words
270   static void fill_to_words(HeapWord* to, size_t count, juint value = 0) {
271     assert_params_ok(to, HeapWordSize);
272     pd_fill_to_words(to, count, value);
273   }
274 
275   static void fill_to_aligned_words(HeapWord* to, size_t count, juint value = 0) {
276     assert_params_aligned(to);
277     pd_fill_to_aligned_words(to, count, value);
278   }
279 
280   // Fill bytes
281   static void fill_to_bytes(void* to, size_t count, jubyte value = 0) {
282     pd_fill_to_bytes(to, count, value);
283   }
284 
285   // Fill a span of memory.  If the span is an integral number of aligned
286   // longs, words, or ints, store to those units atomically.
287   // The largest atomic transfer unit is 8 bytes, or the largest power
288   // of two which divides both to and size, whichever is smaller.
289   static void fill_to_memory_atomic(void* to, size_t size, jubyte value = 0);
290 
291   // Zero-fill methods
292 
293   // Zero word-aligned words, not atomic on each word
294   static void zero_to_words(HeapWord* to, size_t count) {
295     assert_params_ok(to, HeapWordSize);
296     pd_zero_to_words(to, count);
297   }
298 
299   // Zero bytes
300   static void zero_to_bytes(void* to, size_t count) {
301     pd_zero_to_bytes(to, count);
302   }
303 
304  protected:
305   inline static void shared_disjoint_words_atomic(const HeapWord* from,
306                                                   HeapWord* to, size_t count) {
307 
308     switch (count) {
309     case 8:  AtomicAccess::store(&to[7], AtomicAccess::load(&from[7]));
310     case 7:  AtomicAccess::store(&to[6], AtomicAccess::load(&from[6]));
311     case 6:  AtomicAccess::store(&to[5], AtomicAccess::load(&from[5]));
312     case 5:  AtomicAccess::store(&to[4], AtomicAccess::load(&from[4]));
313     case 4:  AtomicAccess::store(&to[3], AtomicAccess::load(&from[3]));
314     case 3:  AtomicAccess::store(&to[2], AtomicAccess::load(&from[2]));
315     case 2:  AtomicAccess::store(&to[1], AtomicAccess::load(&from[1]));
316     case 1:  AtomicAccess::store(&to[0], AtomicAccess::load(&from[0]));
317     case 0:  break;
318     default:
319       while (count-- > 0) {
320         AtomicAccess::store(to++, AtomicAccess::load(from++));
321       }
322       break;
323     }
324   }
325 
326  private:
327   static bool params_disjoint(const HeapWord* from, HeapWord* to, size_t count) {
328     if (from < to) {
329       return pointer_delta(to, from) >= count;
330     }
331     return pointer_delta(from, to) >= count;
332   }
333 
334   // These methods raise a fatal if they detect a problem.
335 
336   static void assert_disjoint(const HeapWord* from, HeapWord* to, size_t count) {
337     assert(params_disjoint(from, to, count), "source and dest overlap");
338   }
339 
340   static void assert_params_ok(const void* from, void* to, intptr_t alignment) {
341     assert(is_aligned(from, alignment), "must be aligned: " PTR_FORMAT, p2i(from));
342     assert(is_aligned(to, alignment),   "must be aligned: " PTR_FORMAT, p2i(to));
343   }
344 
345   static void assert_params_ok(HeapWord* to, intptr_t alignment) {
346     assert(is_aligned(to, alignment), "must be aligned: " PTR_FORMAT, p2i(to));
347   }
348 
349   static void assert_params_aligned(const HeapWord* from, HeapWord* to) {
350     assert(is_aligned(from, BytesPerLong), "must be aligned: " PTR_FORMAT, p2i(from));
351     assert(is_aligned(to, BytesPerLong),   "must be aligned: " PTR_FORMAT, p2i(to));
352   }
353 
354   static void assert_params_aligned(HeapWord* to) {
355     assert(is_aligned(to, BytesPerLong), "must be aligned: " PTR_FORMAT, p2i(to));
356   }
357 
358   static void assert_byte_count_ok(size_t byte_count, size_t unit_size) {
359     assert(is_aligned(byte_count, unit_size), "byte count must be aligned");
360   }
361 
362   // Platform dependent implementations of the above methods.
363 #include CPU_HEADER(copy)
364 
365 };
366 
367 #endif // SHARE_UTILITIES_COPY_HPP