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 | |
| 5 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_EXTENT_RANGES_H__ |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_EXTENT_RANGES_H__ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <set> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include <base/basictypes.h> |
| 13 | |
| 14 | #include "update_engine/delta_diff_generator.h" |
| 15 | #include "update_engine/graph_types.h" |
| 16 | #include "update_engine/update_metadata.pb.h" |
| 17 | |
| 18 | // An ExtentRanges object represents an unordered collection of extents |
| 19 | // (and therefore blocks). Such an object may be modified by adding or |
| 20 | // subtracting blocks (think: set addition or set subtraction). |
| 21 | |
| 22 | namespace chromeos_update_engine { |
| 23 | |
| 24 | struct ExtentLess { |
| 25 | bool operator()(const Extent& x, const Extent& y) const { |
| 26 | return x.start_block() < y.start_block(); |
| 27 | } |
| 28 | }; |
| 29 | |
| 30 | Extent ExtentForRange(uint64_t start_block, uint64_t num_blocks); |
| 31 | |
| 32 | class ExtentRanges { |
| 33 | public: |
| 34 | typedef std::set<Extent, ExtentLess> ExtentSet; |
| 35 | |
| 36 | ExtentRanges() : blocks_(0) {} |
| 37 | void AddBlock(uint64_t block); |
| 38 | void SubtractBlock(uint64_t block); |
| 39 | void AddExtent(Extent extent); |
| 40 | void SubtractExtent(const Extent& extent); |
| 41 | void AddExtents(const std::vector<Extent>& extents); |
| 42 | void SubtractExtents(const std::vector<Extent>& extents); |
| 43 | void AddRepeatedExtents( |
| 44 | const ::google::protobuf::RepeatedPtrField<Extent> &exts); |
| 45 | void SubtractRepeatedExtents( |
| 46 | const ::google::protobuf::RepeatedPtrField<Extent> &exts); |
| 47 | void AddRanges(const ExtentRanges& ranges); |
| 48 | void SubtractRanges(const ExtentRanges& ranges); |
| 49 | |
| 50 | static bool ExtentsOverlapOrTouch(const Extent& a, const Extent& b); |
| 51 | static bool ExtentsOverlap(const Extent& a, const Extent& b); |
| 52 | |
| 53 | // Dumps contents to the log file. Useful for debugging. |
| 54 | void Dump() const; |
| 55 | |
| 56 | uint64_t blocks() const { return blocks_; } |
| 57 | const ExtentSet& extent_set() const { return extent_set_; } |
| 58 | |
| 59 | // Returns an ordered vector of extents for |count| blocks, |
| 60 | // using extents in extent_set_. The returned extents are not |
| 61 | // removed from extent_set_. |count| must be less than or equal to |
| 62 | // the number of blocks in this extent set. |
| 63 | std::vector<Extent> GetExtentsForBlockCount(uint64_t count) const; |
| 64 | |
| 65 | private: |
| 66 | ExtentSet extent_set_; |
| 67 | uint64_t blocks_; |
| 68 | }; |
| 69 | |
| 70 | } // namespace chromeos_update_engine |
| 71 | |
| 72 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_EXTENT_RANGES_H__ |