blob: 7fcf21a33c0c6a97ca5aaa1abb7049bf0cfbdbdf [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -03002#ifndef __API_FD_ARRAY__
3#define __API_FD_ARRAY__
4
5#include <stdio.h>
6
7struct pollfd;
8
Arnaldo Carvalho de Melo2171a922014-09-08 11:24:01 -03009/**
10 * struct fdarray: Array of file descriptors
11 *
12 * @priv: Per array entry priv area, users should access just its contents,
13 * not set it to anything, as it is kept in synch with @entries, being
14 * realloc'ed, * for instance, in fdarray__{grow,filter}.
15 *
16 * I.e. using 'fda->priv[N].idx = * value' where N < fda->nr is ok,
17 * but doing 'fda->priv = malloc(M)' is not allowed.
18 */
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -030019struct fdarray {
20 int nr;
21 int nr_alloc;
22 int nr_autogrow;
23 struct pollfd *entries;
Alexey Budankovab4c1f92020-07-17 09:59:45 +030024 struct priv {
25 union {
26 int idx;
27 void *ptr;
28 };
29 unsigned int flags;
Arnaldo Carvalho de Melo2171a922014-09-08 11:24:01 -030030 } *priv;
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -030031};
32
Alexey Budankovab4c1f92020-07-17 09:59:45 +030033enum fdarray_flags {
34 fdarray_flag__default = 0x00000000,
35 fdarray_flag__nonfilterable = 0x00000001
36};
37
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -030038void fdarray__init(struct fdarray *fda, int nr_autogrow);
39void fdarray__exit(struct fdarray *fda);
40
41struct fdarray *fdarray__new(int nr_alloc, int nr_autogrow);
42void fdarray__delete(struct fdarray *fda);
43
Alexey Budankovab4c1f92020-07-17 09:59:45 +030044int fdarray__add(struct fdarray *fda, int fd, short revents, enum fdarray_flags flags);
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -030045int fdarray__poll(struct fdarray *fda, int timeout);
Arnaldo Carvalho de Melo2171a922014-09-08 11:24:01 -030046int fdarray__filter(struct fdarray *fda, short revents,
Wang Nan258e4bf2016-05-25 13:44:57 +000047 void (*entry_destructor)(struct fdarray *fda, int fd, void *arg),
48 void *arg);
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -030049int fdarray__grow(struct fdarray *fda, int extra);
50int fdarray__fprintf(struct fdarray *fda, FILE *fp);
51
52static inline int fdarray__available_entries(struct fdarray *fda)
53{
54 return fda->nr_alloc - fda->nr;
55}
56
57#endif /* __API_FD_ARRAY__ */