Skip to content

Shared memory manager handling a list of non-persistent memory regions provided over abstract unix sockets

License

Notifications You must be signed in to change notification settings

mertemba/shm_manager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ShmManager

Shared memory manager handling a list of non-persistent memory regions. Clients can request new or existing memory regions based on a string index, which are provided by the manager as file descriptors sent over abstract unix sockets.

No persistent data structures or filesystem features are used. Thus, when all involved processes exited (or died), the shared memory regions (and any leftover sockets) are automatically cleaned up by the kernel (manual release is also possible).

Requirements: C++17, Linux kernel >=3.17 (for memfd_create).

Usage examples

C++

Manager:

#include <shm_manager.hpp>
int main() {
    shm_manager::ShmManager().run();
    return 0;
}

Client 1:

#include <shm_manager.hpp>
int main() {
  auto client = shm_manager::ShmClient::create("my_buffer", 1024);
  client.map_fd();
  *(int*)client.get_addr() = 42;
  return 0;
}

Client 2:

#include <shm_manager.hpp>
int main() {
  auto client = shm_manager::ShmClient::get("my_buffer");
  // remove reference at manager:
  shm_manager::ShmClient::remove("my_buffer");
  client.map_fd();
  int data = *(int*)client.get_addr();
  std::cout << "client got data " << data << std::endl;
  return 0;
}

Python

See examples/example.py.

This library integrates nicely with structstore:

client = shm_manager.ShmClient.create('my_buffer', 2048)
store = stst.StructStoreShared(client.fd, init=True)
store.foo = 'bar'

For a complete example, see examples/example_structstore.py.

Implementation details

Non-persistent shared memory regions are created using memfd_create. File descriptors are sent to clients over abstract unix sockets using SCM_RIGHTS.

License

This repo is released under the BSD 3-Clause License. See LICENSE file for details.

About

Shared memory manager handling a list of non-persistent memory regions provided over abstract unix sockets

Resources

License

Stars

Watchers

Forks