CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-graalvm-sdk--graal-sdk

GraalVM Standard Development Kit providing APIs for polyglot language embedding, native AOT compilation, memory-efficient collections, low-level memory access, JNI utilities, and native bridge communication.

Pending
Overview
Eval results
Files

c-interop.mddocs/

C Interoperability

Low-level APIs for C function calls, data type interoperability, and native code integration in GraalVM native images.

Capabilities

C Function Pointers

Type-safe interfaces for calling C functions from native images with proper argument and return type handling.

/**
 * Base interface for C function pointers
 */
public interface CFunctionPointer extends CodePointer, RelocatedPointer {
}

/**
 * Pointer to executable code
 */
public interface CodePointer extends PointerBase {
    /** Check if code pointer is null */
    boolean isNull();
    
    /** Check if code pointer is not null */
    boolean isNonNull();
}

/**
 * Relocated function pointer for dynamic linking
 */
public interface RelocatedPointer extends ComparableWord {
    /** Check if relocation is resolved */
    boolean isResolved();
    
    /** Get raw address if resolved */
    long getAddress();
}

Usage Examples:

import org.graalvm.nativeimage.c.function.*;

public class CFunctionExample {
    // Define C function signature
    public interface MallocFunction extends CFunctionPointer {
        @CFunction("malloc")
        Pointer call(UnsignedWord size);
    }
    
    public interface FreeFunction extends CFunctionPointer {
        @CFunction("free")
        void call(Pointer ptr);
    }
    
    public void useCFunctions() {
        // Get function pointers
        MallocFunction malloc = CLibrary.getFunction("malloc", MallocFunction.class);
        FreeFunction free = CLibrary.getFunction("free", FreeFunction.class);
        
        // Allocate memory
        UnsignedWord size = WordFactory.unsigned(1024);
        Pointer memory = malloc.call(size);
        
        if (memory.isNonNull()) {
            // Use memory...
            memory.writeInt(WordFactory.unsigned(0), 42);
            
            // Free memory
            free.call(memory);
        }
    }
}

C Primitive Pointers

Typed pointers for accessing C primitive arrays and data structures.

/**
 * Pointer to C char array (byte array)
 */
public interface CCharPointer extends PointerBase {
    /** Read char at pointer location */
    byte read();
    
    /** Read char at given index */
    byte read(int index);
    
    /** Write char at pointer location */
    void write(byte value);
    
    /** Write char at given index */
    void write(int index, byte value);
    
    /** Get pointer to element at index */
    CCharPointer addressOf(int index);
    
    /** Check if pointer is null */
    boolean isNull();
}

/**
 * Pointer to C int array
 */
public interface CIntPointer extends PointerBase {
    /** Read int at pointer location */
    int read();
    
    /** Read int at given index */
    int read(int index);
    
    /** Write int at pointer location */
    void write(int value);
    
    /** Write int at given index */
    void write(int index, int value);
    
    /** Get pointer to element at index */
    CIntPointer addressOf(int index);
}

/**
 * Pointer to C long array
 */
public interface CLongPointer extends PointerBase {
    /** Read long at pointer location */
    long read();
    
    /** Read long at given index */
    long read(int index);
    
    /** Write long at pointer location */
    void write(long value);
    
    /** Write long at given index */
    void write(int index, long value);
    
    /** Get pointer to element at index */
    CLongPointer addressOf(int index);
}

/**
 * Pointer to C short array
 */
public interface CShortPointer extends PointerBase {
    /** Read short at pointer location */
    short read();
    
    /** Read short at given index */
    short read(int index);
    
    /** Write short at pointer location */
    void write(short value);
    
    /** Write short at given index */
    void write(int index, short value);
    
    /** Get pointer to element at index */
    CShortPointer addressOf(int index);
}

/**
 * Pointer to C float array
 */
public interface CFloatPointer extends PointerBase {
    /** Read float at pointer location */
    float read();
    
    /** Read float at given index */
    float read(int index);
    
    /** Write float at pointer location */
    void write(float value);
    
    /** Write float at given index */
    void write(int index, float value);
    
    /** Get pointer to element at index */
    CFloatPointer addressOf(int index);
}

/**
 * Pointer to C double array
 */
public interface CDoublePointer extends PointerBase {
    /** Read double at pointer location */
    double read();
    
    /** Read double at given index */
    double read(int index);
    
    /** Write double at pointer location */
    void write(double value);
    
