Skip to content

Commit 68fd102

Browse files
authored
[lldb] Use std::make_shared for StopInfoSP (#149612)
Use std::make_shared to create a StopInfoSP, which inherits from shared_from_this. It's both the most efficient and safest way to create these objects: - With make_shared, the object and the control block are allocated together, which is more efficient. - With make_shared, the enable_shared_from_this base class is properly linked to the control block before the constructor finishes, so shared_from_this() will be safe to use (though still not recommended during construction).
1 parent 09bea21 commit 68fd102

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

lldb/source/Target/StopInfo.cpp

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -851,8 +851,9 @@ class StopInfoWatchpoint : public StopInfo {
851851
// We have to step over the watchpoint before we know what to do:
852852
StopInfoWatchpointSP me_as_siwp_sp
853853
= std::static_pointer_cast<StopInfoWatchpoint>(shared_from_this());
854-
ThreadPlanSP step_over_wp_sp(new ThreadPlanStepOverWatchpoint(
855-
*(thread_sp.get()), me_as_siwp_sp, wp_sp));
854+
ThreadPlanSP step_over_wp_sp =
855+
std::make_shared<ThreadPlanStepOverWatchpoint>(*(thread_sp.get()),
856+
me_as_siwp_sp, wp_sp);
856857
// When this plan is done we want to stop, so set this as a Controlling
857858
// plan.
858859
step_over_wp_sp->SetIsControllingPlan(true);
@@ -1475,81 +1476,81 @@ StopInfoSP StopInfo::CreateStopReasonWithBreakpointSiteID(Thread &thread,
14751476
break_id_t break_id) {
14761477
thread.SetThreadHitBreakpointSite();
14771478

1478-
return StopInfoSP(new StopInfoBreakpoint(thread, break_id));
1479+
return std::make_shared<StopInfoBreakpoint>(thread, break_id);
14791480
}
14801481

14811482
StopInfoSP StopInfo::CreateStopReasonWithBreakpointSiteID(Thread &thread,
14821483
break_id_t break_id,
14831484
bool should_stop) {
1484-
return StopInfoSP(new StopInfoBreakpoint(thread, break_id, should_stop));
1485+
return std::make_shared<StopInfoBreakpoint>(thread, break_id, should_stop);
14851486
}
14861487

14871488
// LWP_TODO: We'll need a CreateStopReasonWithWatchpointResourceID akin
14881489
// to CreateStopReasonWithBreakpointSiteID
14891490
StopInfoSP StopInfo::CreateStopReasonWithWatchpointID(Thread &thread,
14901491
break_id_t watch_id,
14911492
bool silently_continue) {
1492-
return StopInfoSP(
1493-
new StopInfoWatchpoint(thread, watch_id, silently_continue));
1493+
return std::make_shared<StopInfoWatchpoint>(thread, watch_id,
1494+
silently_continue);
14941495
}
14951496

14961497
StopInfoSP StopInfo::CreateStopReasonWithSignal(Thread &thread, int signo,
14971498
const char *description,
14981499
std::optional<int> code) {
14991500
thread.GetProcess()->GetUnixSignals()->IncrementSignalHitCount(signo);
1500-
return StopInfoSP(new StopInfoUnixSignal(thread, signo, description, code));
1501+
return std::make_shared<StopInfoUnixSignal>(thread, signo, description, code);
15011502
}
15021503

15031504
StopInfoSP StopInfo::CreateStopReasonWithInterrupt(Thread &thread, int signo,
15041505
const char *description) {
1505-
return StopInfoSP(new StopInfoInterrupt(thread, signo, description));
1506+
return std::make_shared<StopInfoInterrupt>(thread, signo, description);
15061507
}
15071508

15081509
StopInfoSP StopInfo::CreateStopReasonToTrace(Thread &thread) {
1509-
return StopInfoSP(new StopInfoTrace(thread));
1510+
return std::make_shared<StopInfoTrace>(thread);
15101511
}
15111512

15121513
StopInfoSP StopInfo::CreateStopReasonWithPlan(
15131514
ThreadPlanSP &plan_sp, ValueObjectSP return_valobj_sp,
15141515
ExpressionVariableSP expression_variable_sp) {
1515-
return StopInfoSP(new StopInfoThreadPlan(plan_sp, return_valobj_sp,
1516-
expression_variable_sp));
1516+
return std::make_shared<StopInfoThreadPlan>(plan_sp, return_valobj_sp,
1517+
expression_variable_sp);
15171518
}
15181519

15191520
StopInfoSP StopInfo::CreateStopReasonWithException(Thread &thread,
15201521
const char *description) {
1521-
return StopInfoSP(new StopInfoException(thread, description));
1522+
return std::make_shared<StopInfoException>(thread, description);
15221523
}
15231524

15241525
StopInfoSP StopInfo::CreateStopReasonProcessorTrace(Thread &thread,
15251526
const char *description) {
1526-
return StopInfoSP(new StopInfoProcessorTrace(thread, description));
1527+
return std::make_shared<StopInfoProcessorTrace>(thread, description);
15271528
}
15281529

15291530
StopInfoSP StopInfo::CreateStopReasonHistoryBoundary(Thread &thread,
15301531
const char *description) {
1531-
return StopInfoSP(new StopInfoHistoryBoundary(thread, description));
1532+
return std::make_shared<StopInfoHistoryBoundary>(thread, description);
15321533
}
15331534

15341535
StopInfoSP StopInfo::CreateStopReasonWithExec(Thread &thread) {
1535-
return StopInfoSP(new StopInfoExec(thread));
1536+
return std::make_shared<StopInfoExec>(thread);
15361537
}
15371538

15381539
StopInfoSP StopInfo::CreateStopReasonFork(Thread &thread,
15391540
lldb::pid_t child_pid,
15401541
lldb::tid_t child_tid) {
1541-
return StopInfoSP(new StopInfoFork(thread, child_pid, child_tid));
1542+
return std::make_shared<StopInfoFork>(thread, child_pid, child_tid);
15421543
}
15431544

15441545

15451546
StopInfoSP StopInfo::CreateStopReasonVFork(Thread &thread,
15461547
lldb::pid_t child_pid,
15471548
lldb::tid_t child_tid) {
1548-
return StopInfoSP(new StopInfoVFork(thread, child_pid, child_tid));
1549+
return std::make_shared<StopInfoVFork>(thread, child_pid, child_tid);
15491550
}
15501551

15511552
StopInfoSP StopInfo::CreateStopReasonVForkDone(Thread &thread) {
1552-
return StopInfoSP(new StopInfoVForkDone(thread));
1553+
return std::make_shared<StopInfoVForkDone>(thread);
15531554
}
15541555

15551556
ValueObjectSP StopInfo::GetReturnValueObject(StopInfoSP &stop_info_sp) {

0 commit comments

Comments
 (0)