blob: e9ab9ebfbae70d94241cc0e5f4724ce50bbd3360 [file] [log] [blame]
Alex Deymo5c6c6552015-06-03 19:06:50 +02001// 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
Alex Deymo52490e72015-06-04 14:53:44 +020037// 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
Alex Deymo5c6c6552015-06-03 19:06:50 +020049bool operator==(const Extent& a, const Extent& b);
50
51} // namespace chromeos_update_engine
52
53#endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_UTILS_H_