Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

53 строки
1.4 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 "unsafe"
  8. // SysvShmAttach attaches the Sysv shared memory segment associated with the
  9. // shared memory identifier id.
  10. func SysvShmAttach(id int, addr uintptr, flag int) ([]byte, error) {
  11. addr, errno := shmat(id, addr, flag)
  12. if errno != nil {
  13. return nil, errno
  14. }
  15. // Retrieve the size of the shared memory to enable slice creation
  16. var info SysvShmDesc
  17. _, err := SysvShmCtl(id, IPC_STAT, &info)
  18. if err != nil {
  19. // release the shared memory if we can't find the size
  20. // ignoring error from shmdt as there's nothing sensible to return here
  21. shmdt(addr)
  22. return nil, err
  23. }
  24. // Use unsafe to convert addr into a []byte.
  25. b := unsafe.Slice((*byte)(unsafe.Pointer(addr)), int(info.Segsz))
  26. return b, nil
  27. }
  28. // SysvShmDetach unmaps the shared memory slice returned from SysvShmAttach.
  29. //
  30. // It is not safe to use the slice after calling this function.
  31. func SysvShmDetach(data []byte) error {
  32. if len(data) == 0 {
  33. return EINVAL
  34. }
  35. return shmdt(uintptr(unsafe.Pointer(&data[0])))
  36. }
  37. // SysvShmGet returns the Sysv shared memory identifier associated with key.
  38. // If the IPC_CREAT flag is specified a new segment is created.
  39. func SysvShmGet(key, size, flag int) (id int, err error) {
  40. return shmget(key, size, flag)
  41. }