You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

62 lines
1.6 KiB

  1. // Copyright 2021 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. //go:build (darwin && !ios) || linux
  5. // +build darwin,!ios linux
  6. package unix
  7. import (
  8. "unsafe"
  9. "golang.org/x/sys/internal/unsafeheader"
  10. )
  11. // SysvShmAttach attaches the Sysv shared memory segment associated with the
  12. // shared memory identifier id.
  13. func SysvShmAttach(id int, addr uintptr, flag int) ([]byte, error) {
  14. addr, errno := shmat(id, addr, flag)
  15. if errno != nil {
  16. return nil, errno
  17. }
  18. // Retrieve the size of the shared memory to enable slice creation
  19. var info SysvShmDesc
  20. _, err := SysvShmCtl(id, IPC_STAT, &info)
  21. if err != nil {
  22. // release the shared memory if we can't find the size
  23. // ignoring error from shmdt as there's nothing sensible to return here
  24. shmdt(addr)
  25. return nil, err
  26. }
  27. // Use unsafe to convert addr into a []byte.
  28. // TODO: convert to unsafe.Slice once we can assume Go 1.17
  29. var b []byte
  30. hdr := (*unsafeheader.Slice)(unsafe.Pointer(&b))
  31. hdr.Data = unsafe.Pointer(addr)
  32. hdr.Cap = int(info.Segsz)
  33. hdr.Len = int(info.Segsz)
  34. return b, nil
  35. }
  36. // SysvShmDetach unmaps the shared memory slice returned from SysvShmAttach.
  37. //
  38. // It is not safe to use the slice after calling this function.
  39. func SysvShmDetach(data []byte) error {
  40. if len(data) == 0 {
  41. return EINVAL
  42. }
  43. return shmdt(uintptr(unsafe.Pointer(&data[0])))
  44. }
  45. // SysvShmGet returns the Sysv shared memory identifier associated with key.
  46. // If the IPC_CREAT flag is specified a new segment is created.
  47. func SysvShmGet(key, size, flag int) (id int, err error) {
  48. return shmget(key, size, flag)
  49. }