83
83
import com .oracle .svm .core .thread .VMThreads .SafepointBehavior ;
84
84
import com .oracle .svm .core .util .UnsignedUtils ;
85
85
import com .oracle .svm .core .util .UserError ;
86
+ import com .oracle .svm .core .util .VMError ;
86
87
87
88
import jdk .graal .compiler .api .directives .GraalDirectives ;
88
89
import jdk .graal .compiler .api .replacements .Fold ;
@@ -339,31 +340,39 @@ public int getClassCount() {
339
340
protected List <Class <?>> getAllClasses () {
340
341
/* Two threads might race to set classList, but they compute the same result. */
341
342
if (classList == null ) {
342
- ArrayList <Class <?>> list = new ArrayList <>(1024 );
343
- for (ImageHeapInfo info = firstImageHeapInfo ; info != null ; info = info .next ) {
344
- ImageHeapWalker .walkRegions (info , new ClassListBuilderVisitor (list ));
345
- }
346
- list .trimToSize ();
347
-
343
+ ArrayList <Class <?>> list = findAllDynamicHubs ();
348
344
/* Ensure that other threads see consistent values once the list is published. */
349
345
MembarNode .memoryBarrier (MembarNode .FenceKind .STORE_STORE );
350
346
classList = list ;
351
347
}
352
- assert classList .size () == getClassCount ();
353
348
return classList ;
354
349
}
355
350
351
+ private ArrayList <Class <?>> findAllDynamicHubs () {
352
+ int dynamicHubCount = getClassCount ();
353
+
354
+ ArrayList <Class <?>> list = new ArrayList <>(dynamicHubCount );
355
+ for (ImageHeapInfo info = firstImageHeapInfo ; info != null ; info = info .next ) {
356
+ ImageHeapWalker .walkRegions (info , new ClassListBuilderVisitor (list .size () + info .dynamicHubCount , list ));
357
+ }
358
+
359
+ VMError .guarantee (dynamicHubCount == list .size (), "Found fewer DynamicHubs in the image heap than expected." );
360
+ return list ;
361
+ }
362
+
356
363
private static class ClassListBuilderVisitor implements MemoryWalker .ImageHeapRegionVisitor , ObjectVisitor {
364
+ private final int dynamicHubCount ;
357
365
private final List <Class <?>> list ;
358
366
359
- ClassListBuilderVisitor (List <Class <?>> list ) {
367
+ ClassListBuilderVisitor (int dynamicHubCount , List <Class <?>> list ) {
368
+ this .dynamicHubCount = dynamicHubCount ;
360
369
this .list = list ;
361
370
}
362
371
363
372
@ Override
364
373
public <T > boolean visitNativeImageHeapRegion (T region , MemoryWalker .NativeImageHeapRegionAccess <T > access ) {
365
374
if (!access .isWritable (region ) && !access .consistsOfHugeObjects (region )) {
366
- access .visitObjects (region , this );
375
+ return access .visitObjects (region , this );
367
376
}
368
377
return true ;
369
378
}
@@ -373,6 +382,7 @@ public <T> boolean visitNativeImageHeapRegion(T region, MemoryWalker.NativeImage
373
382
public boolean visitObject (Object o ) {
374
383
if (o instanceof Class <?>) {
375
384
list .add ((Class <?>) o );
385
+ return list .size () != dynamicHubCount ;
376
386
}
377
387
return true ;
378
388
}
0 commit comments