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