https://github.com/cerus/sharedmem
Shared memory for Java
https://github.com/cerus/sharedmem
cpp hacktoberfest java java-native-interface memory-mapped-file shared-memory
Last synced: 9 months ago
JSON representation
Shared memory for Java
- Host: GitHub
- URL: https://github.com/cerus/sharedmem
- Owner: cerus
- License: mit
- Created: 2021-08-11T18:04:58.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-04-15T22:07:47.000Z (over 2 years ago)
- Last Synced: 2025-04-11T00:52:44.715Z (9 months ago)
- Topics: cpp, hacktoberfest, java, java-native-interface, memory-mapped-file, shared-memory
- Language: Java
- Homepage:
- Size: 106 KB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE.txt
Awesome Lists containing this project
README
███████ ██ ██ █████ ██████ ███████ ██████ ███ ███ ███████ ███ ███
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ████ ██ ████ ████
███████ ███████ ███████ ██████ █████ ██ ██ ██ ████ ██ █████ ██ ████ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
███████ ██ ██ ██ ██ ██ ██ ███████ ██████ ██ ██ ███████ ██ ██
Shared memory for Java
## 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](https://www.boost.org/doc/libs/1_76_0/doc/html/interprocess.html) 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:
```xml
dev.cerus
sharedmem
1.1.0
```
## Usage
[Javadocs](https://cerus.dev/api/sharedmem)
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
```java
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](LICENSE.txt).
Thirdparty licenses: [Boost](BOOST_LICENSE.txt)