blob: 2ab347ae8ee359492a6407985afac5f19b6167c3 [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
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
Darin Petkov94817cb2013-05-08 14:33:24 +020018// An ExtentRanges object represents an unordered collection of extents (and
19// therefore blocks). Such an object may be modified by adding or subtracting
20// blocks (think: set addition or set subtraction). Note that ExtentRanges
21// ignores sparse hole extents mostly to avoid confusion between extending a
22// sparse hole range vs. set addition but also to ensure that the delta
23// generator doesn't use sparse holes as scratch space.
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070024
25namespace chromeos_update_engine {
26
27struct ExtentLess {
28 bool operator()(const Extent& x, const Extent& y) const {
29 return x.start_block() < y.start_block();
30 }
31};
32
33Extent ExtentForRange(uint64_t start_block, uint64_t num_blocks);
34
35class ExtentRanges {
36 public:
37 typedef std::set<Extent, ExtentLess> ExtentSet;
38
39 ExtentRanges() : blocks_(0) {}
40 void AddBlock(uint64_t block);
41 void SubtractBlock(uint64_t block);
42 void AddExtent(Extent extent);
43 void SubtractExtent(const Extent& extent);
44 void AddExtents(const std::vector<Extent>& extents);
45 void SubtractExtents(const std::vector<Extent>& extents);
46 void AddRepeatedExtents(
47 const ::google::protobuf::RepeatedPtrField<Extent> &exts);
48 void SubtractRepeatedExtents(
49 const ::google::protobuf::RepeatedPtrField<Extent> &exts);
50 void AddRanges(const ExtentRanges& ranges);
51 void SubtractRanges(const ExtentRanges& ranges);
52
53 static bool ExtentsOverlapOrTouch(const Extent& a, const Extent& b);
54 static bool ExtentsOverlap(const Extent& a, const Extent& b);
55
56 // Dumps contents to the log file. Useful for debugging.
57 void Dump() const;
Darin Petkov94817cb2013-05-08 14:33:24 +020058
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070059 uint64_t blocks() const { return blocks_; }
60 const ExtentSet& extent_set() const { return extent_set_; }
61
62 // Returns an ordered vector of extents for |count| blocks,
63 // using extents in extent_set_. The returned extents are not
64 // removed from extent_set_. |count| must be less than or equal to
65 // the number of blocks in this extent set.
66 std::vector<Extent> GetExtentsForBlockCount(uint64_t count) const;
67
68 private:
69 ExtentSet extent_set_;
70 uint64_t blocks_;
71};
72
73} // namespace chromeos_update_engine
74
75#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_EXTENT_RANGES_H__