array.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __API_FD_ARRAY__
  3. #define __API_FD_ARRAY__
  4. #include <stdio.h>
  5. struct pollfd;
  6. /**
  7. * struct fdarray: Array of file descriptors
  8. *
  9. * @priv: Per array entry priv area, users should access just its contents,
  10. * not set it to anything, as it is kept in synch with @entries, being
  11. * realloc'ed, * for instance, in fdarray__{grow,filter}.
  12. *
  13. * I.e. using 'fda->priv[N].idx = * value' where N < fda->nr is ok,
  14. * but doing 'fda->priv = malloc(M)' is not allowed.
  15. */
  16. struct fdarray {
  17. int nr;
  18. int nr_alloc;
  19. int nr_autogrow;
  20. struct pollfd *entries;
  21. union {
  22. int idx;
  23. void *ptr;
  24. } *priv;
  25. };
  26. void fdarray__init(struct fdarray *fda, int nr_autogrow);
  27. void fdarray__exit(struct fdarray *fda);
  28. struct fdarray *fdarray__new(int nr_alloc, int nr_autogrow);
  29. void fdarray__delete(struct fdarray *fda);
  30. int fdarray__add(struct fdarray *fda, int fd, short revents);
  31. int fdarray__poll(struct fdarray *fda, int timeout);
  32. int fdarray__filter(struct fdarray *fda, short revents,
  33. void (*entry_destructor)(struct fdarray *fda, int fd, void *arg),
  34. void *arg);
  35. int fdarray__grow(struct fdarray *fda, int extra);
  36. int fdarray__fprintf(struct fdarray *fda, FILE *fp);
  37. static inline int fdarray__available_entries(struct fdarray *fda)
  38. {
  39. return fda->nr_alloc - fda->nr;
  40. }
  41. #endif /* __API_FD_ARRAY__ */