116
116
import com .oracle .truffle .api .TruffleLanguage .ContextReference ;
117
117
import com .oracle .truffle .api .frame .Frame ;
118
118
import com .oracle .truffle .api .frame .FrameDescriptor ;
119
+ import com .oracle .truffle .api .instrumentation .AllocationReporter ;
119
120
import com .oracle .truffle .api .interop .TruffleObject ;
120
121
import com .oracle .truffle .api .nodes .Node ;
121
122
import com .oracle .truffle .api .nodes .NodeCost ;
125
126
126
127
public final class PythonObjectFactory extends Node {
127
128
@ CompilationFinal private ContextReference <PythonContext > contextRef ;
129
+ @ CompilationFinal private AllocationReporter allocationReporter ;
128
130
129
131
private PythonObjectFactory () {
130
132
}
@@ -139,6 +141,10 @@ private PythonClass lookupClass(PythonBuiltinClassType type) {
139
141
140
142
@ SuppressWarnings ("static-method" )
141
143
public final <T > T trace (T allocatedObject ) {
144
+ if (reportAllocations ()) {
145
+ allocationReporter .onEnter (null , 0 , AllocationReporter .SIZE_UNKNOWN );
146
+ allocationReporter .onReturnValue (allocatedObject , 0 , AllocationReporter .SIZE_UNKNOWN );
147
+ }
142
148
return allocatedObject ;
143
149
}
144
150
@@ -148,11 +154,20 @@ public NodeCost getCost() {
148
154
}
149
155
150
156
public PythonCore getCore () {
157
+ return getContext ().getCore ();
158
+ }
159
+
160
+ private PythonContext getContext () {
151
161
if (contextRef == null ) {
152
162
CompilerDirectives .transferToInterpreterAndInvalidate ();
153
163
contextRef = PythonLanguage .getContextRef ();
164
+ allocationReporter = contextRef .get ().getEnv ().lookup (AllocationReporter .class );
154
165
}
155
- return contextRef .get ().getCore ();
166
+ return contextRef .get ();
167
+ }
168
+
169
+ private boolean reportAllocations () {
170
+ return getContext () != null && allocationReporter != null ;
156
171
}
157
172
158
173
/*
0 commit comments