From 58ba2d86e136d2d1180f4d3054384266afa04456 Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Tue, 22 May 2012 15:51:37 +0200 Subject: [PATCH] Move shared c-ares glue code from uv-common to cares.c --- include/uv.h | 2 +- src/cares.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/uv-common.c | 37 ------------------------------------- src/uv-common.h | 16 ---------------- 4 files changed, 47 insertions(+), 54 deletions(-) diff --git a/include/uv.h b/include/uv.h index 0214e635..fb5952cd 100644 --- a/include/uv.h +++ b/include/uv.h @@ -137,7 +137,6 @@ typedef enum { #undef UV_ERRNO_GEN #define UV_HANDLE_TYPE_MAP(XX) \ - XX(ARES_TASK, ares_task) \ XX(ASYNC, async) \ XX(CHECK, check) \ XX(FS_EVENT, fs_event) \ @@ -165,6 +164,7 @@ typedef enum { #define XX(uc, lc) UV_##uc, UV_HANDLE_TYPE_MAP(XX) #undef XX + UV_ARES_TASK, UV_FILE, UV_HANDLE_TYPE_PRIVATE UV_HANDLE_TYPE_MAX diff --git a/src/cares.c b/src/cares.c index 3ef799e6..c0acbec8 100644 --- a/src/cares.c +++ b/src/cares.c @@ -20,6 +20,7 @@ */ #include "uv.h" +#include "tree.h" #include "uv-common.h" #include @@ -28,6 +29,51 @@ #include +struct uv_ares_task_s { + UV_HANDLE_FIELDS + ares_socket_t sock; + uv_poll_t poll_watcher; + RB_ENTRY(uv_ares_task_s) node; +}; + + +static int cmp_ares_tasks(const uv_ares_task_t* a, const uv_ares_task_t* b) { + if (a->sock < b->sock) return -1; + if (a->sock > b->sock) return 1; + return 0; +} + + +RB_GENERATE_STATIC(uv__ares_tasks, uv_ares_task_s, node, cmp_ares_tasks) + + +/* Add ares handle to list. */ +static void uv_add_ares_handle(uv_loop_t* loop, uv_ares_task_t* handle) { + assert(loop == handle->loop); + RB_INSERT(uv__ares_tasks, &loop->ares_handles, handle); +} + + +/* Find matching ares handle in list. */ +static uv_ares_task_t* uv_find_ares_handle(uv_loop_t* loop, ares_socket_t sock) { + uv_ares_task_t handle; + handle.sock = sock; + return RB_FIND(uv__ares_tasks, &loop->ares_handles, &handle); +} + + +/* Remove ares handle from list. */ +static void uv_remove_ares_handle(uv_ares_task_t* handle) { + RB_REMOVE(uv__ares_tasks, &handle->loop->ares_handles, handle); +} + + +/* Returns 1 if the ares_handles list is empty, 0 otherwise. */ +static int uv_ares_handles_empty(uv_loop_t* loop) { + return RB_EMPTY(&loop->ares_handles); +} + + /* This is called once per second by loop->timer. It is used to constantly */ /* call back into c-ares for possibly processing timeouts. */ static void uv__ares_timeout(uv_timer_t* handle, int status) { diff --git a/src/uv-common.c b/src/uv-common.c index 0c6a8198..e81f7a31 100644 --- a/src/uv-common.c +++ b/src/uv-common.c @@ -199,43 +199,6 @@ int uv_ip6_name(struct sockaddr_in6* src, char* dst, size_t size) { } -static int cmp_ares_tasks(const uv_ares_task_t* a, const uv_ares_task_t* b) { - if (a->sock < b->sock) return -1; - if (a->sock > b->sock) return 1; - return 0; -} - - -RB_GENERATE_STATIC(uv__ares_tasks, uv_ares_task_s, node, cmp_ares_tasks) - - -/* add ares handle to list */ -void uv_add_ares_handle(uv_loop_t* loop, uv_ares_task_t* handle) { - assert(loop == handle->loop); - RB_INSERT(uv__ares_tasks, &loop->ares_handles, handle); -} - - -/* find matching ares handle in list */ -uv_ares_task_t* uv_find_ares_handle(uv_loop_t* loop, ares_socket_t sock) { - uv_ares_task_t handle; - handle.sock = sock; - return RB_FIND(uv__ares_tasks, &loop->ares_handles, &handle); -} - - -/* remove ares handle in list */ -void uv_remove_ares_handle(uv_ares_task_t* handle) { - RB_REMOVE(uv__ares_tasks, &handle->loop->ares_handles, handle); -} - - -/* Returns 1 if the ares_handles list is empty. 0 otherwise. */ -int uv_ares_handles_empty(uv_loop_t* loop) { - return RB_EMPTY(&loop->ares_handles); -} - - int uv_tcp_bind(uv_tcp_t* handle, struct sockaddr_in addr) { if (handle->type != UV_TCP || addr.sin_family != AF_INET) { uv__set_artificial_error(handle->loop, UV_EFAULT); diff --git a/src/uv-common.h b/src/uv-common.h index d9c44896..7bf6db81 100644 --- a/src/uv-common.h +++ b/src/uv-common.h @@ -57,22 +57,6 @@ enum { # define UV__ACTIVE 0x00000040 #endif -struct uv_ares_task_s { - UV_HANDLE_FIELDS - ares_socket_t sock; - uv_poll_t poll_watcher; - RB_ENTRY(uv_ares_task_s) node; -}; - - -void uv_remove_ares_handle(uv_ares_task_t* handle); -uv_ares_task_t* uv_find_ares_handle(uv_loop_t*, ares_socket_t sock); - -/* TODO Rename to uv_ares_task_init? */ -void uv_add_ares_handle(uv_loop_t* loop, uv_ares_task_t* handle); - -int uv_ares_handles_empty(uv_loop_t* loop); - extern const uv_err_t uv_ok_; uv_err_code uv_translate_sys_error(int sys_errno);