8346609: Improve MemorySegment.toString

Reviewed-by: mcimadamore
This commit is contained in:
Per Minborg 2025-01-07 15:18:02 +00:00
parent 4d8fb80732
commit c8a9dd3a02
3 changed files with 18 additions and 6 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -486,9 +486,18 @@ public abstract sealed class AbstractMemorySegmentImpl
@Override
public String toString() {
return "MemorySegment{ " +
heapBase().map(hb -> "heapBase: " + hb + ", ").orElse("") +
"address: " + Utils.toHexString(address()) +
final String kind;
if (this instanceof HeapMemorySegmentImpl) {
kind = "heap";
} else if (this instanceof MappedMemorySegmentImpl) {
kind = "mapped";
} else {
kind = "native";
}
return "MemorySegment{ kind: " +
kind +
heapBase().map(hb -> ", heapBase: " + hb).orElse("") +
", address: " + Utils.toHexString(address()) +
", byteSize: " + length +
" }";
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -260,6 +260,7 @@ public class TestByteBuffer {
try (FileChannel fileChannel = FileChannel.open(f.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE)) {
MemorySegment segment = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0L, 8L, arena);
assertTrue(segment.isMapped());
assertTrue(segment.toString().contains("mapped"));
arena.close();
mappedBufferOp.apply(segment);
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -234,8 +234,10 @@ public class TestSegments {
assertTrue(s.contains("byteSize: "));
if (segment.heapBase().isPresent()) {
assertTrue(s.contains("heapBase: ["));
assertFalse(s.contains("native"));
} else {
assertFalse(s.contains("heapBase: "));
assertTrue(s.contains("native"));
}
assertFalse(s.contains("Optional"));
}