1 /*
2 * Copyright 2014 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define IN_FRUIT_CPP_FILE 1
18
19 #include <algorithm>
20 #include <cstdlib>
21 #include <fruit/impl/util/type_info.h>
22 #include <iostream>
23 #include <memory>
24 #include <vector>
25
26 #include <fruit/impl/normalized_component_storage/normalized_component_storage.h>
27
28 #include <fruit/impl/component_storage/component_storage.h>
29 #include <fruit/impl/data_structures/semistatic_graph.templates.h>
30 #include <fruit/impl/data_structures/semistatic_map.templates.h>
31 #include <fruit/impl/injector/injector_storage.h>
32 #include <fruit/impl/normalized_component_storage/binding_normalization.h>
33
34 using std::cout;
35 using std::endl;
36
37 namespace fruit {
38 namespace impl {
39
NormalizedComponentStorage(ComponentStorage && component,const std::vector<TypeId,ArenaAllocator<TypeId>> & exposed_types,MemoryPool & memory_pool,WithPermanentCompression)40 NormalizedComponentStorage::NormalizedComponentStorage(ComponentStorage&& component,
41 const std::vector<TypeId, ArenaAllocator<TypeId>>& exposed_types,
42 MemoryPool& memory_pool, WithPermanentCompression)
43 : normalized_component_memory_pool(),
44 binding_compression_info_map(createHashMapWithArenaAllocator<TypeId, CompressedBindingUndoInfo>(
45 0 /* capacity */, normalized_component_memory_pool)),
46 fully_expanded_components_with_no_args(
47 createLazyComponentWithNoArgsSet(0 /* capacity */, normalized_component_memory_pool)),
48 fully_expanded_components_with_args(
49 createLazyComponentWithArgsSet(0 /* capacity */, normalized_component_memory_pool)),
50 component_with_no_args_replacements(
51 createLazyComponentWithNoArgsReplacementMap(0 /* capacity */, normalized_component_memory_pool)),
52 component_with_args_replacements(
53 createLazyComponentWithArgsReplacementMap(0 /* capacity */, normalized_component_memory_pool)) {
54
55 using bindings_vector_t = std::vector<ComponentStorageEntry, ArenaAllocator<ComponentStorageEntry>>;
56 bindings_vector_t bindings_vector = bindings_vector_t(ArenaAllocator<ComponentStorageEntry>(memory_pool));
57 BindingNormalization::normalizeBindingsWithPermanentBindingCompression(std::move(component).release(),
58 fixed_size_allocator_data, memory_pool,
59 exposed_types, bindings_vector, multibindings);
60
61 bindings = SemistaticGraph<TypeId, NormalizedBinding>(InjectorStorage::BindingDataNodeIter{bindings_vector.begin()},
62 InjectorStorage::BindingDataNodeIter{bindings_vector.end()},
63 memory_pool);
64 }
65
NormalizedComponentStorage(ComponentStorage && component,const std::vector<TypeId,ArenaAllocator<TypeId>> & exposed_types,MemoryPool & memory_pool,WithUndoableCompression)66 NormalizedComponentStorage::NormalizedComponentStorage(ComponentStorage&& component,
67 const std::vector<TypeId, ArenaAllocator<TypeId>>& exposed_types,
68 MemoryPool& memory_pool, WithUndoableCompression)
69 : normalized_component_memory_pool(),
70 binding_compression_info_map(createHashMapWithArenaAllocator<TypeId, CompressedBindingUndoInfo>(
71 20 /* capacity */, normalized_component_memory_pool)),
72 fully_expanded_components_with_no_args(
73 createLazyComponentWithNoArgsSet(20 /* capacity */, normalized_component_memory_pool)),
74 fully_expanded_components_with_args(
75 createLazyComponentWithArgsSet(20 /* capacity */, normalized_component_memory_pool)),
76 component_with_no_args_replacements(
77 createLazyComponentWithNoArgsReplacementMap(20 /* capacity */, normalized_component_memory_pool)),
78 component_with_args_replacements(
79 createLazyComponentWithArgsReplacementMap(20 /* capacity */, normalized_component_memory_pool)) {
80
81 using bindings_vector_t = std::vector<ComponentStorageEntry, ArenaAllocator<ComponentStorageEntry>>;
82 bindings_vector_t bindings_vector = bindings_vector_t(ArenaAllocator<ComponentStorageEntry>(memory_pool));
83 BindingNormalization::normalizeBindingsWithUndoableBindingCompression(
84 std::move(component).release(), fixed_size_allocator_data, memory_pool, normalized_component_memory_pool,
85 normalized_component_memory_pool, exposed_types, bindings_vector, multibindings, binding_compression_info_map,
86 fully_expanded_components_with_no_args, fully_expanded_components_with_args, component_with_no_args_replacements,
87 component_with_args_replacements);
88
89 bindings = SemistaticGraph<TypeId, NormalizedBinding>(InjectorStorage::BindingDataNodeIter{bindings_vector.begin()},
90 InjectorStorage::BindingDataNodeIter{bindings_vector.end()},
91 memory_pool);
92 }
93
~NormalizedComponentStorage()94 NormalizedComponentStorage::~NormalizedComponentStorage() noexcept {
95 for (auto& x : fully_expanded_components_with_args) {
96 x.destroy();
97 }
98
99 for (const auto& pair : component_with_args_replacements) {
100 const LazyComponentWithArgs& replaced_component = pair.first;
101 const ComponentStorageEntry& replacement_component = pair.second;
102 replaced_component.destroy();
103 replacement_component.destroy();
104 }
105
106 for (const auto& pair : component_with_no_args_replacements) {
107 const ComponentStorageEntry& replacement_component = pair.second;
108 replacement_component.destroy();
109 }
110
111 // We must free all the memory in these before the normalized_component_memory_pool is destroyed.
112 binding_compression_info_map = createHashMapWithArenaAllocator<TypeId, CompressedBindingUndoInfo>(
113 0 /* capacity */, normalized_component_memory_pool);
114 fully_expanded_components_with_no_args =
115 createLazyComponentWithNoArgsSet(0 /* capacity */, normalized_component_memory_pool);
116 fully_expanded_components_with_args =
117 createLazyComponentWithArgsSet(0 /* capacity */, normalized_component_memory_pool);
118 component_with_no_args_replacements =
119 createLazyComponentWithNoArgsReplacementMap(0 /* capacity */, normalized_component_memory_pool);
120 component_with_args_replacements =
121 createLazyComponentWithArgsReplacementMap(0 /* capacity */, normalized_component_memory_pool);
122 }
123
124 } // namespace impl
125 // We need a LCOV_EXCL_BR_LINE below because for some reason gcov/lcov think there's a branch there.
126 } // namespace fruit LCOV_EXCL_BR_LINE
127