RFR: 8354954: Typed static memory for late initialization of static class members in Hotspot [v4]

Johan Sjölen jsjolen at openjdk.org
Thu Apr 17 18:33:36 UTC 2025


> Hi,
> 
> This PR introduces a `StableValue<T>` which is sized and aligned identically to a `T`, with the difference that a `StableValue<T>` needs to be explicitly instantiated.
> 
> Dynamic static initalization in C++ leads to unpredictable bugs as there is no defined order in which objects will be initialized, to the degree that 'static initialization fiasco' is a term used. In the code I've worked on in Hotspot we resolve this by having an initialization function, and instead of having static members of `T` we have `T*` instead and use `malloc` in order to gain the memory for the objects. This is workable, but is unnecessary.
> 
> That's why I'd like to have `StableValue<T>`. It let's you avoid the whole `malloc` thing, and we overload `->` to make it behave as if it is actually a `T`. We add in a simple checker in debug mode that checks whether the memory has been initialized before using it.
> 
> In the code I've switched two members to be of `StableValue` instead. One is the malloc case above, the second (MemBaseline) is one where I got a bug while developing. The bug occurred because I  changed the initializer of `MemBaseline` without knowing that it was dynamic-static-allocated, and the exact change I made caused weird crashes (because of initialization order issues).
> 
> This solution is quite practical to me, but I wanted to know what others think.

Johan Sjölen has updated the pull request incrementally with one additional commit since the last revision:

  Move semantics would be nice.

-------------

Changes:
  - all: https://git.openjdk.org/jdk/pull/24689/files
  - new: https://git.openjdk.org/jdk/pull/24689/files/3efa84b1..5507ff4c

Webrevs:
 - full: https://webrevs.openjdk.org/?repo=jdk&pr=24689&range=03
 - incr: https://webrevs.openjdk.org/?repo=jdk&pr=24689&range=02-03

  Stats: 2 lines in 1 file changed: 1 ins; 0 del; 1 mod
  Patch: https://git.openjdk.org/jdk/pull/24689.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/24689/head:pull/24689

PR: https://git.openjdk.org/jdk/pull/24689


More information about the hotspot-dev mailing list