| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "courgette/label_manager.h" | 5 #include "courgette/label_manager.h" |
| 6 | 6 |
| 7 #include <stddef.h> |
| 8 #include <stdint.h> |
| 9 |
| 7 #include <algorithm> | 10 #include <algorithm> |
| 8 | 11 |
| 9 #include "base/logging.h" | 12 #include "base/logging.h" |
| 10 #include "base/numerics/safe_math.h" | 13 #include "base/numerics/safe_math.h" |
| 11 #include "courgette/consecutive_range_visitor.h" | 14 #include "courgette/consecutive_range_visitor.h" |
| 12 | 15 |
| 13 namespace courgette { | 16 namespace courgette { |
| 14 | 17 |
| 15 LabelManager::RvaVisitor::~RvaVisitor() {} | 18 LabelManager::RvaVisitor::~RvaVisitor() {} |
| 16 | 19 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 40 std::sort(rvas.begin(), rvas.end()); | 43 std::sort(rvas.begin(), rvas.end()); |
| 41 size_t num_distinct_rva = 0; | 44 size_t num_distinct_rva = 0; |
| 42 for (CRV it(rvas.begin(), rvas.end()); it.has_more(); it.advance()) | 45 for (CRV it(rvas.begin(), rvas.end()); it.has_more(); it.advance()) |
| 43 ++num_distinct_rva; | 46 ++num_distinct_rva; |
| 44 | 47 |
| 45 // Reserve space for |labels_|, populate with sorted RVA and repeats. | 48 // Reserve space for |labels_|, populate with sorted RVA and repeats. |
| 46 DCHECK(labels_.empty()); | 49 DCHECK(labels_.empty()); |
| 47 labels_.reserve(num_distinct_rva); | 50 labels_.reserve(num_distinct_rva); |
| 48 for (CRV it(rvas.begin(), rvas.end()); it.has_more(); it.advance()) { | 51 for (CRV it(rvas.begin(), rvas.end()); it.has_more(); it.advance()) { |
| 49 labels_.push_back(Label(*it.cur())); | 52 labels_.push_back(Label(*it.cur())); |
| 50 base::CheckedNumeric<uint32> count = it.repeat(); | 53 base::CheckedNumeric<uint32_t> count = it.repeat(); |
| 51 labels_.back().count_ = count.ValueOrDie(); | 54 labels_.back().count_ = count.ValueOrDie(); |
| 52 } | 55 } |
| 53 } | 56 } |
| 54 | 57 |
| 55 void LabelManager::RemoveUnderusedLabels(int32 count_threshold) { | 58 void LabelManager::RemoveUnderusedLabels(int32_t count_threshold) { |
| 56 if (count_threshold <= 0) | 59 if (count_threshold <= 0) |
| 57 return; | 60 return; |
| 58 labels_.erase(std::remove_if(labels_.begin(), labels_.end(), | 61 labels_.erase(std::remove_if(labels_.begin(), labels_.end(), |
| 59 [count_threshold](const Label& label) { | 62 [count_threshold](const Label& label) { |
| 60 return label.count_ < count_threshold; | 63 return label.count_ < count_threshold; |
| 61 }), | 64 }), |
| 62 labels_.end()); | 65 labels_.end()); |
| 63 // Not shrinking |labels_|, since this may cause reallocation. | 66 // Not shrinking |labels_|, since this may cause reallocation. |
| 64 } | 67 } |
| 65 | 68 |
| 66 // Uses binary search to find |rva|. | 69 // Uses binary search to find |rva|. |
| 67 Label* LabelManager::Find(RVA rva) { | 70 Label* LabelManager::Find(RVA rva) { |
| 68 auto it = std::lower_bound( | 71 auto it = std::lower_bound( |
| 69 labels_.begin(), labels_.end(), Label(rva), | 72 labels_.begin(), labels_.end(), Label(rva), |
| 70 [](const Label& l1, const Label& l2) { return l1.rva_ < l2.rva_; }); | 73 [](const Label& l1, const Label& l2) { return l1.rva_ < l2.rva_; }); |
| 71 return it == labels_.end() || it->rva_ != rva ? nullptr : &(*it); | 74 return it == labels_.end() || it->rva_ != rva ? nullptr : &(*it); |
| 72 } | 75 } |
| 73 | 76 |
| 74 } // namespace courgette | 77 } // namespace courgette |
| OLD | NEW |