Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __API_FD_ARRAY__
0003 #define __API_FD_ARRAY__
0004 
0005 #include <stdio.h>
0006 
0007 struct pollfd;
0008 
0009 /**
0010  * struct fdarray: Array of file descriptors
0011  *
0012  * @priv: Per array entry priv area, users should access just its contents,
0013  *    not set it to anything, as it is kept in synch with @entries, being
0014  *    realloc'ed, * for instance, in fdarray__{grow,filter}.
0015  *
0016  *    I.e. using 'fda->priv[N].idx = * value' where N < fda->nr is ok,
0017  *    but doing 'fda->priv = malloc(M)' is not allowed.
0018  */
0019 struct fdarray {
0020     int        nr;
0021     int        nr_alloc;
0022     int        nr_autogrow;
0023     struct pollfd *entries;
0024     struct priv {
0025         union {
0026             int    idx;
0027             void   *ptr;
0028         };
0029         unsigned int flags;
0030     } *priv;
0031 };
0032 
0033 enum fdarray_flags {
0034     fdarray_flag__default       = 0x00000000,
0035     fdarray_flag__nonfilterable = 0x00000001
0036 };
0037 
0038 void fdarray__init(struct fdarray *fda, int nr_autogrow);
0039 void fdarray__exit(struct fdarray *fda);
0040 
0041 struct fdarray *fdarray__new(int nr_alloc, int nr_autogrow);
0042 void fdarray__delete(struct fdarray *fda);
0043 
0044 int fdarray__add(struct fdarray *fda, int fd, short revents, enum fdarray_flags flags);
0045 int fdarray__dup_entry_from(struct fdarray *fda, int pos, struct fdarray *from);
0046 int fdarray__poll(struct fdarray *fda, int timeout);
0047 int fdarray__filter(struct fdarray *fda, short revents,
0048             void (*entry_destructor)(struct fdarray *fda, int fd, void *arg),
0049             void *arg);
0050 int fdarray__grow(struct fdarray *fda, int extra);
0051 int fdarray__fprintf(struct fdarray *fda, FILE *fp);
0052 
0053 static inline int fdarray__available_entries(struct fdarray *fda)
0054 {
0055     return fda->nr_alloc - fda->nr;
0056 }
0057 
0058 #endif /* __API_FD_ARRAY__ */