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 — Alternative to Java Native Interface (JNI) with a superior,
pure-Java
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 — Alternative to 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
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 — Alternative to 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 — Alternative to Java Native Interface (JNI) with a superior, pure-Java
development model.
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 More
Matt Harrison
 

Similar to Panama4Newbies_Atlanta.pdf (20)

Presentation of Python, Django, DockerStack
Presentation of Python, Django, DockerStackPresentation of Python, Django, DockerStack
Presentation of Python, Django, DockerStack
 
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
 
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
 
Deep learning - the conf br 2018
Deep learning - the conf br 2018Deep learning - the conf br 2018
Deep learning - the conf br 2018
 
BioMake BOSC 2004
BioMake BOSC 2004BioMake BOSC 2004
BioMake BOSC 2004
 
Introduction of Pharo 5.0
Introduction of Pharo 5.0Introduction of Pharo 5.0
Introduction of Pharo 5.0
 
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
 
JavaBasicsCore1.ppt
JavaBasicsCore1.pptJavaBasicsCore1.ppt
JavaBasicsCore1.ppt
 
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
 
Embedded Android
Embedded AndroidEmbedded Android
Embedded Android
 
Java Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPRECJava Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPREC
 
Java Notes
Java Notes Java Notes
Java Notes
 
C_and_C++_notes.pdf
C_and_C++_notes.pdfC_and_C++_notes.pdf
C_and_C++_notes.pdf
 
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 ...
 
Srgoc dotnet
Srgoc dotnetSrgoc dotnet
Srgoc dotnet
 
Docker.io
Docker.ioDocker.io
Docker.io
 
R sharing 101
R sharing 101R sharing 101
R sharing 101
 
CMake Tutorial
CMake TutorialCMake Tutorial
CMake Tutorial
 
Debug generic process
Debug generic processDebug generic process
Debug generic process
 

Recently uploaded

Recently uploaded (20)

GraphSummit Milan - Visione e roadmap del prodotto Neo4j
GraphSummit Milan - Visione e roadmap del prodotto Neo4jGraphSummit Milan - Visione e roadmap del prodotto Neo4j
GraphSummit Milan - Visione e roadmap del prodotto Neo4j
 
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
 
Workshop - Architecting Innovative Graph Applications- GraphSummit Milan
Workshop -  Architecting Innovative Graph Applications- GraphSummit MilanWorkshop -  Architecting Innovative Graph Applications- GraphSummit Milan
Workshop - Architecting Innovative Graph Applications- GraphSummit Milan
 
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
 
Software Engineering - Introduction + Process Models + Requirements Engineering
Software Engineering - Introduction + Process Models + Requirements EngineeringSoftware Engineering - Introduction + Process Models + Requirements Engineering
Software Engineering - Introduction + Process Models + Requirements Engineering
 
architecting-ai-in-the-enterprise-apis-and-applications.pdf
architecting-ai-in-the-enterprise-apis-and-applications.pdfarchitecting-ai-in-the-enterprise-apis-and-applications.pdf
architecting-ai-in-the-enterprise-apis-and-applications.pdf
 
The mythical technical debt. (Brooke, please, forgive me)
The mythical technical debt. (Brooke, please, forgive me)The mythical technical debt. (Brooke, please, forgive me)
The mythical technical debt. (Brooke, please, forgive me)
 
Weeding your micro service landscape.pdf
Weeding your micro service landscape.pdfWeeding your micro service landscape.pdf
Weeding your micro service landscape.pdf
 
OpenChain Webinar: AboutCode and Beyond - End-to-End SCA
OpenChain Webinar: AboutCode and Beyond - End-to-End SCAOpenChain Webinar: AboutCode and Beyond - End-to-End SCA
OpenChain Webinar: AboutCode and Beyond - End-to-End SCA
 
GraphSummit Milan - Neo4j: The Art of the Possible with Graph
GraphSummit Milan - Neo4j: The Art of the Possible with GraphGraphSummit Milan - Neo4j: The Art of the Possible with Graph
GraphSummit Milan - Neo4j: The Art of the Possible with Graph
 
Community is Just as Important as Code by Andrea Goulet
Community is Just as Important as Code by Andrea GouletCommunity is Just as Important as Code by Andrea Goulet
Community is Just as Important as Code by Andrea Goulet
 
Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?
 
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
 
Workshop: Enabling GenAI Breakthroughs with Knowledge Graphs - GraphSummit Milan
Workshop: Enabling GenAI Breakthroughs with Knowledge Graphs - GraphSummit MilanWorkshop: Enabling GenAI Breakthroughs with Knowledge Graphs - GraphSummit Milan
Workshop: Enabling GenAI Breakthroughs with Knowledge Graphs - GraphSummit Milan
 
Optimizing Operations by Aligning Resources with Strategic Objectives Using O...
Optimizing Operations by Aligning Resources with Strategic Objectives Using O...Optimizing Operations by Aligning Resources with Strategic Objectives Using O...
Optimizing Operations by Aligning Resources with Strategic Objectives Using O...
 
Auto Affiliate AI Earns First Commission in 3 Hours..pdf
Auto Affiliate  AI Earns First Commission in 3 Hours..pdfAuto Affiliate  AI Earns First Commission in 3 Hours..pdf
Auto Affiliate AI Earns First Commission in 3 Hours..pdf
 
BusinessGPT - Security and Governance for Generative AI
BusinessGPT  - Security and Governance for Generative AIBusinessGPT  - Security and Governance for Generative AI
BusinessGPT - Security and Governance for Generative AI
 
From Theory to Practice: Utilizing SpiraPlan's REST API
From Theory to Practice: Utilizing SpiraPlan's REST APIFrom Theory to Practice: Utilizing SpiraPlan's REST API
From Theory to Practice: Utilizing SpiraPlan's REST API
 
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
 
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
 

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 — Alternative to Java Native Interface (JNI) with a superior, pure-Java 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 — Alternative to 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. 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 — Alternative to Java Native Interface (JNI) with a superior, pure-Java development model.
  • 25. 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 — Alternative to Java Native Interface (JNI) with a superior, pure-Java development model.
  • 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 :-)