Skip to content

Commit 01d4b77

Browse files
author
Kim Barrett
committed
8319242: HotSpot Style Guide should discourage non-local variables with non-trivial initialization or destruction
Reviewed-by: stefank, dcubed, dholmes
1 parent c7125aa commit 01d4b77

File tree

2 files changed

+56
-13
lines changed

2 files changed

+56
-13
lines changed

doc/hotspot-style.html

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ <h1 class="title">HotSpot Coding Style</h1>
7777
<li><a href="#thread_local" id="toc-thread_local">thread_local</a></li>
7878
<li><a href="#nullptr" id="toc-nullptr">nullptr</a></li>
7979
<li><a href="#atomic" id="toc-atomic">&lt;atomic&gt;</a></li>
80+
<li><a href="#initializing-variables-with-static-storage-duration"
81+
id="toc-initializing-variables-with-static-storage-duration">Initializing
82+
variables with static storage duration</a></li>
8083
<li><a href="#uniform-initialization"
8184
id="toc-uniform-initialization">Uniform Initialization</a></li>
8285
<li><a href="#local-function-objects"
@@ -791,6 +794,33 @@ <h3 id="atomic">&lt;atomic&gt;</h3>
791794
"conservative" memory ordering, which may differ from (may be stronger
792795
than) sequentially consistent. There are algorithms in HotSpot that are
793796
believed to rely on that ordering.</p>
797+
<h3
798+
id="initializing-variables-with-static-storage-duration">Initializing
799+
variables with static storage duration</h3>
800+
<p>Variables with static storage duration and <em>dynamic
801+
initialization</em> <a
802+
href="https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf">C++14
803+
3.6.2</a>). should be avoided, unless an implementation is permitted to
804+
perform the initialization as a static initialization. The order in
805+
which dynamic initializations occur is incompletely specified.
806+
Initialization order problems can be difficult to deal with and lead to
807+
surprises.</p>
808+
<p>Variables with static storage duration and non-trivial destructors
809+
should be avoided. HotSpot doesn't generally try to cleanup on exit, and
810+
running destructors at exit can lead to problems.</p>
811+
<p>Some of the approaches used in HotSpot to avoid dynamic
812+
initialization include:</p>
813+
<ul>
814+
<li><p>Use the <code>Deferred&lt;T&gt;</code> class template. Add a call
815+
to its initialization function at an appropriate place during VM
816+
initialization. The underlying object is never destroyed.</p></li>
817+
<li><p>For objects of class type, use a variable whose value is a
818+
pointer to the class, initialized to <code>nullptr</code>. Provide an
819+
initialization function that sets the variable to a dynamically
820+
allocated object. Add a call to that function at an appropriate place
821+
during VM initialization. Such objects are usually never
822+
destroyed.</p></li>
823+
</ul>
794824
<h3 id="uniform-initialization">Uniform Initialization</h3>
795825
<p>The use of <em>uniform initialization</em> (<a
796826
href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2672.htm">n2672</a>),
@@ -1198,12 +1228,6 @@ <h3 id="excluded-features">Excluded Features</h3>
11981228
href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2179.html">n2179</a>)
11991229
— HotSpot does not permit the use of exceptions, so this feature isn't
12001230
useful.</p></li>
1201-
<li><p>Avoid non-local variables with non-constexpr initialization. In
1202-
particular, avoid variables with types requiring non-trivial
1203-
initialization or destruction. Initialization order problems can be
1204-
difficult to deal with and lead to surprises, as can destruction
1205-
ordering. HotSpot doesn't generally try to cleanup on exit, and running
1206-
destructors at exit can also lead to problems.</p></li>
12071231
<li><p>Avoid most operator overloading, preferring named functions. When
12081232
operator overloading is used, ensure the semantics conform to the normal
12091233
expected behavior of the operation.</p></li>

doc/hotspot-style.md

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,32 @@ ordering, which may differ from (may be stronger than) sequentially
770770
consistent. There are algorithms in HotSpot that are believed to rely
771771
on that ordering.
772772

773+
### Initializing variables with static storage duration
774+
775+
Variables with static storage duration and _dynamic initialization_
776+
[C++14 3.6.2](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf)).
777+
should be avoided, unless an implementation is permitted to perform the
778+
initialization as a static initialization. The order in which dynamic
779+
initializations occur is incompletely specified. Initialization order
780+
problems can be difficult to deal with and lead to surprises.
781+
782+
Variables with static storage duration and non-trivial destructors should be
783+
avoided. HotSpot doesn't generally try to cleanup on exit, and running
784+
destructors at exit can lead to problems.
785+
786+
Some of the approaches used in HotSpot to avoid dynamic initialization
787+
include:
788+
789+
* Use the `Deferred<T>` class template. Add a call to its initialization
790+
function at an appropriate place during VM initialization. The underlying
791+
object is never destroyed.
792+
793+
* For objects of class type, use a variable whose value is a pointer to the
794+
class, initialized to `nullptr`. Provide an initialization function that sets
795+
the variable to a dynamically allocated object. Add a call to that function at
796+
an appropriate place during VM initialization. Such objects are usually never
797+
destroyed.
798+
773799
### Uniform Initialization
774800

775801
The use of _uniform initialization_
@@ -1199,13 +1225,6 @@ namespace std;` to avoid needing to qualify Standard Library names.
11991225
([n2179](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2179.html)) &mdash;
12001226
HotSpot does not permit the use of exceptions, so this feature isn't useful.
12011227
1202-
* Avoid non-local variables with non-constexpr initialization.
1203-
In particular, avoid variables with types requiring non-trivial
1204-
initialization or destruction. Initialization order problems can be
1205-
difficult to deal with and lead to surprises, as can destruction
1206-
ordering. HotSpot doesn't generally try to cleanup on exit, and
1207-
running destructors at exit can also lead to problems.
1208-
12091228
* Avoid most operator overloading, preferring named functions. When
12101229
operator overloading is used, ensure the semantics conform to the
12111230
normal expected behavior of the operation.

0 commit comments

Comments
 (0)