Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.
/ JDMA Public archive

Direct Memory Access and other unsafe operations for Java

License

Notifications You must be signed in to change notification settings

ewpratten/JDMA

Repository files navigation

Direct Memory Access for Java

Documentation Build library

jdma is a Java library that provides c-like malloc() and free() functions, along with many others. All functions operate off-heap, meaning no need to deal with the garbage collector. This is useful when dealing with code that must be efficient as possible.

Demo

import static ca.retrylife.jdma.DMA.*;
import static ca.retrylife.jdma.DMAString.*;

// Allocate 50 bytes
@Pointer long bytes = malloc(50);

// Set the first half to 0
memset(bytes, (byte)0, 25);

// Set the second half to 20
memset(bytes + 25, (byte)20, 25);

// Free the bytes
free(bytes);

// Reuse the bytes pointer for a c-like string
bytes = allocateString("Hello, world!");

// Truths about the string
assert strlen(bytes) == 13;
assert toJavaString(bytes, strlen(bytes)).equals("Hello, world!");

// Free the string
free(bytes);

Using in your project

Step 1. Add the RetryLife maven server to your build.gradle file:

repositories {
    maven { 
        name 'retrylife-release'
        url 'https://release.maven.retrylife.ca/' 
    }
}

Step 2. Add this library as a dependency:

dependencies {
    implementation 'ca.retrylife:jdma:1.+'
    implementation 'ca.retrylife:jdma:1.+:sources'
    implementation 'ca.retrylife:jdma:1.+:javadoc'
}

Notes

This library specifically does not use the JVM garbage collector, meaning you must free() everything when finished. All the same warnings that come with C programming apply here too. This library also comes with a @Pointer annotation, which has no effect on program execution, but just exists to visually flag values that are pointers in source code.