From 2b5707d834a6b85b8e589ac04cb61a6db2dab94b Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Fri, 10 Jun 2011 18:15:10 +0200 Subject: [PATCH] Correct uv_counter API --- test/test-delayed-accept.c | 11 +++++------ uv-common.c | 8 ++++++++ uv-unix.c | 17 +++++++++-------- uv-win.c | 26 +++++++++++++------------- uv.h | 20 ++++++++++++-------- 5 files changed, 47 insertions(+), 35 deletions(-) diff --git a/test/test-delayed-accept.c b/test/test-delayed-accept.c index 57e97fe9..f903f990 100644 --- a/test/test-delayed-accept.c +++ b/test/test-delayed-accept.c @@ -24,7 +24,6 @@ #include #include - static char BUFFER[1024]; static int connection_cb_called = 0; @@ -62,14 +61,14 @@ static void do_accept(uv_handle_t* timer_handle, int status) { uv_tcp_init(accepted_handle); - /* Test to that uv_cnt_tcp_init does not increase across the uv_accept. */ - tcpcnt = uv_cnt_tcp_init; + /* Test to that uv_counters()->tcp_init does not increase across the uv_accept. */ + tcpcnt = uv_counters()->tcp_init; server = (uv_tcp_t*)timer_handle->data; r = uv_accept(server, accepted_handle); ASSERT(r == 0); - ASSERT(uv_cnt_tcp_init == tcpcnt); + ASSERT(uv_counters()->tcp_init == tcpcnt); do_accept_called++; @@ -120,8 +119,8 @@ static void start_server() { r = uv_tcp_init(server); ASSERT(r == 0); - ASSERT(uv_cnt_tcp_init == 1); - ASSERT(uv_cnt_handle_init == 1); + ASSERT(uv_counters()->tcp_init == 1); + ASSERT(uv_counters()->handle_init == 1); r = uv_bind(server, addr); ASSERT(r == 0); diff --git a/uv-common.c b/uv-common.c index dd1b00c5..921ac20d 100644 --- a/uv-common.c +++ b/uv-common.c @@ -25,6 +25,14 @@ #include /* NULL */ +static uv_counters_t counters; + + +uv_counters_t* const uv_counters() { + return &counters; +} + + const char* uv_err_name(uv_err_t err) { switch (err.code) { case UV_UNKNOWN: return "UNKNOWN"; diff --git a/uv-unix.c b/uv-unix.c index 7b9ae5ba..b0e85d95 100644 --- a/uv-unix.c +++ b/uv-unix.c @@ -41,6 +41,7 @@ #include #endif + static uv_err_t last_err; @@ -205,7 +206,7 @@ int uv_run() { static void uv__handle_init(uv_handle_t* handle, uv_handle_type type) { - uv_cnt_handle_init++; + uv_counters()->handle_init++; handle->type = type; handle->flags = 0; @@ -220,7 +221,7 @@ static void uv__handle_init(uv_handle_t* handle, uv_handle_type type) { int uv_tcp_init(uv_tcp_t* tcp) { uv__handle_init((uv_handle_t*)tcp, UV_TCP); - uv_cnt_tcp_init++; + uv_counters()->tcp_init++; tcp->alloc_cb = NULL; tcp->connect_req = NULL; @@ -965,7 +966,7 @@ int uv_read_stop(uv_tcp_t* tcp) { void uv_req_init(uv_req_t* req, uv_handle_t* handle, void* cb) { - uv_cnt_req_init++; + uv_counters()->req_init++; req->type = UV_UNKNOWN_REQ; req->cb = cb; req->handle = handle; @@ -984,7 +985,7 @@ static void uv__prepare(EV_P_ ev_prepare* w, int revents) { int uv_prepare_init(uv_prepare_t* prepare) { uv__handle_init((uv_handle_t*)prepare, UV_PREPARE); - uv_cnt_prepare_init++; + uv_counters()->prepare_init++; ev_prepare_init(&prepare->prepare_watcher, uv__prepare); prepare->prepare_watcher.data = prepare; @@ -1034,7 +1035,7 @@ static void uv__check(EV_P_ ev_check* w, int revents) { int uv_check_init(uv_check_t* check) { uv__handle_init((uv_handle_t*)check, UV_CHECK); - uv_cnt_check_init; + uv_counters()->check_init; ev_check_init(&check->check_watcher, uv__check); check->check_watcher.data = check; @@ -1085,7 +1086,7 @@ static void uv__idle(EV_P_ ev_idle* w, int revents) { int uv_idle_init(uv_idle_t* idle) { uv__handle_init((uv_handle_t*)idle, UV_IDLE); - uv_cnt_idle_init++; + uv_counters()->idle_init++; ev_idle_init(&idle->idle_watcher, uv__idle); idle->idle_watcher.data = idle; @@ -1154,7 +1155,7 @@ static void uv__async(EV_P_ ev_async* w, int revents) { int uv_async_init(uv_async_t* async, uv_async_cb async_cb) { uv__handle_init((uv_handle_t*)async, UV_ASYNC); - uv_cnt_async_init++; + uv_counters()->async_init++; ev_async_init(&async->async_watcher, uv__async); async->async_watcher.data = async; @@ -1189,7 +1190,7 @@ static void uv__timer_cb(EV_P_ ev_timer* w, int revents) { int uv_timer_init(uv_timer_t* timer) { uv__handle_init((uv_handle_t*)timer, UV_TIMER); - uv_cnt_timer_init++; + uv_counters()->timer_init++; ev_init(&timer->timer_watcher, uv__timer_cb); timer->timer_watcher.data = timer; diff --git a/uv-win.c b/uv-win.c index 65a436ab..6a42b4d4 100644 --- a/uv-win.c +++ b/uv-win.c @@ -410,7 +410,7 @@ void uv_init() { void uv_req_init(uv_req_t* req, uv_handle_t* handle, void* cb) { - uv_cnt_req_init++; + uv_counters()->req_init++; req->type = UV_UNKNOWN_REQ; req->flags = 0; req->handle = handle; @@ -507,8 +507,8 @@ int uv_tcp_init(uv_tcp_t* handle) { handle->error = uv_ok_; handle->accept_socket = INVALID_SOCKET; - uv_cnt_handle_init++; - uv_cnt_tcp_init++; + uv_counters()->handle_init++; + uv_counters()->tcp_init++; uv_refs_++; @@ -1254,8 +1254,8 @@ RB_GENERATE_STATIC(uv_timer_tree_s, uv_timer_s, tree_entry, uv_timer_compare); int uv_timer_init(uv_timer_t* handle) { - uv_cnt_handle_init++; - uv_cnt_timer_init++; + uv_counters()->handle_init++; + uv_counters()->timer_init++; handle->type = UV_TIMER; handle->flags = 0; @@ -1429,24 +1429,24 @@ static void uv_loop_invoke(uv_handle_t* list) { int uv_prepare_init(uv_prepare_t* handle) { - uv_cnt_handle_init++; - uv_cnt_prepare_init++; + uv_counters()->handle_init++; + uv_counters()->prepare_init++; handle->type = UV_PREPARE; return uv_loop_init((uv_handle_t*)handle); } int uv_check_init(uv_check_t* handle) { - uv_cnt_handle_init++; - uv_cnt_check_init++; + uv_counters()->handle_init++; + uv_counters()->check_init++; handle->type = UV_CHECK; return uv_loop_init((uv_handle_t*)handle); } int uv_idle_init(uv_idle_t* handle) { - uv_cnt_handle_init++; - uv_cnt_idle_init++; + uv_counters()->handle_init++; + uv_counters()->idle_init++; handle->type = UV_IDLE; return uv_loop_init((uv_handle_t*)handle); } @@ -1505,8 +1505,8 @@ int uv_is_active(uv_handle_t* handle) { int uv_async_init(uv_async_t* handle, uv_async_cb async_cb) { uv_req_t* req; - uv_cnt_handle_init++; - uv_cnt_async_init++; + uv_counters()->handle_init++; + uv_counters()->async_init++; handle->type = UV_ASYNC; handle->flags = 0; diff --git a/uv.h b/uv.h index 0e2c045d..cd48fde6 100644 --- a/uv.h +++ b/uv.h @@ -375,14 +375,18 @@ union uv_any_handle { }; /* Diagnostic counters */ -uint64_t uv_cnt_req_init; -uint64_t uv_cnt_handle_init; -uint64_t uv_cnt_tcp_init; -uint64_t uv_cnt_prepare_init; -uint64_t uv_cnt_check_init; -uint64_t uv_cnt_idle_init; -uint64_t uv_cnt_async_init; -uint64_t uv_cnt_timer_init; +typedef struct { + uint64_t req_init; + uint64_t handle_init; + uint64_t tcp_init; + uint64_t prepare_init; + uint64_t check_init; + uint64_t idle_init; + uint64_t async_init; + uint64_t timer_init; +} uv_counters_t; + +uv_counters_t* const uv_counters(); #ifdef __cplusplus }