Module notzed.zcl
Package api

Class Memory

java.lang.Object
api.Memory

public class Memory
extends java.lang.Object
A utility library for memory operations including a stack allocator. The stack allocator works like this try (Memory.Frame f = Memory.frame()) { MemoryAddress a = Memory.alloca(size); } Any memory allocated is freed when the frame is closed. Note that unlike C's alloca() the frame scope is not per-function but application defined.
  • Constructor Summary

    Constructors 
    Constructor Description
    Memory()  
  • Method Summary

    Modifier and Type Method Description
    static void free​(jdk.incubator.foreign.MemoryAddress ptr)  
    static jdk.incubator.foreign.MemoryAddress malloc​(long size)
    Allocate C memory.
    static jdk.incubator.foreign.MemorySegment ofNative​(jdk.incubator.foreign.MemoryAddress addr, long size)
    Create a sized memory segment from a segment allocated with malloc.
    static Allocator stack()
    Create a stack allocator.
    static long toLong​(jdk.incubator.foreign.MemoryAddress addr)
    Get the physical address.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Memory

      public Memory()
  • Method Details

    • stack

      public static Allocator stack()
      Create a stack allocator. The stack allocator uses thread-specific backing buffer. This should only be used for small allocations.
    • ofNative

      public static jdk.incubator.foreign.MemorySegment ofNative​(jdk.incubator.foreign.MemoryAddress addr, long size)
      Create a sized memory segment from a segment allocated with malloc. Closing this segment has no effect on the original, and the original must be used for that.
    • toLong

      public static long toLong​(jdk.incubator.foreign.MemoryAddress addr)
      Get the physical address. I mean how is this an "offset"?
    • malloc

      public static jdk.incubator.foreign.MemoryAddress malloc​(long size)
      Allocate C memory. This is not usable by java directly, see memsize()
    • free

      public static void free​(jdk.incubator.foreign.MemoryAddress ptr)