Exemplo n.º 1
0
func Open(name string, flag int, perm os.FileMode) (o Object, err error) {
	cname := C.CString(name)
	defer C.free(unsafe.Pointer(cname))

	fd, err := C.shm_open(cname, C.int(flag), C.mode_t(perm))
	if err != nil {
		return o, err
	}

	return Object{File: os.NewFile(uintptr(fd), name), name: name}, nil
}
Exemplo n.º 2
0
func shm_open(name string, flag int) (uintptr, error) {
	cname := C.CString(name)
	defer C.free(unsafe.Pointer(cname))

	fd, err := C.shm_open(cname, C.int(flag), 0666)
	if err != nil {
		return 0, err
	}

	return uintptr(fd), nil
}
Exemplo n.º 3
0
Arquivo: shm.go Projeto: tcsc/squaddie
// Creates a new region of shared memory by creating and mapping in a new
// shm object. The returned region "owns" the shm object, and will mark it for
// unlinking when the region is closed.
func NewRegion(name string, length int) (result region, err error) {
	cname := C.CString(name)
	defer C.free(unsafe.Pointer(cname))
	fd, err := C.shm_open(
		cname,
		C.int(os.O_CREATE|os.O_EXCL|os.O_RDWR),
		0600)
	if err != nil {
		return
	}

	file := os.NewFile(uintptr(fd), name)
	defer func() {
		if err != nil {
			file.Close()
		}
	}()

	err = syscall.Ftruncate(int(fd), int64(length))
	if err != nil {
		return
	}

	buf, err := syscall.Mmap(int(fd), 0, length,
		syscall.PROT_READ|syscall.PROT_WRITE,
		syscall.MAP_SHARED)
	if err != nil {
		return
	}

	result = region{
		bytes:  buf,
		fd:     file,
		name:   name,
		unlink: true}
	return
}
Exemplo n.º 4
0
Arquivo: shm.go Projeto: tcsc/squaddie
// Creates a new region instance by attaching to an existing shm object
// and mapping it into the process address space. This region does not "own"
// the underlying shm object, and will not call shm_unlink on it when the
// region is closed.
func OpenRegion(name string) (result region, err error) {
	cname := C.CString(name)
	defer C.free(unsafe.Pointer(cname))
	fd, err := C.shm_open(cname, C.int(os.O_RDWR), 0)
	if err != nil {
		return
	}

	file := os.NewFile(uintptr(fd), name)
	defer func() {
		if err != nil {
			file.Close()
		}
	}()

	shmInfo, err := file.Stat()
	if err != nil {
		rpcLog.Error("Failed to stat the shared memory block: %s", err.Error())
		return
	}

	buf, err := syscall.Mmap(int(fd), 0, int(shmInfo.Size()),
		syscall.PROT_READ|syscall.PROT_WRITE,
		syscall.MAP_SHARED)
	if err != nil {
		return
	}

	result = region{
		bytes:  buf,
		fd:     file,
		name:   name,
		unlink: false,
	}

	return
}