author | mr |
Tue, 29 Oct 2019 13:52:04 -0700 | |
changeset 58850 | f4290bf1cc21 |
parent 48951 | 950c35ea6237 |
permissions | -rw-r--r-- |
1 | 1 |
/* |
48951 | 2 |
* Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved. |
1 | 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 |
* |
|
5547
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1
diff
changeset
|
21 |
* questions. |
1 | 22 |
* |
23 |
*/ |
|
24 |
||
7397 | 25 |
#include "precompiled.hpp" |
26 |
#include "runtime/sharedRuntime.hpp" |
|
46625 | 27 |
#include "utilities/align.hpp" |
7397 | 28 |
#include "utilities/copy.hpp" |
1 | 29 |
|
30 |
||
31 |
// Copy bytes; larger units are filled atomically if everything is aligned. |
|
48951 | 32 |
void Copy::conjoint_memory_atomic(const void* from, void* to, size_t size) { |
33 |
uintptr_t bits = (uintptr_t) from | (uintptr_t) to | (uintptr_t) size; |
|
1 | 34 |
|
35 |
// (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 simple |
|
39 |
// since the simplicity helps clarify the atomicity semantics of |
|
40 |
// this operation. There are also CPU-specific assembly versions |
|
41 |
// which may or may not want to include such optimizations.) |
|
42 |
||
43 |
if (bits % sizeof(jlong) == 0) { |
|
48951 | 44 |
Copy::conjoint_jlongs_atomic((const jlong*) from, (jlong*) to, size / sizeof(jlong)); |
1 | 45 |
} else if (bits % sizeof(jint) == 0) { |
48951 | 46 |
Copy::conjoint_jints_atomic((const jint*) from, (jint*) to, size / sizeof(jint)); |
1 | 47 |
} else if (bits % sizeof(jshort) == 0) { |
48951 | 48 |
Copy::conjoint_jshorts_atomic((const jshort*) from, (jshort*) to, size / sizeof(jshort)); |
1 | 49 |
} else { |
50 |
// Not aligned, so no need to be atomic. |
|
48951 | 51 |
Copy::conjoint_jbytes((const void*) from, (void*) to, size); |
1 | 52 |
} |
53 |
} |
|
54 |
||
36086 | 55 |
class CopySwap : AllStatic { |
56 |
public: |
|
57 |
/** |
|
46504
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
58 |
* Copy and optionally byte swap elements |
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
59 |
* |
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
60 |
* <swap> - true if elements should be byte swapped |
36086 | 61 |
* |
62 |
* @param src address of source |
|
63 |
* @param dst address of destination |
|
64 |
* @param byte_count number of bytes to copy |
|
65 |
* @param elem_size size of the elements to copy-swap |
|
66 |
*/ |
|
46504
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
67 |
template<bool swap> |
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
68 |
static void conjoint_swap_if_needed(const void* src, void* dst, size_t byte_count, size_t elem_size) { |
36086 | 69 |
assert(src != NULL, "address must not be NULL"); |
70 |
assert(dst != NULL, "address must not be NULL"); |
|
71 |
assert(elem_size == 2 || elem_size == 4 || elem_size == 8, |
|
72 |
"incorrect element size: " SIZE_FORMAT, elem_size); |
|
46619
a3919f5e8d2b
8178499: Remove _ptr_ and _size_ infixes from align functions
stefank
parents:
46504
diff
changeset
|
73 |
assert(is_aligned(byte_count, elem_size), |
36086 | 74 |
"byte_count " SIZE_FORMAT " must be multiple of element size " SIZE_FORMAT, byte_count, elem_size); |
75 |
||
46504
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
76 |
address src_end = (address)src + byte_count; |
36086 | 77 |
|
78 |
if (dst <= src || dst >= src_end) { |
|
46504
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
79 |
do_conjoint_swap<RIGHT,swap>(src, dst, byte_count, elem_size); |
36086 | 80 |
} else { |
46504
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
81 |
do_conjoint_swap<LEFT,swap>(src, dst, byte_count, elem_size); |
36086 | 82 |
} |
83 |
} |
|
84 |
||
85 |
private: |
|
86 |
/** |
|
87 |
* Byte swap a 16-bit value |
|
88 |
*/ |
|
89 |
static uint16_t byte_swap(uint16_t x) { |
|
90 |
return (x << 8) | (x >> 8); |
|
91 |
} |
|
92 |
||
93 |
/** |
|
94 |
* Byte swap a 32-bit value |
|
95 |
*/ |
|
96 |
static uint32_t byte_swap(uint32_t x) { |
|
97 |
uint16_t lo = (uint16_t)x; |
|
98 |
uint16_t hi = (uint16_t)(x >> 16); |
|
99 |
||
100 |
return ((uint32_t)byte_swap(lo) << 16) | (uint32_t)byte_swap(hi); |
|
101 |
} |
|
102 |
||
103 |
/** |
|
104 |
* Byte swap a 64-bit value |
|
105 |
*/ |
|
106 |
static uint64_t byte_swap(uint64_t x) { |
|
107 |
uint32_t lo = (uint32_t)x; |
|
108 |
uint32_t hi = (uint32_t)(x >> 32); |
|
109 |
||
110 |
return ((uint64_t)byte_swap(lo) << 32) | (uint64_t)byte_swap(hi); |
|
111 |
} |
|
112 |
||
113 |
enum CopyDirection { |
|
114 |
RIGHT, // lower -> higher address |
|
115 |
LEFT // higher -> lower address |
|
116 |
}; |
|
117 |
||
118 |
/** |
|
119 |
* Copy and byte swap elements |
|
120 |
* |
|
121 |
* <T> - type of element to copy |
|
122 |
* <D> - copy direction |
|
123 |
* <is_src_aligned> - true if src argument is aligned to element size |
|
124 |
* <is_dst_aligned> - true if dst argument is aligned to element size |
|
125 |
* |
|
126 |
* @param src address of source |
|
127 |
* @param dst address of destination |
|
128 |
* @param byte_count number of bytes to copy |
|
129 |
*/ |
|
46504
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
130 |
template <typename T, CopyDirection D, bool swap, bool is_src_aligned, bool is_dst_aligned> |
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
131 |
static void do_conjoint_swap(const void* src, void* dst, size_t byte_count) { |
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
132 |
const char* cur_src; |
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
133 |
char* cur_dst; |
36086 | 134 |
|
135 |
switch (D) { |
|
136 |
case RIGHT: |
|
46504
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
137 |
cur_src = (const char*)src; |
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
138 |
cur_dst = (char*)dst; |
36086 | 139 |
break; |
140 |
case LEFT: |
|
46504
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
141 |
cur_src = (const char*)src + byte_count - sizeof(T); |
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
142 |
cur_dst = (char*)dst + byte_count - sizeof(T); |
36086 | 143 |
break; |
144 |
} |
|
145 |
||
146 |
for (size_t i = 0; i < byte_count / sizeof(T); i++) { |
|
147 |
T tmp; |
|
148 |
||
149 |
if (is_src_aligned) { |
|
150 |
tmp = *(T*)cur_src; |
|
151 |
} else { |
|
152 |
memcpy(&tmp, cur_src, sizeof(T)); |
|
153 |
} |
|
154 |
||
46504
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
155 |
if (swap) { |
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
156 |
tmp = byte_swap(tmp); |
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
157 |
} |
36086 | 158 |
|
159 |
if (is_dst_aligned) { |
|
160 |
*(T*)cur_dst = tmp; |
|
161 |
} else { |
|
162 |
memcpy(cur_dst, &tmp, sizeof(T)); |
|
163 |
} |
|
164 |
||
165 |
switch (D) { |
|
166 |
case RIGHT: |
|
167 |
cur_src += sizeof(T); |
|
168 |
cur_dst += sizeof(T); |
|
169 |
break; |
|
170 |
case LEFT: |
|
171 |
cur_src -= sizeof(T); |
|
172 |
cur_dst -= sizeof(T); |
|
173 |
break; |
|
174 |
} |
|
175 |
} |
|
176 |
} |
|
177 |
||
178 |
/** |
|
179 |
* Copy and byte swap elements |
|
180 |
* |
|
46504
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
181 |
* <T> - type of element to copy |
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
182 |
* <D> - copy direction |
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
183 |
* <swap> - true if elements should be byte swapped |
36086 | 184 |
* |
185 |
* @param src address of source |
|
186 |
* @param dst address of destination |
|
187 |
* @param byte_count number of bytes to copy |
|
188 |
*/ |
|
46504
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
189 |
template <typename T, CopyDirection direction, bool swap> |
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
190 |
static void do_conjoint_swap(const void* src, void* dst, size_t byte_count) { |
46619
a3919f5e8d2b
8178499: Remove _ptr_ and _size_ infixes from align functions
stefank
parents:
46504
diff
changeset
|
191 |
if (is_aligned(src, sizeof(T))) { |
a3919f5e8d2b
8178499: Remove _ptr_ and _size_ infixes from align functions
stefank
parents:
46504
diff
changeset
|
192 |
if (is_aligned(dst, sizeof(T))) { |
46504
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
193 |
do_conjoint_swap<T,direction,swap,true,true>(src, dst, byte_count); |
36086 | 194 |
} else { |
46504
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
195 |
do_conjoint_swap<T,direction,swap,true,false>(src, dst, byte_count); |
36086 | 196 |
} |
197 |
} else { |
|
46619
a3919f5e8d2b
8178499: Remove _ptr_ and _size_ infixes from align functions
stefank
parents:
46504
diff
changeset
|
198 |
if (is_aligned(dst, sizeof(T))) { |
46504
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
199 |
do_conjoint_swap<T,direction,swap,false,true>(src, dst, byte_count); |
36086 | 200 |
} else { |
46504
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
201 |
do_conjoint_swap<T,direction,swap,false,false>(src, dst, byte_count); |
36086 | 202 |
} |
203 |
} |
|
204 |
} |
|
205 |
||
206 |
||
207 |
/** |
|
208 |
* Copy and byte swap elements |
|
209 |
* |
|
46504
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
210 |
* <D> - copy direction |
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
211 |
* <swap> - true if elements should be byte swapped |
36086 | 212 |
* |
213 |
* @param src address of source |
|
214 |
* @param dst address of destination |
|
215 |
* @param byte_count number of bytes to copy |
|
216 |
* @param elem_size size of the elements to copy-swap |
|
217 |
*/ |
|
46504
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
218 |
template <CopyDirection D, bool swap> |
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
219 |
static void do_conjoint_swap(const void* src, void* dst, size_t byte_count, size_t elem_size) { |
36086 | 220 |
switch (elem_size) { |
46504
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
221 |
case 2: do_conjoint_swap<uint16_t,D,swap>(src, dst, byte_count); break; |
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
222 |
case 4: do_conjoint_swap<uint32_t,D,swap>(src, dst, byte_count); break; |
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
223 |
case 8: do_conjoint_swap<uint64_t,D,swap>(src, dst, byte_count); break; |
43407 | 224 |
default: guarantee(false, "do_conjoint_swap: Invalid elem_size " SIZE_FORMAT "\n", elem_size); |
36086 | 225 |
} |
226 |
} |
|
227 |
}; |
|
228 |
||
46504
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
229 |
void Copy::conjoint_copy(const void* src, void* dst, size_t byte_count, size_t elem_size) { |
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
230 |
CopySwap::conjoint_swap_if_needed<false>(src, dst, byte_count, elem_size); |
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
231 |
} |
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
232 |
|
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
233 |
void Copy::conjoint_swap(const void* src, void* dst, size_t byte_count, size_t elem_size) { |
38048d4d20e7
8180032: Unaligned pointer dereference in ClassFileParser
mikael
parents:
43407
diff
changeset
|
234 |
CopySwap::conjoint_swap_if_needed<true>(src, dst, byte_count, elem_size); |
36086 | 235 |
} |
1 | 236 |
|
237 |
// Fill bytes; larger units are filled atomically if everything is aligned. |
|
238 |
void Copy::fill_to_memory_atomic(void* to, size_t size, jubyte value) { |
|
239 |
address dst = (address) to; |
|
240 |
uintptr_t bits = (uintptr_t) to | (uintptr_t) size; |
|
241 |
if (bits % sizeof(jlong) == 0) { |
|
242 |
jlong fill = (julong)( (jubyte)value ); // zero-extend |
|
243 |
if (fill != 0) { |
|
244 |
fill += fill << 8; |
|
245 |
fill += fill << 16; |
|
246 |
fill += fill << 32; |
|
247 |
} |
|
248 |
//Copy::fill_to_jlongs_atomic((jlong*) dst, size / sizeof(jlong)); |
|
249 |
for (uintptr_t off = 0; off < size; off += sizeof(jlong)) { |
|
250 |
*(jlong*)(dst + off) = fill; |
|
251 |
} |
|
252 |
} else if (bits % sizeof(jint) == 0) { |
|
253 |
jint fill = (juint)( (jubyte)value ); // zero-extend |
|
254 |
if (fill != 0) { |
|
255 |
fill += fill << 8; |
|
256 |
fill += fill << 16; |
|
257 |
} |
|
258 |
//Copy::fill_to_jints_atomic((jint*) dst, size / sizeof(jint)); |
|
259 |
for (uintptr_t off = 0; off < size; off += sizeof(jint)) { |
|
260 |
*(jint*)(dst + off) = fill; |
|
261 |
} |
|
262 |
} else if (bits % sizeof(jshort) == 0) { |
|
263 |
jshort fill = (jushort)( (jubyte)value ); // zero-extend |
|
264 |
fill += fill << 8; |
|
265 |
//Copy::fill_to_jshorts_atomic((jshort*) dst, size / sizeof(jshort)); |
|
266 |
for (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. |
|
271 |
Copy::fill_to_bytes(dst, size, value); |
|
272 |
} |
|
273 |
} |