    /** Write double at given index */
    void write(int index, double value);
    
    /** Get pointer to element at index */
    CDoublePointer addressOf(int index);
}

Usage Examples:

public class CPrimitiveArrays {
    public void workWithArrays() {
        // Allocate C int array
        CIntPointer intArray = allocateIntArray(10);
        
        // Write values
        for (int i = 0; i < 10; i++) {
            intArray.write(i, i * i); // Store squares
        }
        
        // Read values
        for (int i = 0; i < 10; i++) {
            int value = intArray.read(i);
            System.out.println("intArray[" + i + "] = " + value);
        }
        
        // Pointer arithmetic
        CIntPointer fifthElement = intArray.addressOf(5);
        int value = fifthElement.read(); // Same as intArray.read(5)
        
        // Work with char array (C string)
        CCharPointer cString = allocateCharArray(256);
        byte[] javaString = "Hello, C!".getBytes();
        
        for (int i = 0; i < javaString.length; i++) {
            cString.write(i, javaString[i]);
        }
        cString.write(javaString.length, (byte) 0); // Null terminator
        
        // Free memory
        free(intArray);
        free(cString);
    }
    
    // Placeholder allocation methods
    private native CIntPointer allocateIntArray(int size);
    private native CCharPointer allocateCharArray(int size);
    private native void free(PointerBase ptr);
}

Pointer-to-Pointer Types

Double indirection pointers for working with C arrays of pointers and complex data structures.

/**
 * Pointer to array of char pointers (array of C strings)
 */
public interface CCharPointerPointer extends PointerBase {
    /** Read char pointer at location */
    CCharPointer read();
    
    /** Read char pointer at given index */
    CCharPointer read(int index);
    
    /** Write char pointer at location */
    void write(CCharPointer value);
    
    /** Write char pointer at given index */
    void write(int index, CCharPointer value);
    
    /** Get pointer to element at index */
    CCharPointerPointer addressOf(int index);
}

/**
 * Pointer to array of int pointers
 */
public interface CIntPointerPointer extends PointerBase {
    /** Read int pointer at location */
    CIntPointer read();
    
    /** Read int pointer at given index */
    CIntPointer read(int index);
    
    /** Write int pointer at location */
    void write(CIntPointer value);
    
    /** Write int pointer at given index */
    void write(int index, CIntPointer value);
    
    /** Get pointer to element at index */
    CIntPointerPointer addressOf(int index);
}

Usage Examples:

public class PointerToPointerExample {
    public void workWithStringArray() {
        // Create array of C strings (char**)
        String[] javaStrings = {"Hello", "World", "from", "Java"};
        CCharPointerPointer stringArray = allocateStringArray(javaStrings.length);
        
        // Convert Java strings to C strings
        for (int i = 0; i < javaStrings.length; i++) {
            CCharPointer cString = createCString(javaStrings[i]);
            stringArray.write(i, cString);
        }
        
        // Read back C strings
        for (int i = 0; i < javaStrings.length; i++) {
            CCharPointer cString = stringArray.read(i);
            String javaString = readCString(cString);
            System.out.println("String[" + i + "] = " + javaString);
        }
        
        // Free memory
        for (int i = 0; i < javaStrings.length; i++) {
            free(stringArray.read(i));
        }
        free(stringArray);
    }
    
    // Helper methods
    private CCharPointer createCString(String javaString) {
        byte[] bytes = javaString.getBytes();
        CCharPointer cString = allocateCharArray(bytes.length + 1);
        
        for (int i = 0; i < bytes.length; i++) {
            cString.write(i, bytes[i]);
        }
        cString.write(bytes.length, (byte) 0); // Null terminator
        
        return cString;
    }
    
    private String readCString(CCharPointer cString) {
        // Find length
        int length = 0;
        while (cString.read(length) != 0) {
            length++;
        }
        
        // Convert to Java string
        byte[] bytes = new byte[length];
        for (int i = 0; i < length; i++) {
            bytes[i] = cString.read(i);
        }
        return new String(bytes);
    }
    
    // Placeholder methods
    private native CCharPointerPointer allocateStringArray(int size);
    private native CCharPointer allocateCharArray(int size);
    private native void free(PointerBase ptr);
}

Generic Pointers

Generic pointer types for untyped memory access and void pointer equivalents.

/**
 * Generic void pointer equivalent
 */
public interface VoidPointer extends PointerBase {
    /** Cast to typed pointer */
    <T extends PointerBase> T cast(Class<T> targetType);
    
