]> PatchyGit - patchyircd.git/commitdiff
netpoll: use uv_poll_t.data for downcasting
authorEgor <egor@patchysicp.xyz>
Sun, 15 Jun 2025 06:26:57 +0000 (09:26 +0300)
committerEgor <egor@patchysicp.xyz>
Sun, 15 Jun 2025 06:26:57 +0000 (09:26 +0300)
src/netpoll.cpp

index eea2810..4703f94 100644 (file)
@@ -4,8 +4,6 @@
 #include "netpoll.hpp"
 #include "util.hpp"
 
-#define container_of(ptr, type, member) ((type *)((char *)(ptr) - offsetof(type, member)))
-
 namespace netpoll {
 struct pollfd {
        void *assoc;
@@ -32,7 +30,7 @@ void fini(void)
 }
 static void cb(uv_poll_t *handle, int status, int events)
 {
-       pollfd *pfd = container_of(handle, pollfd, handle);
+       pollfd *pfd = static_cast<pollfd *>(handle->data);
        if (status == 0) {
                pfd->revents = events;
        }
@@ -95,13 +93,13 @@ void add(uv_os_sock_t fd, int type, void *assoc)
        pfd->assoc = assoc;
        pfd->type = type;
        uv_poll_init_socket(uvloop, &pfd->handle, pfd->fd);
+       pfd->handle.data = pfd;
 }
 void del_and_close(uv_os_sock_t fd)
 {
        auto it = search(fd);
-       pollfd *pfd = *it;
-       uv_close((uv_handle_t *)&pfd->handle, [](uv_handle_t *handle){
-               pollfd *pfd = container_of(handle, pollfd, handle);
+       uv_close((uv_handle_t *)&(*it)->handle, [](uv_handle_t *handle){
+               pollfd *pfd = static_cast<pollfd *>(handle->data);
                close(pfd->fd);
                delete pfd;
        });