1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21b85337dSArnaldo Carvalho de Melo #ifndef __API_FD_ARRAY__
31b85337dSArnaldo Carvalho de Melo #define __API_FD_ARRAY__
41b85337dSArnaldo Carvalho de Melo
51b85337dSArnaldo Carvalho de Melo #include <stdio.h>
61b85337dSArnaldo Carvalho de Melo
71b85337dSArnaldo Carvalho de Melo struct pollfd;
81b85337dSArnaldo Carvalho de Melo
92171a925SArnaldo Carvalho de Melo /**
102171a925SArnaldo Carvalho de Melo * struct fdarray: Array of file descriptors
112171a925SArnaldo Carvalho de Melo *
122171a925SArnaldo Carvalho de Melo * @priv: Per array entry priv area, users should access just its contents,
132171a925SArnaldo Carvalho de Melo * not set it to anything, as it is kept in synch with @entries, being
142171a925SArnaldo Carvalho de Melo * realloc'ed, * for instance, in fdarray__{grow,filter}.
152171a925SArnaldo Carvalho de Melo *
162171a925SArnaldo Carvalho de Melo * I.e. using 'fda->priv[N].idx = * value' where N < fda->nr is ok,
172171a925SArnaldo Carvalho de Melo * but doing 'fda->priv = malloc(M)' is not allowed.
182171a925SArnaldo Carvalho de Melo */
191b85337dSArnaldo Carvalho de Melo struct fdarray {
201b85337dSArnaldo Carvalho de Melo int nr;
211b85337dSArnaldo Carvalho de Melo int nr_alloc;
221b85337dSArnaldo Carvalho de Melo int nr_autogrow;
231b85337dSArnaldo Carvalho de Melo struct pollfd *entries;
24ab4c1f9fSAlexey Budankov struct priv {
252171a925SArnaldo Carvalho de Melo union {
262171a925SArnaldo Carvalho de Melo int idx;
272b438347SWang Nan void *ptr;
28ab4c1f9fSAlexey Budankov };
29ab4c1f9fSAlexey Budankov unsigned int flags;
302171a925SArnaldo Carvalho de Melo } *priv;
311b85337dSArnaldo Carvalho de Melo };
321b85337dSArnaldo Carvalho de Melo
33ab4c1f9fSAlexey Budankov enum fdarray_flags {
34ab4c1f9fSAlexey Budankov fdarray_flag__default = 0x00000000,
35*6562c9acSAdrian Hunter fdarray_flag__nonfilterable = 0x00000001,
36*6562c9acSAdrian Hunter fdarray_flag__non_perf_event = 0x00000002,
37ab4c1f9fSAlexey Budankov };
38ab4c1f9fSAlexey Budankov
391b85337dSArnaldo Carvalho de Melo void fdarray__init(struct fdarray *fda, int nr_autogrow);
401b85337dSArnaldo Carvalho de Melo void fdarray__exit(struct fdarray *fda);
411b85337dSArnaldo Carvalho de Melo
421b85337dSArnaldo Carvalho de Melo struct fdarray *fdarray__new(int nr_alloc, int nr_autogrow);
431b85337dSArnaldo Carvalho de Melo void fdarray__delete(struct fdarray *fda);
441b85337dSArnaldo Carvalho de Melo
45ab4c1f9fSAlexey Budankov int fdarray__add(struct fdarray *fda, int fd, short revents, enum fdarray_flags flags);
46d87c25e8SAlexey Bayduraev int fdarray__dup_entry_from(struct fdarray *fda, int pos, struct fdarray *from);
471b85337dSArnaldo Carvalho de Melo int fdarray__poll(struct fdarray *fda, int timeout);
482171a925SArnaldo Carvalho de Melo int fdarray__filter(struct fdarray *fda, short revents,
49258e4bfcSWang Nan void (*entry_destructor)(struct fdarray *fda, int fd, void *arg),
50258e4bfcSWang Nan void *arg);
511b85337dSArnaldo Carvalho de Melo int fdarray__grow(struct fdarray *fda, int extra);
521b85337dSArnaldo Carvalho de Melo int fdarray__fprintf(struct fdarray *fda, FILE *fp);
531b85337dSArnaldo Carvalho de Melo
fdarray__available_entries(struct fdarray * fda)541b85337dSArnaldo Carvalho de Melo static inline int fdarray__available_entries(struct fdarray *fda)
551b85337dSArnaldo Carvalho de Melo {
561b85337dSArnaldo Carvalho de Melo return fda->nr_alloc - fda->nr;
571b85337dSArnaldo Carvalho de Melo }
581b85337dSArnaldo Carvalho de Melo
591b85337dSArnaldo Carvalho de Melo #endif /* __API_FD_ARRAY__ */
60