Skip to content

Commit 3edee1e

Browse files
committedOct 11, 2021
8272723: Don't use Access API to access primitive fields
Reviewed-by: stefank, eosterlund
1 parent 49f8ce6 commit 3edee1e

9 files changed

+106
-194
lines changed
 

‎src/hotspot/share/classfile/javaClasses.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -4220,8 +4220,8 @@ void java_lang_invoke_MethodHandleNatives_CallSiteContext::serialize_offsets(Ser
42204220

42214221
DependencyContext java_lang_invoke_MethodHandleNatives_CallSiteContext::vmdependencies(oop call_site) {
42224222
assert(java_lang_invoke_MethodHandleNatives_CallSiteContext::is_instance(call_site), "");
4223-
nmethodBucket* volatile* vmdeps_addr = (nmethodBucket* volatile*)call_site->field_addr(_vmdependencies_offset);
4224-
volatile uint64_t* last_cleanup_addr = (volatile uint64_t*)call_site->field_addr(_last_cleanup_offset);
4223+
nmethodBucket* volatile* vmdeps_addr = call_site->field_addr<nmethodBucket* volatile>(_vmdependencies_offset);
4224+
volatile uint64_t* last_cleanup_addr = call_site->field_addr<volatile uint64_t>(_last_cleanup_offset);
42254225
DependencyContext dep_ctx(vmdeps_addr, last_cleanup_addr);
42264226
return dep_ctx;
42274227
}
@@ -4279,19 +4279,19 @@ int java_lang_ClassLoader::_parent_offset;
42794279
ClassLoaderData* java_lang_ClassLoader::loader_data_acquire(oop loader) {
42804280
assert(loader != NULL, "loader must not be NULL");
42814281
assert(oopDesc::is_oop(loader), "loader must be oop");
4282-
return HeapAccess<MO_ACQUIRE>::load_at(loader, _loader_data_offset);
4282+
return Atomic::load_acquire(loader->field_addr<ClassLoaderData*>(_loader_data_offset));
42834283
}
42844284

42854285
ClassLoaderData* java_lang_ClassLoader::loader_data(oop loader) {
42864286
assert(loader != NULL, "loader must not be NULL");
42874287
assert(oopDesc::is_oop(loader), "loader must be oop");
4288-
return HeapAccess<>::load_at(loader, _loader_data_offset);
4288+
return *loader->field_addr<ClassLoaderData*>(_loader_data_offset);
42894289
}
42904290

42914291
void java_lang_ClassLoader::release_set_loader_data(oop loader, ClassLoaderData* new_data) {
42924292
assert(loader != NULL, "loader must not be NULL");
42934293
assert(oopDesc::is_oop(loader), "loader must be oop");
4294-
HeapAccess<MO_RELEASE>::store_at(loader, _loader_data_offset, new_data);
4294+
Atomic::release_store(loader->field_addr<ClassLoaderData*>(_loader_data_offset), new_data);
42954295
}
42964296

42974297
#define CLASSLOADER_FIELDS_DO(macro) \

‎src/hotspot/share/classfile/javaClasses.inline.hpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ bool java_lang_String::is_latin1(oop java_string) {
7777
uint8_t* java_lang_String::flags_addr(oop java_string) {
7878
assert(_initialized, "Must be initialized");
7979
assert(is_instance(java_string), "Must be java string");
80-
return java_string->obj_field_addr<uint8_t>(_flags_offset);
80+
return java_string->field_addr<uint8_t>(_flags_offset);
8181
}
8282

8383
bool java_lang_String::is_flag_set(oop java_string, uint8_t flag_mask) {
@@ -146,7 +146,7 @@ void java_lang_ref_Reference::clear_referent(oop ref) {
146146
}
147147

148148
HeapWord* java_lang_ref_Reference::referent_addr_raw(oop ref) {
149-
return ref->obj_field_addr<HeapWord>(_referent_offset);
149+
return ref->field_addr<HeapWord>(_referent_offset);
150150
}
151151

152152
oop java_lang_ref_Reference::next(oop ref) {
@@ -162,7 +162,7 @@ void java_lang_ref_Reference::set_next_raw(oop ref, oop value) {
162162
}
163163

164164
HeapWord* java_lang_ref_Reference::next_addr_raw(oop ref) {
165-
return ref->obj_field_addr<HeapWord>(_next_offset);
165+
return ref->field_addr<HeapWord>(_next_offset);
166166
}
167167

168168
oop java_lang_ref_Reference::discovered(oop ref) {
@@ -178,7 +178,7 @@ void java_lang_ref_Reference::set_discovered_raw(oop ref, oop value) {
178178
}
179179

180180
HeapWord* java_lang_ref_Reference::discovered_addr_raw(oop ref) {
181-
return ref->obj_field_addr<HeapWord>(_discovered_offset);
181+
return ref->field_addr<HeapWord>(_discovered_offset);
182182
}
183183

184184
bool java_lang_ref_Reference::is_final(oop ref) {

‎src/hotspot/share/jvmci/jvmciJavaClasses.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,13 @@ void HotSpotJVMCI::compute_offsets(TRAPS) {
231231
assert(className::klass() != NULL && className::klass()->is_linked(), "Class not yet linked: " #className); \
232232
InstanceKlass* ik = className::klass(); \
233233
oop base = ik->static_field_base_raw(); \
234-
return HeapAccess<>::load_at(base, className::_##name##_offset); \
234+
return *base->field_addr<jtypename>(className::_##name##_offset); \
235235
} \
236236
void HotSpotJVMCI::className::set_##name(JVMCIEnv* env, jtypename x) { \
237237
assert(className::klass() != NULL && className::klass()->is_linked(), "Class not yet linked: " #className); \
238238
InstanceKlass* ik = className::klass(); \
239239
oop base = ik->static_field_base_raw(); \
240-
HeapAccess<>::store_at(base, _##name##_offset, x); \
240+
*base->field_addr<jtypename>(className::_##name##_offset) = x; \
241241
}
242242

243243
#define STATIC_INT_FIELD(className, name) STATIC_PRIMITIVE_FIELD(className, name, jint)

‎src/hotspot/share/oops/instanceKlass.inline.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ inline void InstanceKlass::release_set_methods_jmethod_ids(jmethodID* jmeths) {
8484

8585
template <typename T, class OopClosureType>
8686
ALWAYSINLINE void InstanceKlass::oop_oop_iterate_oop_map(OopMapBlock* map, oop obj, OopClosureType* closure) {
87-
T* p = (T*)obj->obj_field_addr<T>(map->offset());
87+
T* p = obj->field_addr<T>(map->offset());
8888
T* const end = p + map->count();
8989

9090
for (; p < end; ++p) {
@@ -94,7 +94,7 @@ ALWAYSINLINE void InstanceKlass::oop_oop_iterate_oop_map(OopMapBlock* map, oop o
9494

9595
template <typename T, class OopClosureType>
9696
ALWAYSINLINE void InstanceKlass::oop_oop_iterate_oop_map_reverse(OopMapBlock* map, oop obj, OopClosureType* closure) {
97-
T* const start = (T*)obj->obj_field_addr<T>(map->offset());
97+
T* const start = obj->field_addr<T>(map->offset());
9898
T* p = start + map->count();
9999

100100
while (start < p) {
@@ -105,7 +105,7 @@ ALWAYSINLINE void InstanceKlass::oop_oop_iterate_oop_map_reverse(OopMapBlock* ma
105105

106106
template <typename T, class OopClosureType>
107107
ALWAYSINLINE void InstanceKlass::oop_oop_iterate_oop_map_bounded(OopMapBlock* map, oop obj, OopClosureType* closure, MemRegion mr) {
108-
T* p = (T*)obj->obj_field_addr<T>(map->offset());
108+
T* p = obj->field_addr<T>(map->offset());
109109
T* end = p + map->count();
110110

111111
T* const l = (T*)mr.start();

‎src/hotspot/share/oops/oop.cpp

+24-24
Original file line numberDiff line numberDiff line change
@@ -179,41 +179,41 @@ void oopDesc::obj_field_put_raw(int offset, oop value) { RawAcces
179179
void oopDesc::release_obj_field_put(int offset, oop value) { HeapAccess<MO_RELEASE>::oop_store_at(as_oop(), offset, value); }
180180
void oopDesc::obj_field_put_volatile(int offset, oop value) { HeapAccess<MO_SEQ_CST>::oop_store_at(as_oop(), offset, value); }
181181

182-
address oopDesc::address_field(int offset) const { return HeapAccess<>::load_at(as_oop(), offset); }
183-
address oopDesc::address_field_acquire(int offset) const { return HeapAccess<MO_ACQUIRE>::load_at(as_oop(), offset); }
182+
address oopDesc::address_field(int offset) const { return *field_addr<address>(offset); }
183+
address oopDesc::address_field_acquire(int offset) const { return Atomic::load_acquire(field_addr<address>(offset)); }
184184

185-
void oopDesc::address_field_put(int offset, address value) { HeapAccess<>::store_at(as_oop(), offset, value); }
186-
void oopDesc::release_address_field_put(int offset, address value) { HeapAccess<MO_RELEASE>::store_at(as_oop(), offset, value); }
185+
void oopDesc::address_field_put(int offset, address value) { *field_addr<address>(offset) = value; }
186+
void oopDesc::release_address_field_put(int offset, address value) { Atomic::release_store(field_addr<address>(offset), value); }
187187

188-
Metadata* oopDesc::metadata_field(int offset) const { return HeapAccess<>::load_at(as_oop(), offset); }
189-
void oopDesc::metadata_field_put(int offset, Metadata* value) { HeapAccess<>::store_at(as_oop(), offset, value); }
188+
Metadata* oopDesc::metadata_field(int offset) const { return *field_addr<Metadata*>(offset); }
189+
void oopDesc::metadata_field_put(int offset, Metadata* value) { *field_addr<Metadata*>(offset) = value; }
190190

191-
Metadata* oopDesc::metadata_field_acquire(int offset) const { return HeapAccess<MO_ACQUIRE>::load_at(as_oop(), offset); }
192-
void oopDesc::release_metadata_field_put(int offset, Metadata* value) { HeapAccess<MO_RELEASE>::store_at(as_oop(), offset, value); }
191+
Metadata* oopDesc::metadata_field_acquire(int offset) const { return Atomic::load_acquire(field_addr<Metadata*>(offset)); }
192+
void oopDesc::release_metadata_field_put(int offset, Metadata* value) { Atomic::release_store(field_addr<Metadata*>(offset), value); }
193193

194-
jbyte oopDesc::byte_field_acquire(int offset) const { return HeapAccess<MO_ACQUIRE>::load_at(as_oop(), offset); }
195-
void oopDesc::release_byte_field_put(int offset, jbyte value) { HeapAccess<MO_RELEASE>::store_at(as_oop(), offset, value); }
194+
jbyte oopDesc::byte_field_acquire(int offset) const { return Atomic::load_acquire(field_addr<jbyte>(offset)); }
195+
void oopDesc::release_byte_field_put(int offset, jbyte value) { Atomic::release_store(field_addr<jbyte>(offset), value); }
196196

197-
jchar oopDesc::char_field_acquire(int offset) const { return HeapAccess<MO_ACQUIRE>::load_at(as_oop(), offset); }
198-
void oopDesc::release_char_field_put(int offset, jchar value) { HeapAccess<MO_RELEASE>::store_at(as_oop(), offset, value); }
197+
jchar oopDesc::char_field_acquire(int offset) const { return Atomic::load_acquire(field_addr<jchar>(offset)); }
198+
void oopDesc::release_char_field_put(int offset, jchar value) { Atomic::release_store(field_addr<jchar>(offset), value); }
199199

200-
jboolean oopDesc::bool_field_acquire(int offset) const { return HeapAccess<MO_ACQUIRE>::load_at(as_oop(), offset); }
201-
void oopDesc::release_bool_field_put(int offset, jboolean value) { HeapAccess<MO_RELEASE>::store_at(as_oop(), offset, jboolean(value & 1)); }
200+
jboolean oopDesc::bool_field_acquire(int offset) const { return Atomic::load_acquire(field_addr<jboolean>(offset)); }
201+
void oopDesc::release_bool_field_put(int offset, jboolean value) { Atomic::release_store(field_addr<jboolean>(offset), jboolean(value & 1)); }
202202

203-
jint oopDesc::int_field_acquire(int offset) const { return HeapAccess<MO_ACQUIRE>::load_at(as_oop(), offset); }
204-
void oopDesc::release_int_field_put(int offset, jint value) { HeapAccess<MO_RELEASE>::store_at(as_oop(), offset, value); }
203+
jint oopDesc::int_field_acquire(int offset) const { return Atomic::load_acquire(field_addr<jint>(offset)); }
204+
void oopDesc::release_int_field_put(int offset, jint value) { Atomic::release_store(field_addr<jint>(offset), value); }
205205

206-
jshort oopDesc::short_field_acquire(int offset) const { return HeapAccess<MO_ACQUIRE>::load_at(as_oop(), offset); }
207-
void oopDesc::release_short_field_put(int offset, jshort value) { HeapAccess<MO_RELEASE>::store_at(as_oop(), offset, value); }
206+
jshort oopDesc::short_field_acquire(int offset) const { return Atomic::load_acquire(field_addr<jshort>(offset)); }
207+
void oopDesc::release_short_field_put(int offset, jshort value) { Atomic::release_store(field_addr<jshort>(offset), value); }
208208

209-
jlong oopDesc::long_field_acquire(int offset) const { return HeapAccess<MO_ACQUIRE>::load_at(as_oop(), offset); }
210-
void oopDesc::release_long_field_put(int offset, jlong value) { HeapAccess<MO_RELEASE>::store_at(as_oop(), offset, value); }
209+
jlong oopDesc::long_field_acquire(int offset) const { return Atomic::load_acquire(field_addr<jlong>(offset)); }
210+
void oopDesc::release_long_field_put(int offset, jlong value) { Atomic::release_store(field_addr<jlong>(offset), value); }
211211

212-
jfloat oopDesc::float_field_acquire(int offset) const { return HeapAccess<MO_ACQUIRE>::load_at(as_oop(), offset); }
213-
void oopDesc::release_float_field_put(int offset, jfloat value) { HeapAccess<MO_RELEASE>::store_at(as_oop(), offset, value); }
212+
jfloat oopDesc::float_field_acquire(int offset) const { return Atomic::load_acquire(field_addr<jfloat>(offset)); }
213+
void oopDesc::release_float_field_put(int offset, jfloat value) { Atomic::release_store(field_addr<jfloat>(offset), value); }
214214

215-
jdouble oopDesc::double_field_acquire(int offset) const { return HeapAccess<MO_ACQUIRE>::load_at(as_oop(), offset); }
216-
void oopDesc::release_double_field_put(int offset, jdouble value) { HeapAccess<MO_RELEASE>::store_at(as_oop(), offset, value); }
215+
jdouble oopDesc::double_field_acquire(int offset) const { return Atomic::load_acquire(field_addr<jdouble>(offset)); }
216+
void oopDesc::release_double_field_put(int offset, jdouble value) { Atomic::release_store(field_addr<jdouble>(offset), value); }
217217

218218
#ifdef ASSERT
219219
void oopDesc::verify_forwardee(oop forwardee) {

‎src/hotspot/share/oops/oop.hpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,8 @@ class oopDesc {
124124
inline oop as_oop() const { return const_cast<oopDesc*>(this); }
125125

126126
public:
127-
// field addresses in oop
128-
inline void* field_addr(int offset) const;
129-
130-
// Need this as public for garbage collection.
131-
template <class T> inline T* obj_field_addr(int offset) const;
127+
template<typename T>
128+
inline T* field_addr(int offset) const;
132129

133130
template <typename T> inline size_t field_offset(T* p) const;
134131

‎src/hotspot/share/oops/oop.inline.hpp

+20-22
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,8 @@ bool oopDesc::is_array() const { return klass()->is_array_klass(); }
208208
bool oopDesc::is_objArray() const { return klass()->is_objArray_klass(); }
209209
bool oopDesc::is_typeArray() const { return klass()->is_typeArray_klass(); }
210210

211-
void* oopDesc::field_addr(int offset) const { return reinterpret_cast<void*>(cast_from_oop<intptr_t>(as_oop()) + offset); }
212-
213-
template <class T>
214-
T* oopDesc::obj_field_addr(int offset) const { return (T*) field_addr(offset); }
211+
template<typename T>
212+
T* oopDesc::field_addr(int offset) const { return reinterpret_cast<T*>(cast_from_oop<intptr_t>(as_oop()) + offset); }
215213

216214
template <typename T>
217215
size_t oopDesc::field_offset(T* p) const { return pointer_delta((void*)p, (void*)this, 1); }
@@ -222,30 +220,30 @@ inline oop oopDesc::obj_field(int offset) const { return Hea
222220

223221
inline void oopDesc::obj_field_put(int offset, oop value) { HeapAccess<>::oop_store_at(as_oop(), offset, value); }
224222

225-
inline jbyte oopDesc::byte_field(int offset) const { return HeapAccess<>::load_at(as_oop(), offset); }
226-
inline void oopDesc::byte_field_put(int offset, jbyte value) { HeapAccess<>::store_at(as_oop(), offset, value); }
223+
inline jbyte oopDesc::byte_field(int offset) const { return *field_addr<jbyte>(offset); }
224+
inline void oopDesc::byte_field_put(int offset, jbyte value) { *field_addr<jbyte>(offset) = value; }
227225

228-
inline jchar oopDesc::char_field(int offset) const { return HeapAccess<>::load_at(as_oop(), offset); }
229-
inline void oopDesc::char_field_put(int offset, jchar value) { HeapAccess<>::store_at(as_oop(), offset, value); }
226+
inline jchar oopDesc::char_field(int offset) const { return *field_addr<jchar>(offset); }
227+
inline void oopDesc::char_field_put(int offset, jchar value) { *field_addr<jchar>(offset) = value; }
230228

231-
inline jboolean oopDesc::bool_field(int offset) const { return HeapAccess<>::load_at(as_oop(), offset); }
232-
inline void oopDesc::bool_field_put(int offset, jboolean value) { HeapAccess<>::store_at(as_oop(), offset, jboolean(value & 1)); }
233-
inline jboolean oopDesc::bool_field_volatile(int offset) const { return HeapAccess<MO_SEQ_CST>::load_at(as_oop(), offset); }
234-
inline void oopDesc::bool_field_put_volatile(int offset, jboolean value) { HeapAccess<MO_SEQ_CST>::store_at(as_oop(), offset, jboolean(value & 1)); }
235-
inline jshort oopDesc::short_field(int offset) const { return HeapAccess<>::load_at(as_oop(), offset); }
236-
inline void oopDesc::short_field_put(int offset, jshort value) { HeapAccess<>::store_at(as_oop(), offset, value); }
229+
inline jboolean oopDesc::bool_field(int offset) const { return *field_addr<jboolean>(offset); }
230+
inline void oopDesc::bool_field_put(int offset, jboolean value) { *field_addr<jboolean>(offset) = jboolean(value & 1); }
231+
inline jboolean oopDesc::bool_field_volatile(int offset) const { return RawAccess<MO_SEQ_CST>::load(field_addr<jboolean>(offset)); }
232+
inline void oopDesc::bool_field_put_volatile(int offset, jboolean value) { RawAccess<MO_SEQ_CST>::store(field_addr<jboolean>(offset), jboolean(value & 1)); }
233+
inline jshort oopDesc::short_field(int offset) const { return *field_addr<jshort>(offset); }
234+
inline void oopDesc::short_field_put(int offset, jshort value) { *field_addr<jshort>(offset) = value; }
237235

238-
inline jint oopDesc::int_field(int offset) const { return HeapAccess<>::load_at(as_oop(), offset); }
239-
inline void oopDesc::int_field_put(int offset, jint value) { HeapAccess<>::store_at(as_oop(), offset, value); }
236+
inline jint oopDesc::int_field(int offset) const { return *field_addr<jint>(offset); }
237+
inline void oopDesc::int_field_put(int offset, jint value) { *field_addr<jint>(offset) = value; }
240238

241-
inline jlong oopDesc::long_field(int offset) const { return HeapAccess<>::load_at(as_oop(), offset); }
242-
inline void oopDesc::long_field_put(int offset, jlong value) { HeapAccess<>::store_at(as_oop(), offset, value); }
239+
inline jlong oopDesc::long_field(int offset) const { return *field_addr<jlong>(offset); }
240+
inline void oopDesc::long_field_put(int offset, jlong value) { *field_addr<jlong>(offset) = value; }
243241

244-
inline jfloat oopDesc::float_field(int offset) const { return HeapAccess<>::load_at(as_oop(), offset); }
245-
inline void oopDesc::float_field_put(int offset, jfloat value) { HeapAccess<>::store_at(as_oop(), offset, value); }
242+
inline jfloat oopDesc::float_field(int offset) const { return *field_addr<jfloat>(offset); }
243+
inline void oopDesc::float_field_put(int offset, jfloat value) { *field_addr<jfloat>(offset) = value; }
246244

247-
inline jdouble oopDesc::double_field(int offset) const { return HeapAccess<>::load_at(as_oop(), offset); }
248-
inline void oopDesc::double_field_put(int offset, jdouble value) { HeapAccess<>::store_at(as_oop(), offset, value); }
245+
inline jdouble oopDesc::double_field(int offset) const { return *field_addr<jdouble>(offset); }
246+
inline void oopDesc::double_field_put(int offset, jdouble value) { *field_addr<jdouble>(offset) = value; }
249247

250248
bool oopDesc::is_locked() const {
251249
return mark().is_locked();

‎src/hotspot/share/oops/typeArrayOop.inline.hpp

+28-72
Original file line numberDiff line numberDiff line change
@@ -90,139 +90,95 @@ inline jdouble* typeArrayOopDesc::double_at_addr(int which) const {
9090
}
9191

9292
inline jbyte typeArrayOopDesc::byte_at(int which) const {
93-
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
94-
ptrdiff_t offset = element_offset<jbyte>(which);
95-
return HeapAccess<IS_ARRAY>::load_at(as_oop(), offset);
93+
return *byte_at_addr(which);
9694
}
9795
inline void typeArrayOopDesc::byte_at_put(int which, jbyte contents) {
98-
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
99-
ptrdiff_t offset = element_offset<jbyte>(which);
100-
HeapAccess<IS_ARRAY>::store_at(as_oop(), offset, contents);
96+
*byte_at_addr(which) = contents;
10197
}
10298

10399
inline jboolean typeArrayOopDesc::bool_at(int which) const {
104-
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
105-
ptrdiff_t offset = element_offset<jboolean>(which);
106-
return HeapAccess<IS_ARRAY>::load_at(as_oop(), offset);
100+
return *bool_at_addr(which);
107101
}
108102
inline void typeArrayOopDesc::bool_at_put(int which, jboolean contents) {
109-
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
110-
ptrdiff_t offset = element_offset<jboolean>(which);
111-
HeapAccess<IS_ARRAY>::store_at(as_oop(), offset, jboolean(contents & 1));
103+
*bool_at_addr(which) = jboolean(contents & 1);
112104
}
113105

114106
inline jchar typeArrayOopDesc::char_at(int which) const {
115-
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
116-
ptrdiff_t offset = element_offset<jchar>(which);
117-
return HeapAccess<IS_ARRAY>::load_at(as_oop(), offset);
107+
return *char_at_addr(which);
118108
}
119109
inline void typeArrayOopDesc::char_at_put(int which, jchar contents) {
120-
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
121-
ptrdiff_t offset = element_offset<jchar>(which);
122-
HeapAccess<IS_ARRAY>::store_at(as_oop(), offset, contents);
110+
*char_at_addr(which) = contents;
123111
}
124112

125113
inline jint typeArrayOopDesc::int_at(int which) const {
126-
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
127-
ptrdiff_t offset = element_offset<jint>(which);
128-
return HeapAccess<IS_ARRAY>::load_at(as_oop(), offset);
114+
return *int_at_addr(which);
129115
}
130116
inline void typeArrayOopDesc::int_at_put(int which, jint contents) {
131-
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
132-
ptrdiff_t offset = element_offset<jint>(which);
133-
HeapAccess<IS_ARRAY>::store_at(as_oop(), offset, contents);
117+
*int_at_addr(which) = contents;
134118
}
135119

136120
inline jshort typeArrayOopDesc::short_at(int which) const {
137-
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
138-
ptrdiff_t offset = element_offset<jshort>(which);
139-
return HeapAccess<IS_ARRAY>::load_at(as_oop(), offset);
121+
return *short_at_addr(which);
140122
}
141123
inline void typeArrayOopDesc::short_at_put(int which, jshort contents) {
142-
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
143-
ptrdiff_t offset = element_offset<jshort>(which);
144-
HeapAccess<IS_ARRAY>::store_at(as_oop(), offset, contents);
124+
*short_at_addr(which) = contents;
145125
}
146126

147127
inline jushort typeArrayOopDesc::ushort_at(int which) const {
148-
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
149-
ptrdiff_t offset = element_offset<jushort>(which);
150-
return HeapAccess<IS_ARRAY>::load_at(as_oop(), offset);
128+
return *ushort_at_addr(which);
151129
}
130+
152131
inline void typeArrayOopDesc::ushort_at_put(int which, jushort contents) {
153-
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
154-
ptrdiff_t offset = element_offset<jushort>(which);
155-
HeapAccess<IS_ARRAY>::store_at(as_oop(), offset, contents);
132+
*ushort_at_addr(which) = contents;
156133
}
157134

158135
inline jlong typeArrayOopDesc::long_at(int which) const {
159-
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
160-
ptrdiff_t offset = element_offset<jlong>(which);
161-
return HeapAccess<IS_ARRAY>::load_at(as_oop(), offset);
136+
return *long_at_addr(which);
162137
}
163138
inline void typeArrayOopDesc::long_at_put(int which, jlong contents) {
164-
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
165-
ptrdiff_t offset = element_offset<jlong>(which);
166-
HeapAccess<IS_ARRAY>::store_at(as_oop(), offset, contents);
139+
*long_at_addr(which) = contents;
167140
}
168141

169142
inline jfloat typeArrayOopDesc::float_at(int which) const {
170-
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
171-
ptrdiff_t offset = element_offset<jfloat>(which);
172-
return HeapAccess<IS_ARRAY>::load_at(as_oop(), offset);
143+
return *float_at_addr(which);
173144
}
145+
174146
inline void typeArrayOopDesc::float_at_put(int which, jfloat contents) {
175-
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
176-
ptrdiff_t offset = element_offset<jfloat>(which);
177-
HeapAccess<IS_ARRAY>::store_at(as_oop(), offset, contents);
147+
*float_at_addr(which) = contents;
178148
}
179149

180150
inline jdouble typeArrayOopDesc::double_at(int which) const {
181-
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
182-
ptrdiff_t offset = element_offset<jdouble>(which);
183-
return HeapAccess<IS_ARRAY>::load_at(as_oop(), offset);
151+
return *double_at_addr(which);
184152
}
153+
185154
inline void typeArrayOopDesc::double_at_put(int which, jdouble contents) {
186-
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
187-
ptrdiff_t offset = element_offset<jdouble>(which);
188-
HeapAccess<IS_ARRAY>::store_at(as_oop(), offset, contents);
155+
*double_at_addr(which) = contents;
189156
}
190157

191158
inline jbyte typeArrayOopDesc::byte_at_acquire(int which) const {
192-
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
193-
ptrdiff_t offset = element_offset<jbyte>(which);
194-
return HeapAccess<MO_ACQUIRE | IS_ARRAY>::load_at(as_oop(), offset);
159+
return Atomic::load_acquire(byte_at_addr(which));
195160
}
196161
inline void typeArrayOopDesc::release_byte_at_put(int which, jbyte contents) {
197-
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
198-
ptrdiff_t offset = element_offset<jbyte>(which);
199-
HeapAccess<MO_RELEASE | IS_ARRAY>::store_at(as_oop(), offset, contents);
162+
Atomic::release_store(byte_at_addr(which), contents);
200163
}
201164

202165
// Java thinks Symbol arrays are just arrays of either long or int, since
203166
// there doesn't seem to be T_ADDRESS, so this is a bit of unfortunate
204167
// casting
205168
#ifdef _LP64
206169
inline Symbol* typeArrayOopDesc::symbol_at(int which) const {
207-
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
208-
ptrdiff_t offset = element_offset<jlong>(which);
209-
return (Symbol*)(jlong) HeapAccess<IS_ARRAY>::load_at(as_oop(), offset);
170+
return *reinterpret_cast<Symbol**>(long_at_addr(which));
210171
}
172+
211173
inline void typeArrayOopDesc::symbol_at_put(int which, Symbol* contents) {
212-
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
213-
ptrdiff_t offset = element_offset<jlong>(which);
214-
HeapAccess<IS_ARRAY>::store_at(as_oop(), offset, (jlong)contents);
174+
*reinterpret_cast<Symbol**>(long_at_addr(which)) = contents;
215175
}
216176
#else
217177
inline Symbol* typeArrayOopDesc::symbol_at(int which) const {
218-
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
219-
ptrdiff_t offset = element_offset<jint>(which);
220-
return (Symbol*)(jint) HeapAccess<IS_ARRAY>::load_at(as_oop(), offset);
178+
return *reinterpret_cast<Symbol**>(int_at_addr(which));
221179
}
222180
inline void typeArrayOopDesc::symbol_at_put(int which, Symbol* contents) {
223-
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
224-
ptrdiff_t offset = element_offset<jint>(which);
225-
HeapAccess<IS_ARRAY>::store_at(as_oop(), offset, (jint)contents);
181+
*reinterpret_cast<Symbol**>(int_at_addr(which)) = contents;
226182
}
227183
#endif // _LP64
228184

‎src/hotspot/share/prims/unsafe.cpp

+18-57
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ static inline void assert_field_offset_sane(oop p, jlong field_offset) {
119119
assert(byte_offset >= 0 && byte_offset <= (jlong)MAX_OBJECT_SIZE, "sane offset");
120120
if (byte_offset == (jint)byte_offset) {
121121
void* ptr_plus_disp = cast_from_oop<address>(p) + byte_offset;
122-
assert(p->field_addr((jint)byte_offset) == ptr_plus_disp,
122+
assert(p->field_addr<void>((jint)byte_offset) == ptr_plus_disp,
123123
"raw [ptr+disp] must be consistent with oop::field_addr");
124124
}
125125
jlong p_size = HeapWordSize * (jlong)(p->size());
@@ -218,44 +218,25 @@ class MemoryAccess : StackObj {
218218
}
219219

220220
T get() {
221-
if (_obj == NULL) {
222-
GuardUnsafeAccess guard(_thread);
223-
T ret = RawAccess<>::load(addr());
224-
return normalize_for_read(ret);
225-
} else {
226-
T ret = HeapAccess<>::load_at(_obj, _offset);
227-
return normalize_for_read(ret);
228-
}
221+
GuardUnsafeAccess guard(_thread);
222+
return normalize_for_read(*addr());
229223
}
230224

231225
void put(T x) {
232-
if (_obj == NULL) {
233-
GuardUnsafeAccess guard(_thread);
234-
RawAccess<>::store(addr(), normalize_for_write(x));
235-
} else {
236-
HeapAccess<>::store_at(_obj, _offset, normalize_for_write(x));
237-
}
226+
GuardUnsafeAccess guard(_thread);
227+
*addr() = normalize_for_write(x);
238228
}
239229

240230

241231
T get_volatile() {
242-
if (_obj == NULL) {
243-
GuardUnsafeAccess guard(_thread);
244-
volatile T ret = RawAccess<MO_SEQ_CST>::load(addr());
245-
return normalize_for_read(ret);
246-
} else {
247-
T ret = HeapAccess<MO_SEQ_CST>::load_at(_obj, _offset);
248-
return normalize_for_read(ret);
249-
}
232+
GuardUnsafeAccess guard(_thread);
233+
volatile T ret = RawAccess<MO_SEQ_CST>::load(addr());
234+
return normalize_for_read(ret);
250235
}
251236

252237
void put_volatile(T x) {
253-
if (_obj == NULL) {
254-
GuardUnsafeAccess guard(_thread);
255-
RawAccess<MO_SEQ_CST>::store(addr(), normalize_for_write(x));
256-
} else {
257-
HeapAccess<MO_SEQ_CST>::store_at(_obj, _offset, normalize_for_write(x));
258-
}
238+
GuardUnsafeAccess guard(_thread);
239+
RawAccess<MO_SEQ_CST>::store(addr(), normalize_for_write(x));
259240
}
260241
};
261242

@@ -745,24 +726,14 @@ UNSAFE_ENTRY(jobject, Unsafe_CompareAndExchangeReference(JNIEnv *env, jobject un
745726

746727
UNSAFE_ENTRY(jint, Unsafe_CompareAndExchangeInt(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jint e, jint x)) {
747728
oop p = JNIHandles::resolve(obj);
748-
if (p == NULL) {
749-
volatile jint* addr = (volatile jint*)index_oop_from_field_offset_long(p, offset);
750-
return RawAccess<>::atomic_cmpxchg(addr, e, x);
751-
} else {
752-
assert_field_offset_sane(p, offset);
753-
return HeapAccess<>::atomic_cmpxchg_at(p, (ptrdiff_t)offset, e, x);
754-
}
729+
volatile jint* addr = (volatile jint*)index_oop_from_field_offset_long(p, offset);
730+
return Atomic::cmpxchg(addr, e, x);
755731
} UNSAFE_END
756732

757733
UNSAFE_ENTRY(jlong, Unsafe_CompareAndExchangeLong(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong e, jlong x)) {
758734
oop p = JNIHandles::resolve(obj);
759-
if (p == NULL) {
760-
volatile jlong* addr = (volatile jlong*)index_oop_from_field_offset_long(p, offset);
761-
return RawAccess<>::atomic_cmpxchg(addr, e, x);
762-
} else {
763-
assert_field_offset_sane(p, offset);
764-
return HeapAccess<>::atomic_cmpxchg_at(p, (ptrdiff_t)offset, e, x);
765-
}
735+
volatile jlong* addr = (volatile jlong*)index_oop_from_field_offset_long(p, offset);
736+
return Atomic::cmpxchg(addr, e, x);
766737
} UNSAFE_END
767738

768739
UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSetReference(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject e_h, jobject x_h)) {
@@ -776,24 +747,14 @@ UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSetReference(JNIEnv *env, jobject unsafe
776747

777748
UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSetInt(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jint e, jint x)) {
778749
oop p = JNIHandles::resolve(obj);
779-
if (p == NULL) {
780-
volatile jint* addr = (volatile jint*)index_oop_from_field_offset_long(p, offset);
781-
return RawAccess<>::atomic_cmpxchg(addr, e, x) == e;
782-
} else {
783-
assert_field_offset_sane(p, offset);
784-
return HeapAccess<>::atomic_cmpxchg_at(p, (ptrdiff_t)offset, e, x) == e;
785-
}
750+
volatile jint* addr = (volatile jint*)index_oop_from_field_offset_long(p, offset);
751+
return Atomic::cmpxchg(addr, e, x) == e;
786752
} UNSAFE_END
787753

788754
UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSetLong(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong e, jlong x)) {
789755
oop p = JNIHandles::resolve(obj);
790-
if (p == NULL) {
791-
volatile jlong* addr = (volatile jlong*)index_oop_from_field_offset_long(p, offset);
792-
return RawAccess<>::atomic_cmpxchg(addr, e, x) == e;
793-
} else {
794-
assert_field_offset_sane(p, offset);
795-
return HeapAccess<>::atomic_cmpxchg_at(p, (ptrdiff_t)offset, e, x) == e;
796-
}
756+
volatile jlong* addr = (volatile jlong*)index_oop_from_field_offset_long(p, offset);
757+
return Atomic::cmpxchg(addr, e, x) == e;
797758
} UNSAFE_END
798759

799760
static void post_thread_park_event(EventThreadPark* event, const oop obj, jlong timeout_nanos, jlong until_epoch_millis) {

0 commit comments

Comments
 (0)
Please sign in to comment.