^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #ifndef __API_FD_ARRAY__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #define __API_FD_ARRAY__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) struct pollfd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * struct fdarray: Array of file descriptors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * @priv: Per array entry priv area, users should access just its contents,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * not set it to anything, as it is kept in synch with @entries, being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * realloc'ed, * for instance, in fdarray__{grow,filter}.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * I.e. using 'fda->priv[N].idx = * value' where N < fda->nr is ok,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * but doing 'fda->priv = malloc(M)' is not allowed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct fdarray {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) int nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) int nr_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) int nr_autogrow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct pollfd *entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct priv {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) void *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) unsigned int flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) } *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) enum fdarray_flags {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) fdarray_flag__default = 0x00000000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) fdarray_flag__nonfilterable = 0x00000001
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) void fdarray__init(struct fdarray *fda, int nr_autogrow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) void fdarray__exit(struct fdarray *fda);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct fdarray *fdarray__new(int nr_alloc, int nr_autogrow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) void fdarray__delete(struct fdarray *fda);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) int fdarray__add(struct fdarray *fda, int fd, short revents, enum fdarray_flags flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) int fdarray__poll(struct fdarray *fda, int timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) int fdarray__filter(struct fdarray *fda, short revents,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) void (*entry_destructor)(struct fdarray *fda, int fd, void *arg),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) void *arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) int fdarray__grow(struct fdarray *fda, int extra);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) int fdarray__fprintf(struct fdarray *fda, FILE *fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static inline int fdarray__available_entries(struct fdarray *fda)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return fda->nr_alloc - fda->nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #endif /* __API_FD_ARRAY__ */