116116import com .oracle .truffle .api .TruffleLanguage .ContextReference ;
117117import com .oracle .truffle .api .frame .Frame ;
118118import com .oracle .truffle .api .frame .FrameDescriptor ;
119+ import com .oracle .truffle .api .instrumentation .AllocationReporter ;
119120import com .oracle .truffle .api .interop .TruffleObject ;
120121import com .oracle .truffle .api .nodes .Node ;
121122import com .oracle .truffle .api .nodes .NodeCost ;
125126
126127public final class PythonObjectFactory extends Node {
127128 @ CompilationFinal private ContextReference <PythonContext > contextRef ;
129+ @ CompilationFinal private AllocationReporter allocationReporter ;
128130
129131 private PythonObjectFactory () {
130132 }
@@ -139,6 +141,10 @@ private PythonClass lookupClass(PythonBuiltinClassType type) {
139141
140142 @ SuppressWarnings ("static-method" )
141143 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+ }
142148 return allocatedObject ;
143149 }
144150
@@ -148,11 +154,20 @@ public NodeCost getCost() {
148154 }
149155
150156 public PythonCore getCore () {
157+ return getContext ().getCore ();
158+ }
159+
160+ private PythonContext getContext () {
151161 if (contextRef == null ) {
152162 CompilerDirectives .transferToInterpreterAndInvalidate ();
153163 contextRef = PythonLanguage .getContextRef ();
164+ allocationReporter = contextRef .get ().getEnv ().lookup (AllocationReporter .class );
154165 }
155- return contextRef .get ().getCore ();
166+ return contextRef .get ();
167+ }
168+
169+ private boolean reportAllocations () {
170+ return getContext () != null && allocationReporter != null ;
156171 }
157172
158173 /*
0 commit comments