blob: f14bb61c6ddcf0b20371f80e0cf818d1ea895f46 [file] [log] [blame]
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -07001// 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 Deymo1beda782015-06-07 23:01:25 +02005#ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_RANGES_H_
6#define UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_RANGES_H_
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -07007
8#include <map>
9#include <set>
10#include <vector>
11
Ben Chan05735a12014-09-03 07:48:22 -070012#include <base/macros.h>
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070013
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070014#include "update_engine/update_metadata.pb.h"
15
Darin Petkov94817cb2013-05-08 14:33:24 +020016// 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 Reyes5fdae4a2010-10-05 10:47:42 -070022
23namespace chromeos_update_engine {
24
25struct ExtentLess {
26 bool operator()(const Extent& x, const Extent& y) const {
27 return x.start_block() < y.start_block();
28 }
29};
30
31Extent ExtentForRange(uint64_t start_block, uint64_t num_blocks);
32
33class 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 Deymof0061352015-07-01 14:59:15 -070051 // Returns whether the block |block| is in this ExtentRange.
52 bool ContainsBlock(uint64_t block) const;
53
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070054 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 Petkov94817cb2013-05-08 14:33:24 +020059
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070060 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 Deymoa376a6e2015-06-05 00:31:34 +020074// 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|.
77std::vector<Extent> FilterExtentRanges(const std::vector<Extent>& extents,
78 const ExtentRanges& ranges);
79
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070080} // namespace chromeos_update_engine
81
Alex Deymo1beda782015-06-07 23:01:25 +020082#endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_RANGES_H_