SlideShare a Scribd company logo
1 of 106
Download to read offline
JAVA 19
PROJECT PANAMA


4


NEWBIES
A friendly intro to JEP 419’s Foreign
Function & Memory API
About Me
• A Sr. Developer Advocate at Azul Systems


• Developing software for 20+ years


• Authored & Tech Reviewed Java books


• A JavaFX (fanboy) enthusiast


• Loves sharing and advocating Java based technologies




Other interests:


Graphics, UI, Game programming, GEO spatial data & map
visualizations, Custom UI controls, IoT, Smart phones, AI, and
Robotics and Boating.




 Twitter: @carldea


LinkedIn: https://github.com/carldea


   Email: cdea@azul.com
Carl Dea
Panama4Newbies
https://github.com/carldea/panama4newbies
https://foojay.io/today/project-panama-for-newbies-part-1


 
https://foojay.io/today/project-panama-for-newbies-part-2


https://foojay.io/today/project-panama-for-newbies-part-3
https://foojay.io/today/project-panama-for-newbies-part-4  
OpenGL & SDL RFID Card Reader
JNI
Java interop?
JNI Pain Points
Java Native Interface
Year 2003
The JACOB Project: A JAva-COM Bridge
Now you can call COM Automation from any Win32 Java VM using JNI
Copyright 1999-2004 Dan Adler
Latest Version: October 17, 2004 (V1.8)
https://danadler.com/tech-articles/jacob-2/
jacob.jar
jacob.dll
Win32 DLL
myApplication.jar
Java 

Application
jacob.jar
jacob.dll
Win32 DLL
myApplication.jar
Java Library
jacob.jar
jacob.dll
Win32 DLL
myApplication.jar
Native 

Wrapper lib
jacob.jar
jacob.dll
Win32 DLL
myApplication.jar
OS Native 

Library
jacob.jar
jacob.dll
Win32 DLL
myApplication.jar
OS Native 

Library
Wormholes are hypothetical bridges through space-time (Image credit: Getty)
Future
https://github.com/carldea/panama4newbies
What is


Project Panama?
JEP 389: 1st Incubator
JEP 370: 1st Incubator
Foreign-Memory Access API
Vector API
JEP 338: 1st Incubator JEP 383: 2nd Incubator
JEP 393: 3rd Incubator
Foreign Linker API
Foreign Function


&


Memory Access API
JEP 412: 1st Incubator
Project Panama
JEP 419: 2nd Incubator
JEP 424: Preview
JEP 414: 2nd Incubator
JEP 419: 2nd Incubator
jdk.incubator.foreign
JEP 424 Preview
java.lang.foreign
+

Bug-fixes
Why


Project Panama?
Panama
JNI
Bene
fi
ts
Ease of use — Replace the Java Native Interface (JNI) with a superior, pure-Java
development model.
Performance — Provide performance that is comparable to, if not better than,
existing APIs such as JNI and sun.misc.Unsafe.
General Purpose — Provide ways to operate on different kinds of foreign memory
(e.g., native memory, persistent memory, and managed heap memory) and, over time, to
accommodate other platforms (e.g., 32-bit x86) and foreign functions written in languages
other than C (e.g., C++, Fortran).
Safety — Disable unsafe operations by default, allowing them only after
explicit opt-in from application developers or end users.
Ease of use — Replace the Java Native Interface (JNI) with a superior, pure-Java
development model.
Performance — Provide performance that is comparable to, if not better than,
existing APIs such as JNI and sun.misc.Unsafe.
General Purpose — Provide ways to operate on different kinds of foreign memory
(e.g., native memory, persistent memory, and managed heap memory) and, over time, to
accommodate other platforms (e.g., 32-bit x86) and foreign functions written in languages
other than C (e.g., C++, Fortran).
Safety — Disable unsafe operations by default, allowing them only after
explicit opt-in from application developers or end users.
Bene
fi
ts
Ease of use — Replace the Java Native Interface (JNI) with a superior, pure-Java
development model.
Performance — Provide performance that is comparable to, if not better than,
existing APIs such as JNI and sun.misc.Unsafe.
General Purpose — Provide ways to operate on different kinds of foreign memory
(e.g., native memory, persistent memory, and managed heap memory) and, over time, to
accommodate other platforms (e.g., 32-bit x86) and foreign functions written in languages
other than C (e.g., C++, Fortran).
Safety — Disable unsafe operations by default, allowing them only after
explicit opt-in from application developers or end users.
Bene
fi
ts
Ease of use — Replace the Java Native Interface (JNI) with a superior, pure-Java
development model.
Performance — Provide performance that is comparable to, if not better than,
existing APIs such as JNI and sun.misc.Unsafe.
General Purpose — Provide ways to operate on different kinds of foreign memory
(e.g., native memory, persistent memory, and managed heap memory) and, over time, to
accommodate other platforms (e.g., 32-bit x86) and foreign functions written in languages
other than C (e.g., C++, Fortran).
Safety — Disable unsafe operations by default, allowing them only after
explicit opt-in from application developers or end users.
Bene
fi
ts
ABI
Application


Binary


InterFace
Demos
OpenGL & SDL
Displaying 3D surfaces
RFID Card Reading
Reading keychain tags
Apple’s Text to Speech
Wanna sound like a robot?
FFMpeg


