blob: 1583618112cc298c6d9ce044105c3426959fd25f [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
37bool operator==(const Extent& a, const Extent& b);
38
39} // namespace chromeos_update_engine
40
41#endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_UTILS_H_