23
23
import com .oracle .truffle .api .source .SourceSection ;
24
24
import org .jcodings .specific .UTF8Encoding ;
25
25
import org .truffleruby .Layouts ;
26
- import org .truffleruby .core .rope . CodeRange ;
26
+ import org .truffleruby .core .objectspace . ObjectSpaceManager ;
27
27
import org .truffleruby .core .string .StringNodes ;
28
+ import org .truffleruby .core .string .StringOperations ;
28
29
import org .truffleruby .language .RubyBaseNode ;
29
30
import org .truffleruby .language .arguments .RubyArguments ;
30
31
import org .truffleruby .language .control .RaiseException ;
@@ -81,10 +82,24 @@ public DynamicObject allocateUncached(DynamicObject classToAllocate, Object[] va
81
82
@ CompilerDirectives .TruffleBoundary
82
83
@ Specialization (guards = {"!isSingleton(classToAllocate)" , "isTracing()" },
83
84
assumptions = "getTracingAssumption()" )
84
- public DynamicObject allocateTracing (DynamicObject classToAllocate , Object [] values ,
85
- @ Cached ("create()" ) StringNodes .MakeStringNode makeStringNode ) {
85
+ public DynamicObject allocateTracing (DynamicObject classToAllocate , Object [] values ) {
86
86
final DynamicObject object = allocate (getInstanceFactory (classToAllocate ), values );
87
87
88
+ final ObjectSpaceManager objectSpaceManager = getContext ().getObjectSpaceManager ();
89
+ if (!objectSpaceManager .isTracingPaused ()) {
90
+ objectSpaceManager .setTracingPaused (true );
91
+ try {
92
+ callTraceAllocation (object );
93
+ } finally {
94
+ objectSpaceManager .setTracingPaused (false );
95
+ }
96
+ }
97
+
98
+ return object ;
99
+ }
100
+
101
+ @ CompilerDirectives .TruffleBoundary
102
+ private void callTraceAllocation (DynamicObject object ) {
88
103
final FrameInstance allocatingFrameInstance ;
89
104
final SourceSection allocatingSourceSection ;
90
105
@@ -101,22 +116,23 @@ public DynamicObject allocateTracing(DynamicObject classToAllocate, Object[] val
101
116
final Object allocatingSelf = RubyArguments .getSelf (allocatingFrame );
102
117
final String allocatingMethod = RubyArguments .getMethod (allocatingFrame ).getName ();
103
118
104
- getContext ().getObjectSpaceManager (). traceAllocation (
119
+ getContext ().send ( coreLibrary (). getObjectSpaceModule (), "trace_allocation" ,
105
120
object ,
106
- string (makeStringNode , Layouts .CLASS .getFields (coreLibrary ().getLogicalClass (allocatingSelf )).getName ()),
121
+ string (Layouts .CLASS .getFields (coreLibrary ().getLogicalClass (allocatingSelf )).getName ()),
107
122
getSymbol (allocatingMethod ),
108
- string (makeStringNode , getContext ().getPath (allocatingSourceSection .getSource ())),
109
- allocatingSourceSection .getStartLine ());
110
-
111
- return object ;
123
+ string (getContext ().getPath (allocatingSourceSection .getSource ())),
124
+ allocatingSourceSection .getStartLine (),
125
+ ObjectSpaceManager .getCollectionCount ());
112
126
}
113
127
114
128
protected DynamicObjectFactory getInstanceFactory (DynamicObject classToAllocate ) {
115
129
return Layouts .CLASS .getInstanceFactory (classToAllocate );
116
130
}
117
131
118
- private DynamicObject string (StringNodes .MakeStringNode makeStringNode , String value ) {
119
- return makeStringNode .executeMake (value , UTF8Encoding .INSTANCE , CodeRange .CR_UNKNOWN );
132
+ private DynamicObject string (String value ) {
133
+ // No point to use MakeStringNode (which uses AllocateObjectNode) here, as we should not trace the allocation
134
+ // of Strings used for tracing allocations.
135
+ return StringOperations .createString (getContext (), StringOperations .encodeRope (value , UTF8Encoding .INSTANCE ));
120
136
}
121
137
122
138
@ Specialization (guards = "isSingleton(classToAllocate)" )
0 commit comments