Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Alex Deymo | 1beda78 | 2015-06-07 23:01:25 +0200 | [diff] [blame] | 5 | #ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_RANGES_H_ |
| 6 | #define UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_RANGES_H_ |
Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 7 | |
| 8 | #include <map> |
| 9 | #include <set> |
| 10 | #include <vector> |
| 11 | |
Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame] | 12 | #include <base/macros.h> |
Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 13 | |
Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 14 | #include "update_engine/update_metadata.pb.h" |
| 15 | |
Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 16 | // An ExtentRanges object represents an unordered collection of extents (and |
| 17 | // therefore blocks). Such an object may be modified by adding or subtracting |
| 18 | // blocks (think: set addition or set subtraction). Note that ExtentRanges |
| 19 | // ignores sparse hole extents mostly to avoid confusion between extending a |
| 20 | // sparse hole range vs. set addition but also to ensure that the delta |
| 21 | // generator doesn't use sparse holes as scratch space. |
Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 22 | |
| 23 | namespace chromeos_update_engine { |
| 24 | |
| 25 | struct ExtentLess { |
| 26 | bool operator()(const Extent& x, const Extent& y) const { |
| 27 | return x.start_block() < y.start_block(); |
| 28 | } |
| 29 | }; |
| 30 | |
| 31 | Extent ExtentForRange(uint64_t start_block, uint64_t num_blocks); |
| 32 | |
| 33 | class ExtentRanges { |
| 34 | public: |
| 35 | typedef std::set<Extent, ExtentLess> ExtentSet; |
| 36 | |
| 37 | ExtentRanges() : blocks_(0) {} |
| 38 | void AddBlock(uint64_t block); |
| 39 | void SubtractBlock(uint64_t block); |
| 40 | void AddExtent(Extent extent); |
| 41 | void SubtractExtent(const Extent& extent); |
| 42 | void AddExtents(const std::vector<Extent>& extents); |
| 43 | void SubtractExtents(const std::vector<Extent>& extents); |
| 44 | void AddRepeatedExtents( |
| 45 | const ::google::protobuf::RepeatedPtrField<Extent> &exts); |
| 46 | void SubtractRepeatedExtents( |
| 47 | const ::google::protobuf::RepeatedPtrField<Extent> &exts); |
| 48 | void AddRanges(const ExtentRanges& ranges); |
| 49 | void SubtractRanges(const ExtentRanges& ranges); |
| 50 | |
Alex Deymo | f006135 | 2015-07-01 14:59:15 -0700 | [diff] [blame] | 51 | // Returns whether the block |block| is in this ExtentRange. |
| 52 | bool ContainsBlock(uint64_t block) const; |
| 53 | |
Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 54 | static bool ExtentsOverlapOrTouch(const Extent& a, const Extent& b); |
| 55 | static bool ExtentsOverlap(const Extent& a, const Extent& b); |
| 56 | |
| 57 | // Dumps contents to the log file. Useful for debugging. |
| 58 | void Dump() const; |
Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 59 | |
Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 60 | uint64_t blocks() const { return blocks_; } |
| 61 | const ExtentSet& extent_set() const { return extent_set_; } |
| 62 | |
| 63 | // Returns an ordered vector of extents for |count| blocks, |
| 64 | // using extents in extent_set_. The returned extents are not |
| 65 | // removed from extent_set_. |count| must be less than or equal to |
| 66 | // the number of blocks in this extent set. |
| 67 | std::vector<Extent> GetExtentsForBlockCount(uint64_t count) const; |
| 68 | |
| 69 | private: |
| 70 | ExtentSet extent_set_; |
| 71 | uint64_t blocks_; |
| 72 | }; |
| 73 | |
Alex Deymo | a376a6e | 2015-06-05 00:31:34 +0200 | [diff] [blame] | 74 | // Filters out from the passed list of extents |extents| all the blocks in the |
| 75 | // ExtentRanges set. Note that the order of the blocks in |extents| is preserved |
| 76 | // omitting blocks present in the ExtentRanges |ranges|. |
| 77 | std::vector<Extent> FilterExtentRanges(const std::vector<Extent>& extents, |
| 78 | const ExtentRanges& ranges); |
| 79 | |
Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 80 | } // namespace chromeos_update_engine |
| 81 | |
Alex Deymo | 1beda78 | 2015-06-07 23:01:25 +0200 | [diff] [blame] | 82 | #endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_RANGES_H_ |