<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="https://www.roray.dev/feed_style.xsl" type="text/xsl"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <tabi:metadata xmlns:tabi="https://github.com/welpo/tabi">
        <tabi:base_url>https:&#x2F;&#x2F;www.roray.dev</tabi:base_url>
        <tabi:separator>
            •
        </tabi:separator>
        <tabi:about_feeds>This is a web feed, also known as an Atom feed. Subscribe by copying the URL from the address bar into your newsreader. Visit About Feeds to learn more and get started. It&#x27;s free.</tabi:about_feeds>
        <tabi:visit_the_site>Visit website</tabi:visit_the_site>
        <tabi:recent_posts>Recent posts</tabi:recent_posts>
        <tabi:last_updated_on>Updated on $DATE</tabi:last_updated_on>
        <tabi:default_theme></tabi:default_theme>
        <tabi:post_listing_date>date</tabi:post_listing_date>
        <tabi:current_section>ffm</tabi:current_section>
    </tabi:metadata><link rel="extra-stylesheet" href="https://www.roray.dev/skins/lowcontrast_orange.css?h=43aaccb17d8ec616ace4" /><title>roray.dev - ffm</title>
        <subtitle>home of Rohan Ray&#x27;s coding world</subtitle>
    <link href="https://www.roray.dev/tags/ffm/atom.xml" rel="self" type="application/atom+xml"/>
    <link href="https://www.roray.dev/tags/ffm/" rel="alternate" type="text/html"/>
    <generator uri="https://www.getzola.org/">Zola</generator><updated>2025-11-29T00:00:00+00:00</updated><id>https://www.roray.dev/tags/ffm/atom.xml</id><entry xml:lang="en">
        <title>MYRA stack - modern JAVA FFM based libraries</title>
        <published>2025-11-29T00:00:00+00:00</published>
        <updated>2025-11-29T00:00:00+00:00</updated>
        <author>
            <name>Rohan Ray</name>
        </author>
        <link rel="alternate" href="https://www.roray.dev/blog/myra-stack/" type="text/html"/>
        <id>https://www.roray.dev/blog/myra-stack/</id>
        
            <content type="html">&lt;h1 id=&quot;introducing-myra-what-i-ve-been-building&quot;&gt;Introducing MYRA: What I’ve Been Building&lt;&#x2F;h1&gt;
