Skip to content

Commit 43462a3

Browse files
committedAug 11, 2023
8313224: Avoid calling JavaThread::current() in MemAllocator::Allocation constructor
Reviewed-by: tschatzl, coleenp
1 parent 9abb2a5 commit 43462a3

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed
 

‎src/hotspot/share/gc/shared/memAllocator.cpp

+11-10
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ class MemAllocator::Allocation: StackObj {
5656
bool check_out_of_memory();
5757
void verify_before();
5858
void verify_after();
59-
void notify_allocation(JavaThread* thread);
59+
void notify_allocation();
6060
void notify_allocation_jvmti_sampler();
6161
void notify_allocation_low_memory_detector();
6262
void notify_allocation_jfr_sampler();
63-
void notify_allocation_dtrace_sampler(JavaThread* thread);
63+
void notify_allocation_dtrace_sampler();
6464
#ifdef ASSERT
6565
void check_for_valid_allocation_state() const;
6666
#endif
@@ -70,19 +70,20 @@ class MemAllocator::Allocation: StackObj {
7070
public:
7171
Allocation(const MemAllocator& allocator, oop* obj_ptr)
7272
: _allocator(allocator),
73-
_thread(JavaThread::current()),
73+
_thread(JavaThread::cast(allocator._thread)), // Do not use Allocation in non-JavaThreads.
7474
_obj_ptr(obj_ptr),
7575
_overhead_limit_exceeded(false),
7676
_allocated_outside_tlab(false),
7777
_allocated_tlab_size(0),
7878
_tlab_end_reset_for_sample(false)
7979
{
80+
assert(Thread::current() == allocator._thread, "do not pass MemAllocator across threads");
8081
verify_before();
8182
}
8283

8384
~Allocation() {
8485
if (!check_out_of_memory()) {
85-
notify_allocation(_thread);
86+
notify_allocation();
8687
}
8788
}
8889

@@ -156,7 +157,7 @@ void MemAllocator::Allocation::check_for_valid_allocation_state() const {
156157
assert(!_thread->has_pending_exception(),
157158
"shouldn't be allocating with pending exception");
158159
// Allocation of an oop can always invoke a safepoint.
159-
JavaThread::cast(_thread)->check_for_valid_safepoint_state();
160+
_thread->check_for_valid_safepoint_state();
160161
}
161162
#endif
162163

@@ -217,21 +218,21 @@ void MemAllocator::Allocation::notify_allocation_jfr_sampler() {
217218
}
218219
}
219220

220-
void MemAllocator::Allocation::notify_allocation_dtrace_sampler(JavaThread* thread) {
221+
void MemAllocator::Allocation::notify_allocation_dtrace_sampler() {
221222
if (DTraceAllocProbes) {
222223
// support for Dtrace object alloc event (no-op most of the time)
223224
Klass* klass = obj()->klass();
224225
size_t word_size = _allocator._word_size;
225226
if (klass != nullptr && klass->name() != nullptr) {
226-
SharedRuntime::dtrace_object_alloc(thread, obj(), word_size);
227+
SharedRuntime::dtrace_object_alloc(_thread, obj(), word_size);
227228
}
228229
}
229230
}
230231

231-
void MemAllocator::Allocation::notify_allocation(JavaThread* thread) {
232+
void MemAllocator::Allocation::notify_allocation() {
232233
notify_allocation_low_memory_detector();
233234
notify_allocation_jfr_sampler();
234-
notify_allocation_dtrace_sampler(thread);
235+
notify_allocation_dtrace_sampler();
235236
notify_allocation_jvmti_sampler();
236237
}
237238

@@ -335,7 +336,7 @@ HeapWord* MemAllocator::mem_allocate_inside_tlab_slow(Allocation& allocation) co
335336

336337
HeapWord* MemAllocator::mem_allocate_slow(Allocation& allocation) const {
337338
// Allocation of an oop can always invoke a safepoint.
338-
debug_only(JavaThread::cast(_thread)->check_for_valid_safepoint_state());
339+
debug_only(allocation._thread->check_for_valid_safepoint_state());
339340

340341
if (UseTLAB) {
341342
// Try refilling the TLAB and allocating the object in it.

‎src/hotspot/share/gc/shared/memAllocator.hpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -61,7 +61,9 @@ class MemAllocator: StackObj {
6161
: _thread(thread),
6262
_klass(klass),
6363
_word_size(word_size)
64-
{ }
64+
{
65+
assert(_thread == Thread::current(), "must be");
66+
}
6567

6668
// Initialization provided by subclasses.
6769
virtual oop initialize(HeapWord* mem) const = 0;

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Aug 11, 2023

@openjdk-notifier[bot]
Please sign in to comment.