w/ JavaFX Apps
Overlay graphics on top of video
Where to get Panama?
--add-modules jdk.incubator.foreign
Early Access Builds
https://jdk.java.net/panama
Feedback
panama-dev@openjdk.java.net 
Contribute or Build OpenJDK
yourself
https://github.com/openjdk/panama-foreign
Getting Started
JAVA_HOME
c:> set JAVA_HOME=<unzipped_dir>jdk-19.jdkContentsHome


c:> set PATH=%JAVA_HOME%bin;%PATH%


MacOS/Linux


$ export JAVA_HOME=<untarred_dir>/jdk-19.jdk/Contents/Home


$ export PATH=$JAVA_HOME/bin:$PATH


PATH
Setting Environment Variables
Windows
java -version
Quick Check
openjdk version “19-internal" 2022-09-20


OpenJDK Runtime Environment (build 19-internal+0-
adhoc.jdoe.panama-foreign)


OpenJDK 64-Bit Server VM (build 19-internal+0-
adhoc.jdoe.panama-foreign, mixed mode, sharing)
JDK 19 Panama
MethodHandle(s)
Understanding Low level Panama code such as MethodHandles
since JDK 7
What’s a MethodHandle?
A method handle is a Java object that references a
native C function or C variable in memory and
the ability to accessed or invoked from Java code.




In the C/C++ world symbols are a unique lookup of C
functions and variables from the C linker
(java.library.path).
In layman’s (newbie) terms:
What’s a MethodHandle?
A method handle is a typed, directly executable
reference to an underlying method, constructor,
fi
eld, or
similar low-level operation, with optional
transformations of arguments or return values.These
transformations are quite general, and include such
patterns as conversion, insertion, deletion, and
substitution.
According to the Javadoc Documentation since JDK 7
Unix/Linux functions
Getting the process ID using the getpid() function
Really a C int
Creating a MethodHandle - JDK 18+
MethodHandle class
FunctionDescriptor


describes return type from C
NativeSymbol


C symbols from library path
Note: For better performance make method handles
fi
nal static
Is there a better way?
FILE *freopen(const char *filename, const char *mode, FILE *stream)
jextract
Output: Calling getpid() (1234)
https://github.com/openjdk/jextract
in C
C Standard Input/Output


Header
int printf(const char *format, ...)
Zero means OK.


Anything else is an
error or status code
in Java
Panama
Using


Jextract
jextract -h
Quick Check
Option                         Description                              


------                         -----------                              


-?, -h, --help                 print help                               


-C <String>                    pass through argument for clang          


-I <String>                    specify include files path               


-d <String>                    specify where to place generated files   


--dump-includes <String>       dump included symbols into specified file


--header-class-name <String>   name of the header class                 


--include-function <String>    name of function to include              


--include-macro <String>       name of constant macro to include        


--include-struct <String>      name of struct definition to include     


--include-typedef <String>     name of type definition to include       


--include-union <String>       name of union definition to include      


--include-var <String>         name of global variable to include       


-l <String>                    specify a library                        


--source                       generate java sources                    


-t, --target-package <String>  target package for specified header files


Panama’s JExtract
jextract -h
Quick Check
Option                         Description                              


------                         -----------                              


-?, -h, --help                 print help                               


-C <String>                    pass through argument for clang          


-I <String>                    specify include files path               


-d <String>                    specify where to place generated files   


--dump-includes <String>       dump included symbols into specified file


--header-class-name <String>   name of the header class                 


--include-function <String>    name of function to include              


--include-macro <String>       name of constant macro to include        


--include-struct <String>      name of struct definition to include     


--include-typedef <String>     name of type definition to include       


--include-union <String>       name of union definition to include      


--include-var <String>         name of global variable to include       


-l <String>                    specify a library                        


--source                       generate java sources                    


-t, --target-package <String>  target package for specified header files


Panama’s JExtract


C headers path


ie: /usr/local/include
jextract -h
Quick Check
Option                         Description                              


------                         -----------                              


-?, -h, --help                 print help                               


-C <String>                    pass through argument for clang          


-I <String>                    specify include files path               


-d <String>                    specify where to place generated files   


--dump-includes <String>       dump included symbols into specified file


--header-class-name <String>   name of the header class                 


--include-function <String>    name of function to include              


--include-macro <String>       name of constant macro to include        


--include-struct <String>      name of struct definition to include     


--include-typedef <String>     name of type definition to include       


--include-union <String>       name of union definition to include      


--include-var <String>         name of global variable to include       


-l <String>                    specify a library                        


--source                       generate java sources                    


-t, --target-package <String>  target package for specified header files


Panama’s JExtract


Generated Code


ie: ./classes


      ./generated/src
jextract -h
Quick Check
Option                         Description                              


------                         -----------                              


-?, -h, --help                 print help                               


-C <String>                    pass through argument for clang          


-I <String>                    specify include files path               


-d <String>                    specify where to place generated files   


--dump-includes <String>       dump included symbols into specified file


--header-class-name <String>   name of the header class                 


--include-function <String>    name of function to include              


--include-macro <String>       name of constant macro to include        


--include-struct <String>      name of struct definition to include     


--include-typedef <String>     name of type definition to include       


--include-union <String>       name of union definition to include      


--include-var <String>         name of global variable to include       


-l <String>                    specify a library                        


--source                       generate java sources                    


-t, --target-package <String>  target package for specified header files


Panama’s JExtract


Library


ie: tensorflow
jextract -h
Quick Check
Option                         Description                              


------                         -----------                              