&lt;h2 id=&quot;overview&quot;&gt;Overview&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;strong&gt;MYRA&lt;&#x2F;strong&gt; — &lt;em&gt;Memory Yielded, Rapid Access&lt;&#x2F;em&gt; — is a production-grade ecosystem of Java libraries built on the Foreign Function &amp;amp; Memory (FFM) API, designed for deterministic, sub-microsecond latency applications.&lt;&#x2F;p&gt;
&lt;p&gt;Unlike approaches that rely on &lt;code&gt;Unsafe&lt;&#x2F;code&gt; or JNI boilerplate, MYRA leverages the standardized FFM primitives introduced in Java 22, providing memory safety and future-proof compatibility without sacrificing performance.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;design-principles&quot;&gt;Design Principles&lt;&#x2F;h3&gt;
&lt;p&gt;The ecosystem is built on four core principles:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Zero GC&lt;&#x2F;strong&gt;: Off-heap allocation and deterministic resource management eliminate GC pauses in the critical path.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Zero Allocation&lt;&#x2F;strong&gt;: Reusable object instances and flyweight patterns prevent heap churn.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Zero Copy&lt;&#x2F;strong&gt;: Direct memory access and structured layout eliminate serialization overhead.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Ultra-Low Latency&lt;&#x2F;strong&gt;: Sub-30μs mean latencies with controlled tail behavior, built for high-frequency trading, market data feeds, and real-time systems.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;hr &#x2F;&gt;
&lt;h2 id=&quot;the-problem-ffm-solves&quot;&gt;The Problem FFM Solves&lt;&#x2F;h2&gt;
&lt;p&gt;Performance-sensitive Java systems have historically relied on &lt;code&gt;Unsafe&lt;&#x2F;code&gt; — a powerful but unstable internal API that breaks with each JDK release. The Foreign Function &amp;amp; Memory API provides a safe, standardized alternative for off-heap memory access and native interoperability.&lt;&#x2F;p&gt;
&lt;p&gt;MYRA is built entirely on FFM, proving that it’s not just a replacement for &lt;code&gt;Unsafe&lt;&#x2F;code&gt;, but a foundation for a new class of infrastructure libraries that were previously impossible to build safely on the JVM.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h2 id=&quot;what-s-in-the-box&quot;&gt;What’s in the Box&lt;&#x2F;h2&gt;
&lt;p&gt;MYRA comprises six libraries designed for vertical integration:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;roray-ffm-utils&lt;&#x2F;strong&gt; — Memory arenas, direct buffers, native resource handling. The plumbing layer.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;myra-codec&lt;&#x2F;strong&gt; — Zero-copy serialization that reads and writes directly to off-heap memory. No intermediate objects.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;myra-transport&lt;&#x2F;strong&gt; — Networking built on Linux &lt;code&gt;io_uring&lt;&#x2F;code&gt;. Fewer syscalls, higher throughput.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;MVP.Express RPC&lt;&#x2F;strong&gt; — &lt;em&gt;MYRA Virtual Procedure over Express Link&lt;&#x2F;em&gt; — A lightweight RPC framework on top of the above. Currently in progress.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;JIA-Cache&lt;&#x2F;strong&gt; — &lt;em&gt;Java In-Memory Accelerated Cache&lt;&#x2F;em&gt; — Off-heap caching with predictable latency. Coming soon.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;The libraries share a design philosophy: &lt;strong&gt;zero allocation in the hot path&lt;&#x2F;strong&gt;. If you’re processing millions of messages per second, you shouldn’t be at the mercy of GC pauses.&lt;&#x2F;p&gt;
&lt;p&gt;A key enabler is the &lt;strong&gt;flyweight pattern&lt;&#x2F;strong&gt; — reusable, stateless views over raw memory. Instead of deserializing into objects, myra-codec and myra-transport wrap off-heap buffers directly. No copies, no allocations, no GC pressure. Just pointer arithmetic and bounds checks.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h2 id=&quot;use-cases-industries&quot;&gt;Use Cases &amp;amp; Industries&lt;&#x2F;h2&gt;
&lt;p&gt;MYRA is built for systems where every microsecond counts:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;High-Frequency Trading (HFT)&lt;&#x2F;strong&gt; — Order routing, execution, and market data pipelines where sub-10μs latency directly impacts profitability.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Cryptocurrency Exchanges&lt;&#x2F;strong&gt; — Real-time order books, settlement, and websocket broadcasts at millions of events per second.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;AdTech Real-Time Bidding&lt;&#x2F;strong&gt; — Sub-millisecond ad auctions and bid evaluation at scale.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Market Data Distribution&lt;&#x2F;strong&gt; — Low-latency feeds for equities, derivatives, commodities, and crypto with minimal GC interference.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Game Engines &amp;amp; Networked Games&lt;&#x2F;strong&gt; — High-tick-rate game loops and multiplayer synchronization with deterministic latency.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Financial Risk Systems&lt;&#x2F;strong&gt; — Real-time portfolio valuation, Greeks calculation, and stress testing for trading desks.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Sensor Networks &amp;amp; IoT&lt;&#x2F;strong&gt; — Time-series ingestion and edge processing for IoT data with strict latency budgets.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Telecommunications&lt;&#x2F;strong&gt; — Signaling, traffic analysis, and real-time network telemetry.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Cybersecurity Monitoring&lt;&#x2F;strong&gt; — Real-time threat detection and packet analysis at line rate.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Any system processing high-volume, low-latency, deterministic workloads is a candidate for MYRA.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h2 id=&quot;benchmarks-codec&quot;&gt;Benchmarks: Codec&lt;&#x2F;h2&gt;
&lt;p&gt;Serialization is where myra-codec shines. On an order book snapshot workload (a common HFT&#x2F;trading message type), here’s how it stacks up against established codecs:&lt;&#x2F;p&gt;
&lt;pre class=&quot;z-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;Decode Throughput (ops&#x2F;sec) — Higher is Better
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;════════════════════════════════════════════════════════════════════
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;Myra         ████████████████████████████████████████  4,150,079  ⭐
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;SBE          ████████████████████                      2,204,557
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;FlatBuffers  █████████████████                         1,968,855
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;Kryo         ███████████████                           1,322,754
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;Avro         █████                                       454,553
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;Encode Throughput (ops&#x2F;sec) — Higher is Better
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;════════════════════════════════════════════════════════════════════
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;SBE          ████████████████████████████████████████  4,990,071  
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;Myra         ███████████████                           1,911,781  ⭐
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;Kryo         ███████████                               1,342,611
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;FlatBuffers  ████████                                  1,045,843
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;Avro         ████                                        466,816
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;Myra decode is 2-3x faster than Kryo&#x2F;FlatBuffers&lt;&#x2F;strong&gt; and leads the pack. SBE edges out Myra on encode, but Myra’s decode dominance makes it the better choice for read-heavy workloads (most real systems decode more than they encode).&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Codec&lt;&#x2F;th&gt;&lt;th&gt;Decode (ops&#x2F;s)&lt;&#x2F;th&gt;&lt;th&gt;Encode (ops&#x2F;s)&lt;&#x2F;th&gt;&lt;th&gt;vs Myra (decode)&lt;&#x2F;th&gt;&lt;th&gt;vs Myra (encode)&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Myra&lt;&#x2F;td&gt;&lt;td&gt;4,150,079&lt;&#x2F;td&gt;&lt;td&gt;1,911,781&lt;&#x2F;td&gt;&lt;td&gt;—&lt;&#x2F;td&gt;&lt;td&gt;—&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;SBE&lt;&#x2F;td&gt;&lt;td&gt;2,204,557&lt;&#x2F;td&gt;&lt;td&gt;4,990,071&lt;&#x2F;td&gt;&lt;td&gt;-47%&lt;&#x2F;td&gt;&lt;td&gt;+161%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;FlatBuffers&lt;&#x2F;td&gt;&lt;td&gt;1,968,855&lt;&#x2F;td&gt;&lt;td&gt;1,045,843&lt;&#x2F;td&gt;&lt;td&gt;-53%&lt;&#x2F;td&gt;&lt;td&gt;-45%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Kryo&lt;&#x2F;td&gt;&lt;td&gt;1,322,754&lt;&#x2F;td&gt;&lt;td&gt;1,342,611&lt;&#x2F;td&gt;&lt;td&gt;-68%&lt;&#x2F;td&gt;&lt;td&gt;-30%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Avro&lt;&#x2F;td&gt;&lt;td&gt;454,553&lt;&#x2F;td&gt;&lt;td&gt;466,816&lt;&#x2F;td&gt;&lt;td&gt;-89%&lt;&#x2F;td&gt;&lt;td&gt;-76%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;p&gt;&lt;em&gt;Benchmark: order_book_snapshots workload, JMH on c6a.4xlarge, JDK 25, 5 forks × 5 iterations.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h2 id=&quot;benchmarks-transport&quot;&gt;Benchmarks: Transport&lt;&#x2F;h2&gt;
&lt;p&gt;For networking, myra-transport uses Linux &lt;code&gt;io_uring&lt;&#x2F;code&gt; to bypass the traditional syscall overhead. Here’s how it compares in a ping-pong latency test with realistic payloads:&lt;&#x2F;p&gt;
&lt;pre class=&quot;z-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;Mean Latency (μs) — Lower is Better
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;════════════════════════════════════════════════════════════════════
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;NIO          █████████████                             13.22 μs
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;MYRA_TOKEN   ███████████████████████                   28.70 μs  ⭐
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;MYRA         ████████████████████████████              35.12 μs
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;MYRA_SQPOLL  █████████████████████████████             35.88 μs
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;Netty        ███████████████████████████████           39.34 μs
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;Throughput (ops&#x2F;sec) — Higher is Better
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;════════════════════════════════════════════════════════════════════
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;NIO          ████████████████████████████████████████  75,645
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;MYRA_TOKEN   ██████████████████                        34,843  ⭐
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;MYRA         ███████████████                           28,471
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;MYRA_SQPOLL  ██████████████                            27,873
&lt;&#x2F;span&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;Netty        █████████████                             25,417
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;MYRA_TOKEN beats Netty by 27%&lt;&#x2F;strong&gt; on latency (28.7 μs vs 39.3 μs) and &lt;strong&gt;37%&lt;&#x2F;strong&gt; on throughput. The token-based completion tracking provides the best balance of latency and consistency for io_uring-based networking.&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Implementation&lt;&#x2F;th&gt;&lt;th&gt;Mean (μs)&lt;&#x2F;th&gt;&lt;th&gt;p50 (μs)&lt;&#x2F;th&gt;&lt;th&gt;p99 (μs)&lt;&#x2F;th&gt;&lt;th&gt;Throughput&lt;&#x2F;th&gt;&lt;th&gt;vs Netty&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;NIO (baseline)&lt;&#x2F;td&gt;&lt;td&gt;13.22&lt;&#x2F;td&gt;&lt;td&gt;12.27&lt;&#x2F;td&gt;&lt;td&gt;28.35&lt;&#x2F;td&gt;&lt;td&gt;75.6K ops&#x2F;s&lt;&#x2F;td&gt;&lt;td&gt;+198%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;MYRA_TOKEN ⭐&lt;&#x2F;td&gt;&lt;td&gt;28.70&lt;&#x2F;td&gt;&lt;td&gt;26.72&lt;&#x2F;td&gt;&lt;td&gt;45.76&lt;&#x2F;td&gt;&lt;td&gt;34.8K ops&#x2F;s&lt;&#x2F;td&gt;&lt;td&gt;&lt;strong&gt;+37%&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;MYRA&lt;&#x2F;td&gt;&lt;td&gt;35.12&lt;&#x2F;td&gt;&lt;td&gt;32.16&lt;&#x2F;td&gt;&lt;td&gt;53.25&lt;&#x2F;td&gt;&lt;td&gt;28.5K ops&#x2F;s&lt;&#x2F;td&gt;&lt;td&gt;+12%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;MYRA_SQPOLL&lt;&#x2F;td&gt;&lt;td&gt;35.88&lt;&#x2F;td&gt;&lt;td&gt;25.50&lt;&#x2F;td&gt;&lt;td&gt;63.36&lt;&#x2F;td&gt;&lt;td&gt;27.9K ops&#x2F;s&lt;&#x2F;td&gt;&lt;td&gt;+10%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Netty&lt;&#x2F;td&gt;&lt;td&gt;39.34&lt;&#x2F;td&gt;&lt;td&gt;38.34&lt;&#x2F;td&gt;&lt;td&gt;62.40&lt;&#x2F;td&gt;&lt;td&gt;25.4K ops&#x2F;s&lt;&#x2F;td&gt;&lt;td&gt;—&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;p&gt;&lt;em&gt;Benchmark: RealWorldPayload ping-pong, JMH on ARM64 (AWS Graviton), JDK 25, Nov 29, 2025.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h2 id=&quot;why-java-instead-of-c-c-rust&quot;&gt;Why Java Instead of C&#x2F;C++&#x2F;Rust?&lt;&#x2F;h2&gt;
&lt;p&gt;A common question: &lt;em&gt;“If you need this kind of performance, why not just write it in C&#x2F;C++&#x2F;Rust?”&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;It’s a fair question. The short answer: &lt;strong&gt;developer velocity, safety, and maintainability matter more than the last 5-10% of performance.&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;the-c-c-problem&quot;&gt;The C&#x2F;C++ Problem&lt;&#x2F;h3&gt;
&lt;p&gt;Writing correct, memory-safe high-performance C&#x2F;C++ code is genuinely &lt;em&gt;hard&lt;&#x2F;em&gt;:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Undefined behavior lurks everywhere.&lt;&#x2F;strong&gt; Cache line aliasing, pointer provenance violations, strict aliasing optimizations — seemingly small mistakes cause non-deterministic failures in production.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Memory safety requires obsessive discipline.&lt;&#x2F;strong&gt; Buffer overflows, use-after-free, double-free — these are career-ending bugs that tests often miss until they surface under load.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;SIMD and CPU features are implicit.&lt;&#x2F;strong&gt; Vectorization depends on compiler whim. Portability across architectures (x86 → ARM → POWER) requires conditional code paths and platform-specific profiling.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Latency is still unpredictable.&lt;&#x2F;strong&gt; Memory allocators, cache eviction, TLB misses, branch misprediction — none are under your control. You optimize by convention and prayer.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Recruitment is painful.&lt;&#x2F;strong&gt; Finding developers who can write safe, correct C++ at scale is expensive. Most teams end up with a small cadre of specialists maintaining critical paths.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;the-rust-problem&quot;&gt;The Rust Problem&lt;&#x2F;h3&gt;
&lt;p&gt;Rust solves memory safety, but introduces different tradeoffs:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The learning curve is steep.&lt;&#x2F;strong&gt; Ownership semantics, borrow checking, lifetime parameters — experienced C++ developers typically need several months to become productive. Most teams don’t have that timeline.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Async ecosystem fragmentation.&lt;&#x2F;strong&gt; Tokio, async-std, embassy — Rust has no single async standard. A 5-year-old codebase may be stuck on an abandoned runtime.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Talent pool is shallow.&lt;&#x2F;strong&gt; Rust adoption in production is still niche. Hiring is hard, and most candidates come from crypto&#x2F;systems backgrounds, not mainstream finance.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Performance isn’t guaranteed.&lt;&#x2F;strong&gt; Zero-cost abstractions are a design goal, not a promise. Allocations, copies, and data layout still require deep expertise to optimize.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Build times are brutal.&lt;&#x2F;strong&gt; Incremental compilation is improving, but debug builds still feel glacial compared to JVM languages.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;why-java-ffm-changes-the-equation&quot;&gt;Why Java&#x2F;FFM Changes the Equation&lt;&#x2F;h3&gt;
&lt;p&gt;The MYRA stack bridges the gap:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Memory safety by default.&lt;&#x2F;strong&gt; FFM operates within JVM bounds checking. No segfaults, no undefined behavior. You still get off-heap access and native interop, but with guardrails.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Deterministic behavior.&lt;&#x2F;strong&gt; The JVM’s memory model is formal and well-defined. No undefined behavior. No surprise optimizations that break correctness.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;GC is optional.&lt;&#x2F;strong&gt; With MYRA’s zero-allocation design, you can run on low-pause GCs (ZGC, Shenandoah) or even custom pauseless allocators. You’re not forced into stop-the-world pauses.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Massive ecosystem.&lt;&#x2F;strong&gt; Maven, Spring, Quarkus, Loom — the JVM has 25+ years of production infrastructure. No reinventing logging, serialization, or concurrency primitives.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Tooling maturity.&lt;&#x2F;strong&gt; JVM profilers (async-profiler, Flight Recorder), debuggers, and observability are industry-standard. Compare to gdb + perf or valgrind’s UI.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Developer velocity is real.&lt;&#x2F;strong&gt; Java developers are abundant. They can become productive with MYRA in weeks, not months. Bugs in business logic surface faster than memory corruption.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Performance is competitive.&lt;&#x2F;strong&gt; At 13-28μs ping-pong latency, MYRA is in the same ballpark as hand-optimized C++. Not 2x faster, but also not 2x slower. The 10-15% gap is often &lt;em&gt;worth&lt;&#x2F;em&gt; the 10x faster time-to-market.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;the-reality-check&quot;&gt;The Reality Check&lt;&#x2F;h3&gt;
&lt;p&gt;C&#x2F;C++&#x2F;Rust shine for:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Embedded systems&lt;&#x2F;strong&gt; where memory is scarce (ARM Cortex M, RISC-V).&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Bare-metal or kernel-mode code&lt;&#x2F;strong&gt; where a JVM isn’t an option.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Latency-critical at scale&lt;&#x2F;strong&gt; — when you’re processing trillions of messages&#x2F;year and 1μs matters.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;But for most systems — trading platforms, market data feeds, game servers, real-time analytics — Java + MYRA offers a pragmatic middle ground:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Close to C++ performance&lt;&#x2F;strong&gt; without the memory safety tax.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Safer and faster to develop&lt;&#x2F;strong&gt; than Rust, with a deeper talent pool.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Proven production stability&lt;&#x2F;strong&gt; with 25+ years of JVM track record.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;MYRA’s thesis is simple: &lt;strong&gt;Most teams are over-optimized for raw speed and under-optimized for correctness and velocity.&lt;&#x2F;strong&gt; The JVM with FFM tips that balance back toward sanity.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h2 id=&quot;why-i-m-building-this&quot;&gt;Why I’m Building This&lt;&#x2F;h2&gt;
&lt;p&gt;I’ve spent years in systems where latency matters — where 100μs is slow and a GC pause is a production incident. Java is plenty fast for this, but the tooling hasn’t caught up to the platform’s capabilities.&lt;&#x2F;p&gt;
&lt;p&gt;FFM is the missing piece. It’s finally safe, stable, and performant enough to build real infrastructure on. MYRA is my attempt to do exactly that.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h2 id=&quot;what-s-next&quot;&gt;What’s Next&lt;&#x2F;h2&gt;
&lt;p&gt;I’m currently in the final stretch — optimizations, cleanup, and documentation. The goal is to publicly open source the entire ecosystem by Christmas 2025.&lt;&#x2F;p&gt;
&lt;p&gt;The MYRA ecosystem will always remain &lt;strong&gt;free and open source&lt;&#x2F;strong&gt;. No enterprise tier, no gated features, no open-core model. Development will be sustained through open sponsorships from individuals and organizations who find value in the work.&lt;&#x2F;p&gt;
&lt;p&gt;If you’re curious about FFM, high-performance Java, or just want to see where this goes:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Follow the project:&lt;&#x2F;strong&gt; &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;mvp-express&quot;&gt;github.com&#x2F;mvp-express&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;More soon.&lt;&#x2F;p&gt;
</content>
        <summary type="html">MYRA — Memory Yielded, Rapid Access — is a production-grade ecosystem of Java libraries built on the Foreign Function &amp; Memory (FFM) API, designed for deterministic, sub-microsecond latency applications.</summary>
        </entry><entry xml:lang="en">
        <title>Java FFM - Foreign Function &amp; Memory Access API (Project Panama)</title>
        <published>2025-08-28T00:00:00+00:00</published>
        <updated>2025-08-28T00:00:00+00:00</updated>
        <author>
            <name>Rohan Ray</name>
        </author>
        <link rel="alternate" href="https://www.roray.dev/blog/java-io-uring-ffm/" type="text/html"/>
        <id>https://www.roray.dev/blog/java-io-uring-ffm/</id>
        
            <content type="html">&lt;h1 id=&quot;tl-dr&quot;&gt;TL;DR&lt;&#x2F;h1&gt;
&lt;p&gt;Java’s Foreign Function &amp;amp; Memory (FFM) API enables safe, high-performance interaction with native code and memory, improving interoperability with other languages and access to low-level system resources. Entire source code available on &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;rohanray&#x2F;roray-dev-site&#x2F;tree&#x2F;main&#x2F;code&#x2F;java-iouring-tcp-echo&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;GitHub&lt;&#x2F;a&gt;.
&lt;br&gt;
&lt;br&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;overview&quot;&gt;Overview&lt;&#x2F;h1&gt;
&lt;p&gt;
The Foreign Function &amp; Memory (FFM) API in Java, finalized in JDK 22 with JEP 454, provides a robust and safe mechanism for Java code to interact with foreign functions (native code) and foreign memory (memory outside the Java heap).
&lt;&#x2F;p&gt;
&lt;p&gt;
The FFM API is designed to be a safer, more efficient, and more user-friendly alternative to the Java Native Interface (JNI). JNI is known for its complexity and potential for introducing errors, whereas FFM aims to provide a more &quot;Java-first&quot; programming model for native interoperability.
&lt;&#x2F;p&gt;
&lt;p&gt;
The FFM API introduces several key concepts and components that facilitate foreign function and memory access in Java:
&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Memory Segments&lt;&#x2F;strong&gt;: A memory segment is a contiguous block of memory that can be accessed by Java programs. The FFM API provides a way to create and manage memory segments, allowing developers to allocate and deallocate memory as needed.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Arena Allocation&lt;&#x2F;strong&gt;: The FFM API introduces the concept of arenas, which are memory pools that can be used for efficient allocation and deallocation of memory. Arenas help reduce fragmentation and improve performance when working with native memory.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Value Layouts&lt;&#x2F;strong&gt;: Value layouts define the memory layout of data structures in a platform-independent way. The FFM API allows developers to create value layouts for both Java and foreign types, ensuring compatibility between the two.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Memory Access&lt;&#x2F;strong&gt;: The FFM API provides a set of APIs for reading and writing data to memory segments, making it easy to manipulate native data from Java.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Foreign Functions&lt;&#x2F;strong&gt;: Foreign functions are native functions written in languages like C or C++ that can be called from Java. The FFM API provides a way to define and invoke foreign functions, enabling seamless integration between Java and native code.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;blockquote&gt;
&lt;p&gt;Note: &lt;em&gt;While the primary use case of the Java Foreign Function &amp;amp; Memory (FFM) API is to call C functions from Java (downcalls), it also supports the reverse: calling Java functions from C (upcalls). This is particularly useful for scenarios like C callbacks, where a C library needs to invoke a function provided by Java code.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;br&gt;
&lt;h1 id=&quot;ffm-use-cases&quot;&gt;FFM Use cases&lt;&#x2F;h1&gt;
&lt;p&gt;By leveraging these components, developers can build high-performance applications that take advantage of native libraries and system resources while maintaining the safety and ease of use that Java provides especially in below use cases:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Performance-Critical Applications&lt;&#x2F;strong&gt;: Applications that require high performance such as game engines, scientific computing, Ultra Low Level (ULL) High Frequency Trading (HFT) systems and real-time systems can benefit from direct access to native code and memory.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Big Data and Machine Learning&lt;&#x2F;strong&gt;: Libraries like TensorFlow and PyTorch often require efficient memory management and native code execution, making FFM a suitable choice for integrating these libraries into Java applications.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Interoperability with Other Languages&lt;&#x2F;strong&gt;: FFM allows Java applications to easily call functions written in other languages, such as C or C++, enabling developers to leverage existing libraries and codebases&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Low-Level System Access&lt;&#x2F;strong&gt;: Applications that need to interact with low-level system resources, such as hardware devices or operating system APIs, can use FFM to access these resources directly from Java e.g. accessing NIC for Kernel bypass networking, accessing GPU for parallel processing, etc.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;High Performance Computing (HPC)&lt;&#x2F;strong&gt;: FFM can be used in HPC applications where performance is critical, such as simulations, numerical computations, and data processing tasks that require efficient memory management and native code execution.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;High Performance File&#x2F;Data I&#x2F;O&lt;&#x2F;strong&gt;: Database files, large binary files, and other data-intensive applications can benefit by using techniques like memory-mapped files which allow direct access to file contents in memory, random access, bulk read, minimize OS calls, Zero copy &amp;amp; low GC pressure by avoiding Heap, multi-process file coordination with locks improving I&#x2F;O performance and reducing latency.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Custom High Performance Low Latency RPC&lt;&#x2F;strong&gt;: Remote Procedure Calls (RPC) can be optimized using FFM to reduce serialization&#x2F;deserialization overhead, enabling faster communication with high throughput between distributed systems.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;br&gt;
&lt;h1 id=&quot;demo-example&quot;&gt;Demo Example&lt;&#x2F;h1&gt;
&lt;p&gt;Let’s see a small example of effective usage of FFM. As a Proof of Concept, we will create a TCP server using &lt;a href=&quot;https:&#x2F;&#x2F;developers.redhat.com&#x2F;articles&#x2F;2023&#x2F;04&#x2F;12&#x2F;why-you-should-use-iouring-network-io&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;io_uring&lt;&#x2F;a&gt; (Linux kernel interface for asynchronous I&#x2F;O operations) and Java FFM API. The actual TCP server using io_uring is implemented in C. We will use various APIs (standard TCP operations) viz. accept, listen, connect, send, receive etc. in Java through Foreign Functions mapping the actual C functions.&lt;&#x2F;p&gt;
&lt;p&gt;This is by no means a production ready code. The goal is to showcase the ease of use and integration of FFM with existing C libraries. We also won’t be going into details of TCP server implementation in C nor what is io_uring as it’s beyond the scope of this article.&lt;&#x2F;p&gt;
&lt;br&gt;
&lt;h2 id=&quot;tcp-server-in-c-using-async-io-uring&quot;&gt;TCP server in C using async io-uring&lt;&#x2F;h2&gt;
&lt;p&gt;Let’s look at the &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;rohanray&#x2F;roray-dev-site&#x2F;blob&#x2F;main&#x2F;code&#x2F;java-iouring-tcp-echo&#x2F;c-iouring&#x2F;io_uring_tcp_io.c&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;C code&lt;&#x2F;a&gt; first especially the below APIs:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;io_uring_global_init&lt;&#x2F;code&gt; - Initializes the global io_uring context and sets up the underlying ring buffers for asynchronous I&#x2F;O operations&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;io_uring_listen&lt;&#x2F;code&gt; - Creates a TCP socket, binds it to a specified port, and starts listening for incoming connections&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;io_uring_accept&lt;&#x2F;code&gt; - Accepts an incoming connection request and returns a new socket file descriptor for the client&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;io_uring_connect&lt;&#x2F;code&gt; - Establishes an outbound TCP connection to a remote server at the specified address and port&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;io_uring_send&lt;&#x2F;code&gt; - Sends data through an established TCP connection using io_uring’s asynchronous write operations&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;io_uring_receive&lt;&#x2F;code&gt; - Receives data from an established TCP connection using io_uring’s asynchronous read operations&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;java-ffm-integration&quot;&gt;Java - FFM Integration&lt;&#x2F;h2&gt;
&lt;p&gt;Now let’s look at the Java side which is the point of interest of this article. We have 2 classes:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;FfmReceiver&lt;&#x2F;code&gt; - Responsible for receiving data from the TCP server&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;FfmSender&lt;&#x2F;code&gt; - Responsible for sending data to the TCP server&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;&lt;em&gt;Note: &lt;strong&gt;The actual TCP server is implemented in C using io_uring. The C library exposes lifecycle methods for managing the server. These methods are then called in Java using FFM.&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;ffmreceiver&quot;&gt;FfmReceiver&lt;&#x2F;h3&gt;
&lt;pre data-linenos data-lang=&quot;FfmReceiver.java&quot; class=&quot;language-FfmReceiver.java z-code&quot;&gt;&lt;code class=&quot;language-FfmReceiver.java&quot; data-lang=&quot;FfmReceiver.java&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;1&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;import java.lang.foreign.Arena;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;2&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;import java.lang.foreign.FunctionDescriptor;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;3&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;import java.lang.foreign.Linker;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;4&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;import java.lang.foreign.MemorySegment;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;5&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;import java.lang.foreign.SymbolLookup;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;6&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;import java.lang.foreign.ValueLayout;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;7&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;import java.lang.invoke.MethodHandle;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;8&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;import java.nio.charset.StandardCharsets;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;9&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;10&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;public class FfmReceiver {
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;11&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;12&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;    private static final ValueLayout.OfByte BYTE = ValueLayout.Java_BYTE;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;13&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;14&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;    public static void main(String[] args) throws Throwable {
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;15&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;        int queueDepth = 32;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;16&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;        int port = 22345;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;17&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;        int backlog = 128;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;18&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;        long bufferSize = 8 * 1024 * 1024; &#x2F;&#x2F; 8 MB buffer per client
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;19&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;20&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;        try (Arena arena = Arena.ofShared()) {
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;21&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;22&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            &#x2F;&#x2F; Load the shared library
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;23&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            SymbolLookup lib = SymbolLookup.libraryLookup(&amp;quot;.&#x2F;libiouring_tcp.so&amp;quot;, arena);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;24&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            Linker linker = Linker.nativeLinker();
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;25&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;26&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            &#x2F;&#x2F; 1️⃣ Global Init
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;27&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            MemorySegment globalInitAddr = lib.find(&amp;quot;io_uring_global_init&amp;quot;).get();
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;28&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            MethodHandle mhGlobalInit = linker.downcallHandle(globalInitAddr,
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;29&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                    FunctionDescriptor.of(ValueLayout.Java_INT, ValueLayout.Java_INT));
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;30&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            int ret = (int) mhGlobalInit.invokeExact(queueDepth);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;31&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            System.out.println(&amp;quot;io_uring_global_init returned: &amp;quot; + ret);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;32&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;33&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            &#x2F;&#x2F; 2️⃣ Listen (server socket)
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;34&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            MemorySegment listenAddr = lib.find(&amp;quot;io_uring_listen&amp;quot;).get();
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;35&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            MethodHandle mhListen = linker.downcallHandle(listenAddr,
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;36&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                    FunctionDescriptor.of(ValueLayout.Java_INT, ValueLayout.Java_INT, ValueLayout.Java_INT));
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;37&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            int listenFd = (int) mhListen.invokeExact(port, backlog);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;38&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            if (listenFd &amp;lt; 0) {
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;39&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                throw new RuntimeException(&amp;quot;io_uring_listen failed, fd=&amp;quot; + listenFd);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;40&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            }
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;41&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            System.out.println(&amp;quot;Server listening on port &amp;quot; + port + &amp;quot;, fd=&amp;quot; + listenFd);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;42&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;43&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            &#x2F;&#x2F; 3️⃣ Accept clients in a loop
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;44&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            MemorySegment acceptAddr = lib.find(&amp;quot;io_uring_accept&amp;quot;).get();
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;45&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            MethodHandle mhAccept = linker.downcallHandle(acceptAddr,
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;46&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                    FunctionDescriptor.of(ValueLayout.Java_INT, ValueLayout.Java_INT));
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;47&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;48&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            MemorySegment recvAddr = lib.find(&amp;quot;io_uring_recv&amp;quot;).get();
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;49&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            MethodHandle mhRecv = linker.downcallHandle(recvAddr,
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;50&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                    FunctionDescriptor.of(ValueLayout.Java_INT, ValueLayout.Java_INT, ValueLayout.ADDRESS, ValueLayout.Java_LONG));
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;51&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;52&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            while (true) {
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;53&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                int clientFd = (int) mhAccept.invokeExact(listenFd);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;54&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                if (clientFd &amp;lt; 0) {
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;55&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                    System.err.println(&amp;quot;Accept failed, fd=&amp;quot; + clientFd);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;56&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                    continue;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;57&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                }
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;58&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                System.out.println(&amp;quot;Client connected, fd=&amp;quot; + clientFd);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;59&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;60&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                &#x2F;&#x2F; Allocate buffer for this client
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;61&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                MemorySegment buffer = arena.allocate(bufferSize);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;62&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;63&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                int bytesReceived = (int) mhRecv.invokeExact(clientFd, buffer, bufferSize);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;64&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                if (bytesReceived &amp;lt; 0) {
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;65&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                    System.err.println(&amp;quot;io_uring_recv failed, bytes=&amp;quot; + bytesReceived);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;66&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                    continue;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;67&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                }
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;68&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                System.out.println(&amp;quot;Received bytes: &amp;quot; + bytesReceived);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;69&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;70&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                &#x2F;&#x2F; Print first 128 bytes for debugging (optional)
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;71&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                int displayLen = Math.min(128, bytesReceived);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;72&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                byte[] arr = new byte[displayLen];
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;73&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                for (int i = 0; i &amp;lt; displayLen; i++) {
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;74&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                    arr[i] = buffer.get(BYTE, i);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;75&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                }
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;76&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                System.out.println(&amp;quot;First &amp;quot; + displayLen + &amp;quot; bytes:\n&amp;quot; +
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;77&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                        new String(arr, StandardCharsets.US_ASCII));
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;78&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;79&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                &#x2F;&#x2F; Close client socket
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;80&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                MemorySegment closeAddr = lib.find(&amp;quot;io_uring_close&amp;quot;).get();
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;81&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                MethodHandle mhClose = linker.downcallHandle(closeAddr,
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;82&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                        FunctionDescriptor.ofVoid(ValueLayout.Java_INT));
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;83&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                mhClose.invokeExact(clientFd);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;84&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                System.out.println(&amp;quot;Client fd &amp;quot; + clientFd + &amp;quot; closed.&amp;quot;);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;85&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            }
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;86&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;87&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            &#x2F;&#x2F; 4️⃣ Optional: shutdown ring (never reached in infinite loop)
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;88&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            &#x2F;&#x2F; MemorySegment shutdownAddr = lib.find(&amp;quot;io_uring_global_shutdown&amp;quot;).get();
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;89&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            &#x2F;&#x2F; MethodHandle mhShutdown = linker.downcallHandle(shutdownAddr, FunctionDescriptor.ofVoid());
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;90&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            &#x2F;&#x2F; mhShutdown.invokeExact();
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;91&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;        }
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;92&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;    }
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;93&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This FfmReceiver class demonstrates a complete FFM workflow for calling native C functions from Java. Here’s the high-level technical breakdown:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Library Loading &amp;amp; Symbol Resolution&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;FFM loads the compiled C shared library containing io_uring functions. SymbolLookup provides a bridge to find C function symbols by name.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Key FFM Components in Action&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Arena: Manages native memory lifecycle - automatically frees all allocated memory when closed (safe malloc&#x2F;free).&lt;&#x2F;li&gt;
&lt;li&gt;SymbolLookup: This is how Java finds function pointers in a native library. Think of SymbolLookup as a “directory of functions” inside the .so file.&lt;&#x2F;li&gt;
&lt;li&gt;Linker: The bridge between Java and native code, which knows how to convert between Java calling conventions and the platform’s native ABI (x86_64 Linux, ARM64, etc.).&lt;&#x2F;li&gt;
&lt;li&gt;FunctionDescriptor: Defines the C&#x2F;native function signature (return type + parameter types)&lt;&#x2F;li&gt;
&lt;li&gt;ValueLayout: Maps Java types to native types (Java_INT → C int, ADDRESS → C pointer)&lt;&#x2F;li&gt;
&lt;li&gt;MethodHandle: Type-safe wrapper for C&#x2F;native function calls&lt;&#x2F;li&gt;
&lt;li&gt;MemorySegment: Represents native memory buffers for data exchange; essentially a safe pointer with guardrails.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;&lt;strong&gt;Memory Management&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Native memory is allocated outside the Java heap. Data is directly read&#x2F;written to native buffers without copying (zero copy). Arena ensures safe &amp;amp; automatic cleanup when the try-with-resources block exits.&lt;&#x2F;p&gt;
&lt;p&gt;Here, arena is used both to load the library and allocate buffers for receiving data.&lt;&#x2F;p&gt;
&lt;p&gt;Here, libraryLookup loads our custom shared library (.so) and lets us find symbols like io_uring_global_init, io_uring_listen, etc.&lt;&#x2F;p&gt;
&lt;p&gt;Then with linker, we can create MethodHandles that behave like normal Java methods but actually invoke C functions. We can think of a MethodHandle as a Java wrapper around a native C function. A linker can be thought of as a translator between Java’s JVM world and C’s native&#x2F;binary code.&lt;&#x2F;p&gt;
&lt;p&gt;Next, we define the function signature with FunctionDescriptor.of(Java_INT, Java_INT) which includes parameter(input) types &amp;amp; all return types. In this case, the function says it takes one int argument &amp;amp; returns one int as the result. invokeExact(queueDepth) → actually executes the C function.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Safety &amp;amp; Type Checking&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;invokeExact() enforces exact type matching between Java and C function signatures. Compile-time validation prevents type mismatches that would cause runtime errors. No manual memory management or pointer arithmetic required. This approach eliminates JNI’s complexity while providing direct, high-performance access to native libraries with Java’s safety guarantees intact.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;Click &lt;a href=&quot;#memo-walking-through-ffmreceiver-a-first-taste-of-java-ffm-ai-generated&quot;&gt;here&lt;&#x2F;a&gt; to see a more detailed AI generated walk through of FfmReceiver implementation.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Sample Output:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;img&#x2F;ffmreceiver-output.png&quot; alt=&quot;alt text&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;ffmsender&quot;&gt;FfmSender&lt;&#x2F;h3&gt;
&lt;pre data-linenos data-lang=&quot;FfmSender.java&quot; class=&quot;language-FfmSender.java z-code&quot;&gt;&lt;code class=&quot;language-FfmSender.java&quot; data-lang=&quot;FfmSender.java&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;1&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;import java.lang.foreign.*;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;2&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;import java.lang.invoke.MethodHandle;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;3&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;import java.nio.charset.StandardCharsets;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;4&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;5&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;public class FfmSender {
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;6&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;    public static void main(String[] args) throws Throwable {
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;7&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;        int queueDepth = 32;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;8&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;        int port = 22345;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;9&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;        long bufferSize = 4 * 1024; &#x2F;&#x2F; 4 KB buffer per client
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;10&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;11&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;        System.out.println(&amp;quot;FfmSender running...&amp;quot;);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;12&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;        try (Arena arena = Arena.ofShared()) {
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;13&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            String hi = &amp;quot;ping&amp;quot;;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;14&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;15&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            &#x2F;&#x2F; Load the shared library
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;16&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            SymbolLookup lib = SymbolLookup.libraryLookup(&amp;quot;.&#x2F;libiouring_tcp.so&amp;quot;, arena);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;17&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            Linker linker = Linker.nativeLinker();
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;18&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;19&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            &#x2F;&#x2F; Global IO URing Init
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;20&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            MemorySegment globalInitAddrMS = lib.find(&amp;quot;io_uring_global_init&amp;quot;).get();
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;21&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            FunctionDescriptor globalInitFD = FunctionDescriptor.of(
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;22&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                    ValueLayout.Java_INT,
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;23&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                    ValueLayout.Java_INT);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;24&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            MethodHandle globalInitMH = linker.downcallHandle(globalInitAddrMS, globalInitFD);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;25&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            int ret = (int) globalInitMH.invokeExact(queueDepth);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;26&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            System.out.println(&amp;quot;io_uring_global_init returned: &amp;quot; + ret);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;27&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;28&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            &#x2F;&#x2F; TCP Open Socket FD
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;29&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            MemorySegment tcpSocketAddrMS = lib.find(&amp;quot;io_uring_connect&amp;quot;).get();
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;30&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            FunctionDescriptor tcpConnectFD = FunctionDescriptor.of(
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;31&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                    ValueLayout.Java_INT, &#x2F;&#x2F; return value of socket File Descriptor
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;32&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                    ValueLayout.ADDRESS, &#x2F;&#x2F; server address
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;33&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                    ValueLayout.Java_INT); &#x2F;&#x2F; server port
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;34&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            MethodHandle tcpConnectMH = linker.downcallHandle(tcpSocketAddrMS, tcpConnectFD);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;35&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;36&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            String ip = &amp;quot;127.0.0.1&amp;quot;; &#x2F;&#x2F; destination IP
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;37&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            byte[] ipBytes = ip.getBytes(StandardCharsets.UTF_8);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;38&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            MemorySegment ipStr = arena.allocate(ipBytes.length + 1);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;39&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            ipStr.asSlice(0, ipBytes.length).copyFrom(MemorySegment.ofArray(ipBytes));
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;40&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            ipStr.set(ValueLayout.Java_BYTE, ipBytes.length, (byte) 0); &#x2F;&#x2F; Null-terminate for C
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;41&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;42&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            int socketFD = (int) tcpConnectMH.invokeExact(ipStr, port);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;43&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;44&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            &#x2F;&#x2F; IO URing send data through memory segment off-heap buffer
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;45&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            MemorySegment sendBufAddrMS = lib.find(&amp;quot;io_uring_send_all&amp;quot;).get();
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;46&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            FunctionDescriptor sendBufFD = FunctionDescriptor.of(
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;47&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                    ValueLayout.Java_INT, &#x2F;&#x2F; return value: total sent bytes
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;48&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                    ValueLayout.Java_INT, &#x2F;&#x2F; socket fd
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;49&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                    ValueLayout.ADDRESS, &#x2F;&#x2F; buffer address
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;50&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;                    ValueLayout.Java_LONG); &#x2F;&#x2F; buffer length
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;51&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            MethodHandle sendBufMH = linker.downcallHandle(sendBufAddrMS, sendBufFD);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;52&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;53&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            &#x2F;&#x2F; Prepare buffer
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;54&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            MemorySegment sendBuf = arena.allocateFrom(hi);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;55&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;56&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            int totalBytesSent = (int) sendBufMH.invokeExact(socketFD, sendBuf, (long) hi.length());
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;57&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;58&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;            System.out.println(&amp;quot;Total bytes sent Java FFM: &amp;quot; + totalBytesSent);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;59&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;60&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;        }
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;61&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;62&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;    }
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;63&lt;&#x2F;td&gt;&lt;td&gt;&lt;span class=&quot;z-text z-plain&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Sample Output:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;img&#x2F;ffmsender-output.png&quot; alt=&quot;alt text&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;This FfmSender class demonstrates sending data to the TCP server using FFM. The flow is similar to FfmReceiver. We send “ping” text to the TCP Server &amp;gt; FfmReceiver.&lt;&#x2F;p&gt;
&lt;br&gt;
&lt;h1 id=&quot;appendix&quot;&gt;Appendix&lt;&#x2F;h1&gt;
&lt;h3 id=&quot;java-versions-support&quot;&gt;Java Versions support&lt;&#x2F;h3&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;JDK Version&lt;&#x2F;th&gt;&lt;th&gt;API Status&lt;&#x2F;th&gt;&lt;th&gt;Enabling Preview Features&lt;&#x2F;th&gt;&lt;th&gt;Key Enhancements (FFM API versions)&lt;&#x2F;th&gt;&lt;th&gt;Package&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;JDK 14&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;Foreign-Memory Access API (Incubator)&lt;&#x2F;td&gt;&lt;td&gt;Not applicable (Incubator)&lt;&#x2F;td&gt;&lt;td&gt;Memory segments, Arenas (via Foreign-Memory Access API)&lt;&#x2F;td&gt;&lt;td&gt;&lt;a href=&quot;https:&#x2F;&#x2F;docs.oracle.com&#x2F;en&#x2F;java&#x2F;javase&#x2F;14&#x2F;docs&#x2F;api&#x2F;jdk.incubator.foreign&#x2F;jdk&#x2F;incubator&#x2F;foreign&#x2F;package-use.html&quot;&gt;jdk.incubator.foreign&lt;&#x2F;a&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;JDK 16&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;Incubator&lt;&#x2F;td&gt;&lt;td&gt;Not applicable (Incubator)&lt;&#x2F;td&gt;&lt;td&gt;Calling native functions (via Foreign Linker API)&lt;&#x2F;td&gt;&lt;td&gt;&lt;a href=&quot;https:&#x2F;&#x2F;docs.oracle.com&#x2F;en&#x2F;java&#x2F;javase&#x2F;16&#x2F;docs&#x2F;api&#x2F;jdk.incubator.foreign&#x2F;jdk&#x2F;incubator&#x2F;foreign&#x2F;package-use.html&quot;&gt;jdk.incubator.foreign&lt;&#x2F;a&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;JDK 17&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;Incubator&lt;&#x2F;td&gt;&lt;td&gt;Not applicable (Incubator)&lt;&#x2F;td&gt;&lt;td&gt;Unified FFM API, enhancements to MemorySegment and MemoryAddress abstractions, improved memory layout hierarchy&lt;&#x2F;td&gt;&lt;td&gt;&lt;a href=&quot;https:&#x2F;&#x2F;docs.oracle.com&#x2F;en&#x2F;java&#x2F;javase&#x2F;17&#x2F;docs&#x2F;api&#x2F;jdk.incubator.foreign&#x2F;jdk&#x2F;incubator&#x2F;foreign&#x2F;package-use.html&quot;&gt;jdk.incubator.foreign&lt;&#x2F;a&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;JDK 18&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;Incubator&lt;&#x2F;td&gt;&lt;td&gt;Not applicable (Incubator)&lt;&#x2F;td&gt;&lt;td&gt;Refinements based on feedback&lt;&#x2F;td&gt;&lt;td&gt;&lt;a href=&quot;https:&#x2F;&#x2F;docs.oracle.com&#x2F;en&#x2F;java&#x2F;javase&#x2F;18&#x2F;docs&#x2F;api&#x2F;jdk.incubator.foreign&#x2F;jdk&#x2F;incubator&#x2F;foreign&#x2F;package-use.html&quot;&gt;jdk.incubator.foreign&lt;&#x2F;a&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;JDK 19&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;Preview&lt;&#x2F;td&gt;&lt;td&gt;–enable-preview flag required&lt;&#x2F;td&gt;&lt;td&gt;Refinements based on feedback&lt;&#x2F;td&gt;&lt;td&gt;&lt;a href=&quot;https:&#x2F;&#x2F;docs.oracle.com&#x2F;en&#x2F;java&#x2F;javase&#x2F;19&#x2F;docs&#x2F;api&#x2F;java.base&#x2F;java&#x2F;lang&#x2F;foreign&#x2F;package-summary.html&quot;&gt;java.lang.foreign&lt;&#x2F;a&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;JDK 20&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;Second Preview&lt;&#x2F;td&gt;&lt;td&gt;–enable-preview flag required&lt;&#x2F;td&gt;&lt;td&gt;Refinements based on feedback, including linker option for heap segments, Enable-Native-Access attribute, programmatic function descriptors&lt;&#x2F;td&gt;&lt;td&gt;&lt;a href=&quot;https:&#x2F;&#x2F;docs.oracle.com&#x2F;en&#x2F;java&#x2F;javase&#x2F;20&#x2F;docs&#x2F;api&#x2F;java.base&#x2F;java&#x2F;lang&#x2F;foreign&#x2F;package-summary.html&quot;&gt;java.lang.foreign&lt;&#x2F;a&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;JDK 21&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;Third Preview&lt;&#x2F;td&gt;&lt;td&gt;–enable-preview flag required&lt;&#x2F;td&gt;&lt;td&gt;Further refinements based on feedback&lt;&#x2F;td&gt;&lt;td&gt;&lt;a href=&quot;https:&#x2F;&#x2F;docs.oracle.com&#x2F;en&#x2F;java&#x2F;javase&#x2F;21&#x2F;docs&#x2F;api&#x2F;java.base&#x2F;java&#x2F;lang&#x2F;foreign&#x2F;package-summary.html&quot;&gt;java.lang.foreign&lt;&#x2F;a&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;JDK 22+&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;Finalized&lt;&#x2F;td&gt;&lt;td&gt;Not required (Finalized)&lt;&#x2F;td&gt;&lt;td&gt;API stabilization, minor refinements&lt;&#x2F;td&gt;&lt;td&gt;&lt;a href=&quot;https:&#x2F;&#x2F;docs.oracle.com&#x2F;en&#x2F;java&#x2F;javase&#x2F;22&#x2F;docs&#x2F;api&#x2F;java.base&#x2F;java&#x2F;lang&#x2F;foreign&#x2F;package-summary.html&quot;&gt;java.lang.foreign&lt;&#x2F;a&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;p&gt;&lt;strong&gt;Explanation&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;ul class=&quot;explanation&quot;&gt;
&lt;li&gt;Incubator Phase: The initial steps of the FFM API were as incubating features, meaning they were experimental and subject to change. These releases introduced the core concepts like memory access and foreign function invocation. JEP 434 mentions that the FFM API was in its incubator phase in JDK 17 and JDK 18.&lt;&#x2F;li&gt;
&lt;br&gt;
&lt;li&gt;Preview Phase: In the preview phase, the API&#x27;s design and implementation were complete, but still subject to potential changes based on user feedback. OpenJDK JEP 424 describes the Foreign Function &amp; Memory API as a preview API in JDK 19.&lt;&#x2F;li&gt;
&lt;br&gt;
&lt;li&gt;Finalized: In JDK 22, the FFM API was declared stable and ready for production use, removing the need for preview flags and providing a more robust and reliable native interoperability solution.&lt;&#x2F;li&gt;
&lt;br&gt;
&lt;li&gt;Key Enhancements: This section highlights the significant changes and refinements introduced in each version of the FFM API as it evolved.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;hr&gt;
&lt;h3 id=&quot;memo-walking-through-ffmreceiver-a-first-taste-of-java-ffm-ai-generated&quot;&gt;📝 Walking Through FfmReceiver — A First Taste of Java FFM (&lt;em&gt;AI generated&lt;&#x2F;em&gt;)&lt;&#x2F;h3&gt;
&lt;p&gt;In this demo, we’re calling native io_uring functions in C from Java, to build a &lt;strong&gt;high-performance TCP receiver&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;1. &lt;code&gt;Arena&lt;&#x2F;code&gt; — Memory Lifetime Manager&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;java&quot; class=&quot;language-java z-code&quot;&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;z-source z-java&quot;&gt;&lt;span class=&quot;z-keyword z-control z-exception z-try z-java&quot;&gt;try&lt;&#x2F;span&gt; &lt;span class=&quot;z-meta z-parens z-java&quot;&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-begin z-java&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class z-java&quot;&gt;Arena&lt;&#x2F;span&gt; arena &lt;span class=&quot;z-meta z-assignment z-rhs z-java&quot;&gt;&lt;span class=&quot;z-keyword z-operator z-assignment z-java&quot;&gt;=&lt;&#x2F;span&gt; &lt;span class=&quot;z-support z-class z-java&quot;&gt;Arena&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-accessor z-dot z-java&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-java&quot;&gt;&lt;span class=&quot;z-variable z-function z-java&quot;&gt;ofShared&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-begin z-java&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-end z-java&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-end z-java&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt; &lt;span class=&quot;z-meta z-block z-java&quot;&gt;&lt;span class=&quot;z-punctuation z-section z-block z-begin z-java&quot;&gt;{&lt;&#x2F;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source z-java&quot;&gt;&lt;span class=&quot;z-meta z-block z-java&quot;&gt;    &lt;span class=&quot;z-punctuation z-accessor z-dot z-java&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-accessor z-dot z-java&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-accessor z-dot z-java&quot;&gt;.&lt;&#x2F;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source z-java&quot;&gt;&lt;span class=&quot;z-meta z-block z-java&quot;&gt;&lt;span class=&quot;z-punctuation z-section z-block z-end z-java&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Arena&lt;&#x2F;strong&gt; is a memory allocator with automatic cleanup.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Think of it as a &lt;strong&gt;“scope for native memory”&lt;&#x2F;strong&gt;: allocate native buffers inside it, and when the &lt;code&gt;try&lt;&#x2F;code&gt; block exits, the arena frees them automatically.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Variants:&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Arena Type&lt;&#x2F;th&gt;&lt;th&gt;Bounded Lifetime&lt;&#x2F;th&gt;&lt;th&gt;Manual Close (arena.close())&lt;&#x2F;th&gt;&lt;th&gt;Thread Access&lt;&#x2F;th&gt;&lt;th&gt;Common Use Case&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Global&lt;&#x2F;td&gt;&lt;td&gt;No&lt;&#x2F;td&gt;&lt;td&gt;No&lt;&#x2F;td&gt;&lt;td&gt;Yes (accessible by any thread)&lt;&#x2F;td&gt;&lt;td&gt;For memory that needs to persist for the entire lifetime of the application.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Automatic&lt;&#x2F;td&gt;&lt;td&gt;Yes&lt;&#x2F;td&gt;&lt;td&gt;No (managed by Garbage Collector)&lt;&#x2F;td&gt;&lt;td&gt;Yes (accessible by any thread)&lt;&#x2F;td&gt;&lt;td&gt;Simplest memory management for bounded lifetimes. The memory is deallocated when the arena becomes unreachable by the garbage collector.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Confined&lt;&#x2F;td&gt;&lt;td&gt;Yes&lt;&#x2F;td&gt;&lt;td&gt;Yes (mandatory)&lt;&#x2F;td&gt;&lt;td&gt;No (only by the creating thread)&lt;&#x2F;td&gt;&lt;td&gt;Perfect for single-threaded applications. Provides deterministic deallocation of memory when the arena is closed, often used with try-with-resources.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Shared&lt;&#x2F;td&gt;&lt;td&gt;Yes&lt;&#x2F;td&gt;&lt;td&gt;Yes (mandatory)&lt;&#x2F;td&gt;&lt;td&gt;Yes (accessible by multiple threads)&lt;&#x2F;td&gt;&lt;td&gt;For concurrent applications where memory segments must be shared between threads. Closing the arena is safe and atomic, even when accessed by multiple threads.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;p&gt;Here we use &lt;code&gt;ofShared()&lt;&#x2F;code&gt; because multiple native calls may work with the allocated memory.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;2. &lt;code&gt;SymbolLookup&lt;&#x2F;code&gt; — Finding Functions in a Shared Library&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;java&quot; class=&quot;language-java z-code&quot;&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;z-source z-java&quot;&gt;&lt;span class=&quot;z-support z-class z-java&quot;&gt;SymbolLookup&lt;&#x2F;span&gt; lib &lt;span class=&quot;z-meta z-assignment z-rhs z-java&quot;&gt;&lt;span class=&quot;z-keyword z-operator z-assignment z-java&quot;&gt;=&lt;&#x2F;span&gt; &lt;span class=&quot;z-support z-class z-java&quot;&gt;SymbolLookup&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-accessor z-dot z-java&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-java&quot;&gt;&lt;span class=&quot;z-variable z-function z-java&quot;&gt;libraryLookup&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-begin z-java&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string z-quoted z-double z-java&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-begin z-java&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;.&#x2F;libiouring_tcpa.so&lt;span class=&quot;z-punctuation z-definition z-string z-end z-java&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-comma z-java&quot;&gt;,&lt;&#x2F;span&gt; arena&lt;span class=&quot;z-punctuation z-section z-parens z-end z-java&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-terminator z-java&quot;&gt;;&lt;&#x2F;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;SymbolLookup&lt;&#x2F;code&gt; is how Java finds functions or variables inside a native shared library (&lt;code&gt;.so&lt;&#x2F;code&gt;, &lt;code&gt;.dll&lt;&#x2F;code&gt;, &lt;code&gt;.dylib&lt;&#x2F;code&gt;).&lt;&#x2F;li&gt;
&lt;li&gt;You pass the library name and a scope (&lt;code&gt;arena&lt;&#x2F;code&gt;).&lt;&#x2F;li&gt;
&lt;li&gt;Later, we’ll do &lt;code&gt;lib.find(&quot;io_uring_listen&quot;)&lt;&#x2F;code&gt; to grab the raw address of that function.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;👉 Without &lt;code&gt;SymbolLookup&lt;&#x2F;code&gt;, Java wouldn’t know &lt;em&gt;where in memory&lt;&#x2F;em&gt; the C function lives.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;3. &lt;code&gt;Linker&lt;&#x2F;code&gt; — Bridging Java ↔ Native&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;java&quot; class=&quot;language-java z-code&quot;&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;z-source z-java&quot;&gt;&lt;span class=&quot;z-support z-class z-java&quot;&gt;Linker&lt;&#x2F;span&gt; linker &lt;span class=&quot;z-meta z-assignment z-rhs z-java&quot;&gt;&lt;span class=&quot;z-keyword z-operator z-assignment z-java&quot;&gt;=&lt;&#x2F;span&gt; &lt;span class=&quot;z-support z-class z-java&quot;&gt;Linker&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-accessor z-dot z-java&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-java&quot;&gt;&lt;span class=&quot;z-variable z-function z-java&quot;&gt;nativeLinker&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-begin z-java&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-end z-java&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-terminator z-java&quot;&gt;;&lt;&#x2F;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Linker&lt;&#x2F;strong&gt; builds a bridge between Java and C functions.&lt;&#x2F;li&gt;
&lt;li&gt;It knows how to:
&lt;ul&gt;
&lt;li&gt;Translate Java values to C values (e.g., &lt;code&gt;int&lt;&#x2F;code&gt; ↔ &lt;code&gt;int32_t&lt;&#x2F;code&gt;)&lt;&#x2F;li&gt;
&lt;li&gt;Call into native functions and return values back&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;&lt;code&gt;nativeLinker()&lt;&#x2F;code&gt; automatically picks the system ABI (x86_64, ARM64, etc.).&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;4. &lt;code&gt;MemorySegment&lt;&#x2F;code&gt; — Safe, Structured Native Memory&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;java&quot; class=&quot;language-java z-code&quot;&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;z-source z-java&quot;&gt;&lt;span class=&quot;z-support z-class z-java&quot;&gt;MemorySegment&lt;&#x2F;span&gt; buffer &lt;span class=&quot;z-meta z-assignment z-rhs z-java&quot;&gt;&lt;span class=&quot;z-keyword z-operator z-assignment z-java&quot;&gt;=&lt;&#x2F;span&gt; arena&lt;span class=&quot;z-punctuation z-accessor z-dot z-java&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-java&quot;&gt;&lt;span class=&quot;z-variable z-function z-java&quot;&gt;allocate&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-begin z-java&quot;&gt;(&lt;&#x2F;span&gt;bufferSize&lt;span class=&quot;z-punctuation z-section z-parens z-end z-java&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-terminator z-java&quot;&gt;;&lt;&#x2F;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;MemorySegment&lt;&#x2F;strong&gt; is a safe view of native memory.&lt;&#x2F;li&gt;
&lt;li&gt;Unlike &lt;code&gt;ByteBuffer&lt;&#x2F;code&gt; (which is limited and unsafe), a &lt;code&gt;MemorySegment&lt;&#x2F;code&gt; tracks bounds, lifetime, and thread access.&lt;&#x2F;li&gt;
&lt;li&gt;Example:&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre data-lang=&quot;java&quot; class=&quot;language-java z-code&quot;&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;z-source z-java&quot;&gt;&lt;span class=&quot;z-storage z-type z-primitive z-java&quot;&gt;byte&lt;&#x2F;span&gt; b &lt;span class=&quot;z-meta z-assignment z-rhs z-java&quot;&gt;&lt;span class=&quot;z-keyword z-operator z-assignment z-java&quot;&gt;=&lt;&#x2F;span&gt; buffer&lt;span class=&quot;z-punctuation z-accessor z-dot z-java&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-java&quot;&gt;&lt;span class=&quot;z-variable z-function z-java&quot;&gt;get&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-begin z-java&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class z-java&quot;&gt;ValueLayout&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-accessor z-dot z-java&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class z-java&quot;&gt;Java_BYTE&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-comma z-java&quot;&gt;,&lt;&#x2F;span&gt; offset&lt;span class=&quot;z-punctuation z-section z-parens z-end z-java&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-terminator z-java&quot;&gt;;&lt;&#x2F;span&gt;
&lt;&#x2F;span&gt;&lt;span class=&quot;z-source z-java&quot;&gt;buffer&lt;span class=&quot;z-punctuation z-accessor z-dot z-java&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-java&quot;&gt;&lt;span class=&quot;z-variable z-function z-java&quot;&gt;set&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-begin z-java&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class z-java&quot;&gt;ValueLayout&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-accessor z-dot z-java&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class z-java&quot;&gt;Java_BYTE&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-comma z-java&quot;&gt;,&lt;&#x2F;span&gt; offset&lt;span class=&quot;z-punctuation z-separator z-comma z-java&quot;&gt;,&lt;&#x2F;span&gt; &lt;span class=&quot;z-meta z-parens z-java&quot;&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-begin z-java&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type z-primitive z-java&quot;&gt;byte&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-end z-java&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt; &lt;span class=&quot;z-constant z-numeric z-integer z-decimal z-java&quot;&gt;42&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-end z-java&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-terminator z-java&quot;&gt;;&lt;&#x2F;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ul&gt;
&lt;li&gt;Here, we allocate an &lt;strong&gt;8 MB receive buffer&lt;&#x2F;strong&gt; per client.&lt;&#x2F;li&gt;
&lt;li&gt;Later, we copy received bytes into a Java &lt;code&gt;byte[]&lt;&#x2F;code&gt; for debugging.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;&lt;strong&gt;5. &lt;code&gt;MethodHandle&lt;&#x2F;code&gt; — Calling Native Functions&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Every native function we call has 3 steps:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Find its address&lt;&#x2F;strong&gt;:&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;pre data-lang=&quot;java&quot; class=&quot;language-java z-code&quot;&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;z-source z-java&quot;&gt;&lt;span class=&quot;z-support z-class z-java&quot;&gt;MemorySegment&lt;&#x2F;span&gt; addr &lt;span class=&quot;z-meta z-assignment z-rhs z-java&quot;&gt;&lt;span class=&quot;z-keyword z-operator z-assignment z-java&quot;&gt;=&lt;&#x2F;span&gt; lib&lt;span class=&quot;z-punctuation z-accessor z-dot z-java&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-java&quot;&gt;&lt;span class=&quot;z-variable z-function z-java&quot;&gt;find&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-begin z-java&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string z-quoted z-double z-java&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-begin z-java&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;io_uring_global_init&lt;span class=&quot;z-punctuation z-definition z-string z-end z-java&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-end z-java&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-accessor z-dot z-java&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-java&quot;&gt;&lt;span class=&quot;z-variable z-function z-java&quot;&gt;get&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-begin z-java&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-end z-java&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-terminator z-java&quot;&gt;;&lt;&#x2F;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;&lt;strong&gt;Describe its signature&lt;&#x2F;strong&gt; using &lt;code&gt;FunctionDescriptor&lt;&#x2F;code&gt;:&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;pre data-lang=&quot;java&quot; class=&quot;language-java z-code&quot;&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;z-source z-java&quot;&gt;&lt;span class=&quot;z-support z-class z-java&quot;&gt;FunctionDescriptor&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-accessor z-dot z-java&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-java&quot;&gt;&lt;span class=&quot;z-variable z-function z-java&quot;&gt;of&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-begin z-java&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class z-java&quot;&gt;ValueLayout&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-accessor z-dot z-java&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class z-java&quot;&gt;Java_INT&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-comma z-java&quot;&gt;,&lt;&#x2F;span&gt; &lt;span class=&quot;z-support z-class z-java&quot;&gt;ValueLayout&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-accessor z-dot z-java&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class z-java&quot;&gt;Java_INT&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-end z-java&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;→ “returns int, takes an int argument”&lt;&#x2F;p&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;&lt;strong&gt;Bind with a&lt;&#x2F;strong&gt; &lt;code&gt;MethodHandle&lt;&#x2F;code&gt;:&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;pre data-lang=&quot;java&quot; class=&quot;language-java z-code&quot;&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;z-source z-java&quot;&gt;&lt;span class=&quot;z-support z-class z-java&quot;&gt;MethodHandle&lt;&#x2F;span&gt; mhGlobalInit &lt;span class=&quot;z-meta z-assignment z-rhs z-java&quot;&gt;&lt;span class=&quot;z-keyword z-operator z-assignment z-java&quot;&gt;=&lt;&#x2F;span&gt; linker&lt;span class=&quot;z-punctuation z-accessor z-dot z-java&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-java&quot;&gt;&lt;span class=&quot;z-variable z-function z-java&quot;&gt;downcallHandle&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-begin z-java&quot;&gt;(&lt;&#x2F;span&gt;globalInitAddr&lt;span class=&quot;z-punctuation z-separator z-comma z-java&quot;&gt;,&lt;&#x2F;span&gt; descriptor&lt;span class=&quot;z-punctuation z-section z-parens z-end z-java&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-terminator z-java&quot;&gt;;&lt;&#x2F;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ol start=&quot;4&quot;&gt;
&lt;li&gt;&lt;strong&gt;Call it&lt;&#x2F;strong&gt; like a Java method:&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;pre data-lang=&quot;java&quot; class=&quot;language-java z-code&quot;&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;z-source z-java&quot;&gt;&lt;span class=&quot;z-storage z-type z-primitive z-java&quot;&gt;int&lt;&#x2F;span&gt; ret &lt;span class=&quot;z-meta z-assignment z-rhs z-java&quot;&gt;&lt;span class=&quot;z-keyword z-operator z-assignment z-java&quot;&gt;=&lt;&#x2F;span&gt; &lt;span class=&quot;z-meta z-parens z-java&quot;&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-begin z-java&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type z-primitive z-java&quot;&gt;int&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-end z-java&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt; mhGlobalInit&lt;span class=&quot;z-punctuation z-accessor z-dot z-java&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-java&quot;&gt;&lt;span class=&quot;z-variable z-function z-java&quot;&gt;invokeExact&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-begin z-java&quot;&gt;(&lt;&#x2F;span&gt;queueDepth&lt;span class=&quot;z-punctuation z-section z-parens z-end z-java&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-terminator z-java&quot;&gt;;&lt;&#x2F;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;👉 A &lt;code&gt;MethodHandle&lt;&#x2F;code&gt; is like a strongly-typed function pointer.&lt;&#x2F;p&gt;
&lt;p&gt;It hides the messy details of JNI — no boilerplate, no unsafe casts.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;6. &lt;code&gt;ValueLayout&lt;&#x2F;code&gt; — Mapping Java ↔ C Types&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;ValueLayout&lt;&#x2F;code&gt; tells the FFM API how Java types map to C types.&lt;&#x2F;li&gt;
&lt;li&gt;Examples:
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Java_INT&lt;&#x2F;code&gt; → C &lt;code&gt;int32_t&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;Java_LONG&lt;&#x2F;code&gt; → C &lt;code&gt;int64_t&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;Java_BYTE&lt;&#x2F;code&gt; → C &lt;code&gt;int8_t&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;ADDRESS&lt;&#x2F;code&gt; → C pointers&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;So when we say:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;java&quot; class=&quot;language-java z-code&quot;&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;z-source z-java&quot;&gt;&lt;span class=&quot;z-support z-class z-java&quot;&gt;FunctionDescriptor&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-accessor z-dot z-java&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-java&quot;&gt;&lt;span class=&quot;z-variable z-function z-java&quot;&gt;of&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-begin z-java&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class z-java&quot;&gt;ValueLayout&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-accessor z-dot z-java&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class z-java&quot;&gt;Java_INT&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-comma z-java&quot;&gt;,&lt;&#x2F;span&gt; &lt;span class=&quot;z-support z-class z-java&quot;&gt;ValueLayout&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-accessor z-dot z-java&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class z-java&quot;&gt;Java_INT&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-comma z-java&quot;&gt;,&lt;&#x2F;span&gt; &lt;span class=&quot;z-support z-class z-java&quot;&gt;ValueLayout&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-accessor z-dot z-java&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other z-java&quot;&gt;ADDRESS&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-comma z-java&quot;&gt;,&lt;&#x2F;span&gt; &lt;span class=&quot;z-support z-class z-java&quot;&gt;ValueLayout&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-accessor z-dot z-java&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class z-java&quot;&gt;Java_LONG&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-end z-java&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We mean:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; class=&quot;language-c z-code&quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span class=&quot;z-source z-c&quot;&gt;&lt;span class=&quot;z-storage z-type z-c&quot;&gt;int&lt;&#x2F;span&gt; &lt;span class=&quot;z-meta z-function z-c&quot;&gt;&lt;span class=&quot;z-entity z-name z-function z-c&quot;&gt;io_uring_recv&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function z-parameters z-c&quot;&gt;&lt;span class=&quot;z-meta z-group z-c&quot;&gt;&lt;span class=&quot;z-punctuation z-section z-group z-begin z-c&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function z-parameters z-c&quot;&gt;&lt;span class=&quot;z-meta z-group z-c&quot;&gt;&lt;span class=&quot;z-storage z-type z-c&quot;&gt;int&lt;&#x2F;span&gt; &lt;span class=&quot;z-variable z-parameter z-c&quot;&gt;clientFd&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-c&quot;&gt;,&lt;&#x2F;span&gt; &lt;span class=&quot;z-storage z-type z-c&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-c&quot;&gt;*&lt;&#x2F;span&gt; &lt;span class=&quot;z-variable z-parameter z-c&quot;&gt;buffer&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-c&quot;&gt;,&lt;&#x2F;span&gt; &lt;span class=&quot;z-storage z-type z-c&quot;&gt;long&lt;&#x2F;span&gt; &lt;span class=&quot;z-variable z-parameter z-c&quot;&gt;length&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section z-group z-end z-c&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-terminator z-c&quot;&gt;;&lt;&#x2F;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;7. Putting It All Together&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;In the demo flow:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Initialize io_uring&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;pre data-lang=&quot;java&quot; class=&quot;language-java z-code&quot;&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;z-source z-java&quot;&gt;mhGlobalInit&lt;span class=&quot;z-punctuation z-accessor z-dot z-java&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-java&quot;&gt;&lt;span class=&quot;z-variable z-function z-java&quot;&gt;invokeExact&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-begin z-java&quot;&gt;(&lt;&#x2F;span&gt;queueDepth&lt;span class=&quot;z-punctuation z-section z-parens z-end z-java&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-terminator z-java&quot;&gt;;&lt;&#x2F;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;Create a TCP server socket&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;pre data-lang=&quot;java&quot; class=&quot;language-java z-code&quot;&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;z-source z-java&quot;&gt;&lt;span class=&quot;z-storage z-type z-primitive z-java&quot;&gt;int&lt;&#x2F;span&gt; listenFd &lt;span class=&quot;z-meta z-assignment z-rhs z-java&quot;&gt;&lt;span class=&quot;z-keyword z-operator z-assignment z-java&quot;&gt;=&lt;&#x2F;span&gt; &lt;span class=&quot;z-meta z-parens z-java&quot;&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-begin z-java&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type z-primitive z-java&quot;&gt;int&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-end z-java&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt; mhListen&lt;span class=&quot;z-punctuation z-accessor z-dot z-java&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-java&quot;&gt;&lt;span class=&quot;z-variable z-function z-java&quot;&gt;invokeExact&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-begin z-java&quot;&gt;(&lt;&#x2F;span&gt;port&lt;span class=&quot;z-punctuation z-separator z-comma z-java&quot;&gt;,&lt;&#x2F;span&gt; backlog&lt;span class=&quot;z-punctuation z-section z-parens z-end z-java&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-terminator z-java&quot;&gt;;&lt;&#x2F;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;Accept connections&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;pre data-lang=&quot;java&quot; class=&quot;language-java z-code&quot;&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;z-source z-java&quot;&gt;&lt;span class=&quot;z-storage z-type z-primitive z-java&quot;&gt;int&lt;&#x2F;span&gt; clientFd &lt;span class=&quot;z-meta z-assignment z-rhs z-java&quot;&gt;&lt;span class=&quot;z-keyword z-operator z-assignment z-java&quot;&gt;=&lt;&#x2F;span&gt; &lt;span class=&quot;z-meta z-parens z-java&quot;&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-begin z-java&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type z-primitive z-java&quot;&gt;int&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-end z-java&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt; mhAccept&lt;span class=&quot;z-punctuation z-accessor z-dot z-java&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-java&quot;&gt;&lt;span class=&quot;z-variable z-function z-java&quot;&gt;invokeExact&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-begin z-java&quot;&gt;(&lt;&#x2F;span&gt;listenFd&lt;span class=&quot;z-punctuation z-section z-parens z-end z-java&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-terminator z-java&quot;&gt;;&lt;&#x2F;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ol start=&quot;4&quot;&gt;
&lt;li&gt;Receive data into a native buffer&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;pre data-lang=&quot;java&quot; class=&quot;language-java z-code&quot;&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;z-source z-java&quot;&gt;&lt;span class=&quot;z-storage z-type z-primitive z-java&quot;&gt;int&lt;&#x2F;span&gt; bytesReceived &lt;span class=&quot;z-meta z-assignment z-rhs z-java&quot;&gt;&lt;span class=&quot;z-keyword z-operator z-assignment z-java&quot;&gt;=&lt;&#x2F;span&gt; &lt;span class=&quot;z-meta z-parens z-java&quot;&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-begin z-java&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type z-primitive z-java&quot;&gt;int&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-end z-java&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt; mhRecv&lt;span class=&quot;z-punctuation z-accessor z-dot z-java&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-java&quot;&gt;&lt;span class=&quot;z-variable z-function z-java&quot;&gt;invokeExact&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-begin z-java&quot;&gt;(&lt;&#x2F;span&gt;clientFd&lt;span class=&quot;z-punctuation z-separator z-comma z-java&quot;&gt;,&lt;&#x2F;span&gt; buffer&lt;span class=&quot;z-punctuation z-separator z-comma z-java&quot;&gt;,&lt;&#x2F;span&gt; bufferSize&lt;span class=&quot;z-punctuation z-section z-parens z-end z-java&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-terminator z-java&quot;&gt;;&lt;&#x2F;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ol start=&quot;5&quot;&gt;
&lt;li&gt;Inspect data in Java (first 128 bytes)&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;pre data-lang=&quot;java&quot; class=&quot;language-java z-code&quot;&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;z-source z-java&quot;&gt;&lt;span class=&quot;z-storage z-type z-primitive z-java&quot;&gt;byte&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier z-array z-java&quot;&gt;[]&lt;&#x2F;span&gt; arr &lt;span class=&quot;z-meta z-assignment z-rhs z-java&quot;&gt;&lt;span class=&quot;z-keyword z-operator z-assignment z-java&quot;&gt;=&lt;&#x2F;span&gt; &lt;span class=&quot;z-meta z-instantiation z-java&quot;&gt;&lt;span class=&quot;z-keyword z-other z-storage z-new z-java&quot;&gt;new&lt;&#x2F;span&gt; &lt;span class=&quot;z-storage z-type z-primitive z-java&quot;&gt;byte&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-brackets z-array-initialization z-java&quot;&gt;&lt;span class=&quot;z-punctuation z-section z-brackets z-begin z-java&quot;&gt;[&lt;&#x2F;span&gt;displayLen&lt;span class=&quot;z-punctuation z-section z-brackets z-end z-java&quot;&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-terminator z-java&quot;&gt;;&lt;&#x2F;span&gt;
&lt;&#x2F;span&gt;&lt;span class=&quot;z-source z-java&quot;&gt;arr[i] &lt;span class=&quot;z-meta z-assignment z-rhs z-java&quot;&gt;&lt;span class=&quot;z-keyword z-operator z-assignment z-java&quot;&gt;=&lt;&#x2F;span&gt; buffer&lt;span class=&quot;z-punctuation z-accessor z-dot z-java&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-java&quot;&gt;&lt;span class=&quot;z-variable z-function z-java&quot;&gt;get&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-begin z-java&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other z-java&quot;&gt;BYTE&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-comma z-java&quot;&gt;,&lt;&#x2F;span&gt; i&lt;span class=&quot;z-punctuation z-section z-parens z-end z-java&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-terminator z-java&quot;&gt;;&lt;&#x2F;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ol start=&quot;6&quot;&gt;
&lt;li&gt;Close the socket&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;pre data-lang=&quot;java&quot; class=&quot;language-java z-code&quot;&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;z-source z-java&quot;&gt;mhClose&lt;span class=&quot;z-punctuation z-accessor z-dot z-java&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-java&quot;&gt;&lt;span class=&quot;z-variable z-function z-java&quot;&gt;invokeExact&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section z-parens z-begin z-java&quot;&gt;(&lt;&#x2F;span&gt;clientFd&lt;span class=&quot;z-punctuation z-section z-parens z-end z-java&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-terminator z-java&quot;&gt;;&lt;&#x2F;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;8. Why This Matters&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Traditionally, Java needed &lt;strong&gt;JNI&lt;&#x2F;strong&gt; for native calls — painful, verbose, unsafe.&lt;&#x2F;p&gt;
&lt;p&gt;With &lt;strong&gt;FFM&lt;&#x2F;strong&gt;, we now have:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Memory safety&lt;&#x2F;strong&gt; (bounds checks, lifetime control)&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Performance&lt;&#x2F;strong&gt; (zero-copy native memory access)&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Clarity&lt;&#x2F;strong&gt; (call native functions like Java methods)&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Portability&lt;&#x2F;strong&gt; (same code runs across OS&#x2F;CPU with correct ABI handling)&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;This demo shows: &lt;strong&gt;pure Java controlling a high-performance Linux feature (io_uring)&lt;&#x2F;strong&gt; — something unimaginable&#x2F;complex with old JNI boilerplate.&lt;&#x2F;p&gt;
</content>
        <summary type="html">Sharing my initial learnings using Java FFM, Foreign Function &amp; Memory Access.</summary>
        </entry>
</feed>
