Question

on epoll, the epoll_data structure is important

    typedef union epoll_data {
        void *ptr;
        int fd;
        __uint32_t u32;
        __uint64_t u64;
    } epoll_data_t;

if for some events, I need to relate it to some additional information, for the epoll_data, I need to construct a structure and use the ptr to point to that structure. And inside the structure I put a fd element, something like

           typedef {
              int fd;
              char* info;
           } compound;

but when I want to check the active event, I have to do something like:

for( ; ; )
    {
        nfds = epoll_wait(epfd,events,20,500);
        for(i=0;i<nfds;++i)
        {
            if(events[i].data.fd==listenfd) {
                ...
            }
            else if( ((compound*)events[i].events.ptr)->fd ....){
               ...
            }
    }

the checking is inconsistent, sometimes I need to check events[i].data.fd and sometimes I need to check events[i].data.ptr->fd. If I put all events comsistent, namely, let all of them have a void ptr, for some sockets, it is a waste because they are not related to additional information.

so I think epoll_data should have a independent int fd element because fd is usually very important.

how is your opinion on this? thanks!

Was it helpful?

Solution

You can always use file descriptor stored in epoll_data instance to index/key into your own data array/hash. Or you can always use the pointer member of that union and have all your metadata in your custom data structure. In other words, you already can make it consistent.

In my opinion, the minimal OS interface is preferable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top