41
41
import java .nio .LongBuffer ;
42
42
import java .nio .ShortBuffer ;
43
43
import java .util .*;
44
+ import java .util .function .BiFunction ;
44
45
import java .util .function .Consumer ;
45
46
import java .util .function .Function ;
46
47
import java .util .function .IntFunction ;
51
52
import jdk .internal .access .foreign .UnmapperProxy ;
52
53
import jdk .internal .misc .ScopedMemoryAccess ;
53
54
import jdk .internal .util .ArraysSupport ;
55
+ import jdk .internal .util .Preconditions ;
54
56
import jdk .internal .vm .annotation .ForceInline ;
55
57
56
58
import static java .lang .foreign .ValueLayout .JAVA_BYTE ;
64
66
* are defined for each memory segment kind, see {@link NativeMemorySegmentImpl}, {@link HeapMemorySegmentImpl} and
65
67
* {@link MappedMemorySegmentImpl}.
66
68
*/
67
- public abstract non-sealed class AbstractMemorySegmentImpl implements MemorySegment , SegmentAllocator , Scoped {
69
+ public abstract non-sealed class AbstractMemorySegmentImpl implements MemorySegment , SegmentAllocator , Scoped , BiFunction < String , List < Number >, RuntimeException > {
68
70
69
71
private static final ScopedMemoryAccess SCOPED_MEMORY_ACCESS = ScopedMemoryAccess .getScopedMemoryAccess ();
70
72
@@ -394,13 +396,20 @@ private int checkArraySize(String typeName, int elemSize) {
394
396
@ ForceInline
395
397
void checkBounds (long offset , long length ) {
396
398
if (length > 0 ) {
397
- Objects .checkIndex (offset , this .length - length + 1 );
399
+ Preconditions .checkIndex (offset , this .length - length + 1 , this );
398
400
} else if (length < 0 || offset < 0 ||
399
401
offset > this .length - length ) {
400
402
throw outOfBoundException (offset , length );
401
403
}
402
404
}
403
405
406
+ @ Override
407
+ public RuntimeException apply (String s , List <Number > numbers ) {
408
+ long offset = numbers .get (0 ).longValue ();
409
+ long length = byteSize () - numbers .get (1 ).longValue () + 1 ;
410
+ return outOfBoundException (offset , length );
411
+ }
412
+
404
413
@ Override
405
414
@ ForceInline
406
415
public MemorySessionImpl sessionImpl () {
@@ -413,7 +422,7 @@ public MemorySession session() {
413
422
}
414
423
415
424
private IndexOutOfBoundsException outOfBoundException (long offset , long length ) {
416
- return new IndexOutOfBoundsException (String .format ("Out of bound access on segment %s; new offset = %d; new length = %d" ,
425
+ return new IndexOutOfBoundsException (String .format ("Out of bound access on segment %s; offset = %d; length = %d" ,
417
426
this , offset , length ));
418
427
}
419
428
0 commit comments