summaryrefslogtreecommitdiff
path: root/chromium/base/metrics/ranges_manager.cc
blob: caea82b12cfee33700ed61d44f605208c0b6d8d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/metrics/ranges_manager.h"

namespace base {

RangesManager::RangesManager() = default;

RangesManager::~RangesManager() {
  if (!do_not_release_ranges_on_destroy_for_testing_)
    ReleaseBucketRanges();
}

size_t RangesManager::BucketRangesHash::operator()(
    const BucketRanges* const a) const {
  return a->checksum();
}

bool RangesManager::BucketRangesEqual::operator()(
    const BucketRanges* const a,
    const BucketRanges* const b) const {
  return a->Equals(b);
}

const BucketRanges* RangesManager::RegisterOrDeleteDuplicateRanges(
    const BucketRanges* ranges) {
  DCHECK(ranges->HasValidChecksum());

  // Attempt to insert |ranges| into the set of registered BucketRanges. If an
  // equivalent one already exists (one with the exact same ranges), this
  // fetches the pre-existing one and does not insert the passed |ranges|.
  const BucketRanges* const registered = *ranges_.insert(ranges).first;

  // If there is already a registered equivalent BucketRanges, delete the passed
  // |ranges|.
  if (registered != ranges)
    delete ranges;

  return registered;
}

std::vector<const BucketRanges*> RangesManager::GetBucketRanges() {
  std::vector<const BucketRanges*> out;
  out.reserve(ranges_.size());
  out.assign(ranges_.begin(), ranges_.end());
  return out;
}

void RangesManager::ReleaseBucketRanges() {
  for (auto* range : ranges_) {
    delete range;
  }
  ranges_.clear();
}

void RangesManager::DoNotReleaseRangesOnDestroyForTesting() {
  do_not_release_ranges_on_destroy_for_testing_ = true;
}

}  // namespace base