Giter VIP home page Giter VIP logo

sharedmem's Introduction

███████ ██   ██  █████  ██████  ███████ ██████  ███    ███ ███████ ███    ███
██      ██   ██ ██   ██ ██   ██ ██      ██   ██ ████  ████ ██      ████  ████
███████ ███████ ███████ ██████  █████   ██   ██ ██ ████ ██ █████   ██ ████ ██
     ██ ██   ██ ██   ██ ██   ██ ██      ██   ██ ██  ██  ██ ██      ██  ██  ██
███████ ██   ██ ██   ██ ██   ██ ███████ ██████  ██      ██ ███████ ██      ██
Shared memory for Java
    

GitHub GitHub issues GitHub release (latest by date) GitHub Repo stars GitHub Sponsors

Table of contents

  • What is sharedmem?
  • Compatibility
  • Installation
  • Usage
  • Examples
  • Contributing
  • Building from source
  • Licenses

What is sharedmem?

sharedmem allows you to access shared memory / memory mapped files in Java. sharedmem is basically an abstraction layer on top of the boost interprocess library.

WARNING
I have basically zero experience with C++ so the native code is probably awful. Someone with more experience than me has to clean that up eventually.

Compatibility

sharedmem is compatible with Linux and Windows.

Installation

Simply integrate sharedmem with your favorite build tool into your project, and you are good to go.

Maven:

<dependencies>
    <dependency>
        <groupId>dev.cerus</groupId>
        <artifactId>sharedmem</artifactId>
        <version>1.1.0</version>
    </dependency>
</dependencies>

Usage

Javadocs

Create a new memory mapped file object:
MemoryMappedFile mmf = MemoryMappedFile.of("name");

Don't forget to open before you start reading / writing:
mmf.open(MemoryMappedFile.OpenMode.CREATE_OR_OPEN, MemoryMappedFile.RWMode.READ_WRITE, 16);

Read:
byte[] data = mmf.read(0, -1);

Write:
mmf.write(0, new byte[] { 1, 2, 3 }

In order to load the native library, sharedmem needs to save it to a temporary folder on the disk. If you don't want that you can enable "primitive loading" by calling LibraryLoader.enablePrimitiveLoading(). Ensure that the native library is in Java's library folder if you enable this.

Examples

class Example {

    public static void main(String[] args) {
        // Opens or creates "Local\\test_map" and writes a sequence of [1, 2, 3] at random places

        // Create and open file
        final MemoryMappedFile file = MemoryMappedFile.of("Local\\test_map");
        long capacity = 32; // capacity = size in bytes - Only used when creating a memory mapped file
        file.open(MemoryMappedFile.OpenMode.CREATE_OR_OPEN, MemoryMappedFile.RWMode.READ_WRITE, capacity);

        // Write sequence and read whole file
        file.write(ThreadLocalRandom.current().nextInt(0, 29), new byte[] {1, 2, 3});
        int length = -1; // -1 if you want to read the whole file
        final byte[] read = file.read(0, length);

        // Close file (will *not* delete/remove the memory mapped file)
        file.close();

        // Print contents
        System.out.println(Arrays.toString(read));

        // Example output after running this for a few times:
        // [0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 1, 2, 3, 1, 2, 3, 0, 0, 0, 1, 2, 3, 0, 0, 0, 1, 2, 3, 0, 1, 2, 3]
    }
}

Building from source

Linux
Requirements: Java 11, Maven, Git, g++
Adjust your JAVA_HOME in build.sh and run ./build.sh

Windows
Requirements: Java 11, Maven, MinGW GCC, Boost libraries

  1. Download the Boost libraries (https://www.boost.org/users/download/)
  2. Unzip the libraries in the project directory
  3. Run build_native.bat
  4. Copy target\libsharedmem.dll into src\main\resources
  5. Run mvn clean package

Licenses

This project is licenses under the MIT License.

Thirdparty licenses: Boost

sharedmem's People

Contributors

cerus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

sharedmem's Issues

Clean up / improve C++ code

I don't have a lot of experience with C++ so the C++ code is probably pretty awful. Someone with more experience could probably improve or clean up a few things.
Files in question: Everything in this directory

Comment if you want to work on this

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.