array.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. } *priv;
  23. };
  24. void fdarray__init(struct fdarray *fda, int nr_autogrow);
  25. void fdarray__exit(struct fdarray *fda);
  26. struct fdarray *fdarray__new(int nr_alloc, int nr_autogrow);
  27. void fdarray__delete(struct fdarray *fda);
  28. int fdarray__add(struct fdarray *fda, int fd, short revents);
  29. int fdarray__poll(struct fdarray *fda, int timeout);
  30. int fdarray__filter(struct fdarray *fda, short revents,
  31. void (*entry_destructor)(struct fdarray *fda, int fd));
  32. int fdarray__grow(struct fdarray *fda, int extra);
  33. int fdarray__fprintf(struct fdarray *fda, FILE *fp);
  34. static inline int fdarray__available_entries(struct fdarray *fda)
  35. {
  36. return fda->nr_alloc - fda->nr;
  37. }
  38. #endif /* __API_FD_ARRAY__ */