blob: 8e3e2619773bd3501f68341e0397f02b55cb3112 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2010 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070016
Alex Deymo1beda782015-06-07 23:01:25 +020017#ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_RANGES_H_
18#define UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_RANGES_H_
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070019
20#include <map>
21#include <set>
22#include <vector>
23
Ben Chan05735a12014-09-03 07:48:22 -070024#include <base/macros.h>
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070025
Kelvin Zhangd567c8b2021-07-08 14:10:23 -040026#include "update_engine/common/utils.h"
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070027#include "update_engine/update_metadata.pb.h"
28
Darin Petkov94817cb2013-05-08 14:33:24 +020029// An ExtentRanges object represents an unordered collection of extents (and
30// therefore blocks). Such an object may be modified by adding or subtracting
31// blocks (think: set addition or set subtraction). Note that ExtentRanges
32// ignores sparse hole extents mostly to avoid confusion between extending a
33// sparse hole range vs. set addition but also to ensure that the delta
34// generator doesn't use sparse holes as scratch space.
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070035
36namespace chromeos_update_engine {
37
38struct ExtentLess {
39 bool operator()(const Extent& x, const Extent& y) const {
Kelvin Zhang9351f5d2021-08-17 19:29:49 -070040 if (x.start_block() == y.start_block()) {
41 return x.num_blocks() < y.num_blocks();
42 }
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070043 return x.start_block() < y.start_block();
44 }
45};
46
47Extent ExtentForRange(uint64_t start_block, uint64_t num_blocks);
Sen Jiang0a582fb2018-06-26 19:27:21 -070048Extent ExtentForBytes(uint64_t block_size,
49 uint64_t start_bytes,
50 uint64_t size_bytes);
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070051
52class ExtentRanges {
53 public:
54 typedef std::set<Extent, ExtentLess> ExtentSet;
55
56 ExtentRanges() : blocks_(0) {}
57 void AddBlock(uint64_t block);
58 void SubtractBlock(uint64_t block);
59 void AddExtent(Extent extent);
60 void SubtractExtent(const Extent& extent);
61 void AddExtents(const std::vector<Extent>& extents);
62 void SubtractExtents(const std::vector<Extent>& extents);
63 void AddRepeatedExtents(
Amin Hassani232f8f92019-01-14 16:15:31 -080064 const ::google::protobuf::RepeatedPtrField<Extent>& exts);
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070065 void SubtractRepeatedExtents(
Amin Hassani232f8f92019-01-14 16:15:31 -080066 const ::google::protobuf::RepeatedPtrField<Extent>& exts);
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070067 void AddRanges(const ExtentRanges& ranges);
68 void SubtractRanges(const ExtentRanges& ranges);
69
Tianjie87af6c02020-08-11 15:06:26 -070070 // Returns true if the input extent overlaps with the current ExtentRanges.
71 bool OverlapsWithExtent(const Extent& extent) const;
72
Alex Deymof0061352015-07-01 14:59:15 -070073 // Returns whether the block |block| is in this ExtentRange.
74 bool ContainsBlock(uint64_t block) const;
75
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070076 static bool ExtentsOverlapOrTouch(const Extent& a, const Extent& b);
77 static bool ExtentsOverlap(const Extent& a, const Extent& b);
78
79 // Dumps contents to the log file. Useful for debugging.
80 void Dump() const;
Darin Petkov94817cb2013-05-08 14:33:24 +020081
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070082 uint64_t blocks() const { return blocks_; }
83 const ExtentSet& extent_set() const { return extent_set_; }
84
85 // Returns an ordered vector of extents for |count| blocks,
86 // using extents in extent_set_. The returned extents are not
87 // removed from extent_set_. |count| must be less than or equal to
88 // the number of blocks in this extent set.
89 std::vector<Extent> GetExtentsForBlockCount(uint64_t count) const;
90
Kelvin Zhangd567c8b2021-07-08 14:10:23 -040091 // Compute the intersection between this ExtentRange and the |extent|
92 // parameter. Return results in a vector. If there's no intersection, an empty
93 // vector is returned.
94 std::vector<Extent> GetIntersectingExtents(const Extent& extent) const;
95
96 // Get a range of extents that possibly intersect with |extent|. (Returned
97 // extents do not necessarily intersect!). It is perfectly acceptable to just
98 // return all extents in this set, though more efficient solution using binary
99 // search is preferred.
100 Range<ExtentSet::const_iterator> GetCandidateRange(
101 const Extent& extent) const;
102
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700103 private:
104 ExtentSet extent_set_;
105 uint64_t blocks_;
106};
107
Alex Deymoa376a6e2015-06-05 00:31:34 +0200108// Filters out from the passed list of extents |extents| all the blocks in the
109// ExtentRanges set. Note that the order of the blocks in |extents| is preserved
110// omitting blocks present in the ExtentRanges |ranges|.
111std::vector<Extent> FilterExtentRanges(const std::vector<Extent>& extents,
112 const ExtentRanges& ranges);
113
Kelvin Zhangd567c8b2021-07-08 14:10:23 -0400114Extent GetOverlapExtent(const Extent& extent1, const Extent& extent2);
115
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700116} // namespace chromeos_update_engine
117
Alex Deymo1beda782015-06-07 23:01:25 +0200118#endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_RANGES_H_