Skip to content

Commit aafc6b7

Browse files
mdempskycommit-bot@chromium.org
authored andcommitted
[vm] Use std::unique_ptr for IsolateSpawnState
Updates #37244. Change-Id: I03b4f0b291bd4e4e9cde5de31ec80da2b8b6c6cf Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/101740 Reviewed-by: Siva Annamalai <asiva@google.com> Commit-Queue: Matthew Dempsky <mdempsky@google.com>
1 parent 29b346e commit aafc6b7

3 files changed

Lines changed: 22 additions & 23 deletions

File tree

runtime/lib/isolate.cc

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
#include <memory>
6+
#include <utility>
7+
58
#include "include/dart_native_api.h"
69
#include "platform/assert.h"
710
#include "platform/unicode.h"
@@ -113,8 +116,9 @@ static void ThrowIsolateSpawnException(const String& message) {
113116

114117
class SpawnIsolateTask : public ThreadPool::Task {
115118
public:
116-
SpawnIsolateTask(Isolate* parent_isolate, IsolateSpawnState* state)
117-
: parent_isolate_(parent_isolate), state_(state) {
119+
SpawnIsolateTask(Isolate* parent_isolate,
120+
std::unique_ptr<IsolateSpawnState> state)
121+
: parent_isolate_(parent_isolate), state_(std::move(state)) {
118122
parent_isolate->IncrementSpawnCount();
119123
}
120124

@@ -131,8 +135,6 @@ class SpawnIsolateTask : public ThreadPool::Task {
131135
if (callback == NULL) {
132136
ReportError(
133137
"Isolate spawn is not supported by this Dart implementation\n");
134-
delete state_;
135-
state_ = NULL;
136138
return;
137139
}
138140

@@ -149,8 +151,6 @@ class SpawnIsolateTask : public ThreadPool::Task {
149151
parent_isolate_ = nullptr;
150152
if (isolate == NULL) {
151153
ReportError(error);
152-
delete state_;
153-
state_ = NULL;
154154
free(error);
155155
return;
156156
}
@@ -162,8 +162,7 @@ class SpawnIsolateTask : public ThreadPool::Task {
162162
}
163163
MutexLocker ml(isolate->mutex());
164164
state_->set_isolate(isolate);
165-
isolate->set_spawn_state(state_);
166-
state_ = NULL;
165+
isolate->set_spawn_state(std::move(state_));
167166
if (isolate->is_runnable()) {
168167
isolate->Run();
169168
}
@@ -181,7 +180,7 @@ class SpawnIsolateTask : public ThreadPool::Task {
181180
}
182181

183182
Isolate* parent_isolate_;
184-
IsolateSpawnState* state_;
183+
std::unique_ptr<IsolateSpawnState> state_;
185184

186185
DISALLOW_COPY_AND_ASSIGN(SpawnIsolateTask);
187186
};
@@ -238,20 +237,19 @@ DEFINE_NATIVE_ENTRY(Isolate_spawnFunction, 0, 11) {
238237
const char* utf8_debug_name =
239238
debugName.IsNull() ? NULL : String2UTF8(debugName);
240239

241-
IsolateSpawnState* state = new IsolateSpawnState(
240+
std::unique_ptr<IsolateSpawnState> state(new IsolateSpawnState(
242241
port.Id(), isolate->origin_id(), String2UTF8(script_uri), func,
243242
&message_buffer, utf8_package_config, paused.value(), fatal_errors,
244-
on_exit_port, on_error_port, utf8_debug_name);
243+
on_exit_port, on_error_port, utf8_debug_name));
245244

246245
// Since this is a call to Isolate.spawn, copy the parent isolate's code.
247246
state->isolate_flags()->copy_parent_code = true;
248247

249-
ThreadPool::Task* spawn_task = new SpawnIsolateTask(isolate, state);
248+
ThreadPool::Task* spawn_task =
249+
new SpawnIsolateTask(isolate, std::move(state));
250250

251251
if (!Dart::thread_pool()->Run(spawn_task)) {
252252
// Running on the thread pool failed. Clean up everything.
253-
delete state;
254-
state = NULL;
255253
delete spawn_task;
256254
spawn_task = NULL;
257255
}
@@ -360,10 +358,10 @@ DEFINE_NATIVE_ENTRY(Isolate_spawnUri, 0, 13) {
360358
const char* utf8_debug_name =
361359
debugName.IsNull() ? NULL : String2UTF8(debugName);
362360

363-
IsolateSpawnState* state = new IsolateSpawnState(
361+
std::unique_ptr<IsolateSpawnState> state(new IsolateSpawnState(
364362
port.Id(), canonical_uri, utf8_package_config, &arguments_buffer,
365363
&message_buffer, paused.value(), fatal_errors, on_exit_port,
366-
on_error_port, utf8_debug_name);
364+
on_error_port, utf8_debug_name));
367365

368366
// If we were passed a value then override the default flags state for
369367
// checked mode.
@@ -375,12 +373,11 @@ DEFINE_NATIVE_ENTRY(Isolate_spawnUri, 0, 13) {
375373
// Since this is a call to Isolate.spawnUri, don't copy the parent's code.
376374
state->isolate_flags()->copy_parent_code = false;
377375

378-
ThreadPool::Task* spawn_task = new SpawnIsolateTask(isolate, state);
376+
ThreadPool::Task* spawn_task =
377+
new SpawnIsolateTask(isolate, std::move(state));
379378

380379
if (!Dart::thread_pool()->Run(spawn_task)) {
381380
// Running on the thread pool failed. Clean up everything.
382-
delete state;
383-
state = NULL;
384381
delete spawn_task;
385382
spawn_task = NULL;
386383
}

runtime/vm/isolate.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,6 @@ Isolate::~Isolate() {
978978
nullptr; // Fail fast if we send messages to a dead isolate.
979979
ASSERT(deopt_context_ ==
980980
nullptr); // No deopt in progress when isolate deleted.
981-
delete spawn_state_;
982981
ASSERT(spawn_count_ == 0);
983982
delete safepoint_handler_;
984983
delete thread_registry_;

runtime/vm/isolate.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#endif
1111

1212
#include <memory>
13+
#include <utility>
1314

1415
#include "include/dart_api.h"
1516
#include "platform/assert.h"
@@ -346,8 +347,10 @@ class Isolate : public BaseIsolate {
346347
isolate_flags_ = CompactionInProgressBit::update(value, isolate_flags_);
347348
}
348349

349-
IsolateSpawnState* spawn_state() const { return spawn_state_; }
350-
void set_spawn_state(IsolateSpawnState* value) { spawn_state_ = value; }
350+
IsolateSpawnState* spawn_state() const { return spawn_state_.get(); }
351+
void set_spawn_state(std::unique_ptr<IsolateSpawnState> value) {
352+
spawn_state_ = std::move(value);
353+
}
351354

352355
Mutex* mutex() { return &mutex_; }
353356
Mutex* symbols_mutex() { return &symbols_mutex_; }
@@ -1033,7 +1036,7 @@ class Isolate : public BaseIsolate {
10331036
Mutex kernel_data_class_cache_mutex_;
10341037
Mutex kernel_constants_mutex_;
10351038
MessageHandler* message_handler_ = nullptr;
1036-
IsolateSpawnState* spawn_state_ = nullptr;
1039+
std::unique_ptr<IsolateSpawnState> spawn_state_;
10371040
intptr_t defer_finalization_count_ = 0;
10381041
MallocGrowableArray<PendingLazyDeopt>* pending_deopts_;
10391042
DeoptContext* deopt_context_ = nullptr;

0 commit comments

Comments
 (0)