@@ -167,9 +167,15 @@ template <typename T> struct ResourceWrapper {
167
167
union {
168
168
struct {
169
169
bool initialized;
170
- };
170
+ } data ;
171
171
std::max_align_t _unused;
172
- };
172
+ } payload;
173
+
174
+ bool initialized () const { return payload.data .initialized ; }
175
+
176
+ void set_initialized (bool initialized) {
177
+ payload.data .initialized = initialized;
178
+ }
173
179
174
180
const T *resource () const { return reinterpret_cast <const T *>(this + 1 ); }
175
181
@@ -183,7 +189,7 @@ template <typename T> struct ResourceWrapper {
183
189
static void dtor (ErlNifEnv *env, void *ptr) {
184
190
auto resource_wrapper = reinterpret_cast <ResourceWrapper<T> *>(ptr);
185
191
186
- if (resource_wrapper->initialized ) {
192
+ if (resource_wrapper->initialized () ) {
187
193
if constexpr (has_destructor<T>::value) {
188
194
resource_wrapper->resource ()->destructor (env);
189
195
}
@@ -297,14 +303,14 @@ ResourcePtr<T> make_resource(Args &&...args) {
297
303
// We use a wrapper struct with an extra field to track if the
298
304
// resource has actually been initialized. This way if the constructor
299
305
// below throws, we can skip the destructor calls in the Erlang dtor
300
- resource_wrapper->initialized = false ;
306
+ resource_wrapper->set_initialized ( false ) ;
301
307
302
308
// Invoke the constructor with prefect forwarding to initialize the
303
309
// object at the VM-allocated memory
304
310
new (reinterpret_cast <U *>(resource_wrapper->resource ()))
305
311
U (std::forward<Args>(args)...);
306
312
307
- resource_wrapper->initialized = true ;
313
+ resource_wrapper->set_initialized ( true ) ;
308
314
309
315
return resource;
310
316
}
0 commit comments