hc
2024-02-19 151fecfb72a0d602dfe79790602ef64b4e241574
kernel/tools/lib/api/fd/array.c
....@@ -1,7 +1,6 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * 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)
54 */
65 #include "array.h"
76 #include <errno.h>
....@@ -9,6 +8,7 @@
98 #include <poll.h>
109 #include <stdlib.h>
1110 #include <unistd.h>
11
+#include <string.h>
1212
1313 void fdarray__init(struct fdarray *fda, int nr_autogrow)
1414 {
....@@ -20,7 +20,7 @@
2020
2121 int fdarray__grow(struct fdarray *fda, int nr)
2222 {
23
- void *priv;
23
+ struct priv *priv;
2424 int nr_alloc = fda->nr_alloc + nr;
2525 size_t psize = sizeof(fda->priv[0]) * nr_alloc;
2626 size_t size = sizeof(struct pollfd) * nr_alloc;
....@@ -34,6 +34,9 @@
3434 free(entries);
3535 return -ENOMEM;
3636 }
37
+
38
+ memset(&entries[fda->nr_alloc], 0, sizeof(struct pollfd) * nr);
39
+ memset(&priv[fda->nr_alloc], 0, sizeof(fda->priv[0]) * nr);
3740
3841 fda->nr_alloc = nr_alloc;
3942 fda->entries = entries;
....@@ -70,7 +73,7 @@
7073 free(fda);
7174 }
7275
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)
7477 {
7578 int pos = fda->nr;
7679
....@@ -80,6 +83,7 @@
8083
8184 fda->entries[fda->nr].fd = fd;
8285 fda->entries[fda->nr].events = revents;
86
+ fda->priv[fda->nr].flags = flags;
8387 fda->nr++;
8488 return pos;
8589 }
....@@ -94,22 +98,22 @@
9498 return 0;
9599
96100 for (fd = 0; fd < fda->nr; ++fd) {
101
+ if (!fda->entries[fd].events)
102
+ continue;
103
+
97104 if (fda->entries[fd].revents & revents) {
98105 if (entry_destructor)
99106 entry_destructor(fda, fd, arg);
100107
108
+ fda->entries[fd].revents = fda->entries[fd].events = 0;
101109 continue;
102110 }
103111
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;
110114 }
111115
112
- return fda->nr = nr;
116
+ return nr;
113117 }
114118
115119 int fdarray__poll(struct fdarray *fda, int timeout)