Giter VIP home page Giter VIP logo

javasharedmemory's Introduction

JavaSharedMemory Jitpack

Shared memory between processes. (Java, C++, Rust, etc.) Based on Java 19 Foreign Function & Memory API

Support Windows (CreateFileMapping), Linux and macOS (mmap and shmget).

Note Compared with standard input and output (anonymous pipes) on Windows, more than 2x performance improvement.

Usage

Java

repositories {
    maven { url 'https://www.jitpack.io' }
}

dependencies {
    implementation 'com.github.ShirasawaSama:JavaSharedMemory:<version>'
}

tasks.withType(JavaCompile).each {
    it.options.compilerArgs.add('--enable-preview')
}

// kotlin script
// tasks.withType<JavaCompile> {
//     options.compilerArgs = options.compilerArgs + listOf("--enable-preview")
// }
import cn.apisium.shm;

public class Main {
    public static void main(String[] args) {
        // var shm = SharedMemory.open("test", 1024)
        try (var shm = SharedMemory.create("test", 1024)) {
            var buf = shm.toByteBuffer();
            buf.put("Hello, World!".getBytes());
        }
    }
}

C++

git submodule add https://github.com/ShirasawaSama/JavaSharedMemory.git

CMakeLists.txt:

add_subdirectory(JavaSharedMemory)
target_link_libraries(${PROJECT_NAME} java_shared_memory)

main.cpp:

#include <jshm.h>

int main() {
    auto shm = jshm::shared_memory::create("test", 1024);
    // auto shm = jshm::shared_memory::open("test", 1024);
    strcpy((char*)shm->address(), "Hello, World!");
    delete shm;
}

Author

Shirasawa

License

MIT

javasharedmemory's People

Contributors

shirasawasama avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

javasharedmemory's Issues

Java21 SharedMemory.open Exception

win10

SharedMemory sharedMemory = SharedMemory.create("test", 1024);
try (var shm = sharedMemory) {
var buf = shm.toByteBuffer();
buf.put(bytes);

}
System.out.println("write success ");
sharedMemory.close();
SharedMemory test = SharedMemory.open("test", 1024);  // <---
System.out.println(test.getSize());
WindowsSharedMemory:69

        hMapFile = isCreate ? (MemorySegment) createFileMapping.invokeExact(
                (MemorySegment) MemorySegment.ofAddress(-1),
                MemorySegment.NULL,
                PAGE_READWRITE | SEC_COMMIT,
                0,
                size,
                (MemorySegment) Arena.ofAuto().allocateUtf8String(name)
        ) : (MemorySegment) openFileMapping.invokeExact(SECTION_MAP_WRITE | SECTION_MAP_READ, 0, name);// <--------
        if (hMapFile.address() == 0) throw new IllegalStateException("CreateFileMapping failed.");
java.lang.RuntimeException: java.lang.invoke.WrongMethodTypeException: handle's method type (int,int,MemorySegment)MemorySegment but found (int,int,String)MemorySegment
	at cn.apisium.shm.SharedMemory.init(SharedMemory.java:86)
	at cn.apisium.shm.SharedMemory.open(SharedMemory.java:54)
	at top.war3rpg.Main.main(Main.java:47)
Caused by: java.lang.invoke.WrongMethodTypeException: handle's method type (int,int,MemorySegment)MemorySegment but found (int,int,String)MemorySegment
	at java.base/java.lang.invoke.Invokers.newWrongMethodTypeException(Invokers.java:522)
	at java.base/java.lang.invoke.Invokers.checkExactType(Invokers.java:531)
	at cn.apisium.shm.impl.WindowsSharedMemory.<init>(WindowsSharedMemory.java:69)
	at cn.apisium.shm.SharedMemory.init(SharedMemory.java:81)
	... 2 more

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.