// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build (darwin && !ios) || linux
// +build darwin,!ios linux

package unix

import 

// SysvShmAttach attaches the Sysv shared memory segment associated with the
// shared memory identifier id.
func ( int,  uintptr,  int) ([]byte, error) {
	,  := shmat(, , )
	if  != nil {
		return nil, 
	}

	// Retrieve the size of the shared memory to enable slice creation
	var  SysvShmDesc

	,  := SysvShmCtl(, IPC_STAT, &)
	if  != nil {
		// release the shared memory if we can't find the size

		// ignoring error from shmdt as there's nothing sensible to return here
		shmdt()
		return nil, 
	}

	// Use unsafe to convert addr into a []byte.
	 := unsafe.Slice((*byte)(unsafe.Pointer()), int(.Segsz))
	return , nil
}

// SysvShmDetach unmaps the shared memory slice returned from SysvShmAttach.
//
// It is not safe to use the slice after calling this function.
func ( []byte) error {
	if len() == 0 {
		return EINVAL
	}

	return shmdt(uintptr(unsafe.Pointer(&[0])))
}

// SysvShmGet returns the Sysv shared memory identifier associated with key.
// If the IPC_CREAT flag is specified a new segment is created.
func (, ,  int) ( int,  error) {
	return shmget(, , )
}