-?, -h, --help                 print help                               


-C <String>                    pass through argument for clang          


-I <String>                    specify include files path               


-d <String>                    specify where to place generated files   


--dump-includes <String>       dump included symbols into specified file


--header-class-name <String>   name of the header class                 


--include-function <String>    name of function to include              


--include-macro <String>       name of constant macro to include        


--include-struct <String>      name of struct definition to include     


--include-typedef <String>     name of type definition to include       


--include-union <String>       name of union definition to include      


--include-var <String>         name of global variable to include       


-l <String>                    specify a library                        


--source                       generate java sources                    


-t, --target-package <String>  target package for specified header files


Panama’s JExtract


Generated Java Source


ie: org/unix/foo_h.java
jextract -h
Quick Check
Option                         Description                              


------                         -----------                              


-?, -h, --help                 print help                               


-C <String>                    pass through argument for clang          


-I <String>                    specify include files path               


-d <String>                    specify where to place generated files   


--dump-includes <String>       dump included symbols into specified file


--header-class-name <String>   name of the header class                 


--include-function <String>    name of function to include              


--include-macro <String>       name of constant macro to include        


--include-struct <String>      name of struct definition to include     


--include-typedef <String>     name of type definition to include       


--include-union <String>       name of union definition to include      


--include-var <String>         name of global variable to include       


-l <String>                    specify a library                        


--source                       generate java sources                    


-t, --target-package <String>  target package for specified header files


Panama’s JExtract


Package namespace prefix


ie: org.unix
$ export C_INCLUDE=/usr/local/include


# or depending where the defaults are installed


$ export C_INCLUDE=/usr/include
MacOS


$ export C_INCLUDE=Applications/Xcode.app/Contents/
Developer/Platforms/MacOSX.platform/Developer/SDKs/
MacOSX.sdk/usr/include
Let’s jextract stdio.h
Windows


Tech Note: UsingVSCode 2022 - https://docs.microsoft.com/en-us/cpp/build/
walkthrough-compile-a-c-program-on-the-command-line?
view=msvc-170#open-a-developer-command-prompt-in-visual-studio-2022
Linux
Create a convenient
environment variable
C_INCLUDE
$ jextract -d classes 


-t org.unix 


-I $C_INCLUDE 


$C_INCLUDE/stdio.h


Generate Java Classes
$ jextract -d classes 


-t org.unix 


-I $C_INCLUDE 


$C_INCLUDE/stdio.h


Generate Java Classes
Destination Directory ./classes
$ jextract -d classes 


-t org.unix 


-I $C_INCLUDE 


$C_INCLUDE/stdio.h


Generate Java Classes
Package Namespace: org.unix
$ jextract -d classes 


-t org.unix 


-I $C_INCLUDE 


$C_INCLUDE/stdio.h


Generate Java Classes
Header include directory
$ jextract -d classes 


-t org.unix 


-I $C_INCLUDE 


$C_INCLUDE/stdio.h


Generate Java Classes
Target Header
fi
le
Generate Java Classes
Generated Java Classes
$ jextract --source 


-d generated/src 


-t org.unix 


-I $C_INCLUDE 


  $C_INCLUDE/stdio.h


Generate Java Sources
$ jextract --source 


-d generated/src 


-t org.unix 


-I $C_INCLUDE 


  $C_INCLUDE/stdio.h


Generate Java Sources
Generate Java Source Code
Generate Source Directory
Generate Java Sources
Generated Java Sources
--add-modules jdk.incubator.foreign


IntelliJ - Preferences
IDE Setup
imports
HelloWorld.java
HelloWorld.java
JDK 18+ ResourceScope
Implements Closable.
Cleans up after try block.
allocateUtf8String()
HelloWorld.java


JDK 18+
C Functions
MemorySegment


to


Java String
C String converts to Java String object
Let’s Run HelloWorld.java
Enabling Native Access
Let’s Run HelloWorld.java
Adding foreign function & memory module
Let’s Run HelloWorld.java
Java Application
Output of HelloWorld.java
Memory Allocation
RAM
ValueLayout
To create a C primitive use CLinker’s C ValueLayouts such as


C_INT


C_DOUBLE


C_POINTER, etc.
MemorySegment
ValueLayout
To create a C primitive use CLinker’s C ValueLayouts such as


C_INT


C_DOUBLE


C_POINTER, etc. Native Allocator
ValueLayout
To create a C primitive use CLinker’s C ValueLayouts such as


C_INT


C_DOUBLE


C_POINTER, etc.
ValueLayout
To create a C primitive use CLinker’s C ValueLayouts such as


C_INT


C_DOUBLE


C_POINTER, etc.
JDK 18+ uses the


MemorySegment class’


get()/set() methods
C Primitive Arrays
SegmentAllocator.allocateArray()
Primitive Array - JDK 18+
To create a C primitive Arrays use:
SegmentAllocator.implicitAllocator().allocateArray()
00 00 00 00 00 00 00 01
0
00 00 00 00 00 00 00 02
1
Primitive Array
Output:
Summary
•The 3Ws of Project Panama
•MethodHandle class
•jextract
•MethodSegment class
•ValueLayout class
•allocateArray() method
But there’s more…
But there’s more…
C Structs
C Pointers
C Callbacks
Other Languages


(Polyglot)
https://github.com/carldea/panama4newbies
Q/A
Thank you for coming!
Twitter: @carldea


    Blog: carlfx.com  & foojay.io


  Github: https://github.com/carldea


   Email: cdea@azul.com


