blob: 04897931767fe9f835487797bc74951304d316e5 [file] [log] [blame]
Alex Deymo759c2752014-03-17 21:09:36 -07001// Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Alex Deymo161c4a12014-05-16 15:56:21 -07005#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_GENERATOR_GRAPH_UTILS_H_
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_GENERATOR_GRAPH_UTILS_H_
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -08007
8#include <vector>
Alex Deymo161c4a12014-05-16 15:56:21 -07009
10#include <base/basictypes.h>
11
12#include "update_engine/payload_generator/graph_types.h"
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -080013#include "update_engine/update_metadata.pb.h"
14
15// A few utility functions for graphs
16
17namespace chromeos_update_engine {
18
19namespace graph_utils {
20
21// Returns the number of blocks represented by all extents in the edge.
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070022uint64_t EdgeWeight(const Graph& graph, const Edge& edge);
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -080023
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -070024// These add a read-before dependency from graph[src] -> graph[dst]. If the dep
25// already exists, the block/s is/are added to the existing edge.
26void AddReadBeforeDep(Vertex* src,
27 Vertex::Index dst,
28 uint64_t block);
29void AddReadBeforeDepExtents(Vertex* src,
30 Vertex::Index dst,
31 const std::vector<Extent>& extents);
32
33void DropWriteBeforeDeps(Vertex::EdgeMap* edge_map);
34
35// For each node N in graph, drop all edges N->|index|.
36void DropIncomingEdgesTo(Graph* graph, Vertex::Index index);
37
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -080038// block must either be the next block in the last extent or a block
39// in the next extent. This function will not handle inserting block
40// into an arbitrary place in the extents.
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070041void AppendBlockToExtents(std::vector<Extent>* extents, uint64_t block);
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -080042
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -070043// Get/SetElement are intentionally overloaded so that templated functions
44// can accept either type of collection of Extents.
45Extent GetElement(const std::vector<Extent>& collection, size_t index);
46Extent GetElement(
47 const google::protobuf::RepeatedPtrField<Extent>& collection,
48 size_t index);
49
50template<typename T>
51uint64_t BlocksInExtents(const T& collection) {
52 uint64_t ret = 0;
53 for (size_t i = 0; i < static_cast<size_t>(collection.size()); ++i) {
54 ret += GetElement(collection, i).num_blocks();
55 }
56 return ret;
57}
58
59void DumpGraph(const Graph& graph);
60
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -080061} // namespace graph_utils
62
63} // namespace chromeos_update_engine
64
Alex Deymo161c4a12014-05-16 15:56:21 -070065#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_GENERATOR_GRAPH_UTILS_H_