| Index: components/metrics/leak_detector/leak_detector.cc
|
| diff --git a/components/metrics/leak_detector/leak_detector.cc b/components/metrics/leak_detector/leak_detector.cc
|
| index a7151a86382990e1313982dc2be1c41bfd2d287b..45fb32e698e15cad51133df918717b997fe55652 100644
|
| --- a/components/metrics/leak_detector/leak_detector.cc
|
| +++ b/components/metrics/leak_detector/leak_detector.cc
|
| @@ -6,6 +6,8 @@
|
|
|
| #include <stdint.h>
|
|
|
| +#include <algorithm>
|
| +
|
| #include "base/allocator/allocator_extension.h"
|
| #include "base/bind.h"
|
| #include "base/lazy_instance.h"
|
| @@ -24,7 +26,6 @@
|
|
|
| namespace metrics {
|
|
|
| -using LeakReport = LeakDetector::LeakReport;
|
| using InternalLeakReport = leak_detector::LeakDetectorImpl::LeakReport;
|
| template <typename T>
|
| using InternalVector = leak_detector::LeakDetectorImpl::InternalVector<T>;
|
| @@ -100,25 +101,26 @@ inline uint64_t PointerToHash(const void* ptr) {
|
| return reinterpret_cast<uint64_t>(ptr) * kMultiplier;
|
| }
|
|
|
| -// Converts a vector of leak reports generated by LeakDetectorImpl
|
| -// (InternalLeakReport) to a vector of leak reports suitable for sending to
|
| -// LeakDetector's observers (LeakReport).
|
| -void GetReportsForObservers(
|
| - const InternalVector<InternalLeakReport>& leak_reports,
|
| - std::vector<LeakReport>* reports_for_observers) {
|
| - reports_for_observers->clear();
|
| - reports_for_observers->reserve(leak_reports.size());
|
| - for (const InternalLeakReport& report : leak_reports) {
|
| - reports_for_observers->push_back(LeakReport());
|
| - LeakReport* new_report = &reports_for_observers->back();
|
| -
|
| - new_report->alloc_size_bytes = report.alloc_size_bytes();
|
| - if (!report.call_stack().empty()) {
|
| - new_report->call_stack.resize(report.call_stack().size());
|
| - memcpy(new_report->call_stack.data(), report.call_stack().data(),
|
| - report.call_stack().size() * sizeof(report.call_stack()[0]));
|
| +// Converts an memory leak report generated by LeakDetectorImpl
|
| +// (InternalLeakReport) to protobuf format (MemoryLeakReportProto).
|
| +MemoryLeakReportProto ConvertLeakReportToProto(
|
| + const InternalLeakReport& report) {
|
| + MemoryLeakReportProto proto;
|
| +
|
| + proto.set_size_bytes(report.alloc_size_bytes());
|
| + for (auto call_stack_entry : report.call_stack()) {
|
| + proto.add_call_stack(call_stack_entry);
|
| + }
|
| +
|
| + for (const auto& entry : report.alloc_breakdown_history()) {
|
| + auto* proto_entry = proto.add_alloc_breakdown_history();
|
| + for (const uint32_t count : entry.counts_by_size) {
|
| + proto_entry->add_counts_by_size(count);
|
| }
|
| + proto_entry->set_count_for_call_stack(entry.count_for_call_stack);
|
| }
|
| +
|
| + return proto;
|
| }
|
|
|
| // The only instance of LeakDetector that should be used.
|
| @@ -153,12 +155,6 @@ inline void StoreHookDataToTLS(HookData hook_data) {
|
|
|
| } // namespace
|
|
|
| -LeakDetector::LeakReport::LeakReport() {}
|
| -
|
| -LeakDetector::LeakReport::LeakReport(const LeakReport& other) = default;
|
| -
|
| -LeakDetector::LeakReport::~LeakReport() {}
|
| -
|
| // static
|
| LeakDetector* LeakDetector::GetInstance() {
|
| return g_instance.Pointer();
|
| @@ -274,9 +270,10 @@ void LeakDetector::AllocHook(const void* ptr, size_t size) {
|
| detector->impl_->TestForLeaks(&leak_reports);
|
|
|
| // Pass leak reports to observers.
|
| - std::vector<LeakReport> leak_reports_for_observers;
|
| - GetReportsForObservers(leak_reports, &leak_reports_for_observers);
|
| - detector->NotifyObservers(leak_reports_for_observers);
|
| + std::vector<MemoryLeakReportProto> leak_report_protos;
|
| + std::transform(leak_reports.begin(), leak_reports.end(),
|
| + leak_report_protos.begin(), &ConvertLeakReportToProto);
|
| + detector->NotifyObservers(leak_report_protos);
|
| }
|
| }
|
|
|
| @@ -319,7 +316,8 @@ inline bool LeakDetector::ShouldSample(const void* ptr) const {
|
| return PointerToHash(ptr) < sampling_factor_;
|
| }
|
|
|
| -void LeakDetector::NotifyObservers(const std::vector<LeakReport>& reports) {
|
| +void LeakDetector::NotifyObservers(
|
| + const std::vector<MemoryLeakReportProto>& reports) {
|
| if (reports.empty())
|
| return;
|
|
|
| @@ -331,9 +329,9 @@ void LeakDetector::NotifyObservers(const std::vector<LeakReport>& reports) {
|
| return;
|
| }
|
|
|
| - for (const LeakReport& report : reports) {
|
| + {
|
| base::AutoLock lock(observers_lock_);
|
| - FOR_EACH_OBSERVER(Observer, observers_, OnLeakFound(report));
|
| + FOR_EACH_OBSERVER(Observer, observers_, OnLeaksFound(reports));
|
| }
|
| }
|
|
|
|
|