LinkedIn: https://www.linkedin.com/in/carldea
References
Links, Mailing Lists, videos, blogs
• Panama 4 Newbies – https://foojay.io/today/project-panama-for-newbies-part-1


• Panama EA builds - https://jdk.java.net/panama


• Feedback - panama-dev@openjdk.java.net


• Mailing List     - https://mail.openjdk.java.net/mailman/listinfo/panama-dev


• JEP 412 1st incubator - https://openjdk.java.net/jeps/412


• JEP 419 2nd incubator - https://openjdk.java.net/jeps/419


• JEP 424 Preview - https://openjdk.java.net/jeps/424


• Foojay.io – https://foojay.io


• Panama Samples - https://github.com/sundararajana/panama-jextract-samples


• Panama at GitHub – https://github.com/openjdk/panama-foreign


• Panama Update by Maurizio Cimadamore - https://www.youtube.com/watch?v=r4dNRVWYaZI


• Upcalls by M.C.  - https://cr.openjdk.java.net/~mcimadamore/panama/f
fi
.html


• Going Meta to Valhalla & Panama w/ John Rose - https://www.youtube.com/watch?v=49GUljUmGHg


• Rust & Panama - https://jornvernee.github.io/rust/panama-f
fi
/2021/09/03/rust-panama-helloworld.html


• Method Handles - https://download.java.net/java/early_access/panama/docs/api/java.base/java/lang/
invoke/MethodHandle.html


• Jextract on GitHub - https://github.com/openjdk/jextract
Supplemental
Slides & Demos
What’s a C Pointer?
C supports the use of pointers, a type of reference that records the
address or location of an object or function in memory. Pointers can
be dereferenced to access data stored at the address pointed to, or
to invoke a pointed-to function. Pointers can be manipulated using
assignment or pointer arithmetic.
The C Programming Language book


by


Brian W. Kernighan & Dennis M. Ritchie
What’s a C Pointer?
ptr = &x x = 5
0x0DDBA11 0xCAFEBABE 0xCAFEBABE 5
Address or Location
What’s a C Pointer?
ptr = &x x = 5
0x0DDBA11 0xCAFEBABE 0xCAFEBABE 5
Storage
Obtaining a C Pointer in Panama
The reference or address of a C int variable:
How to Dereference A Pointer?
Obtain value at the address referenced by the pointer.
Dereferencing a C Pointer in Panama - JDK 18+
MemoryAddress class
What’s a C Struct?
To put it simply, this is the ancestor to C++ & Java's
concept of classes or records. 
Declaring pt of type Point struct
How to Create a Struct in
Panama?
Use GroupLayout and MemoryLayout to describe a C struct.
Creating C Structs in Panama
Using MemoryLayout’s structLayout() method.
Accessing C Structs in Panama - JDK 18+
To access a MemoryLayout (C Struct) use VarHandles.
Present Day
JNI
JNI Requires Native Wrapper Code
Rock, Paper Scissors
Playing against the computer you lose every time
Call Backs
Native Access to


Mic & Sound Audio
Realtime audio input and output
Apple’s Touch ID
Access Denied with your
fi
ngerprint :-)
Apple’s Touch ID
Access Denied with your
fi
ngerprint :-)
Panama4Newbies
https://github.com/carldea/panama4newbies
https://foojay.io/today/project-panama-for-newbies-part-1


 
https://foojay.io/today/project-panama-for-newbies-part-2


https://foojay.io/today/project-panama-for-newbies-part-3
https://foojay.io/today/project-panama-for-newbies-part-4  

More Related Content

Similar to Panama4Newbies_Atlanta.pdf

PyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and MorePyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and MoreMatt Harrison
 
Eric Lafortune - Fighting application size with ProGuard and beyond
Eric Lafortune - Fighting application size with ProGuard and beyondEric Lafortune - Fighting application size with ProGuard and beyond
Eric Lafortune - Fighting application size with ProGuard and beyondGuardSquare
 
Eric Lafortune - Fighting application size with ProGuard and beyond
Eric Lafortune - Fighting application size with ProGuard and beyondEric Lafortune - Fighting application size with ProGuard and beyond
Eric Lafortune - Fighting application size with ProGuard and beyondGuardSquare
 
Introduction of Pharo 5.0
Introduction of Pharo 5.0Introduction of Pharo 5.0
Introduction of Pharo 5.0Masashi Umezawa
 
C_and_C++_notes.pdf
C_and_C++_notes.pdfC_and_C++_notes.pdf
C_and_C++_notes.pdfTigabu Yaya
 
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/OCore Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/OWebStackAcademy
 
Embedded Android
Embedded AndroidEmbedded Android
Embedded Android晓东 杜
 
Deep learning - the conf br 2018
Deep learning - the conf br 2018Deep learning - the conf br 2018
Deep learning - the conf br 2018Fabio Janiszevski
 
Java Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPRECJava Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPRECSreedhar Chowdam
 
Unit No. 1 Introduction to Java.pptx
Unit No. 1 Introduction to Java.pptxUnit No. 1 Introduction to Java.pptx
Unit No. 1 Introduction to Java.pptxDrYogeshDeshmukh1
 
JavaBasicsCore1.ppt
JavaBasicsCore1.pptJavaBasicsCore1.ppt
JavaBasicsCore1.pptbuvanabala
 
