Skip to content

Commit

Permalink
8313224: Avoid calling JavaThread::current() in MemAllocator::Allocat…
Browse files Browse the repository at this point in the history
…ion constructor

Reviewed-by: tschatzl, coleenp
  • Loading branch information
iklam committed Aug 11, 2023
1 parent 9abb2a5 commit 43462a3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
21 changes: 11 additions & 10 deletions src/hotspot/share/gc/shared/memAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ class MemAllocator::Allocation: StackObj {
bool check_out_of_memory();
void verify_before();
void verify_after();
void notify_allocation(JavaThread* thread);
void notify_allocation();
void notify_allocation_jvmti_sampler();
void notify_allocation_low_memory_detector();
void notify_allocation_jfr_sampler();
void notify_allocation_dtrace_sampler(JavaThread* thread);
void notify_allocation_dtrace_sampler();
#ifdef ASSERT
void check_for_valid_allocation_state() const;
#endif
Expand All @@ -70,19 +70,20 @@ class MemAllocator::Allocation: StackObj {
public:
Allocation(const MemAllocator& allocator, oop* obj_ptr)
: _allocator(allocator),
_thread(JavaThread::current()),
_thread(JavaThread::cast(allocator._thread)), // Do not use Allocation in non-JavaThreads.
_obj_ptr(obj_ptr),
_overhead_limit_exceeded(false),
_allocated_outside_tlab(false),
_allocated_tlab_size(0),
_tlab_end_reset_for_sample(false)
{
assert(Thread::current() == allocator._thread, "do not pass MemAllocator across threads");
verify_before();
}

~Allocation() {
if (!check_out_of_memory()) {
notify_allocation(_thread);
notify_allocation();
}
}

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

Expand Down Expand Up @@ -217,21 +218,21 @@ void MemAllocator::Allocation::notify_allocation_jfr_sampler() {
}
}

void MemAllocator::Allocation::notify_allocation_dtrace_sampler(JavaThread* thread) {
void MemAllocator::Allocation::notify_allocation_dtrace_sampler() {
if (DTraceAllocProbes) {
// support for Dtrace object alloc event (no-op most of the time)
Klass* klass = obj()->klass();
size_t word_size = _allocator._word_size;
if (klass != nullptr && klass->name() != nullptr) {
SharedRuntime::dtrace_object_alloc(thread, obj(), word_size);
SharedRuntime::dtrace_object_alloc(_thread, obj(), word_size);
}
}
}

void MemAllocator::Allocation::notify_allocation(JavaThread* thread) {
void MemAllocator::Allocation::notify_allocation() {
notify_allocation_low_memory_detector();
notify_allocation_jfr_sampler();
notify_allocation_dtrace_sampler(thread);
notify_allocation_dtrace_sampler();
notify_allocation_jvmti_sampler();
}

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

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

if (UseTLAB) {
// Try refilling the TLAB and allocating the object in it.
Expand Down
6 changes: 4 additions & 2 deletions src/hotspot/share/gc/shared/memAllocator.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -61,7 +61,9 @@ class MemAllocator: StackObj {
: _thread(thread),
_klass(klass),
_word_size(word_size)
{ }
{
assert(_thread == Thread::current(), "must be");
}

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

1 comment on commit 43462a3

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.