array.h 1.3 KB

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