@@ -77,13 +77,17 @@ <h1 class="title">HotSpot Coding Style</h1>
77
77
< li > < a href ="#thread_local " id ="toc-thread_local "> thread_local</ a > </ li >
78
78
< li > < a href ="#nullptr " id ="toc-nullptr "> nullptr</ a > </ li >
79
79
< li > < a href ="#atomic " id ="toc-atomic "> <atomic></ 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 >
80
83
< li > < a href ="#uniform-initialization "
81
84
id ="toc-uniform-initialization "> Uniform Initialization</ a > </ li >
82
85
< li > < a href ="#local-function-objects "
83
86
id ="toc-local-function-objects "> Local Function Objects</ a > </ li >
84
87
< li > < a href ="#inheriting-constructors "
85
88
id ="toc-inheriting-constructors "> Inheriting constructors</ a > </ li >
86
89
< li > < a href ="#attributes " id ="toc-attributes "> Attributes</ a > </ li >
90
+ < li > < a href ="#noexcept " id ="toc-noexcept "> noexcept</ a > </ li >
87
91
< li > < a href ="#additional-permitted-features "
88
92
id ="toc-additional-permitted-features "> Additional Permitted
89
93
Features</ a > </ li >
@@ -791,6 +795,33 @@ <h3 id="atomic"><atomic></h3>
791
795
"conservative" memory ordering, which may differ from (may be stronger
792
796
than) sequentially consistent. There are algorithms in HotSpot that are
793
797
believed to rely on that ordering.</ p >
798
+ < h3
799
+ id ="initializing-variables-with-static-storage-duration "> Initializing
800
+ variables with static storage duration</ h3 >
801
+ < p > Variables with static storage duration and < em > dynamic
802
+ initialization</ em > < a
803
+ href ="https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf "> C++14
804
+ 3.6.2</ a > ). should be avoided, unless an implementation is permitted to
805
+ perform the initialization as a static initialization. The order in
806
+ which dynamic initializations occur is incompletely specified.
807
+ Initialization order problems can be difficult to deal with and lead to
808
+ surprises.</ p >
809
+ < p > Variables with static storage duration and non-trivial destructors
810
+ should be avoided. HotSpot doesn't generally try to cleanup on exit, and
811
+ running destructors at exit can lead to problems.</ p >
812
+ < p > Some of the approaches used in HotSpot to avoid dynamic
813
+ initialization include:</ p >
814
+ < ul >
815
+ < li > < p > Use the < code > Deferred<T></ code > class template. Add a call
816
+ to its initialization function at an appropriate place during VM
817
+ initialization. The underlying object is never destroyed.</ p > </ li >
818
+ < li > < p > For objects of class type, use a variable whose value is a
819
+ pointer to the class, initialized to < code > nullptr</ code > . Provide an
820
+ initialization function that sets the variable to a dynamically
821
+ allocated object. Add a call to that function at an appropriate place
822
+ during VM initialization. Such objects are usually never
823
+ destroyed.</ p > </ li >
824
+ </ ul >
794
825
< h3 id ="uniform-initialization "> Uniform Initialization</ h3 >
795
826
< p > The use of < em > uniform initialization</ em > (< a
796
827
href ="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2672.htm "> n2672</ a > ),
@@ -1110,6 +1141,58 @@ <h3 id="attributes">Attributes</h3>
1110
1141
< code > memory_order_consume</ code > .</ li >
1111
1142
< li > < code > [[deprecated]]</ code > - Not relevant in HotSpot code.</ li >
1112
1143
</ ul >
1144
+ < h3 id ="noexcept "> noexcept</ h3 >
1145
+ < p > Use of < code > noexcept</ code > exception specifications (< a
1146
+ href ="http://wg21.link/n3050 "> n3050</ a > ) are permitted with restrictions
1147
+ described below.</ p >
1148
+ < ul >
1149
+ < li > Only the argument-less form of < code > noexcept</ code > exception
1150
+ specifications are permitted.</ li >
1151
+ < li > Allocation functions that may return < code > nullptr</ code > to
1152
+ indicate allocation failure must be declared < code > noexcept</ code > .</ li >
1153
+ < li > All other uses of < code > noexcept</ code > exception specifications are
1154
+ forbidden.</ li >
1155
+ < li > < code > noexcept</ code > expressions are forbidden.</ li >
1156
+ < li > Dynamic exception specifications are forbidden.</ li >
1157
+ </ ul >
1158
+ < p > HotSpot is built with exceptions disabled, e.g. compile with
1159
+ < code > -fno-exceptions</ code > (gcc, clang) or no < code > /EH</ code > option
1160
+ (MSVC++). So why do we need to consider < code > noexcept</ code > at all?
1161
+ It's because < code > noexcept</ code > exception specifications serve two
1162
+ distinct purposes.</ p >
1163
+ < p > The first is to allow the compiler to avoid generating code or data
1164
+ in support of exceptions being thrown by a function. But this is
1165
+ unnecessary, because exceptions are disabled.</ p >
1166
+ < p > The second is to allow the compiler and library code to choose
1167
+ different algorithms, depending on whether some function may throw
1168
+ exceptions. This is only relevant to a certain set of functions.</ p >
1169
+ < ul >
1170
+ < li > < p > Some allocation functions (< code > operator new</ code > and
1171
+ < code > operator new[]</ code > ) return < code > nullptr</ code > to indicate
1172
+ allocation failure. If a < code > new</ code > expression calls such an
1173
+ allocation function, it must check for and handle that possibility.
1174
+ Declaring such a function < code > noexcept</ code > informs the compiler
1175
+ that < code > nullptr</ code > is a possible result. If an allocation
1176
+ function is not declared < code > noexcept</ code > then the compiler may
1177
+ elide that checking and handling for a < code > new</ code > expression
1178
+ calling that function.</ p > </ li >
1179
+ < li > < p > Certain Standard Library facilities (notably containers) provide
1180
+ different guarantees for some operations (and may choose different
1181
+ algorithms to implement those operations), depending on whether certain
1182
+ functions (constructors, copy/move operations, swap) are nothrow or not.
1183
+ They detect this using type traits that test whether a function is
1184
+ declared < code > noexcept</ code > . This can have a significant performance
1185
+ impact if, for example, copying is chosen over a potentially throwing
1186
+ move. But this isn't relevant, since HotSpot forbids the use of most
1187
+ Standard Library facilities.</ p > </ li >
1188
+ </ ul >
1189
+ < p > HotSpot code can assume no exceptions will ever be thrown, even from
1190
+ functions not declared < code > noexcept</ code > . So HotSpot code doesn't
1191
+ ever need to check, either with conditional exception specifications or
1192
+ with < code > noexcept</ code > expressions.</ p >
1193
+ < p > Dynamic exception specifications were deprecated in C++11. C++17
1194
+ removed all but < code > throw()</ code > , with that remaining a deprecated
1195
+ equivalent to < code > noexcept</ code > .</ p >
1113
1196
< h3 id ="additional-permitted-features "> Additional Permitted
1114
1197
Features</ h3 >
1115
1198
< ul >
@@ -1198,12 +1281,6 @@ <h3 id="excluded-features">Excluded Features</h3>
1198
1281
href ="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2179.html "> n2179</ a > )
1199
1282
— HotSpot does not permit the use of exceptions, so this feature isn't
1200
1283
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 >
1207
1284
< li > < p > Avoid most operator overloading, preferring named functions. When
1208
1285
operator overloading is used, ensure the semantics conform to the normal
1209
1286
expected behavior of the operation.</ p > </ li >
0 commit comments