array.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (C) 2014, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
  3. *
  4. * Released under the GPL v2. (and only v2, not any later version)
  5. */
  6. #include "array.h"
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <poll.h>
  10. #include <stdlib.h>
  11. #include <unistd.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. void *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. fda->nr_alloc = nr_alloc;
  34. fda->entries = entries;
  35. fda->priv = priv;
  36. return 0;
  37. }
  38. struct fdarray *fdarray__new(int nr_alloc, int nr_autogrow)
  39. {
  40. struct fdarray *fda = calloc(1, sizeof(*fda));
  41. if (fda != NULL) {
  42. if (fdarray__grow(fda, nr_alloc)) {
  43. free(fda);
  44. fda = NULL;
  45. } else {
  46. fda->nr_autogrow = nr_autogrow;
  47. }
  48. }
  49. return fda;
  50. }
  51. void fdarray__exit(struct fdarray *fda)
  52. {
  53. free(fda->entries);
  54. free(fda->priv);
  55. fdarray__init(fda, 0);
  56. }
  57. void fdarray__delete(struct fdarray *fda)
  58. {
  59. fdarray__exit(fda);
  60. free(fda);
  61. }
  62. int fdarray__add(struct fdarray *fda, int fd, short revents)
  63. {
  64. int pos = fda->nr;
  65. if (fda->nr == fda->nr_alloc &&
  66. fdarray__grow(fda, fda->nr_autogrow) < 0)
  67. return -ENOMEM;
  68. fda->entries[fda->nr].fd = fd;
  69. fda->entries[fda->nr].events = revents;
  70. fda->nr++;
  71. return pos;
  72. }
  73. int fdarray__filter(struct fdarray *fda, short revents,
  74. void (*entry_destructor)(struct fdarray *fda, int fd))
  75. {
  76. int fd, nr = 0;
  77. if (fda->nr == 0)
  78. return 0;
  79. for (fd = 0; fd < fda->nr; ++fd) {
  80. if (fda->entries[fd].revents & revents) {
  81. if (entry_destructor)
  82. entry_destructor(fda, fd);
  83. continue;
  84. }
  85. if (fd != nr) {
  86. fda->entries[nr] = fda->entries[fd];
  87. fda->priv[nr] = fda->priv[fd];
  88. }
  89. ++nr;
  90. }
  91. return fda->nr = nr;
  92. }
  93. int fdarray__poll(struct fdarray *fda, int timeout)
  94. {
  95. return poll(fda->entries, fda->nr, timeout);
  96. }
  97. int fdarray__fprintf(struct fdarray *fda, FILE *fp)
  98. {
  99. int fd, printed = fprintf(fp, "%d [ ", fda->nr);
  100. for (fd = 0; fd < fda->nr; ++fd)
  101. printed += fprintf(fp, "%s%d", fd ? ", " : "", fda->entries[fd].fd);
  102. return printed + fprintf(fp, " ]");
  103. }