Skip to content

Commit

Permalink
Update usage of SegmentAllocator
Browse files Browse the repository at this point in the history
  • Loading branch information
squid233 committed Jan 5, 2024
1 parent a44ecd3 commit a1b34dd
Show file tree
Hide file tree
Showing 39 changed files with 679 additions and 675 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ allprojects {
options {
if (this is CoreJavadocOptions) {
if (jdkEnablePreview.toBoolean()) {
addBooleanOption("-enable-preview")
addBooleanOption("-enable-preview", true)
}
addStringOption("source", targetJavaVersion.toString())
if (this is StandardJavadocDocletOptions) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2022-2023 Overrun Organization
* Copyright (c) 2022-2024 Overrun Organization
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -17,6 +17,7 @@
package overrungl;

import overrungl.internal.Exceptions;
import overrungl.internal.RuntimeHelper;

import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.ValueLayout;
Expand All @@ -42,7 +43,9 @@
*
* @author squid233
* @since 0.1.0
* @deprecated This class is going to be removed (but is not marked as removal).
*/
@Deprecated(since = "0.1.0")
public enum FunctionDescriptors {
// 1
D, F, I, J, P, p, V,
Expand Down Expand Up @@ -121,8 +124,9 @@ public static ValueLayout ofValue(char c) throws IllegalArgumentException {
case 'F' -> JAVA_FLOAT;
case 'D' -> JAVA_DOUBLE;
case 'P' -> ADDRESS;
case 'p' -> RuntimeHelper.ADDRESS_UNBOUNDED;
default ->
throw Exceptions.IAE. "Invalid argument c: expected one of B, S, I, J, C, Z, F, D or P; got '\{ c }'" ;
throw Exceptions.IAE."Invalid argument c: expected one of B, S, I, J, C, Z, F, D or P; got '\{c}'";
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private static MemorySegment reinterpreting(MemorySegment pointerToPointer, int
* @return the string.
*/
public static String unboundPointerString(MemorySegment segment) {
return reinterpreting(segment, 0, MemoryUtil::strlen).getString(0);
return unboundPointerString(segment, 0);
}

/**
Expand All @@ -89,7 +89,7 @@ public static String unboundPointerString(MemorySegment segment) {
* @return the string.
*/
public static String unboundPointerString(MemorySegment segment, int index) {
return reinterpreting(segment, index, MemoryUtil::strlen).getString(0);
return reinterpreting(segment, index, str -> MemoryUtil.strlen(str) + 1).getString(0);
}

/**
Expand All @@ -99,7 +99,7 @@ public static String unboundPointerString(MemorySegment segment, int index) {
* @return the string
*/
public static String getString(MemorySegment segment) {
return segment.reinterpret(MemoryUtil.strlen(segment)).getString(0);
return segment.reinterpret(MemoryUtil.strlen(segment) + 1).getString(0);
}

/**
Expand All @@ -120,7 +120,7 @@ public static String unknownToken(int token) {
* @return the string is formatted in {@code STR."\{description} [0x\{toHexString(token)}]"}.
*/
public static String unknownToken(String description, int token) {
return STR. "\{ description } [0x\{ Integer.toHexString(token) }]" ;
return STR."\{description} [0x\{Integer.toHexString(token)}]";
}

/**
Expand All @@ -144,7 +144,7 @@ public static SymbolLookup load(String module, String basename, String version)
uri = localFile.toURI();
} else {
// 2. Load from classpath
var file = new File(tmpdir, STR. "overrungl\{ System.getProperty("user.name") }" );
var file = new File(tmpdir, STR."overrungl\{System.getProperty("user.name")}");
if (!file.exists()) {
// Create directory
file.mkdir();
Expand All @@ -154,15 +154,15 @@ public static SymbolLookup load(String module, String basename, String version)
// Create directory
file.mkdir();
}
var libFile = new File(file, STR. "\{ basename }-\{ version }\{ suffix }" );
var libFile = new File(file, STR."\{basename}-\{version}\{suffix}");
if (!libFile.exists()) {
// Extract
final String fromPath = STR. "\{ module }/\{ os.familyName() }/\{ Architecture.current() }/\{ path }" ;
final String fromPath = STR."\{module}/\{os.familyName()}/\{Architecture.current()}/\{path}";
try (var is = STACK_WALKER.getCallerClass().getClassLoader().getResourceAsStream(fromPath)) {
Files.copy(Objects.requireNonNull(is, STR. "File not found: \{ fromPath }" ),
Files.copy(Objects.requireNonNull(is, STR."File not found: \{fromPath}"),
Path.of(libFile.getAbsolutePath()));
} catch (Exception e) {
throw new IllegalStateException(STR. "File not found: \{ file }; try setting property -Doverrungl.natives to a valid path" , e);
throw new IllegalStateException(STR."File not found: \{file}; try setting property -Doverrungl.natives to a valid path", e);
}
}
uri = libFile.toURI();
Expand Down
16 changes: 8 additions & 8 deletions modules/overrungl.glfw/src/main/java/overrungl/glfw/GLFW.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2022-2023 Overrun Organization
* Copyright (c) 2022-2024 Overrun Organization
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -1091,7 +1091,7 @@ public static MemorySegment ngetVersionString() {
* @see #ngetVersionString() ngetVersionString
*/
public static String getVersionString() {
return ngetVersionString().getUtf8String(0);
return ngetVersionString().getString(0);
}

/**
Expand Down Expand Up @@ -1630,7 +1630,7 @@ public static MemorySegment ngetMonitorName(MemorySegment monitor) {
@Nullable
public static String getMonitorName(MemorySegment monitor) {
var pName = ngetMonitorName(monitor);
return RuntimeHelper.isNullptr(pName) ? null : pName.getUtf8String(0);
return RuntimeHelper.isNullptr(pName) ? null : pName.getString(0);
}

/**
Expand Down Expand Up @@ -4136,7 +4136,7 @@ public static MemorySegment ngetKeyName(int key, int scancode) {
@Nullable
public static String getKeyName(int key, int scancode) {
var pName = ngetKeyName(key, scancode);
return RuntimeHelper.isNullptr(pName) ? null : pName.getUtf8String(0);
return RuntimeHelper.isNullptr(pName) ? null : pName.getString(0);
}

/**
Expand Down Expand Up @@ -5064,7 +5064,7 @@ public static MemorySegment ngetJoystickName(int jid) {
@Nullable
public static String getJoystickName(int jid) {
var pName = ngetJoystickName(jid);
return RuntimeHelper.isNullptr(pName) ? null : pName.getUtf8String(0);
return RuntimeHelper.isNullptr(pName) ? null : pName.getString(0);
}

/**
Expand Down Expand Up @@ -5117,7 +5117,7 @@ public static MemorySegment ngetJoystickGUID(int jid) {
@Nullable
public static String getJoystickGUID(int jid) {
var pGUID = ngetJoystickGUID(jid);
return RuntimeHelper.isNullptr(pGUID) ? null : pGUID.getUtf8String(0);
return RuntimeHelper.isNullptr(pGUID) ? null : pGUID.getString(0);
}

/**
Expand Down Expand Up @@ -5334,7 +5334,7 @@ public static MemorySegment ngetGamepadName(int jid) {
@Nullable
public static String getGamepadName(int jid) {
var pName = ngetGamepadName(jid);
return RuntimeHelper.isNullptr(pName) ? null : pName.getUtf8String(0);
return RuntimeHelper.isNullptr(pName) ? null : pName.getString(0);
}

/**
Expand Down Expand Up @@ -5464,7 +5464,7 @@ public static MemorySegment ngetClipboardString() {
@Nullable
public static String getClipboardString(@Deprecated MemorySegment window) {
var pString = ngetClipboardString();
return RuntimeHelper.isNullptr(pString) ? null : pString.getUtf8String(0);
return RuntimeHelper.isNullptr(pString) ? null : pString.getString(0);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2022-2023 Overrun Organization
* Copyright (c) 2022-2024 Overrun Organization
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -42,7 +42,7 @@
* @since 0.1.0
*/
public final class GLFWGammaRamp extends Struct {
private static final AddressLayout SHORT_ARRAY = ADDRESS.withTargetLayout(MemoryLayout.sequenceLayout(JAVA_SHORT));
private static final AddressLayout SHORT_ARRAY = ADDRESS.withTargetLayout(MemoryLayout.sequenceLayout(Long.MAX_VALUE / JAVA_SHORT.byteSize(), JAVA_SHORT));
/**
* The struct layout.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2022-2023 Overrun Organization
* Copyright (c) 2022-2024 Overrun Organization
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -18,6 +18,7 @@

import overrungl.ArrayPointer;
import overrungl.Struct;
import overrungl.internal.RuntimeHelper;

import java.lang.foreign.*;
import java.lang.foreign.MemoryLayout.PathElement;
Expand Down Expand Up @@ -47,7 +48,7 @@ public sealed class GLFWImage extends Struct {
public static final StructLayout LAYOUT = MemoryLayout.structLayout(
JAVA_INT.withName("width"),
JAVA_INT.withName("height"),
ADDRESS.withTargetLayout(MemoryLayout.sequenceLayout(JAVA_BYTE)).withName("pixels")
ADDRESS.withTargetLayout(RuntimeHelper.ADDRESS_UNBOUNDED).withName("pixels")
);
private static final VarHandle
pWidth = LAYOUT.varHandle(PathElement.groupElement("width")),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Overrun Organization
* Copyright (c) 2023-2024 Overrun Organization
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -68,7 +68,7 @@ public static MemorySegment ngetWin32Adapter(MemorySegment monitor) {
* @see #ngetWin32Adapter(MemorySegment) ngetWin32Adapter
*/
public static String getWin32Adapter(MemorySegment monitor) {
return ngetWin32Adapter(monitor).getUtf8String(0);
return ngetWin32Adapter(monitor).getString(0);
}

/**
Expand Down Expand Up @@ -100,7 +100,7 @@ public static MemorySegment ngetWin32Monitor(MemorySegment monitor) {
* @see #ngetWin32Monitor(MemorySegment) ngetWin32Monitor
*/
public static String getWin32Monitor(MemorySegment monitor) {
return ngetWin32Monitor(monitor).getUtf8String(0);
return ngetWin32Monitor(monitor).getString(0);
}

/**
Expand Down Expand Up @@ -343,7 +343,7 @@ public static MemorySegment ngetX11SelectionString() {
*/
public static String getX11SelectionString() {
final MemorySegment seg = ngetX11SelectionString();
return seg != null ? seg.getUtf8String(0) : null;
return seg != null ? seg.getString(0) : null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public sealed class GLFWVidMode extends Struct {
/**
* Create a {@code GLFWvidmode} instance.
*
* @param address the address.
* @param address the address.
*/
public GLFWVidMode(MemorySegment address) {
super(address, LAYOUT);
Expand Down Expand Up @@ -120,42 +120,42 @@ public Value value() {
* {@return the width, in screen coordinates, of the video mode}
*/
public int width() {
return (int) pWidth.get(segment());
return (int) pWidth.get(segment(), 0L);
}

/**
* {@return the height, in screen coordinates, of the video mode}
*/
public int height() {
return (int) pHeight.get(segment());
return (int) pHeight.get(segment(), 0L);
}

/**
* {@return the bit depth of the red channel of the video mode}
*/
public int redBits() {
return (int) pRedBits.get(segment());
return (int) pRedBits.get(segment(), 0L);
}

/**
* {@return the bit depth of the green channel of the video mode}
*/
public int greenBits() {
return (int) pGreenBits.get(segment());
return (int) pGreenBits.get(segment(), 0L);
}

/**
* {@return the bit depth of the blue channel of the video mode}
*/
public int blueBits() {
return (int) pBlueBits.get(segment());
return (int) pBlueBits.get(segment(), 0L);
}

/**
* {@return the refresh rate, in Hz, of the video mode}
*/
public int refreshRate() {
return (int) pRefreshRate.get(segment());
return (int) pRefreshRate.get(segment(), 0L);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2022-2023 Overrun Organization
* Copyright (c) 2022-2024 Overrun Organization
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -214,7 +214,7 @@ private static MethodHandle downcall(String name,

private static MethodHandle downcallTrivial(String name,
FunctionDescriptors function) {
return downcall(name, function, Linker.Option.isTrivial());
return downcall(name, function, Linker.Option.critical(false));
}

private static MethodHandle downcallNative(String name,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Overrun Organization
* Copyright (c) 2023-2024 Overrun Organization
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -3224,7 +3224,7 @@ fun glExtCaps() {
| var pExtsI = new MemorySegment[1];
| if (!getExtensions(allocator, version, pExts, pNumExtsI, pExtsI, caps)) return false;
|
| String exts = pExts.getUtf8String(0);
| String exts = pExts.getString(0);
| int numExtsI = pNumExtsI.get(JAVA_INT, 0);
| var extsI = pExtsI[0];
|
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2022-2023 Overrun Organization
* Copyright (c) 2022-2024 Overrun Organization
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -98,7 +98,7 @@ public static DrawArraysIndirectCommand create(SegmentAllocator allocator) {
* @return the instance
*/
public static Buffer create(SegmentAllocator allocator, long count) {
return new Buffer(allocator.allocateArray(LAYOUT, count), count);
return new Buffer(allocator.allocate(LAYOUT, count), count);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2022-2023 Overrun Organization
* Copyright (c) 2022-2024 Overrun Organization
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -101,7 +101,7 @@ public static DrawElementsIndirectCommand create(SegmentAllocator allocator) {
* @return the instance
*/
public static Buffer create(SegmentAllocator allocator, long count) {
return new Buffer(allocator.allocateArray(LAYOUT, count), count);
return new Buffer(allocator.allocate(LAYOUT, count), count);
}

/**
Expand Down
Loading

0 comments on commit a1b34dd

Please sign in to comment.