@@ -43,6 +43,9 @@ ConstraintGraph::~ConstraintGraph() {
43
43
delete impl.getGraphNode ();
44
44
impl.setGraphNode (nullptr );
45
45
}
46
+ for (auto *node : FreeList) {
47
+ delete node;
48
+ }
46
49
}
47
50
48
51
#pragma mark Graph accessors
@@ -55,11 +58,20 @@ ConstraintGraph::lookupNode(TypeVariableType *typeVar) {
55
58
assert (impl.getGraphIndex () < TypeVariables.size () && " Out-of-bounds index" );
56
59
assert (TypeVariables[impl.getGraphIndex ()] == typeVar &&
57
60
" Type variable mismatch" );
61
+ ASSERT (nodePtr->TypeVar == typeVar &&
62
+ " Use-after-free" );
58
63
return { *nodePtr, impl.getGraphIndex () };
59
64
}
60
65
61
66
// Allocate the new node.
62
- auto nodePtr = new ConstraintGraphNode (*this , typeVar);
67
+ ConstraintGraphNode *nodePtr;
68
+ if (FreeList.empty ())
69
+ nodePtr = new ConstraintGraphNode (*this , typeVar);
70
+ else {
71
+ nodePtr = FreeList.back ();
72
+ FreeList.pop_back ();
73
+ nodePtr->initTypeVariable (typeVar);
74
+ }
63
75
unsigned index = TypeVariables.size ();
64
76
impl.setGraphNode (nodePtr);
65
77
impl.setGraphIndex (index);
@@ -84,6 +96,17 @@ ConstraintGraph::lookupNode(TypeVariableType *typeVar) {
84
96
return { *nodePtr, index };
85
97
}
86
98
99
+ void ConstraintGraphNode::reset () {
100
+ ASSERT (TypeVar);
101
+ TypeVar = nullptr ;
102
+ Bindings.reset ();
103
+ Constraints.clear ();
104
+ ConstraintIndex.clear ();
105
+ ReferencedBy.clear ();
106
+ References.clear ();
107
+ EquivalenceClass.clear ();
108
+ }
109
+
87
110
bool ConstraintGraphNode::forRepresentativeVar () const {
88
111
auto *typeVar = getTypeVariable ();
89
112
return typeVar == typeVar->getImpl ().getRepresentative (nullptr );
@@ -422,7 +445,9 @@ void ConstraintGraph::removeNode(TypeVariableType *typeVar) {
422
445
// Remove this node.
423
446
auto &impl = typeVar->getImpl ();
424
447
unsigned index = impl.getGraphIndex ();
425
- delete impl.getGraphNode ();
448
+ auto *node = impl.getGraphNode ();
449
+ node->reset ();
450
+ FreeList.push_back (node);
426
451
impl.setGraphNode (nullptr );
427
452
428
453
// Remove this type variable from the list.
0 commit comments