1
0

array.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2014, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
  4. */
  5. #include "array.h"
  6. #include <errno.h>
  7. #include <fcntl.h>
  8. #include <poll.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #include <string.h>
  12. void fdarray__init(struct fdarray *fda, int nr_autogrow)
  13. {
  14. fda->entries = NULL;
  15. fda->priv = NULL;
  16. fda->nr = fda->nr_alloc = 0;
  17. fda->nr_autogrow = nr_autogrow;
  18. }
  19. int fdarray__grow(struct fdarray *fda, int nr)
  20. {
  21. struct priv *priv;
  22. int nr_alloc = fda->nr_alloc + nr;
  23. size_t psize = sizeof(fda->priv[0]) * nr_alloc;
  24. size_t size = sizeof(struct pollfd) * nr_alloc;
  25. struct pollfd *entries = realloc(fda->entries, size);
  26. if (entries == NULL)
  27. return -ENOMEM;
  28. priv = realloc(fda->priv, psize);
  29. if (priv == NULL) {
  30. free(entries);
  31. return -ENOMEM;
  32. }
  33. memset(&entries[fda->nr_alloc], 0, sizeof(struct pollfd) * nr);
  34. memset(&priv[fda->nr_alloc], 0, sizeof(fda->priv[0]) * nr);
  35. fda->nr_alloc = nr_alloc;
  36. fda->entries = entries;
  37. fda->priv = priv;
  38. return 0;
  39. }
  40. struct fdarray *fdarray__new(int nr_alloc, int nr_autogrow)
  41. {
  42. struct fdarray *fda = calloc(1, sizeof(*fda));
  43. if (fda != NULL) {
  44. if (fdarray__grow(fda, nr_alloc)) {
  45. free(fda);
  46. fda = NULL;
  47. } else {
  48. fda->nr_autogrow = nr_autogrow;
  49. }
  50. }
  51. return fda;
  52. }
  53. void fdarray__exit(struct fdarray *fda)
  54. {
  55. free(fda->entries);
  56. free(fda->priv);
  57. fdarray__init(fda, 0);
  58. }
  59. void fdarray__delete(struct fdarray *fda)
  60. {
  61. fdarray__exit(fda);
  62. free(fda);
  63. }
  64. int fdarray__add(struct fdarray *fda, int fd, short revents, enum fdarray_flags flags)
  65. {
  66. int pos = fda->nr;
  67. if (fda->nr == fda->nr_alloc &&
  68. fdarray__grow(fda, fda->nr_autogrow) < 0)
  69. return -ENOMEM;
  70. fda->entries[fda->nr].fd = fd;
  71. fda->entries[fda->nr].events = revents;
  72. fda->priv[fda->nr].flags = flags;
  73. fda->nr++;
  74. return pos;
  75. }
  76. int fdarray__dup_entry_from(struct fdarray *fda, int pos, struct fdarray *from)
  77. {
  78. struct pollfd *entry;
  79. int npos;
  80. if (pos >= from->nr)
  81. return -EINVAL;
  82. entry = &from->entries[pos];
  83. npos = fdarray__add(fda, entry->fd, entry->events, from->priv[pos].flags);
  84. if (npos >= 0)
  85. fda->priv[npos] = from->priv[pos];
  86. return npos;
  87. }
  88. int fdarray__filter(struct fdarray *fda, short revents,
  89. void (*entry_destructor)(struct fdarray *fda, int fd, void *arg),
  90. void *arg)
  91. {
  92. int fd, nr = 0;
  93. if (fda->nr == 0)
  94. return 0;
  95. for (fd = 0; fd < fda->nr; ++fd) {
  96. if (!fda->entries[fd].events)
  97. continue;
  98. if (fda->entries[fd].revents & revents) {
  99. if (entry_destructor)
  100. entry_destructor(fda, fd, arg);
  101. fda->entries[fd].revents = fda->entries[fd].events = 0;
  102. continue;
  103. }
  104. if (!(fda->priv[fd].flags & fdarray_flag__nonfilterable))
  105. ++nr;
  106. }
  107. return nr;
  108. }
  109. int fdarray__poll(struct fdarray *fda, int timeout)
  110. {
  111. return poll(fda->entries, fda->nr, timeout);
  112. }
  113. int fdarray__fprintf(struct fdarray *fda, FILE *fp)
  114. {
  115. int fd, printed = fprintf(fp, "%d [ ", fda->nr);
  116. for (fd = 0; fd < fda->nr; ++fd)
  117. printed += fprintf(fp, "%s%d", fd ? ", " : "", fda->entries[fd].fd);
  118. return printed + fprintf(fp, " ]");
  119. }