1 : // Copyright 2012 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 : //
15 : // Declares an ordering that randomizes blocks within their respective sections.
16 :
17 : #ifndef SYZYGY_BLOCK_GRAPH_ORDERERS_RANDOM_ORDERER_H_
18 : #define SYZYGY_BLOCK_GRAPH_ORDERERS_RANDOM_ORDERER_H_
19 :
20 : #include "syzygy/block_graph/orderers/named_orderer.h"
21 : #include "syzygy/core/random_number_generator.h"
22 :
23 m : namespace block_graph {
24 m : namespace orderers {
25 :
26 m : class RandomOrderer
27 m : : public block_graph::orderers::NamedOrdererImpl<RandomOrderer> {
28 m : public:
29 : // Constructs a random-orderer that works over the specified block-graph.
30 : // Initializes the random-number generator using the current time.
31 : // @param default_shuffle_section if true then the blocks in each section will
32 : // be shuffled. If false the blocks in the section will remain in the same
33 : // order as input. This sets the default value that is initially applied
34 : // to all sections. Individual sections may have their value changed using
35 : // SetShuffleSection.
36 m : explicit RandomOrderer(bool default_shuffle_section);
37 :
38 : // Constructs a random-orderer that works over the specified block-graph.
39 : // @param default_shuffle_section if true then the blocks in each section will
40 : // be shuffled. If false the blocks in the section will remain in the same
41 : // order as input. This sets the default value that is initially applied
42 : // to all sections. Individual sections may have their value changed using
43 : // SetShuffleSection.
44 : // @param seed the seed to be used by the random number generator.
45 m : RandomOrderer(bool default_shuffle_section, uint32 seed);
46 :
47 : // Configures whether or not the given section should have its blocks
48 : // shuffled. This overrides the default value specified in the constructor.
49 : // @param section the section to configure.
50 : // @param shuffle true if the section should be shuffled, false otherwise.
51 m : void SetShuffleSection(const BlockGraph::Section* section, bool shuffle);
52 :
53 : // Determines whether or not the blocks will be shuffled for the given
54 : // section.
55 m : bool ShouldShuffleSection(const BlockGraph::Section* section) const;
56 :
57 : // Applies this orderer to the provided block graph.
58 : //
59 : // @param ordered_block_graph the block graph to order.
60 : // @param header_block The header block of the block graph to transform.
61 : // This transform does not use this value, so NULL may safely be passed
62 : // in.
63 : // @returns true on success, false otherwise.
64 m : virtual bool OrderBlockGraph(OrderedBlockGraph* ordered_block_graph,
65 m : BlockGraph::Block* header_block) override;
66 :
67 m : static const char kOrdererName[];
68 :
69 m : private:
70 : // Shuffles the blocks in the given section.
71 m : void ShuffleBlocks(const OrderedBlockGraph::OrderedSection* section,
72 m : OrderedBlockGraph* obg);
73 :
74 : // The default shuffle setting.
75 m : bool default_shuffle_section_;
76 : // The random number generator we use.
77 m : core::RandomNumberGenerator rng_;
78 :
79 : // A per-section shuffle setting.
80 m : typedef std::map<const BlockGraph::Section*, bool> ShuffleMap;
81 m : ShuffleMap shuffle_map_;
82 :
83 m : DISALLOW_COPY_AND_ASSIGN(RandomOrderer);
84 m : };
85 :
86 m : } // namespace orderers
87 m : } // namespace block_graph
88 :
89 : #endif // SYZYGY_BLOCK_GRAPH_ORDERERS_RANDOM_ORDERER_H_
|