From af1583eb2f282242d542c1b40ec428f16bd7968f Mon Sep 17 00:00:00 2001 From: squid233 <60126026+squid233@users.noreply.github.com> Date: Thu, 30 Jan 2025 15:22:48 +0800 Subject: [PATCH] feat(impl): [core] MemoryUtil::asAddressArray use MemorySegment::elements --- .../main/java/overrungl/util/MemoryUtil.java | 30 ++++--------------- 1 file changed, 6 insertions(+), 24 deletions(-) diff --git a/modules/overrungl.core/src/main/java/overrungl/util/MemoryUtil.java b/modules/overrungl.core/src/main/java/overrungl/util/MemoryUtil.java index 34a2e142..6ed6d907 100644 --- a/modules/overrungl.core/src/main/java/overrungl/util/MemoryUtil.java +++ b/modules/overrungl.core/src/main/java/overrungl/util/MemoryUtil.java @@ -659,18 +659,6 @@ public static double[] asDoubleArray(MemorySegment segment) { return isNullPointer(segment) ? null : segment.toArray(JAVA_DOUBLE); } - private static int checkArraySize(MemorySegment segment, String typeName, int elemSize) { - long length = segment.byteSize(); - if ((length & (elemSize - 1)) != 0) { - throw new IllegalStateException(String.format("Segment size is not a multiple of %d. Size: %d", elemSize, length)); - } - long arraySize = length / elemSize; - if (arraySize > (Integer.MAX_VALUE - 8)) { - throw new IllegalStateException(String.format("Segment is too large to wrap as %s. Size: %d", typeName, length)); - } - return (int) arraySize; - } - /// Gets addresses from the given segment. /// /// The returned segments are zero-length segments. @@ -679,12 +667,9 @@ private static int checkArraySize(MemorySegment segment, String typeName, int el /// @return the address array; or `null` if _`segment`_ is `NULL` public static MemorySegment[] asAddressArray(MemorySegment segment) { if (isNullPointer(segment)) return null; - int size = checkArraySize(segment, MemorySegment[].class.getSimpleName(), (int) ADDRESS.byteSize()); - var arr = new MemorySegment[size]; - for (int i = 0; i < size; i++) { - arr[i] = segment.getAtIndex(ADDRESS, i); - } - return arr; + return segment.elements(ADDRESS) + .map(s -> s.get(ADDRESS, 0)) + .toArray(MemorySegment[]::new); } /// Gets strings from the given segment. @@ -694,12 +679,9 @@ public static MemorySegment[] asAddressArray(MemorySegment segment) { /// @return the string array; or `null` if _`segment`_ is `NULL` public static String[] asStringArray(MemorySegment segment, Charset charset) { if (isNullPointer(segment)) return null; - int size = checkArraySize(segment, String[].class.getSimpleName(), (int) ADDRESS.byteSize()); - var arr = new String[size]; - for (int i = 0; i < size; i++) { - arr[i] = segment.getAtIndex(STR_LAYOUT, i).getString(0L, charset); - } - return arr; + return segment.elements(ADDRESS) + .map(s -> s.get(STR_LAYOUT, 0).getString(0L, charset)) + .toArray(String[]::new); } /// Gets strings encoded with UTF-8 from the given segment.