pollfd.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /** @file pollfd.c
  2. */
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <assert.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10. #include <sys/types.h>
  11. #include <poll.h>
  12. #include "faux/faux.h"
  13. #include "faux/net.h"
  14. #include "faux/vec.h"
  15. #include "private.h"
  16. /** @brief Function to search specified fd within pollfd structures.
  17. */
  18. static int cmp_by_fd(const void *key, const void *item)
  19. {
  20. int k = *(int *)key;
  21. struct pollfd *i = (struct pollfd *)item;
  22. if (k == i->fd)
  23. return 0;
  24. return -1;
  25. }
  26. /** @brief Allocates memory for faux_pollfd_t object.
  27. *
  28. * @return Allocated faux_pollfd_t object or NULL on error.
  29. */
  30. faux_pollfd_t *faux_pollfd_new(void)
  31. {
  32. faux_pollfd_t *faux_pollfd = NULL;
  33. faux_pollfd = faux_zmalloc(sizeof(*faux_pollfd));
  34. assert(faux_pollfd);
  35. if (!faux_pollfd)
  36. return NULL;
  37. faux_pollfd->vec = faux_vec_new(sizeof(struct pollfd), cmp_by_fd);
  38. return faux_pollfd;
  39. }
  40. /** @brief Frees previously allocated faux_pollfd_t object.
  41. *
  42. * @param [in] faux_pollfd Allocated faux_pollfd_t object.
  43. */
  44. void faux_pollfd_free(faux_pollfd_t *faux_pollfd)
  45. {
  46. if (!faux_pollfd)
  47. return;
  48. faux_vec_free(faux_pollfd->vec);
  49. faux_free(faux_pollfd);
  50. }
  51. struct pollfd *faux_pollfd_vector(faux_pollfd_t *faux_pollfd)
  52. {
  53. assert(faux_pollfd);
  54. if (!faux_pollfd)
  55. return NULL;
  56. return faux_vec_data(faux_pollfd->vec);
  57. }
  58. size_t faux_pollfd_len(faux_pollfd_t *faux_pollfd)
  59. {
  60. assert(faux_pollfd);
  61. if (!faux_pollfd)
  62. return 0;
  63. return faux_vec_len(faux_pollfd->vec);
  64. }
  65. struct pollfd *faux_pollfd_item(faux_pollfd_t *faux_pollfd, unsigned int index)
  66. {
  67. assert(faux_pollfd);
  68. if (!faux_pollfd)
  69. return NULL;
  70. return (struct pollfd *)faux_vec_item(faux_pollfd->vec, index);
  71. }
  72. struct pollfd *faux_pollfd_find(faux_pollfd_t *faux_pollfd, int fd)
  73. {
  74. int index = 0;
  75. assert(faux_pollfd);
  76. if (!faux_pollfd)
  77. return NULL;
  78. assert(fd >= 0);
  79. if (fd < 0)
  80. return NULL;
  81. index = faux_vec_find(faux_pollfd->vec, &fd, 0);
  82. if (index < 0)
  83. return NULL;
  84. return (struct pollfd *)faux_vec_item(faux_pollfd->vec, index);
  85. }
  86. struct pollfd *faux_pollfd_add(faux_pollfd_t *faux_pollfd, int fd)
  87. {
  88. struct pollfd *pollfd = NULL;
  89. assert(faux_pollfd);
  90. if (!faux_pollfd)
  91. return NULL;
  92. assert(fd >= 0);
  93. if (fd < 0)
  94. return NULL;
  95. // Don't add duplicate fd
  96. pollfd = faux_pollfd_find(faux_pollfd, fd);
  97. if (pollfd)
  98. return pollfd;
  99. // Create new item
  100. pollfd = faux_vec_add(faux_pollfd->vec);
  101. assert(pollfd);
  102. if (!pollfd)
  103. return NULL;
  104. pollfd->fd = fd;
  105. return pollfd;
  106. }
  107. int faux_pollfd_del_by_fd(faux_pollfd_t *faux_pollfd, int fd)
  108. {
  109. int index = 0;
  110. assert(faux_pollfd);
  111. if (!faux_pollfd)
  112. return -1;
  113. assert(fd >= 0);
  114. if (fd < 0)
  115. return -1;
  116. index = faux_vec_find(faux_pollfd->vec, &fd, 0);
  117. if (index < 0) // Not found
  118. return -1;
  119. return faux_vec_del(faux_pollfd->vec, index);
  120. }
  121. int faux_pollfd_del_by_index(faux_pollfd_t *faux_pollfd, unsigned int index)
  122. {
  123. assert(faux_pollfd);
  124. if (!faux_pollfd)
  125. return -1;
  126. return faux_vec_del(faux_pollfd->vec, index);
  127. }
  128. void faux_pollfd_init_iterator(faux_pollfd_t *faux_pollfd, faux_pollfd_iterator_t *iterator)
  129. {
  130. assert(faux_pollfd);
  131. if (!faux_pollfd)
  132. return;
  133. if (!iterator)
  134. return;
  135. *iterator = 0;
  136. }
  137. struct pollfd *faux_pollfd_each(faux_pollfd_t *faux_pollfd, faux_pollfd_iterator_t *iterator)
  138. {
  139. unsigned int old_iterator = 0;
  140. assert(faux_pollfd);
  141. if (!faux_pollfd)
  142. return NULL;
  143. if (!iterator)
  144. return NULL;
  145. old_iterator = *iterator;
  146. (*iterator)++;
  147. return faux_pollfd_item(faux_pollfd, old_iterator);
  148. }
  149. struct pollfd *faux_pollfd_each_active(faux_pollfd_t *faux_pollfd, faux_pollfd_iterator_t *iterator)
  150. {
  151. struct pollfd *pollfd = NULL;
  152. assert(faux_pollfd);
  153. if (!faux_pollfd)
  154. return NULL;
  155. if (!iterator)
  156. return NULL;
  157. while ((pollfd = faux_pollfd_each(faux_pollfd, iterator))) {
  158. if (pollfd->revents != 0)
  159. return pollfd;
  160. }
  161. return NULL;
  162. }