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