v0.0.4 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Dec 12, 2019 License: LGPL-2.1 Imports: 5 Imported by: 1



Shared memory is an inter-process communication mechanism that allows for multiple, independent processes to access and modify the same portion of system memory for the purpose of sharing data between them. This library implements a Golang wrapper around the original implementation of this which is present on almost all *NIX systems that implement portions of the UNIX System V feature set.

The use of the calls implemented by this library has largely been supplanted by POSIX shared memory ( and the mmap() system call, but there are some use cases that still require this particular approach to shared memory management. One notable example is the MIT-SHM X Server extension ( which expects SysV shared memory semantics.



View Source
const (
	IpcNone                        = 0
	IpcCreate    SharedMemoryFlags = C.IPC_CREAT
	IpcExclusive                   = C.IPC_EXCL
	HugePages                      = C.SHM_HUGETLB
	NoReserve                      = C.SHM_NORESERVE
View Source
const Version = `0.0.4`


This section is empty.


func DestroySegment

func DestroySegment(id int) error

Destroy a shared memory segment by its ID


type Segment

type Segment struct {
	Id   int
	Size int64
	// contains filtered or unexported fields

A native representation of a SysV shared memory segment

func Create

func Create(size int) (*Segment, error)

Create a new shared memory segment with the given size (in bytes). The system will automatically round the size up to the nearest memory page boundary (typically 4KB).

func Open

func Open(id int) (*Segment, error)

Open an existing shared memory segment located at the given ID. This ID is returned in the struct that is populated by Create(), or by the shmget() system call.

func OpenSegment

func OpenSegment(size int, flags SharedMemoryFlags, perms os.FileMode) (*Segment, error)

Creates a shared memory segment of a given size, and also allows for the specification of creation flags supported by the shmget() call, as well as specifying permissions.

func (*Segment) Attach

func (self *Segment) Attach() (unsafe.Pointer, error)

Attaches the segment to the current processes resident memory. The pointer that is returned is the actual memory address of the shared memory segment for use with third party libraries that can directly read from memory.

func (*Segment) Destroy

func (self *Segment) Destroy() error

Destroys the current shared memory segment.

func (*Segment) Detach

func (self *Segment) Detach(addr unsafe.Pointer) error

Detaches the segment from the current processes memory space.

func (*Segment) Position

func (self *Segment) Position() int64

Returns the current position of the Read/Write pointer.

func (*Segment) Read

func (self *Segment) Read(p []byte) (n int, err error)

Implements the io.Reader interface for shared memory

func (*Segment) ReadChunk

func (self *Segment) ReadChunk(length int64, start int64) ([]byte, error)

Read some or all of the shared memory segment and return a byte slice.

func (*Segment) Reset

func (self *Segment) Reset()

Resets the internal offset counter for this segment, allowing subsequent calls to Read() or Write() to start from the beginning.

func (*Segment) Seek

func (self *Segment) Seek(offset int64, whence int) (int64, error)

Implements the io.Seeker interface for shared memory. Subsequent calls to Read() or Write() will start from this position.

func (*Segment) Write

func (self *Segment) Write(p []byte) (n int, err error)

Implements the io.Writer interface for shared memory

type SharedMemoryFlags

type SharedMemoryFlags int

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL