Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typos in FFM tutorials #135

Open
danthe1st opened this issue Dec 28, 2024 · 1 comment
Open

Typos in FFM tutorials #135

danthe1st opened this issue Dec 28, 2024 · 1 comment

Comments

@danthe1st
Copy link
Contributor

There are a few typos and similar issues in the new FFM tutorials.


https://dev.java/learn/ffm/access-memory/

In the section "Allocate a MemorySegment with an Arena", the text "should be deallocated and no longer valid." is missing the word "is". It should be "Each arena has a scope, which specifies when the off-heap memory (associated with the MemorySegment object) should be deallocated and is no longer valid."

The section "Print a Value Stored in the Off-Heap Memory" contains the text "With the methods within MemorySegment interface" which is missing "the" (and a comma at the end). It should be "With the methods within the MemorySegment interface, , you can read from or write to memory segments."

In the same section, "For example, MemoryLayout.get(ValueLayout.OfByte,long) takes as an argument ValueLayout.JAVA_BYTE" refers to a nonexistent method. I assume it should be MemorySegment instead of MemoryLayout.

The codeblock at the end of that article contains the following code:

String pattern = "********"; 

try (Arena arena = Arena.ofConfined()) {
    MemorySegment nativeString = arena.allocateFrom(pattern);
} 

for (int i = 0; i < s.length(); i++ ) {
    // Exception in thread "main" java.lang.IllegalStateException: Already closed
    System.out.print((char)nativeString.get(ValueLayout.JAVA_BYTE, i)); 
}

Here, nativeString is declared within the try-with-resources block so accessing the variable outside is a compiler-error.


https://dev.java/learn/ffm/native/

The first sentence contains "As Foreign Function and Memory API can help you" which is missing a missing "the" It should be "As the Foreign Function and Memory API can help you with invoking native code,"

The first codeblock in the section "Locate the Address of the C Function" mentions "libc.so.6" which probably doesn't work on Windows. It might be a good idea to add a comment orr similar to avoid confusion (or mention the Windows DLL providing that function).

The same codeblock also contains the line "MemorySegment strlen_addr = stdLib.find("strdup").get(); ". This MemorySegment is named strlen_addr even though the function is not strlen.

The section "Describe the C Function Signature" contains invalid links to StructLayout and UnionLayout (starting with javadoc:). This is likely due to using inline Markdown codeblocks within links


https://dev.java/learn/ffm/access-structure/

The first paragraph of this article contains "An elegant solution is to use memory layouts that are more efficiently to initialize and can access more complex native data types such as C structures." - What's meant with "are more efficiently"?

In the final code snippet of the first section, the line "resultDenominator = d1 * d2;" references variables that are not declared at that point. I think this line should be removed.


https://dev.java/learn/ffm/upcall/

The second codeblock in the section "Create a Downcall Method Handle for the Foreign Function" contains a declaration "final static Linker linker". Since most people put static before final, I suggest reordering that.


https://dev.java/learn/ffm/troubleshoot/

The first section of this article contains the sentence "While not directly accessible, the goal of a zero-length memory segments is to pass them to other pointer-accepting foreign functions". I think the article "a" is incorrect there due to the plural afterwards and should therefore be removed.

The table at the end of that article (listing restricted methods) is out of bounds on small screens (e.g. mobile phones).


https://dev.java/learn/ffm/jextract/

The teapot code uses the line glutInit(argc, argc);. Iss it intentional that argc is used twice there? While it shouldn't matter (because argc is 0), I think this should at least be explained (or replaced by another MemorySegment).

@ammbra
Copy link
Collaborator

ammbra commented Dec 28, 2024

Hi Dan,

Thank you for taking the time for reporting these typos. Most of them were addressed.

What's meant with "are more efficiently"?

As the previous article in the series explained working with MemorySegment, that paragraph references the knowledge previously gained as a way to deal with structured data. It is possible to use multiple MemorySegmentobjects to describe the same as MemoryLayout would, but that makes the code more complicated to maintain, hence less efficient.

Wishing you happy holidays,
Ana

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants