array.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. struct priv {
  22. union {
  23. int idx;
  24. void *ptr;
  25. };
  26. unsigned int flags;
  27. } *priv;
  28. };
  29. enum fdarray_flags {
  30. fdarray_flag__default = 0x00000000,
  31. fdarray_flag__nonfilterable = 0x00000001,
  32. fdarray_flag__non_perf_event = 0x00000002,
  33. };
  34. void fdarray__init(struct fdarray *fda, int nr_autogrow);
  35. void fdarray__exit(struct fdarray *fda);
  36. struct fdarray *fdarray__new(int nr_alloc, int nr_autogrow);
  37. void fdarray__delete(struct fdarray *fda);
  38. int fdarray__add(struct fdarray *fda, int fd, short revents, enum fdarray_flags flags);
  39. int fdarray__dup_entry_from(struct fdarray *fda, int pos, struct fdarray *from);
  40. int fdarray__poll(struct fdarray *fda, int timeout);
  41. int fdarray__filter(struct fdarray *fda, short revents,
  42. void (*entry_destructor)(struct fdarray *fda, int fd, void *arg),
  43. void *arg);
  44. int fdarray__grow(struct fdarray *fda, int extra);
  45. int fdarray__fprintf(struct fdarray *fda, FILE *fp);
  46. static inline int fdarray__available_entries(struct fdarray *fda)
  47. {
  48. return fda->nr_alloc - fda->nr;
  49. }
  50. #endif /* __API_FD_ARRAY__ */