extent_utils.h revision 52490e776a9d34a94fb18ab1fe7d416425e94dda
1// Copyright 2015 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 UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_UTILS_H_
6#define UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_UTILS_H_
7
8#include <vector>
9
10#include "update_engine/update_metadata.pb.h"
11
12// Utility functions for manipulating Extents and lists of blocks.
13
14namespace chromeos_update_engine {
15
16// |block| must either be the next block in the last extent or a block
17// in the next extent. This function will not handle inserting block
18// into an arbitrary place in the extents.
19void AppendBlockToExtents(std::vector<Extent>* extents, uint64_t block);
20
21// Get/SetElement are intentionally overloaded so that templated functions
22// can accept either type of collection of Extents.
23Extent GetElement(const std::vector<Extent>& collection, size_t index);
24Extent GetElement(
25    const google::protobuf::RepeatedPtrField<Extent>& collection,
26    size_t index);
27
28template<typename T>
29uint64_t BlocksInExtents(const T& collection) {
30  uint64_t ret = 0;
31  for (size_t i = 0; i < static_cast<size_t>(collection.size()); ++i) {
32    ret += GetElement(collection, i).num_blocks();
33  }
34  return ret;
35}
36
37// Takes a vector of extents and normalizes those extents. Expects the extents
38// to be sorted by start block. E.g. if |extents| is [(1, 2), (3, 5), (10, 2)]
39// then |extents| will be changed to [(1, 7), (10, 2)].
40void NormalizeExtents(std::vector<Extent>* extents);
41
42// Return a subsequence of the list of blocks passed. Both the passed list of
43// blocks |extents| and the return value are expressed as a list of Extent, not
44// blocks. The returned list skips the first |block_offset| blocks from the
45// |extents| and cotains |block_count| blocks (or less if |extents| is shorter).
46std::vector<Extent> ExtentsSublist(const std::vector<Extent>& extents,
47                                   uint64_t block_offset, uint64_t block_count);
48
49bool operator==(const Extent& a, const Extent& b);
50
51}  // namespace chromeos_update_engine
52
53#endif  // UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_UTILS_H_
54