port-names.lisp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. (defcfun ("mach_port_type" %mach-port-type)
  2. err
  3. (task ipc-space)
  4. (name port)
  5. (ptype :pointer))
  6. (defun port-type (port &optional (task (task-self)))
  7. "Return the characteristics of the target port name."
  8. (with-foreign-object (ptype 'port-type-t)
  9. (let ((return-code
  10. (%mach-port-type task port ptype)))
  11. (select-error return-code (mem-ref ptype 'port-type-t)))))
  12. (defcfun ("mach_port_names" %mach-port-names)
  13. err
  14. (task ipc-space)
  15. (names :pointer)
  16. (ncount :pointer)
  17. (types :pointer)
  18. (tcount :pointer))
  19. (defun port-names (&optional (task (task-self)))
  20. "Get a list with port names and the associated port type for the port names in task."
  21. (with-foreign-pointer (names (foreign-type-size :pointer))
  22. (with-foreign-pointer (ncount (foreign-type-size :pointer))
  23. (with-foreign-pointer (types (foreign-type-size :pointer))
  24. (with-foreign-pointer (tcount (foreign-type-size :pointer))
  25. (let ((return-code
  26. (%mach-port-names task
  27. names
  28. ncount
  29. types
  30. tcount)))
  31. (select-error return-code
  32. (let ((names-list (%port-names-to-list names ncount))
  33. (types-list (%port-types-to-list types tcount)))
  34. ;; The GNU Mach reference manual says we
  35. ;; should free the newly allocated memory
  36. (munmap (mem-ref names :pointer)
  37. (* (mem-ref ncount 'msg-type-number)
  38. (foreign-type-size 'port)))
  39. (munmap (mem-ref types :pointer)
  40. (* (mem-ref tcount 'msg-type-number)
  41. (foreign-type-size 'port-type-t)))
  42. (mapcar cons
  43. names-list types-list)))))))))
  44. (defun %port-names-to-list (names-addr ncount)
  45. "Transforms a foreign array of port names into a list."
  46. (loop for i from 0 below (mem-ref ncount 'msg-type-number)
  47. collect (mem-aref names-addr 'port i)))
  48. (defun %port-types-to-list (types-addr tcount)
  49. "Transforms a foreign array of port types into a list."
  50. (loop for i from 0 below (mem-ref tcount 'msg-type-number)
  51. collect (mem-aref types-addr 'port-type-t i)))
  52. (defcfun ("mach_port_rename" %mach-port-rename)
  53. err
  54. (task ipc-space)
  55. (old port)
  56. (new port))
  57. (defun port-rename (old-name new-name &optional (task (task-self)))
  58. "Renames the port name 'old-name' to 'new-name'. Returns 'new-name' on success, nil otherwise."
  59. (let ((return-code
  60. (%mach-port-rename task old-name new-name)))
  61. (select-error return-code
  62. new-name)))