    /** Get raw memory address */
    long getAddress();
    
    /** Create from raw address */
    static VoidPointer fromAddress(long address);
}

/**
 * Pointer sized to hold a machine word
 */
public interface WordPointer extends PointerBase {
    /** Read word value at pointer location */
    UnsignedWord read();
    
    /** Read word value at given index */
    UnsignedWord read(int index);
    
    /** Write word value at pointer location */
    void write(UnsignedWord value);
    
    /** Write word value at given index */
    void write(int index, UnsignedWord value);
    
    /** Get pointer to element at index */
    WordPointer addressOf(int index);
}

Usage Examples:

public class GenericPointers {
    public void useGenericPointers() {
        // Allocate generic memory
        VoidPointer memory = allocateMemory(1024);
        
        // Cast to specific types as needed
        CIntPointer intPtr = memory.cast(CIntPointer.class);
        intPtr.write(0, 42);
        
        CFloatPointer floatPtr = memory.cast(CFloatPointer.class);
        floatPtr.write(1, 3.14f);
        
        // Work with word-sized values
        WordPointer wordPtr = memory.cast(WordPointer.class);
        wordPtr.write(2, WordFactory.unsigned(0xDEADBEEF));
        
        // Read back values
        System.out.println("Int: " + intPtr.read(0));
        System.out.println("Float: " + floatPtr.read(1));
        System.out.println("Word: " + wordPtr.read(2).rawValue());
        
        // Get raw address for external APIs
        long address = memory.getAddress();
        System.out.println("Memory address: 0x" + Long.toHexString(address));
        
        free(memory);
    }
    
    // Placeholder methods
    private native VoidPointer allocateMemory(int size);
    private native void free(VoidPointer ptr);
}

C Structure Support

Support for defining and accessing C structures through Java interfaces.

/**
 * Base interface for C structure types
 */
public interface CStruct extends PointerBase {
    /** Get size of structure in bytes */
    UnsignedWord sizeof();
    
    /** Get address of structure */
    Pointer getAddress();
}

/**
 * Support for C unions
 */
public interface CUnion extends PointerBase {
    /** Get size of union in bytes */
    UnsignedWord sizeof();
    
    /** Get address of union */
    Pointer getAddress();
}

Usage Examples:

// Define C structure interface
public interface Point extends CStruct {
    @CField("x")
    int getX();
    
    @CField("x")
    void setX(int value);
    
    @CField("y") 
    int getY();
    
    @CField("y")
    void setY(int value);
}

public class StructureExample {
    public void useStructures() {
        // Allocate structure
        Point point = allocatePoint();
        
        // Set values
        point.setX(10);
        point.setY(20);
        
        // Read values
        System.out.println("Point: (" + point.getX() + ", " + point.getY() + ")");
        
        // Pass to C function
        double distance = calculateDistance(point);
        System.out.println("Distance from origin: " + distance);
        
        free(point);
    }
    
    // Placeholder methods
    private native Point allocatePoint();
    private native double calculateDistance(Point point);
    private native void free(CStruct struct);
}

Types

// Annotations for C interoperability (from substitutions module)
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)  
public @interface CFunction {
    /** C function name to bind to */
    String value() default "";
    
    /** Library containing the function */
    String library() default "";
}

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CField {
    /** C field name to bind to */
    String value();
    
    /** Offset within structure */
    int offset() default -1;
}

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface CStruct {
    /** C structure name */
    String value() default "";
    
    /** Add padding for alignment */
    boolean addStructKeyword() default false;
}

// Exception types
public class CInterfaceException extends RuntimeException {
    public CInterfaceException(String message);
    public CInterfaceException(String message, Throwable cause);
}

// Utility classes
public final class CTypeConversion {
    /** Convert Java string to C string */
    public static CCharPointer toCString(String javaString);
    
    /** Convert C string to Java string */
    public static String toJavaString(CCharPointer cString);
    
    /** Convert Java byte array to C char array */
    public static CCharPointer toCBytes(byte[] javaBytes);
    
    /** Convert C char array to Java byte array */
    public static byte[] toJavaBytes(CCharPointer cBytes, int length);
}

Install with Tessl CLI

npx tessl i tessl/maven-org-graalvm-sdk--graal-sdk

docs

c-interop.md

collections.md

config.md

index.md

jni-utils.md

native-bridge.md

native-image.md

polyglot.md

substitutions.md

word.md

tile.json