port-creation.scm 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. (define-module (mach port-creation)
  2. #:use-module (mach ffi)
  3. #:use-module (mach types)
  4. #:use-module (mach port-right)
  5. #:use-module (mach task)
  6. #:export (port-allocate-name reply-port port-allocate))
  7. ;;
  8. ;; This file contains functions to deal with port creation.
  9. ;;
  10. (define-ffi ("mach_port_allocate" %mach-port-allocate)
  11. (err identity) ; ???
  12. (task ffi:ipc-space %unwrap-ipc-space)
  13. (right-type ffi:port-right-type %unwrap-port-right-type)
  14. ;; IIUC, this is were the actual reply port is written.
  15. (reply-port-box ffi:pointer identity))
  16. ;; XXX I don't understand what this does exactly.
  17. (define* (port-allocate right-type #:optional (task (task-self)))
  18. ;; XXX mention RIGHT-TYPE, and explain what TASK!=(task-self)
  19. ;; implies.
  20. "Creates a new right in the specified task."
  21. ;; The initial value of the foreign box does not matter.
  22. (let* ((port-box (make-c-struct '(*) %null-pointer))
  23. (return-code
  24. (%mach-port-allocate task right-type port-box)))
  25. ;; XXX what is select-error?
  26. (select-error return-code (dereference-pointer port-box))))
  27. (define-ffi ("mach_reply_port" %mach-reply-port)
  28. (port %wrap-mach-port))
  29. (define (reply-port)
  30. "Creates a reply port in the calling task."
  31. (%mach-reply-port))
  32. (define-ffi ("mach_port_allocate_name" %mach-port-allocate-name)
  33. (err identity)
  34. (task ffi:ipc-space %unwrap-ipc-space)
  35. (right-type ffi:port-right-type %unwrap-port-right-type)
  36. ;; XXX a port name is a port itself?
  37. (name ffi:mach-port %unwrap-mach-port))
  38. (define* (port-allocate-name right-type port-name #:optional (task (task-self)))
  39. ;; XXX and what on failure?
  40. ;; XXX what are port names?
  41. "Creates a new right in the specified task, with a specified name for the new right. name must not already be in use for some right, and it can't be the reserved values nil or :dead. On success the port-name is returned."
  42. (let ((return-code
  43. (%mach-port-allocate-name task right-type port-name)))
  44. (select-error return-code port-name)))