extent_ranges.h 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. //
  16. #ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_RANGES_H_
  17. #define UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_RANGES_H_
  18. #include <map>
  19. #include <set>
  20. #include <vector>
  21. #include <base/macros.h>
  22. #include "update_engine/update_metadata.pb.h"
  23. // An ExtentRanges object represents an unordered collection of extents (and
  24. // therefore blocks). Such an object may be modified by adding or subtracting
  25. // blocks (think: set addition or set subtraction). Note that ExtentRanges
  26. // ignores sparse hole extents mostly to avoid confusion between extending a
  27. // sparse hole range vs. set addition but also to ensure that the delta
  28. // generator doesn't use sparse holes as scratch space.
  29. namespace chromeos_update_engine {
  30. struct ExtentLess {
  31. bool operator()(const Extent& x, const Extent& y) const {
  32. return x.start_block() < y.start_block();
  33. }
  34. };
  35. Extent ExtentForRange(uint64_t start_block, uint64_t num_blocks);
  36. Extent ExtentForBytes(uint64_t block_size,
  37. uint64_t start_bytes,
  38. uint64_t size_bytes);
  39. class ExtentRanges {
  40. public:
  41. typedef std::set<Extent, ExtentLess> ExtentSet;
  42. ExtentRanges() : blocks_(0) {}
  43. void AddBlock(uint64_t block);
  44. void SubtractBlock(uint64_t block);
  45. void AddExtent(Extent extent);
  46. void SubtractExtent(const Extent& extent);
  47. void AddExtents(const std::vector<Extent>& extents);
  48. void SubtractExtents(const std::vector<Extent>& extents);
  49. void AddRepeatedExtents(
  50. const ::google::protobuf::RepeatedPtrField<Extent>& exts);
  51. void SubtractRepeatedExtents(
  52. const ::google::protobuf::RepeatedPtrField<Extent>& exts);
  53. void AddRanges(const ExtentRanges& ranges);
  54. void SubtractRanges(const ExtentRanges& ranges);
  55. // Returns whether the block |block| is in this ExtentRange.
  56. bool ContainsBlock(uint64_t block) const;
  57. static bool ExtentsOverlapOrTouch(const Extent& a, const Extent& b);
  58. static bool ExtentsOverlap(const Extent& a, const Extent& b);
  59. // Dumps contents to the log file. Useful for debugging.
  60. void Dump() const;
  61. uint64_t blocks() const { return blocks_; }
  62. const ExtentSet& extent_set() const { return extent_set_; }
  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. private:
  69. ExtentSet extent_set_;
  70. uint64_t blocks_;
  71. };
  72. // Filters out from the passed list of extents |extents| all the blocks in the
  73. // ExtentRanges set. Note that the order of the blocks in |extents| is preserved
  74. // omitting blocks present in the ExtentRanges |ranges|.
  75. std::vector<Extent> FilterExtentRanges(const std::vector<Extent>& extents,
  76. const ExtentRanges& ranges);
  77. } // namespace chromeos_update_engine
  78. #endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_RANGES_H_