PyParis 2017 / Camisole : A secure online sandbox to grade student - Antoine ...
PyParis 2017 / Camisole : A secure online sandbox to grade student - Antoine ...PyParis 2017 / Camisole : A secure online sandbox to grade student - Antoine ...
PyParis 2017 / Camisole : A secure online sandbox to grade student - Antoine ...Pôle Systematic Paris-Region
 
CMake Tutorial
CMake TutorialCMake Tutorial
CMake TutorialFu Haiping
 
Hack Like It's 2013 (The Workshop)
Hack Like It's 2013 (The Workshop)Hack Like It's 2013 (The Workshop)
Hack Like It's 2013 (The Workshop)Itzik Kotler
 

Similar to Panama4Newbies_Atlanta.pdf (20)

BioMake BOSC 2004
BioMake BOSC 2004BioMake BOSC 2004
BioMake BOSC 2004
 
PyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and MorePyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and More
 
Eric Lafortune - Fighting application size with ProGuard and beyond
Eric Lafortune - Fighting application size with ProGuard and beyondEric Lafortune - Fighting application size with ProGuard and beyond
Eric Lafortune - Fighting application size with ProGuard and beyond
 
Eric Lafortune - Fighting application size with ProGuard and beyond
Eric Lafortune - Fighting application size with ProGuard and beyondEric Lafortune - Fighting application size with ProGuard and beyond
Eric Lafortune - Fighting application size with ProGuard and beyond
 
JAVA BASICS
JAVA BASICSJAVA BASICS
JAVA BASICS
 
Introduction of Pharo 5.0
Introduction of Pharo 5.0Introduction of Pharo 5.0
Introduction of Pharo 5.0
 
C_and_C++_notes.pdf
C_and_C++_notes.pdfC_and_C++_notes.pdf
C_and_C++_notes.pdf
 
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/OCore Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
 
Embedded Android
Embedded AndroidEmbedded Android
Embedded Android
 
Deep learning - the conf br 2018
Deep learning - the conf br 2018Deep learning - the conf br 2018
Deep learning - the conf br 2018
 
R sharing 101
R sharing 101R sharing 101
R sharing 101
 
Srgoc dotnet
Srgoc dotnetSrgoc dotnet
Srgoc dotnet
 
Java Notes
Java Notes Java Notes
Java Notes
 
Java Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPRECJava Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPREC
 
Docker.io
Docker.ioDocker.io
Docker.io
 
Unit No. 1 Introduction to Java.pptx
Unit No. 1 Introduction to Java.pptxUnit No. 1 Introduction to Java.pptx
Unit No. 1 Introduction to Java.pptx
 
JavaBasicsCore1.ppt
JavaBasicsCore1.pptJavaBasicsCore1.ppt
JavaBasicsCore1.ppt
 
PyParis 2017 / Camisole : A secure online sandbox to grade student - Antoine ...
PyParis 2017 / Camisole : A secure online sandbox to grade student - Antoine ...PyParis 2017 / Camisole : A secure online sandbox to grade student - Antoine ...
PyParis 2017 / Camisole : A secure online sandbox to grade student - Antoine ...
 
CMake Tutorial
CMake TutorialCMake Tutorial
CMake Tutorial
 
Hack Like It's 2013 (The Workshop)
Hack Like It's 2013 (The Workshop)Hack Like It's 2013 (The Workshop)
Hack Like It's 2013 (The Workshop)
 

Recently uploaded

办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?Watsoo Telematics
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 

Recently uploaded (20)

Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 

