epoll: Don't save fd into the field of epoll_data_t

since epoll_data_t is reserved to the caller and
then shouldn't be touched by the implementation

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao 2020-08-21 01:37:17 +08:00 committed by Abdelatif Guettouche
parent a354b9a9d1
commit 389b45359d
2 changed files with 37 additions and 37 deletions

View File

@ -49,6 +49,18 @@
#include <nuttx/kmalloc.h> #include <nuttx/kmalloc.h>
/****************************************************************************
* Private Types
****************************************************************************/
struct epoll_head
{
int size;
int occupied;
FAR epoll_data_t *data;
FAR struct pollfd *poll;
};
/**************************************************************************** /****************************************************************************
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
@ -67,11 +79,14 @@
int epoll_create(int size) int epoll_create(int size)
{ {
FAR struct epoll_head *eph = FAR struct epoll_head *eph =
(FAR struct epoll_head *)kmm_malloc(sizeof(struct epoll_head)); (FAR struct epoll_head *)kmm_malloc(sizeof(struct epoll_head) +
sizeof(epoll_data_t) * size +
sizeof(struct pollfd) * size);
eph->size = size; eph->size = size;
eph->occupied = 0; eph->occupied = 0;
eph->evs = kmm_malloc(sizeof(struct epoll_event) * eph->size); eph->data = (FAR epoll_data_t *)(eph + 1);
eph->poll = (FAR struct pollfd *)(eph->data + size);
/* REVISIT: This will not work on machines where: /* REVISIT: This will not work on machines where:
* sizeof(struct epoll_head *) > sizeof(int) * sizeof(struct epoll_head *) > sizeof(int)
@ -124,7 +139,6 @@ void epoll_close(int epfd)
FAR struct epoll_head *eph = (FAR struct epoll_head *)((intptr_t)epfd); FAR struct epoll_head *eph = (FAR struct epoll_head *)((intptr_t)epfd);
kmm_free(eph->evs);
kmm_free(eph); kmm_free(eph);
} }
@ -153,8 +167,9 @@ int epoll_ctl(int epfd, int op, int fd, struct epoll_event *ev)
finfo("%08x CTL ADD(%d): fd=%d ev=%08x\n", finfo("%08x CTL ADD(%d): fd=%d ev=%08x\n",
epfd, eph->occupied, fd, ev->events); epfd, eph->occupied, fd, ev->events);
eph->evs[eph->occupied].events = ev->events | POLLERR | POLLHUP; eph->data[eph->occupied] = ev->data;
eph->evs[eph->occupied++].data.fd = fd; eph->poll[eph->occupied].events = ev->events | POLLERR | POLLHUP;
eph->poll[eph->occupied++].fd = fd;
return 0; return 0;
case EPOLL_CTL_DEL: case EPOLL_CTL_DEL:
@ -163,12 +178,14 @@ int epoll_ctl(int epfd, int op, int fd, struct epoll_event *ev)
for (i = 0; i < eph->occupied; i++) for (i = 0; i < eph->occupied; i++)
{ {
if (eph->evs[i].data.fd == fd) if (eph->poll[i].fd == fd)
{ {
if (i != eph->occupied - 1) if (i != eph->occupied - 1)
{ {
memmove(&eph->evs[i], &eph->evs[i + 1], memmove(&eph->data[i], &eph->data[i + 1],
eph->occupied - i); sizeof(epoll_data_t) * (eph->occupied - i));
memmove(&eph->poll[i], &eph->poll[i + 1],
sizeof(struct pollfd) * (eph->occupied - i));
} }
eph->occupied--; eph->occupied--;
@ -188,9 +205,10 @@ int epoll_ctl(int epfd, int op, int fd, struct epoll_event *ev)
for (i = 0; i < eph->occupied; i++) for (i = 0; i < eph->occupied; i++)
{ {
if (eph->evs[i].data.fd == fd) if (eph->poll[i].fd == fd)
{ {
eph->evs[i].events = ev->events | POLLERR | POLLHUP; eph->data[i] = ev->data;
eph->poll[i].events = ev->events | POLLERR | POLLHUP;
return 0; return 0;
} }
} }
@ -220,8 +238,7 @@ int epoll_pwait(int epfd, FAR struct epoll_event *evs,
if (timeout < 0) if (timeout < 0)
{ {
rc = ppoll((FAR struct pollfd *)eph->evs, rc = ppoll(eph->poll, eph->occupied, NULL, sigmask);
eph->occupied, NULL, sigmask);
} }
else else
{ {
@ -230,8 +247,7 @@ int epoll_pwait(int epfd, FAR struct epoll_event *evs,
timeout_ts.tv_sec = timeout / 1000; timeout_ts.tv_sec = timeout / 1000;
timeout_ts.tv_nsec = timeout % 1000 * 1000; timeout_ts.tv_nsec = timeout % 1000 * 1000;
rc = ppoll((FAR struct pollfd *)eph->evs, rc = ppoll(eph->poll, eph->occupied, &timeout_ts, sigmask);
eph->occupied, &timeout_ts, sigmask);
} }
if (rc <= 0) if (rc <= 0)
@ -243,7 +259,7 @@ int epoll_pwait(int epfd, FAR struct epoll_event *evs,
for (i = 0; i < eph->occupied; i++) for (i = 0; i < eph->occupied; i++)
{ {
ferr(" %02d: fd=%d\n", i, eph->evs[i].data.fd); ferr(" %02d: fd=%d\n", i, eph->poll[i].fd);
} }
} }
@ -259,16 +275,14 @@ int epoll_pwait(int epfd, FAR struct epoll_event *evs,
for (i = 0, counter = 0; i < rc && counter < eph->occupied; counter++) for (i = 0, counter = 0; i < rc && counter < eph->occupied; counter++)
{ {
if (eph->evs[counter].revents != 0) if (eph->poll[counter].revents != 0)
{ {
evs[i].data.fd = eph->evs[counter].data.fd; evs[i].data = eph->data[counter];
evs[i].events = eph->evs[counter].revents; evs[i++].events = eph->poll[counter].revents;
if (eph->evs[counter].events & EPOLLONESHOT) if (eph->poll[counter].events & EPOLLONESHOT)
{ {
eph->evs[counter].events = 0; /* Disable oneshot internally */ eph->poll[counter].events = 0; /* Disable oneshot internally */
} }
i += 1;
} }
} }

View File

@ -95,22 +95,8 @@ typedef union poll_data
struct epoll_event struct epoll_event
{ {
uint32_t events;
epoll_data_t data; epoll_data_t data;
pollevent_t events; /* The input event flags */
/* Non-standard fields used internally by NuttX. */
pollevent_t revents; /* The output event flags */
FAR void *reserved; /* reserved field sync with struct pollfd */
FAR sem_t *sem; /* Pointer to semaphore used to post output event */
FAR void *priv; /* For use by drivers */
};
struct epoll_head
{
int size;
int occupied;
FAR struct epoll_event *evs;
}; };
/**************************************************************************** /****************************************************************************