From 16bc7b48b5c313a6156bf33d8f9ad96cd922bbc8 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sat, 21 Jan 2017 14:33:17 -0800 Subject: [PATCH] Make profile_items use unique_ptr instead of raw pointers --- src/parser.cpp | 12 ++++++------ src/parser.h | 7 +++++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/parser.cpp b/src/parser.cpp index d50d9ecec..767f8013b 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -234,20 +234,20 @@ void parser_t::forbid_function(const wcstring &function) { forbidden_function.pu void parser_t::allow_function() { forbidden_function.pop_back(); } /// Print profiling information to the specified stream. -static void print_profile(const std::vector &items, FILE *out) { +static void print_profile(const std::vector> &items, FILE *out) { for (size_t pos = 0; pos < items.size(); pos++) { const profile_item_t *me, *prev; size_t i; int my_time; - me = items.at(pos); + me = items.at(pos).get(); if (me->skipped) { continue; } my_time = me->parse + me->exec; for (i = pos + 1; i < items.size(); i++) { - prev = items.at(i); + prev = items.at(i).get(); if (prev->skipped) { continue; } @@ -569,10 +569,10 @@ job_t *parser_t::job_get_from_pid(int pid) { } profile_item_t *parser_t::create_profile_item() { - profile_item_t *result = NULL; + profile_item_t *result = nullptr; if (g_profiling_active) { - result = new profile_item_t(); - profile_items.push_back(result); + profile_items.emplace_back(new profile_item_t()); + result = profile_items.back().get(); } return result; } diff --git a/src/parser.h b/src/parser.h index 380c70fcb..e4d3603b8 100644 --- a/src/parser.h +++ b/src/parser.h @@ -196,8 +196,11 @@ class parser_t { wcstring block_stack_description() const; #endif - /// List of profile items, allocated with new. - std::vector profile_items; + /// List of profile items + /// These are pointers because we return pointers to them to callers, + /// who may hold them across blocks (which would cause reallocations internal + /// to profile_items) + std::vector> profile_items; // No copying allowed. parser_t(const parser_t &);