]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
daemon/worker: satisfy subreq sockets from freelist
authorMarek Vavruša <marek.vavrusa@nic.cz>
Tue, 14 Jul 2015 11:24:46 +0000 (13:24 +0200)
committerMarek Vavruša <marek.vavrusa@nic.cz>
Tue, 14 Jul 2015 11:24:46 +0000 (13:24 +0200)
since the structures are ~ same size as I/O requests (udp_send_t being 60B larger than others), they all share the same freelist

daemon/io.c
daemon/io.h
daemon/worker.c

index f43baf7f0018daac3581d8f95f88fed1b6ef8a45..4457f456e5fc24cd41d2ee6b170c083476436307 100644 (file)
 
 static void *handle_alloc(uv_loop_t *loop, size_t size)
 {
-       uv_handle_t *handle = malloc(size);
-       if (handle) {
-               memset(handle, 0, size);
-       }
-       return handle;
+       return malloc(size);
 }
 
 static void handle_free(uv_handle_t *handle)
@@ -127,8 +123,12 @@ static void tcp_accept(uv_stream_t *master, int status)
                return;
        }
 
-       uv_stream_t *client = (uv_stream_t *)io_create(master->loop, SOCK_STREAM);
-       if (!client || uv_accept(master, client) != 0) {
+       uv_stream_t *client = handle_alloc(master->loop, sizeof(*client));
+       if (!client) {
+               return;
+       }
+       io_create(master->loop, (uv_handle_t *)client, SOCK_STREAM);
+       if (uv_accept(master, client) != 0) {
                handle_free((uv_handle_t *)client);
                return;
        }
@@ -163,28 +163,15 @@ void tcp_unbind(struct endpoint *ep)
        uv_close((uv_handle_t *)&ep->tcp, NULL);
 }
 
-uv_handle_t *io_create(uv_loop_t *loop, int type)
+void io_create(uv_loop_t *loop, uv_handle_t *handle, int type)
 {
        if (type == SOCK_DGRAM) {
-               uv_udp_t *handle = handle_alloc(loop, sizeof(*handle));
-               if (handle) {
-                       uv_udp_init(loop, handle);
-               }
-               return (uv_handle_t *)handle;
+               uv_udp_init(loop, (uv_udp_t *)handle);
        } else {
-               uv_tcp_t *handle = handle_alloc(loop, sizeof(*handle));
-               if (handle) {
-                       uv_tcp_init(loop, handle);
-               }
-               return (uv_handle_t *)handle;
+               uv_tcp_init(loop, (uv_tcp_t *)handle);
        }
 }
 
-void io_close(uv_handle_t *handle)
-{
-       uv_close(handle, (uv_close_cb) handle_free);
-}
-
 int io_start_read(uv_handle_t *handle)
 {
        if (handle->type == UV_UDP) {
index 83e01a0683228e235b87f98263dfe6eeecbc43dc..35e882dc163028b7a4cefe87143ed615f1d39f4e 100644 (file)
@@ -24,7 +24,6 @@ int udp_bind(struct endpoint *ep, struct sockaddr *addr);
 void udp_unbind(struct endpoint *ep);
 int tcp_bind(struct endpoint *ep, struct sockaddr *addr);
 void tcp_unbind(struct endpoint *ep);
-uv_handle_t *io_create(uv_loop_t *loop, int type);
-void io_close(uv_handle_t *handle);
+void io_create(uv_loop_t *loop, uv_handle_t *handle, int type);
 int io_start_read(uv_handle_t *handle);
 int io_stop_read(uv_handle_t *handle);
\ No newline at end of file
index 8b446875ac5c0fcda3f8a8c8ed77476b0d202687..680849e1402a50505e8154591ef3d5e0a0919b01 100644 (file)
 /* @internal IO request entry. */
 struct ioreq
 {
-        union {
-                uv_udp_send_t send;
-                uv_write_t    write;
-                uv_connect_t  connect;
-        } as;
+       union {
+               uv_udp_t      udp;
+               uv_tcp_t      tcp;
+               uv_udp_send_t send;
+               uv_write_t    write;
+               uv_connect_t  connect;
+       } as;
 };
 
 static inline struct ioreq *ioreq_take(struct worker_ctx *worker)
 {
-        struct ioreq *req = NULL;
+       struct ioreq *req = NULL;
        if (worker->ioreqs.len > 0) {
                req = array_tail(worker->ioreqs);
                array_pop(worker->ioreqs);
@@ -209,18 +211,24 @@ static int qr_task_on_send(struct qr_task *task, int status)
        return status;
 }
 
+static void on_close(uv_handle_t *handle)
+{
+       struct qr_task *task = handle->data;
+       ioreq_release(task->worker, (struct ioreq *)handle);
+}
+
 static void on_send(uv_udp_send_t *req, int status)
 {
        struct qr_task *task = req->data;
-        qr_task_on_send(task, status);
-        ioreq_release(task->worker, (struct ioreq *)req);
+       qr_task_on_send(task, status);
+       ioreq_release(task->worker, (struct ioreq *)req);
 }
 
 static void on_write(uv_write_t *req, int status)
 {
        struct qr_task *task = req->data;
-        qr_task_on_send(task, status);
-        ioreq_release(task->worker, (struct ioreq *)req);
+       qr_task_on_send(task, status);
+       ioreq_release(task->worker, (struct ioreq *)req);
 }
 
 static int qr_task_send(struct qr_task *task, uv_handle_t *handle, struct sockaddr *addr, knot_pkt_t *pkt)
@@ -291,7 +299,7 @@ static int qr_task_step(struct qr_task *task, knot_pkt_t *packet)
        if (task->next_handle) {
                if (!uv_is_closing(task->next_handle)) {
                        io_stop_read(task->next_handle);
-                       uv_close(task->next_handle, (uv_close_cb) free);
+                       uv_close(task->next_handle, on_close);
                }
                uv_timer_stop(&task->timeout);
                task->next_handle = NULL;
@@ -320,12 +328,13 @@ static int qr_task_step(struct qr_task *task, knot_pkt_t *packet)
        }
 
        /* Create connection for iterative query */
-       task->next_handle = io_create(task->worker->loop, sock_type);
-       if (task->next_handle == NULL) {
+       task->next_handle = (uv_handle_t *)ioreq_take(task->worker);
+       if (!task->next_handle) {
                return qr_task_finalize(task, KNOT_STATE_FAIL);
        }
 
        /* Connect or issue query datagram */
+       io_create(task->worker->loop, task->next_handle, sock_type);
        task->next_handle->data = task;
        if (sock_type == SOCK_STREAM) {
                struct ioreq *req = ioreq_take(task->worker);