github.com/jmigpin/editor@v1.6.0/driver/xdriver/wimage/shmopen.go (about)

     1  //go:build !windows
     2  
     3  package wimage
     4  
     5  import (
     6  	"fmt"
     7  
     8  	"golang.org/x/sys/unix"
     9  )
    10  
    11  // These constants are from /usr/include/linux/ipc.h
    12  const (
    13  	ipcPrivate = 0
    14  	ipcRmID    = 0
    15  )
    16  
    17  func ShmOpen(size int) (shmid, addr uintptr, err error) {
    18  	shmid, _, errno0 := unix.Syscall(unix.SYS_SHMGET, ipcPrivate, uintptr(size), 0600)
    19  	if errno0 != 0 {
    20  		return 0, 0, fmt.Errorf("shmget: %v", errno0)
    21  	}
    22  	p, _, errno1 := unix.Syscall(unix.SYS_SHMAT, shmid, 0, 0)
    23  	if errno1 != 0 {
    24  		return 0, 0, fmt.Errorf("shmat: %v", errno1)
    25  	}
    26  	return shmid, p, nil
    27  }
    28  
    29  func ShmClose(shmid, addr uintptr) error {
    30  	_, _, errno := unix.Syscall(unix.SYS_SHMDT, addr, 0, 0)
    31  	_, _, errno2 := unix.Syscall(unix.SYS_SHMCTL, shmid, ipcRmID, 0)
    32  	if errno != 0 {
    33  		return fmt.Errorf("shmdt: %v", errno)
    34  	}
    35  	if errno2 != 0 {
    36  		return fmt.Errorf("shmctl: %v", errno2)
    37  	}
    38  	return nil
    39  }