github.com/as/shiny@v0.8.2/driver/x11driver/shm_shmopen_syscall.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build linux dragonfly
     6  // +build amd64 arm arm64 mips64 mips64le
     7  
     8  package x11driver
     9  
    10  import (
    11  	"fmt"
    12  	"syscall"
    13  	"unsafe"
    14  )
    15  
    16  // These constants are from /usr/include/linux/ipc.h
    17  const (
    18  	ipcPrivate = 0
    19  	ipcRmID    = 0
    20  )
    21  
    22  func shmOpen(size int) (shmid uintptr, addr unsafe.Pointer, err error) {
    23  	shmid, _, errno0 := syscall.RawSyscall(syscall.SYS_SHMGET, ipcPrivate, uintptr(size), 0600)
    24  	if errno0 != 0 {
    25  		return 0, unsafe.Pointer(uintptr(0)), fmt.Errorf("shmget: %v", errno0)
    26  	}
    27  	p, _, errno1 := syscall.RawSyscall(syscall.SYS_SHMAT, shmid, 0, 0)
    28  	_, _, errno2 := syscall.RawSyscall(syscall.SYS_SHMCTL, shmid, ipcRmID, 0)
    29  	if errno1 != 0 {
    30  		return 0, unsafe.Pointer(uintptr(0)), fmt.Errorf("shmat: %v", errno1)
    31  	}
    32  	if errno2 != 0 {
    33  		return 0, unsafe.Pointer(uintptr(0)), fmt.Errorf("shmctl: %v", errno2)
    34  	}
    35  	return shmid, unsafe.Pointer(p), nil
    36  }
    37  
    38  func shmClose(p unsafe.Pointer) error {
    39  	_, _, errno := syscall.RawSyscall(syscall.SYS_SHMDT, uintptr(p), 0, 0)
    40  	if errno != 0 {
    41  		return fmt.Errorf("shmdt: %v", errno)
    42  	}
    43  	return nil
    44  }