Panama4Newbies_Atlanta.pdf

  • 1. JAVA 19 PROJECT PANAMA 
 4 
 NEWBIES A friendly intro to JEP 419’s Foreign Function & Memory API
  • 2. About Me • A Sr. Developer Advocate at Azul Systems • Developing software for 20+ years • Authored & Tech Reviewed Java books • A JavaFX (fanboy) enthusiast • Loves sharing and advocating Java based technologies 
 
 Other interests: 
 Graphics, UI, Game programming, GEO spatial data & map visualizations, Custom UI controls, IoT, Smart phones, AI, and Robotics and Boating. 
  Twitter: @carldea 
 LinkedIn: https://github.com/carldea 
    Email: cdea@azul.com Carl Dea
  • 4. OpenGL & SDL RFID Card Reader JNI Java interop?
  • 5. JNI Pain Points Java Native Interface
  • 7.
  • 8. The JACOB Project: A JAva-COM Bridge Now you can call COM Automation from any Win32 Java VM using JNI Copyright 1999-2004 Dan Adler Latest Version: October 17, 2004 (V1.8) https://danadler.com/tech-articles/jacob-2/
  • 14. Wormholes are hypothetical bridges through space-time (Image credit: Getty) Future https://github.com/carldea/panama4newbies
  • 15.
  • 17. JEP 389: 1st Incubator JEP 370: 1st Incubator Foreign-Memory Access API Vector API JEP 338: 1st Incubator JEP 383: 2nd Incubator JEP 393: 3rd Incubator Foreign Linker API Foreign Function 
 & 
 Memory Access API JEP 412: 1st Incubator Project Panama JEP 419: 2nd Incubator JEP 424: Preview JEP 414: 2nd Incubator
  • 18.
  • 19. JEP 419: 2nd Incubator jdk.incubator.foreign JEP 424 Preview java.lang.foreign +
 Bug-fixes
  • 22. Bene fi ts Ease of use — Replace the Java Native Interface (JNI) with a superior, pure-Java development model. Performance — Provide performance that is comparable to, if not better than, existing APIs such as JNI and sun.misc.Unsafe. General Purpose — Provide ways to operate on different kinds of foreign memory (e.g., native memory, persistent memory, and managed heap memory) and, over time, to accommodate other platforms (e.g., 32-bit x86) and foreign functions written in languages other than C (e.g., C++, Fortran). Safety — Disable unsafe operations by default, allowing them only after explicit opt-in from application developers or end users.
  • 23. Ease of use — Replace the Java Native Interface (JNI) with a superior, pure-Java development model. Performance — Provide performance that is comparable to, if not better than, existing APIs such as JNI and sun.misc.Unsafe. General Purpose — Provide ways to operate on different kinds of foreign memory (e.g., native memory, persistent memory, and managed heap memory) and, over time, to accommodate other platforms (e.g., 32-bit x86) and foreign functions written in languages other than C (e.g., C++, Fortran). Safety — Disable unsafe operations by default, allowing them only after explicit opt-in from application developers or end users. Bene fi ts
  • 24. Ease of use — Replace the Java Native Interface (JNI) with a superior, pure-Java development model. Performance — Provide performance that is comparable to, if not better than, existing APIs such as JNI and sun.misc.Unsafe. General Purpose — Provide ways to operate on different kinds of foreign memory (e.g., native memory, persistent memory, and managed heap memory) and, over time, to accommodate other platforms (e.g., 32-bit x86) and foreign functions written in languages other than C (e.g., C++, Fortran). Safety — Disable unsafe operations by default, allowing them only after explicit opt-in from application developers or end users. Bene fi ts
  • 25. Ease of use — Replace the Java Native Interface (JNI) with a superior, pure-Java development model. Performance — Provide performance that is comparable to, if not better than, existing APIs such as JNI and sun.misc.Unsafe. General Purpose — Provide ways to operate on different kinds of foreign memory (e.g., native memory, persistent memory, and managed heap memory) and, over time, to accommodate other platforms (e.g., 32-bit x86) and foreign functions written in languages other than C (e.g., C++, Fortran). Safety — Disable unsafe operations by default, allowing them only after explicit opt-in from application developers or end users. Bene fi ts
  • 27. Demos
  • 28. OpenGL & SDL Displaying 3D surfaces
  • 29. RFID Card Reading Reading keychain tags
  • 30. Apple’s Text to Speech Wanna sound like a robot?
  • 31. FFMpeg 
 w/ JavaFX Apps Overlay graphics on top of video
  • 32. Where to get Panama? --add-modules jdk.incubator.foreign
  • 33. Early Access Builds https://jdk.java.net/panama Feedback panama-dev@openjdk.java.net  Contribute or Build OpenJDK yourself https://github.com/openjdk/panama-foreign
  • 35. JAVA_HOME c:> set JAVA_HOME=<unzipped_dir>jdk-19.jdkContentsHome c:> set PATH=%JAVA_HOME%bin;%PATH% MacOS/Linux $ export JAVA_HOME=<untarred_dir>/jdk-19.jdk/Contents/Home $ export PATH=$JAVA_HOME/bin:$PATH PATH Setting Environment Variables Windows
  • 36. java -version Quick Check openjdk version “19-internal" 2022-09-20 OpenJDK Runtime Environment (build 19-internal+0- adhoc.jdoe.panama-foreign) OpenJDK 64-Bit Server VM (build 19-internal+0- adhoc.jdoe.panama-foreign, mixed mode, sharing) JDK 19 Panama
  • 37. MethodHandle(s) Understanding Low level Panama code such as MethodHandles since JDK 7
  • 38. What’s a MethodHandle? A method handle is a Java object that references a native C function or C variable in memory and the ability to accessed or invoked from Java code. 
 
 In the C/C++ world symbols are a unique lookup of C functions and variables from the C linker (java.library.path). In layman’s (newbie) terms:
  • 39. What’s a MethodHandle? A method handle is a typed, directly executable reference to an underlying method, constructor, fi eld, or similar low-level operation, with optional transformations of arguments or return values.These transformations are quite general, and include such patterns as conversion, insertion, deletion, and substitution. According to the Javadoc Documentation since JDK 7
  • 40. Unix/Linux functions Getting the process ID using the getpid() function Really a C int
  • 41. Creating a MethodHandle - JDK 18+ MethodHandle class FunctionDescriptor 
 describes return type from C NativeSymbol 
 C symbols from library path Note: For better performance make method handles fi nal static
  • 42. Is there a better way? FILE *freopen(const char *filename, const char *mode, FILE *stream)
  • 43. jextract Output: Calling getpid() (1234) https://github.com/openjdk/jextract
  • 44. in C
  • 46. int printf(const char *format, ...)
  • 47. Zero means OK. 
 Anything else is an error or status code
  • 49. jextract -h Quick Check Option                         Description                               ------                         -----------                               -?, -h, --help                 print help                                -C <String>                    pass through argument for clang           -I <String>                    specify include files path                -d <String>                    specify where to place generated files    --dump-includes <String>       dump included symbols into specified file --header-class-name <String>   name of the header class                  --include-function <String>    name of function to include               --include-macro <String>       name of constant macro to include         --include-struct <String>      name of struct definition to include      --include-typedef <String>     name of type definition to include        --include-union <String>       name of union definition to include       --include-var <String>         name of global variable to include        -l <String>                    specify a library                         --source                       generate java sources                     -t, --target-package <String>  target package for specified header files Panama’s JExtract
  • 50. jextract -h Quick Check Option                         Description                               ------                         -----------                               -?, -h, --help                 print help                                -C <String>                    pass through argument for clang           -I <String>                    specify include files path                -d <String>                    specify where to place generated files    --dump-includes <String>       dump included symbols into specified file --header-class-name <String>   name of the header class                  --include-function <String>    name of function to include               --include-macro <String>       name of constant macro to include         --include-struct <String>      name of struct definition to include      --include-typedef <String>     name of type definition to include        --include-union <String>       name of union definition to include       --include-var <String>         name of global variable to include        -l <String>                    specify a library                         --source                       generate java sources                     -t, --target-package <String>  target package for specified header files Panama’s JExtract C headers path 
 ie: /usr/local/include
  • 51. jextract -h Quick Check Option                         Description                               ------                         -----------                               -?, -h, --help                 print help                                -C <String>                    pass through argument for clang           -I <String>                    specify include files path                -d <String>                    specify where to place generated files    --dump-includes <String>       dump included symbols into specified file --header-class-name <String>   name of the header class                  --include-function <String>    name of function to include               --include-macro <String>       name of constant macro to include         --include-struct <String>      name of struct definition to include      --include-typedef <String>     name of type definition to include        --include-union <String>       name of union definition to include       --include-var <String>         name of global variable to include        -l <String>                    specify a library                         --source                       generate java sources                     -t, --target-package <String>  target package for specified header files Panama’s JExtract Generated Code 
 ie: ./classes 
       ./generated/src
  • 52. jextract -h Quick Check Option                         Description                               ------                         -----------                               -?, -h, --help                 print help                                -C <String>                    pass through argument for clang           -I <String>                    specify include files path                -d <String>                    specify where to place generated files    --dump-includes <String>       dump included symbols into specified file --header-class-name <String>   name of the header class                  --include-function <String>    name of function to include               --include-macro <String>       name of constant macro to include         --include-struct <String>      name of struct definition to include      --include-typedef <String>     name of type definition to include        --include-union <String>       name of union definition to include       --include-var <String>         name of global variable to include        -l <String>                    specify a library                         --source                       generate java sources                     -t, --target-package <String>  target package for specified header files Panama’s JExtract Library 
 ie: tensorflow
  • 53. jextract -h Quick Check Option                         Description                               ------                         -----------                               -?, -h, --help                 print help                                -C <String>                    pass through argument for clang           -I <String>                    specify include files path                -d <String>                    specify where to place generated files    --dump-includes <String>       dump included symbols into specified file --header-class-name <String>   name of the header class                  --include-function <String>    name of function to include               --include-macro <String>       name of constant macro to include         --include-struct <String>      name of struct definition to include      --include-typedef <String>     name of type definition to include        --include-union <String>       name of union definition to include       --include-var <String>         name of global variable to include        -l <String>                    specify a library                         --source                       generate java sources                     -t, --target-package <String>  target package for specified header files Panama’s JExtract Generated Java Source 
 ie: org/unix/foo_h.java
  • 54. jextract -h Quick Check Option                         Description                               ------                         -----------                               -?, -h, --help                 print help                                -C <String>                    pass through argument for clang           -I <String>                    specify include files path                -d <String>                    specify where to place generated files    --dump-includes <String>       dump included symbols into specified file --header-class-name <String>   name of the header class                  --include-function <String>    name of function to include               --include-macro <String>       name of constant macro to include         --include-struct <String>      name of struct definition to include      --include-typedef <String>     name of type definition to include        --include-union <String>       name of union definition to include       --include-var <String>         name of global variable to include        -l <String>                    specify a library                         --source                       generate java sources                     -t, --target-package <String>  target package for specified header files Panama’s JExtract Package namespace prefix 
 ie: org.unix
  • 55. $ export C_INCLUDE=/usr/local/include 
 # or depending where the defaults are installed $ export C_INCLUDE=/usr/include MacOS $ export C_INCLUDE=Applications/Xcode.app/Contents/ Developer/Platforms/MacOSX.platform/Developer/SDKs/ MacOSX.sdk/usr/include Let’s jextract stdio.h Windows Tech Note: UsingVSCode 2022 - https://docs.microsoft.com/en-us/cpp/build/ walkthrough-compile-a-c-program-on-the-command-line? view=msvc-170#open-a-developer-command-prompt-in-visual-studio-2022 Linux Create a convenient environment variable C_INCLUDE
  • 56. $ jextract -d classes 
 -t org.unix 
 -I $C_INCLUDE 
 $C_INCLUDE/stdio.h Generate Java Classes
  • 57. $ jextract -d classes 
 -t org.unix 
 -I $C_INCLUDE 
 $C_INCLUDE/stdio.h Generate Java Classes Destination Directory ./classes
  • 58. $ jextract -d classes 
 -t org.unix 
 -I $C_INCLUDE 
 $C_INCLUDE/stdio.h Generate Java Classes Package Namespace: org.unix
  • 59. $ jextract -d classes 
 -t org.unix 
 -I $C_INCLUDE 
 $C_INCLUDE/stdio.h Generate Java Classes Header include directory
  • 60. $ jextract -d classes 
 -t org.unix 
 -I $C_INCLUDE 
 $C_INCLUDE/stdio.h Generate Java Classes Target Header fi le
  • 62. $ jextract --source 
 -d generated/src 
 -t org.unix 
 -I $C_INCLUDE   $C_INCLUDE/stdio.h Generate Java Sources
  • 63. $ jextract --source 
 -d generated/src 
 -t org.unix 
 -I $C_INCLUDE   $C_INCLUDE/stdio.h Generate Java Sources Generate Java Source Code Generate Source Directory
  • 67. HelloWorld.java JDK 18+ ResourceScope Implements Closable. Cleans up after try block.
  • 69. MemorySegment 
 to 
 Java String C String converts to Java String object
  • 71. Let’s Run HelloWorld.java Adding foreign function & memory module
  • 75. ValueLayout To create a C primitive use CLinker’s C ValueLayouts such as 
 C_INT 
 C_DOUBLE 
 C_POINTER, etc. MemorySegment
  • 76. ValueLayout To create a C primitive use CLinker’s C ValueLayouts such as 
 C_INT 
 C_DOUBLE 
 C_POINTER, etc. Native Allocator
  • 77. ValueLayout To create a C primitive use CLinker’s C ValueLayouts such as 
 C_INT 
 C_DOUBLE 
 C_POINTER, etc.
  • 78. ValueLayout To create a C primitive use CLinker’s C ValueLayouts such as 
 C_INT 
 C_DOUBLE 
 C_POINTER, etc. JDK 18+ uses the 
 MemorySegment class’ 
 get()/set() methods
  • 80. Primitive Array - JDK 18+ To create a C primitive Arrays use: SegmentAllocator.implicitAllocator().allocateArray() 00 00 00 00 00 00 00 01 0 00 00 00 00 00 00 00 02 1
  • 82. Summary •The 3Ws of Project Panama •MethodHandle class •jextract •MethodSegment class •ValueLayout class •allocateArray() method
  • 84. But there’s more… C Structs C Pointers C Callbacks Other Languages 
 (Polyglot)
  • 86. Q/A Thank you for coming! Twitter: @carldea 
     Blog: carlfx.com  & foojay.io 
   Github: https://github.com/carldea    Email: cdea@azul.com 
 LinkedIn: https://www.linkedin.com/in/carldea
  • 87. References Links, Mailing Lists, videos, blogs • Panama 4 Newbies – https://foojay.io/today/project-panama-for-newbies-part-1 • Panama EA builds - https://jdk.java.net/panama • Feedback - panama-dev@openjdk.java.net • Mailing List     - https://mail.openjdk.java.net/mailman/listinfo/panama-dev • JEP 412 1st incubator - https://openjdk.java.net/jeps/412 • JEP 419 2nd incubator - https://openjdk.java.net/jeps/419 • JEP 424 Preview - https://openjdk.java.net/jeps/424 • Foojay.io – https://foojay.io • Panama Samples - https://github.com/sundararajana/panama-jextract-samples • Panama at GitHub – https://github.com/openjdk/panama-foreign • Panama Update by Maurizio Cimadamore - https://www.youtube.com/watch?v=r4dNRVWYaZI • Upcalls by M.C.  - https://cr.openjdk.java.net/~mcimadamore/panama/f fi .html • Going Meta to Valhalla & Panama w/ John Rose - https://www.youtube.com/watch?v=49GUljUmGHg • Rust & Panama - https://jornvernee.github.io/rust/panama-f fi /2021/09/03/rust-panama-helloworld.html • Method Handles - https://download.java.net/java/early_access/panama/docs/api/java.base/java/lang/ invoke/MethodHandle.html • Jextract on GitHub - https://github.com/openjdk/jextract
  • 89. What’s a C Pointer? C supports the use of pointers, a type of reference that records the address or location of an object or function in memory. Pointers can be dereferenced to access data stored at the address pointed to, or to invoke a pointed-to function. Pointers can be manipulated using assignment or pointer arithmetic. The C Programming Language book 
 by 
 Brian W. Kernighan & Dennis M. Ritchie
  • 90. What’s a C Pointer? ptr = &x x = 5 0x0DDBA11 0xCAFEBABE 0xCAFEBABE 5 Address or Location
  • 91. What’s a C Pointer? ptr = &x x = 5 0x0DDBA11 0xCAFEBABE 0xCAFEBABE 5 Storage
  • 92. Obtaining a C Pointer in Panama The reference or address of a C int variable:
  • 93. How to Dereference A Pointer? Obtain value at the address referenced by the pointer.
  • 94. Dereferencing a C Pointer in Panama - JDK 18+ MemoryAddress class
  • 95. What’s a C Struct? To put it simply, this is the ancestor to C++ & Java's concept of classes or records.  Declaring pt of type Point struct
  • 96. How to Create a Struct in Panama? Use GroupLayout and MemoryLayout to describe a C struct.
  • 97. Creating C Structs in Panama Using MemoryLayout’s structLayout() method.
  • 98. Accessing C Structs in Panama - JDK 18+ To access a MemoryLayout (C Struct) use VarHandles.
  • 99. Present Day JNI JNI Requires Native Wrapper Code
  • 100. Rock, Paper Scissors Playing against the computer you lose every time
  • 102. Native Access to 
 Mic & Sound Audio Realtime audio input and output
  • 103.
  • 104. Apple’s Touch ID Access Denied with your fi ngerprint :-)
  • 105. Apple’s Touch ID Access Denied with your fi ngerprint :-)