Skip to content

eslizn/shm

Repository files navigation

Go Shared Memory Library (shm)

A cross-platform Go library for creating and managing shared memory objects.

Features

  • Cross-platform support (Linux, Unix, Windows)

Installation

go get github.com/eslizn/shm

Quick Start

package main

import (
	"fmt"
	"github.com/eslizn/shm"
)

type Data struct {
	Value int
}

func main() {
	// Create a new shared memory object
	obj, err := shm.New[Data]()
	if err != nil {
		panic(err)
	}
	defer obj.Close()

	// Access the shared data
	obj.Value = 42
	fmt.Println("Value:", obj.Value)

	// Reset the memory to zero values
	shm.Memset(obj)
	if err != nil {
		panic(err)
	}
}

API Reference

func New[T any](opts ...Option) (*T, error)

Creates or opens a shared memory object of type T.

Options:

  • WithName(name string) - Specifies a custom name for the shared memory mapping file
  • WithFinder(finder Finder) - Specifies a custom file finder

func Memset[T any](p *T)

Resets the shared memory object to its zero value.

func Close[T any](p *T) error

Releases the shared memory object.

func Sizeof(in any) (int, error)

Returns the size in bytes of type T.

Platform Support

  • Linux: Uses /dev/shm directory
  • Unix: Uses system temp directory
  • Windows: Uses Windows file mapping API

Configuration

You can customize the shared memory location:

// Custom name
obj, err := shm.New[Data](shm.WithName("my-shared-data"))

// Custom finder
finder := func(name string) string {
    return filepath.Join("/custom/path", name)
}
obj, err := shm.New[Data](shm.WithFinder(finder))

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published