ipc.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2022 Agustina Arzille.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * Definitions for inter-process communication.
  18. */
  19. #ifndef KERN_IPC_H
  20. #define KERN_IPC_H
  21. #include <stdbool.h>
  22. #include <stdint.h>
  23. #include <uio.h>
  24. #include <kern/types.h>
  25. #define IPC_IOV_CACHE_SIZE 8
  26. struct ipc_iov_cache
  27. {
  28. struct iovec iovs[IPC_IOV_CACHE_SIZE];
  29. int idx;
  30. int size;
  31. };
  32. struct ipc_iter
  33. {
  34. struct iovec *iovs;
  35. int cur_iov;
  36. int nr_iovs;
  37. struct iovec cur;
  38. struct ipc_iov_cache cache;
  39. };
  40. static inline void
  41. ipc_iter_set_invalid (struct ipc_iter *it)
  42. {
  43. it->cur_iov = -1;
  44. }
  45. static inline bool
  46. ipc_iter_valid (const struct ipc_iter *it)
  47. {
  48. return (it->cur_iov >= 0);
  49. }
  50. static inline void
  51. ipc_iter_init_buf (struct ipc_iter *it, void *buf, size_t size)
  52. {
  53. it->iovs = NULL;
  54. it->cur_iov = it->nr_iovs = 0;
  55. it->cur.iov_base = buf;
  56. it->cur.iov_len = size;
  57. it->cache.idx = it->cache.size = 0;
  58. if (! size)
  59. ipc_iter_set_invalid (it);
  60. }
  61. static inline size_t
  62. ipc_iter_cur_size (const struct ipc_iter *it)
  63. {
  64. return (it->cur.iov_len);
  65. }
  66. static inline void*
  67. ipc_iter_cur_ptr (const struct ipc_iter *it)
  68. {
  69. return ((void *)it->cur.iov_base);
  70. }
  71. struct thread;
  72. // Initialize an IPC iterator with a number of iovec's.
  73. int ipc_iter_init_iov (struct ipc_iter *it,
  74. struct iovec *iovs, uint32_t nr_iovs);
  75. // Advance an IPC iterator by OFF bytes.
  76. int ipc_iter_adv (struct ipc_iter *it, size_t off);
  77. /* Copy data through iterators. Returns the number of bytes copied,
  78. * or a negative value if there was an error. */
  79. ssize_t ipc_copy_iter (struct ipc_iter *src_it, struct thread *src_thr,
  80. struct ipc_iter *dst_it, struct thread *dst_thr);
  81. #endif