| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 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 Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 16 |  | 
| Alex Deymo | 1beda78 | 2015-06-07 23:01:25 +0200 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_RANGES_H_ | 
|  | 18 | #define UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_RANGES_H_ | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 19 |  | 
|  | 20 | #include <map> | 
|  | 21 | #include <set> | 
|  | 22 | #include <vector> | 
|  | 23 |  | 
| Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame] | 24 | #include <base/macros.h> | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 25 |  | 
| Kelvin Zhang | d567c8b | 2021-07-08 14:10:23 -0400 | [diff] [blame] | 26 | #include "update_engine/common/utils.h" | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 27 | #include "update_engine/update_metadata.pb.h" | 
|  | 28 |  | 
| Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 29 | // 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 Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 35 |  | 
|  | 36 | namespace chromeos_update_engine { | 
|  | 37 |  | 
|  | 38 | struct ExtentLess { | 
|  | 39 | bool operator()(const Extent& x, const Extent& y) const { | 
| Kelvin Zhang | 9351f5d | 2021-08-17 19:29:49 -0700 | [diff] [blame] | 40 | if (x.start_block() == y.start_block()) { | 
|  | 41 | return x.num_blocks() < y.num_blocks(); | 
|  | 42 | } | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 43 | return x.start_block() < y.start_block(); | 
|  | 44 | } | 
|  | 45 | }; | 
|  | 46 |  | 
|  | 47 | Extent ExtentForRange(uint64_t start_block, uint64_t num_blocks); | 
| Sen Jiang | 0a582fb | 2018-06-26 19:27:21 -0700 | [diff] [blame] | 48 | Extent ExtentForBytes(uint64_t block_size, | 
|  | 49 | uint64_t start_bytes, | 
|  | 50 | uint64_t size_bytes); | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 51 |  | 
|  | 52 | class ExtentRanges { | 
|  | 53 | public: | 
|  | 54 | typedef std::set<Extent, ExtentLess> ExtentSet; | 
|  | 55 |  | 
| Kelvin Zhang | bef99c3 | 2021-08-18 09:57:02 -0700 | [diff] [blame] | 56 | ExtentRanges() = default; | 
|  | 57 | // When |merge_touching_extents| is set to false, extents that are only | 
|  | 58 | // touching but not overlapping won't be merged. This slightly decreases | 
|  | 59 | // space/time efficiency, but should not impact correctness. | 
|  | 60 | // Only intended usecase is for VABC XOR. | 
|  | 61 | // E.g. [5-9] and [10-14] will be merged iff |merge_touching_extents| is true | 
|  | 62 | explicit ExtentRanges(bool merge_touching_extents) | 
|  | 63 | : merge_touching_extents_(merge_touching_extents) {} | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 64 | void AddBlock(uint64_t block); | 
|  | 65 | void SubtractBlock(uint64_t block); | 
|  | 66 | void AddExtent(Extent extent); | 
|  | 67 | void SubtractExtent(const Extent& extent); | 
|  | 68 | void AddExtents(const std::vector<Extent>& extents); | 
|  | 69 | void SubtractExtents(const std::vector<Extent>& extents); | 
|  | 70 | void AddRepeatedExtents( | 
| Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 71 | const ::google::protobuf::RepeatedPtrField<Extent>& exts); | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 72 | void SubtractRepeatedExtents( | 
| Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 73 | const ::google::protobuf::RepeatedPtrField<Extent>& exts); | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 74 | void AddRanges(const ExtentRanges& ranges); | 
|  | 75 | void SubtractRanges(const ExtentRanges& ranges); | 
|  | 76 |  | 
| Tianjie | 87af6c0 | 2020-08-11 15:06:26 -0700 | [diff] [blame] | 77 | // Returns true if the input extent overlaps with the current ExtentRanges. | 
|  | 78 | bool OverlapsWithExtent(const Extent& extent) const; | 
|  | 79 |  | 
| Alex Deymo | f006135 | 2015-07-01 14:59:15 -0700 | [diff] [blame] | 80 | // Returns whether the block |block| is in this ExtentRange. | 
|  | 81 | bool ContainsBlock(uint64_t block) const; | 
|  | 82 |  | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 83 | static bool ExtentsOverlapOrTouch(const Extent& a, const Extent& b); | 
|  | 84 | static bool ExtentsOverlap(const Extent& a, const Extent& b); | 
|  | 85 |  | 
|  | 86 | // Dumps contents to the log file. Useful for debugging. | 
|  | 87 | void Dump() const; | 
| Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 88 |  | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 89 | uint64_t blocks() const { return blocks_; } | 
|  | 90 | const ExtentSet& extent_set() const { return extent_set_; } | 
|  | 91 |  | 
|  | 92 | // Returns an ordered vector of extents for |count| blocks, | 
|  | 93 | // using extents in extent_set_. The returned extents are not | 
|  | 94 | // removed from extent_set_. |count| must be less than or equal to | 
|  | 95 | // the number of blocks in this extent set. | 
|  | 96 | std::vector<Extent> GetExtentsForBlockCount(uint64_t count) const; | 
|  | 97 |  | 
| Kelvin Zhang | d567c8b | 2021-07-08 14:10:23 -0400 | [diff] [blame] | 98 | // Compute the intersection between this ExtentRange and the |extent| | 
|  | 99 | // parameter. Return results in a vector. If there's no intersection, an empty | 
|  | 100 | // vector is returned. | 
|  | 101 | std::vector<Extent> GetIntersectingExtents(const Extent& extent) const; | 
|  | 102 |  | 
|  | 103 | // Get a range of extents that possibly intersect with |extent|. (Returned | 
|  | 104 | // extents do not necessarily intersect!). It is perfectly acceptable to just | 
|  | 105 | // return all extents in this set, though more efficient solution using binary | 
|  | 106 | // search is preferred. | 
|  | 107 | Range<ExtentSet::const_iterator> GetCandidateRange( | 
|  | 108 | const Extent& extent) const; | 
|  | 109 |  | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 110 | private: | 
|  | 111 | ExtentSet extent_set_; | 
| Kelvin Zhang | bef99c3 | 2021-08-18 09:57:02 -0700 | [diff] [blame] | 112 | uint64_t blocks_ = 0; | 
| Kelvin Zhang | 76f10b8 | 2021-06-25 18:45:46 -0400 | [diff] [blame] | 113 | bool merge_touching_extents_ = true; | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 114 | }; | 
|  | 115 |  | 
| Alex Deymo | a376a6e | 2015-06-05 00:31:34 +0200 | [diff] [blame] | 116 | // Filters out from the passed list of extents |extents| all the blocks in the | 
|  | 117 | // ExtentRanges set. Note that the order of the blocks in |extents| is preserved | 
|  | 118 | // omitting blocks present in the ExtentRanges |ranges|. | 
|  | 119 | std::vector<Extent> FilterExtentRanges(const std::vector<Extent>& extents, | 
|  | 120 | const ExtentRanges& ranges); | 
|  | 121 |  | 
| Kelvin Zhang | d567c8b | 2021-07-08 14:10:23 -0400 | [diff] [blame] | 122 | Extent GetOverlapExtent(const Extent& extent1, const Extent& extent2); | 
|  | 123 |  | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 124 | }  // namespace chromeos_update_engine | 
|  | 125 |  | 
| Alex Deymo | 1beda78 | 2015-06-07 23:01:25 +0200 | [diff] [blame] | 126 | #endif  // UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_RANGES_H_ |