RFR: 8354954: Typed static memory for late initialization of static class members in Hotspot [v11]
Stefan Karlsson
stefank at openjdk.org
Thu Jun 19 07:47:41 UTC 2025
On Wed, 16 Apr 2025 17:37:16 GMT, Johan Sjölen <jsjolen at openjdk.org> wrote:
>> src/hotspot/share/nmt/memoryFileTracker.cpp line 129:
>>
>>> 127: bool MemoryFileTracker::Instance::initialize(NMT_TrackingLevel tracking_level) {
>>> 128: if (tracking_level == NMT_TrackingLevel::NMT_off) return true;
>>> 129: new (_tracker.as()) MemoryFileTracker(tracking_level == NMT_TrackingLevel::NMT_detail);
>>
>> Maybe you could add an `init` function that forwards the constructor arguments, with an extra check to see if the memory has already been initialized:
>>
>>
>> template<typename...As>
>> void init(As&&...args) {
>> assert(is_death_pattern(), "StaticArea already initialized");
>> new (as()) T(std::forward<As>(args)...);
>> }
>>
>>
>> Suggestion:
>>
>> _tracker.init(tracking_level == NMT_TrackingLevel::NMT_detail);
>
> I think that's a good idea. Unfortunately, move semantics and Rvalue references are currently undecided in the style guide, so we can't write this exact code. We can still do
>
> ```c++
> template<typename... As>
> void init(As&... args) {
> assert(is_death_pattern(), "StaticArea already initialized");
> new (as()) T(args...);
> }
>
>
> Which is pretty good.
FWIW, the current implementation doesn't allow me to do:
struct Thing {
Thing(int value) {}
};
...
Defered<Thing> _deferred;
...
_deferred.initialize(1);
I have to write last piece as:
int temp = 1;
_deferred.initialize(temp);
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/24689#discussion_r2156365079
More information about the hotspot-dev
mailing list