[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).
This commit is contained in:
Jonas Devlieghere
2025-07-18 16:15:38 -07:00
committed by GitHub
parent 09bea21d95
commit 68fd102598

View File

@@ -851,8 +851,9 @@ protected:
// We have to step over the watchpoint before we know what to do:
StopInfoWatchpointSP me_as_siwp_sp
= std::static_pointer_cast<StopInfoWatchpoint>(shared_from_this());
ThreadPlanSP step_over_wp_sp(new ThreadPlanStepOverWatchpoint(
*(thread_sp.get()), me_as_siwp_sp, wp_sp));
ThreadPlanSP step_over_wp_sp =
std::make_shared<ThreadPlanStepOverWatchpoint>(*(thread_sp.get()),
me_as_siwp_sp, wp_sp);
// When this plan is done we want to stop, so set this as a Controlling
// plan.
step_over_wp_sp->SetIsControllingPlan(true);
@@ -1475,13 +1476,13 @@ StopInfoSP StopInfo::CreateStopReasonWithBreakpointSiteID(Thread &thread,
break_id_t break_id) {
thread.SetThreadHitBreakpointSite();
return StopInfoSP(new StopInfoBreakpoint(thread, break_id));
return std::make_shared<StopInfoBreakpoint>(thread, break_id);
}
StopInfoSP StopInfo::CreateStopReasonWithBreakpointSiteID(Thread &thread,
break_id_t break_id,
bool should_stop) {
return StopInfoSP(new StopInfoBreakpoint(thread, break_id, should_stop));
return std::make_shared<StopInfoBreakpoint>(thread, break_id, should_stop);
}
// LWP_TODO: We'll need a CreateStopReasonWithWatchpointResourceID akin
@@ -1489,67 +1490,67 @@ StopInfoSP StopInfo::CreateStopReasonWithBreakpointSiteID(Thread &thread,
StopInfoSP StopInfo::CreateStopReasonWithWatchpointID(Thread &thread,
break_id_t watch_id,
bool silently_continue) {
return StopInfoSP(
new StopInfoWatchpoint(thread, watch_id, silently_continue));
return std::make_shared<StopInfoWatchpoint>(thread, watch_id,
silently_continue);
}
StopInfoSP StopInfo::CreateStopReasonWithSignal(Thread &thread, int signo,
const char *description,
std::optional<int> code) {
thread.GetProcess()->GetUnixSignals()->IncrementSignalHitCount(signo);
return StopInfoSP(new StopInfoUnixSignal(thread, signo, description, code));
return std::make_shared<StopInfoUnixSignal>(thread, signo, description, code);
}
StopInfoSP StopInfo::CreateStopReasonWithInterrupt(Thread &thread, int signo,
const char *description) {
return StopInfoSP(new StopInfoInterrupt(thread, signo, description));
return std::make_shared<StopInfoInterrupt>(thread, signo, description);
}
StopInfoSP StopInfo::CreateStopReasonToTrace(Thread &thread) {
return StopInfoSP(new StopInfoTrace(thread));
return std::make_shared<StopInfoTrace>(thread);
}
StopInfoSP StopInfo::CreateStopReasonWithPlan(
ThreadPlanSP &plan_sp, ValueObjectSP return_valobj_sp,
ExpressionVariableSP expression_variable_sp) {
return StopInfoSP(new StopInfoThreadPlan(plan_sp, return_valobj_sp,
expression_variable_sp));
return std::make_shared<StopInfoThreadPlan>(plan_sp, return_valobj_sp,
expression_variable_sp);
}
StopInfoSP StopInfo::CreateStopReasonWithException(Thread &thread,
const char *description) {
return StopInfoSP(new StopInfoException(thread, description));
return std::make_shared<StopInfoException>(thread, description);
}
StopInfoSP StopInfo::CreateStopReasonProcessorTrace(Thread &thread,
const char *description) {
return StopInfoSP(new StopInfoProcessorTrace(thread, description));
return std::make_shared<StopInfoProcessorTrace>(thread, description);
}
StopInfoSP StopInfo::CreateStopReasonHistoryBoundary(Thread &thread,
const char *description) {
return StopInfoSP(new StopInfoHistoryBoundary(thread, description));
return std::make_shared<StopInfoHistoryBoundary>(thread, description);
}
StopInfoSP StopInfo::CreateStopReasonWithExec(Thread &thread) {
return StopInfoSP(new StopInfoExec(thread));
return std::make_shared<StopInfoExec>(thread);
}
StopInfoSP StopInfo::CreateStopReasonFork(Thread &thread,
lldb::pid_t child_pid,
lldb::tid_t child_tid) {
return StopInfoSP(new StopInfoFork(thread, child_pid, child_tid));
return std::make_shared<StopInfoFork>(thread, child_pid, child_tid);
}
StopInfoSP StopInfo::CreateStopReasonVFork(Thread &thread,
lldb::pid_t child_pid,
lldb::tid_t child_tid) {
return StopInfoSP(new StopInfoVFork(thread, child_pid, child_tid));
return std::make_shared<StopInfoVFork>(thread, child_pid, child_tid);
}
StopInfoSP StopInfo::CreateStopReasonVForkDone(Thread &thread) {
return StopInfoSP(new StopInfoVForkDone(thread));
return std::make_shared<StopInfoVForkDone>(thread);
}
ValueObjectSP StopInfo::GetReturnValueObject(StopInfoSP &stop_info_sp) {