@@ -25,19 +25,20 @@ namespace ir {
25
25
struct ParametricStorageManager {
26
26
using StorageBase = StorageManager::StorageBase;
27
27
28
- ParametricStorageManager () {}
28
+ explicit ParametricStorageManager (std::function<void (StorageBase *)> destroy)
29
+ : destroy_(destroy) {}
29
30
30
31
~ParametricStorageManager () {
31
32
for (const auto &instance : parametric_instances_) {
32
- delete instance.second ;
33
+ destroy_ ( instance.second ) ;
33
34
}
34
35
parametric_instances_.clear ();
35
36
}
36
37
37
38
// Get the storage of parametric type, if not in the cache, create and
38
39
// insert the cache.
39
40
StorageBase *GetOrCreate (std::size_t hash_value,
40
- std::function<bool (const StorageBase *)> equal_func,
41
+ std::function<bool (StorageBase *)> equal_func,
41
42
std::function<StorageBase *()> constructor) {
42
43
if (parametric_instances_.count (hash_value) != 0 ) {
43
44
auto pr = parametric_instances_.equal_range (hash_value);
@@ -62,6 +63,7 @@ struct ParametricStorageManager {
62
63
// In order to prevent hash conflicts, the unordered_multimap data structure
63
64
// is used for storage.
64
65
std::unordered_multimap<size_t , StorageBase *> parametric_instances_;
66
+ std::function<void (StorageBase *)> destroy_;
65
67
};
66
68
67
69
StorageManager::StorageManager () {}
@@ -95,12 +97,13 @@ StorageManager::StorageBase *StorageManager::GetParameterlessStorageImpl(
95
97
return parameterless_instance;
96
98
}
97
99
98
- void StorageManager::RegisterParametricStorageImpl (TypeId type_id) {
100
+ void StorageManager::RegisterParametricStorageImpl (
101
+ TypeId type_id, std::function<void (StorageBase *)> destroy) {
99
102
std::lock_guard<ir::SpinLock> guard (parametric_instance_lock_);
100
103
VLOG (4 ) << " Register a parametric storage of: [TypeId_hash="
101
104
<< std::hash<ir::TypeId>()(type_id) << " ]." ;
102
- parametric_instance_.emplace (type_id,
103
- std::make_unique<ParametricStorageManager>());
105
+ parametric_instance_.emplace (
106
+ type_id, std::make_unique<ParametricStorageManager>(destroy ));
104
107
}
105
108
106
109
void StorageManager::RegisterParameterlessStorageImpl (
0 commit comments