.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * Copyright (C) 2014, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> |
---|
3 | | - * |
---|
4 | | - * Released under the GPL v2. (and only v2, not any later version) |
---|
5 | 4 | */ |
---|
6 | 5 | #include "array.h" |
---|
7 | 6 | #include <errno.h> |
---|
.. | .. |
---|
9 | 8 | #include <poll.h> |
---|
10 | 9 | #include <stdlib.h> |
---|
11 | 10 | #include <unistd.h> |
---|
| 11 | +#include <string.h> |
---|
12 | 12 | |
---|
13 | 13 | void fdarray__init(struct fdarray *fda, int nr_autogrow) |
---|
14 | 14 | { |
---|
.. | .. |
---|
20 | 20 | |
---|
21 | 21 | int fdarray__grow(struct fdarray *fda, int nr) |
---|
22 | 22 | { |
---|
23 | | - void *priv; |
---|
| 23 | + struct priv *priv; |
---|
24 | 24 | int nr_alloc = fda->nr_alloc + nr; |
---|
25 | 25 | size_t psize = sizeof(fda->priv[0]) * nr_alloc; |
---|
26 | 26 | size_t size = sizeof(struct pollfd) * nr_alloc; |
---|
.. | .. |
---|
34 | 34 | free(entries); |
---|
35 | 35 | return -ENOMEM; |
---|
36 | 36 | } |
---|
| 37 | + |
---|
| 38 | + memset(&entries[fda->nr_alloc], 0, sizeof(struct pollfd) * nr); |
---|
| 39 | + memset(&priv[fda->nr_alloc], 0, sizeof(fda->priv[0]) * nr); |
---|
37 | 40 | |
---|
38 | 41 | fda->nr_alloc = nr_alloc; |
---|
39 | 42 | fda->entries = entries; |
---|
.. | .. |
---|
70 | 73 | free(fda); |
---|
71 | 74 | } |
---|
72 | 75 | |
---|
73 | | -int fdarray__add(struct fdarray *fda, int fd, short revents) |
---|
| 76 | +int fdarray__add(struct fdarray *fda, int fd, short revents, enum fdarray_flags flags) |
---|
74 | 77 | { |
---|
75 | 78 | int pos = fda->nr; |
---|
76 | 79 | |
---|
.. | .. |
---|
80 | 83 | |
---|
81 | 84 | fda->entries[fda->nr].fd = fd; |
---|
82 | 85 | fda->entries[fda->nr].events = revents; |
---|
| 86 | + fda->priv[fda->nr].flags = flags; |
---|
83 | 87 | fda->nr++; |
---|
84 | 88 | return pos; |
---|
85 | 89 | } |
---|
.. | .. |
---|
94 | 98 | return 0; |
---|
95 | 99 | |
---|
96 | 100 | for (fd = 0; fd < fda->nr; ++fd) { |
---|
| 101 | + if (!fda->entries[fd].events) |
---|
| 102 | + continue; |
---|
| 103 | + |
---|
97 | 104 | if (fda->entries[fd].revents & revents) { |
---|
98 | 105 | if (entry_destructor) |
---|
99 | 106 | entry_destructor(fda, fd, arg); |
---|
100 | 107 | |
---|
| 108 | + fda->entries[fd].revents = fda->entries[fd].events = 0; |
---|
101 | 109 | continue; |
---|
102 | 110 | } |
---|
103 | 111 | |
---|
104 | | - if (fd != nr) { |
---|
105 | | - fda->entries[nr] = fda->entries[fd]; |
---|
106 | | - fda->priv[nr] = fda->priv[fd]; |
---|
107 | | - } |
---|
108 | | - |
---|
109 | | - ++nr; |
---|
| 112 | + if (!(fda->priv[fd].flags & fdarray_flag__nonfilterable)) |
---|
| 113 | + ++nr; |
---|
110 | 114 | } |
---|
111 | 115 | |
---|
112 | | - return fda->nr = nr; |
---|
| 116 | + return nr; |
---|
113 | 117 | } |
---|
114 | 118 | |
---|
115 | 119 | int fdarray__poll(struct fdarray *fda, int timeout) |
---|