diff --git a/test/benchmark-ping-pongs.c b/test/benchmark-ping-pongs.c index 536be20e..3b0c3ce9 100644 --- a/test/benchmark-ping-pongs.c +++ b/test/benchmark-ping-pongs.c @@ -164,7 +164,7 @@ static void pinger_connect_cb(uv_req_t *req, int status) { pinger_write_ping(pinger); - if (uv_read_start((uv_tcp_t*)(req->handle), pinger_read_cb)) { + if (uv_read_start((uv_tcp_t*)(req->handle), buf_alloc, pinger_read_cb)) { FATAL("uv_read_start failed"); } } @@ -196,7 +196,7 @@ static void pinger_new() { BENCHMARK_IMPL(ping_pongs) { - uv_init(buf_alloc); + uv_init(); start_time = uv_now(); pinger_new(); diff --git a/test/benchmark-pump.c b/test/benchmark-pump.c index 3c5b5c06..f032ed11 100644 --- a/test/benchmark-pump.c +++ b/test/benchmark-pump.c @@ -261,7 +261,7 @@ static void accept_cb(uv_tcp_t* s) { r = uv_accept(s, tcp, read_sockets_close_cb, NULL); ASSERT(r == 0); - r = uv_read_start(tcp, read_cb); + r = uv_read_start(tcp, buf_alloc, read_cb); ASSERT(r == 0); read_sockets++; @@ -345,7 +345,7 @@ static void buf_free(uv_buf_t uv_buf_t) { HELPER_IMPL(pump_server) { int r; - uv_init(buf_alloc); + uv_init(); listen_addr = uv_ip4_addr("0.0.0.0", TEST_PORT); /* Server */ @@ -368,7 +368,7 @@ void pump(int n) { ASSERT(n <= MAX_WRITE_HANDLES); TARGET_CONNECTIONS = n; - uv_init(buf_alloc); + uv_init(); connect_addr = uv_ip4_addr("127.0.0.1", TEST_PORT); diff --git a/test/echo-server.c b/test/echo-server.c index 3605d865..069618a7 100644 --- a/test/echo-server.c +++ b/test/echo-server.c @@ -118,6 +118,14 @@ static void on_close(uv_handle_t* peer, int status) { } +static uv_buf_t echo_alloc(uv_tcp_t* handle, size_t suggested_size) { + uv_buf_t buf; + buf.base = (char*) malloc(suggested_size); + buf.len = suggested_size; + return buf; +} + + static void on_accept(uv_tcp_t* server) { uv_tcp_t* handle = (uv_tcp_t*) malloc(sizeof *handle); @@ -125,7 +133,7 @@ static void on_accept(uv_tcp_t* server) { FATAL("uv_accept failed"); } - uv_read_start(handle, after_read); + uv_read_start(handle, echo_alloc, after_read); } @@ -164,16 +172,8 @@ static int echo_start(int port) { } -static uv_buf_t echo_alloc(uv_tcp_t* handle, size_t suggested_size) { - uv_buf_t buf; - buf.base = (char*) malloc(suggested_size); - buf.len = suggested_size; - return buf; -} - - HELPER_IMPL(echo_server) { - uv_init(echo_alloc); + uv_init(); if (echo_start(TEST_PORT)) return 1; diff --git a/test/test-async.c b/test/test-async.c index 1095a9ab..810de292 100644 --- a/test/test-async.c +++ b/test/test-async.c @@ -120,13 +120,6 @@ static void close_cb(uv_handle_t* handle, int status) { } -static uv_buf_t alloc_cb(uv_tcp_t* handle, size_t size) { - uv_buf_t buf = {0, 0}; - FATAL("alloc should not be called"); - return buf; -} - - static void async1_cb(uv_handle_t* handle, int status) { ASSERT(handle == (uv_handle_t*)&async1_handle); ASSERT(status == 0); @@ -196,7 +189,7 @@ static void prepare_cb(uv_handle_t* handle, int status) { TEST_IMPL(async) { int r; - uv_init(alloc_cb); + uv_init(); r = uv_prepare_init(&prepare_handle, close_cb, NULL); ASSERT(r == 0); diff --git a/test/test-bind-error.c b/test/test-bind-error.c index 32e5a378..a195fb2c 100644 --- a/test/test-bind-error.c +++ b/test/test-bind-error.c @@ -36,19 +36,12 @@ static void close_cb(uv_handle_t* handle, int status) { } -static uv_buf_t alloc_cb(uv_tcp_t* handle, size_t size) { - uv_buf_t buf = {0, 0}; - FATAL("alloc should not be called"); - return buf; -} - - TEST_IMPL(bind_error_addrinuse) { struct sockaddr_in addr = uv_ip4_addr("0.0.0.0", TEST_PORT); uv_tcp_t server1, server2; int r; - uv_init(alloc_cb); + uv_init(); r = uv_tcp_init(&server1, close_cb, NULL); ASSERT(r == 0); @@ -83,7 +76,7 @@ TEST_IMPL(bind_error_addrnotavail_1) { uv_tcp_t server; int r; - uv_init(alloc_cb); + uv_init(); r = uv_tcp_init(&server, close_cb, NULL); ASSERT(r == 0); @@ -109,7 +102,7 @@ TEST_IMPL(bind_error_addrnotavail_2) { uv_tcp_t server; int r; - uv_init(alloc_cb); + uv_init(); r = uv_tcp_init(&server, close_cb, NULL); ASSERT(r == 0); @@ -135,7 +128,7 @@ TEST_IMPL(bind_error_fault) { garbage_addr = (struct sockaddr_in*) &garbage; - uv_init(alloc_cb); + uv_init(); r = uv_tcp_init(&server, close_cb, NULL); ASSERT(r == 0); @@ -161,7 +154,7 @@ TEST_IMPL(bind_error_inval) { uv_tcp_t server; int r; - uv_init(alloc_cb); + uv_init(); r = uv_tcp_init(&server, close_cb, NULL); ASSERT(r == 0); diff --git a/test/test-callback-stack.c b/test/test-callback-stack.c index a8bcdb2f..b1755b65 100644 --- a/test/test-callback-stack.c +++ b/test/test-callback-stack.c @@ -43,6 +43,15 @@ static int bytes_received = 0; static int shutdown_cb_called = 0; +static uv_buf_t alloc_cb(uv_tcp_t* tcp, size_t size) { + uv_buf_t buf; + buf.len = size; + buf.base = (char*) malloc(size); + ASSERT(buf.base); + return buf; +} + + static void close_cb(uv_handle_t* handle, int status) { ASSERT(status == 0); ASSERT(nested == 0 && "close_cb must be called from a fresh stack"); @@ -111,7 +120,7 @@ static void timer_cb(uv_handle_t* handle, int status) { puts("Timeout complete. Now read data..."); nested++; - if (uv_read_start(&client, read_cb)) { + if (uv_read_start(&client, alloc_cb, read_cb)) { FATAL("uv_read_start failed"); } nested--; @@ -171,19 +180,10 @@ static void connect_cb(uv_req_t* req, int status) { } -static uv_buf_t alloc_cb(uv_tcp_t* tcp, size_t size) { - uv_buf_t buf; - buf.len = size; - buf.base = (char*) malloc(size); - ASSERT(buf.base); - return buf; -} - - TEST_IMPL(callback_stack) { struct sockaddr_in addr = uv_ip4_addr("127.0.0.1", TEST_PORT); - uv_init(alloc_cb); + uv_init(); if (uv_tcp_init(&client, &close_cb, NULL)) { FATAL("uv_tcp_init failed"); diff --git a/test/test-connection-fail.c b/test/test-connection-fail.c index 03f048c6..9f8d8324 100644 --- a/test/test-connection-fail.c +++ b/test/test-connection-fail.c @@ -46,18 +46,11 @@ static void on_connect(uv_req_t *req, int status) { } -static uv_buf_t alloc_cb(uv_tcp_t* tcp, size_t size) { - uv_buf_t buf = {0, 0}; - FATAL("alloc should not be called"); - return buf; -} - - TEST_IMPL(connection_fail) { struct sockaddr_in client_addr, server_addr; int r; - uv_init(alloc_cb); + uv_init(); client_addr = uv_ip4_addr("0.0.0.0", 0); diff --git a/test/test-delayed-accept.c b/test/test-delayed-accept.c index f4f99d13..9ab66e52 100644 --- a/test/test-delayed-accept.c +++ b/test/test-delayed-accept.c @@ -33,6 +33,14 @@ static int close_cb_called = 0; static int connect_cb_called = 0; +static uv_buf_t alloc_cb(uv_tcp_t* tcp, size_t size) { + uv_buf_t buf; + buf.base = (char*)malloc(size); + buf.len = size; + return buf; +} + + static void close_cb(uv_handle_t* handle, int status) { ASSERT(handle != NULL); ASSERT(status == 0); @@ -131,7 +139,7 @@ static void connect_cb(uv_req_t* req, int status) { /* Not that the server will send anything, but otherwise we'll never know */ /* when te server closes the connection. */ - r = uv_read_start((uv_tcp_t*)(req->handle), read_cb); + r = uv_read_start((uv_tcp_t*)(req->handle), alloc_cb, read_cb); ASSERT(r == 0); connect_cb_called++; @@ -158,17 +166,9 @@ static void client_connect() { } -static uv_buf_t alloc_cb(uv_tcp_t* tcp, size_t size) { - uv_buf_t buf; - buf.base = (char*)malloc(size); - buf.len = size; - return buf; -} - - TEST_IMPL(delayed_accept) { - uv_init(alloc_cb); + uv_init(); start_server(); diff --git a/test/test-loop-handles.c b/test/test-loop-handles.c index 694e7c9c..89d10e5e 100644 --- a/test/test-loop-handles.c +++ b/test/test-loop-handles.c @@ -313,18 +313,11 @@ static void prepare_1_close_cb(uv_handle_t* handle, int status){ } -static uv_buf_t alloc_cb(uv_tcp_t* tcp, size_t size) { - uv_buf_t rv = { 0, 0 }; - FATAL("alloc_cb should never be called in this test"); - return rv; -} - - TEST_IMPL(loop_handles) { int i; int r; - uv_init(alloc_cb); + uv_init(); r = uv_prepare_init(&prepare_1_handle, prepare_1_close_cb, NULL); ASSERT(r == 0); @@ -387,7 +380,7 @@ TEST_IMPL(loop_handles) { TEST_IMPL(ref) { - uv_init(alloc_cb); + uv_init(); uv_run(); return 0; } @@ -395,7 +388,7 @@ TEST_IMPL(ref) { TEST_IMPL(idle_ref) { uv_idle_t h; - uv_init(alloc_cb); + uv_init(); uv_idle_init(&h, NULL, NULL); uv_idle_start(&h, NULL); uv_unref(); @@ -406,7 +399,7 @@ TEST_IMPL(idle_ref) { TEST_IMPL(async_ref) { uv_async_t h; - uv_init(alloc_cb); + uv_init(); uv_async_init(&h, NULL, NULL, NULL); uv_unref(); uv_run(); @@ -416,7 +409,7 @@ TEST_IMPL(async_ref) { TEST_IMPL(prepare_ref) { uv_prepare_t h; - uv_init(alloc_cb); + uv_init(); uv_prepare_init(&h, NULL, NULL); uv_prepare_start(&h, NULL); uv_unref(); @@ -427,7 +420,7 @@ TEST_IMPL(prepare_ref) { TEST_IMPL(check_ref) { uv_check_t h; - uv_init(alloc_cb); + uv_init(); uv_check_init(&h, NULL, NULL); uv_check_start(&h, NULL); uv_unref(); diff --git a/test/test-ping-pong.c b/test/test-ping-pong.c index a94e658a..02ef56c1 100644 --- a/test/test-ping-pong.c +++ b/test/test-ping-pong.c @@ -48,6 +48,14 @@ typedef struct { void pinger_try_read(pinger_t* pinger); +static uv_buf_t alloc_cb(uv_tcp_t* tcp, size_t size) { + uv_buf_t buf; + buf.base = (char*)malloc(size); + buf.len = size; + return buf; +} + + static void pinger_on_close(uv_handle_t* handle, int status) { pinger_t* pinger = (pinger_t*)handle->data; @@ -130,7 +138,7 @@ static void pinger_on_connect(uv_req_t *req, int status) { pinger_write_ping(pinger); - uv_read_start((uv_tcp_t*)(req->handle), pinger_read_cb); + uv_read_start((uv_tcp_t*)(req->handle), alloc_cb, pinger_read_cb); } @@ -157,16 +165,8 @@ static void pinger_new() { } -static uv_buf_t alloc_cb(uv_tcp_t* tcp, size_t size) { - uv_buf_t buf; - buf.base = (char*)malloc(size); - buf.len = size; - return buf; -} - - TEST_IMPL(ping_pong) { - uv_init(alloc_cb); + uv_init(); pinger_new(); uv_run(); diff --git a/test/test-tcp-writealot.c b/test/test-tcp-writealot.c index 5755e4e8..26a079cb 100644 --- a/test/test-tcp-writealot.c +++ b/test/test-tcp-writealot.c @@ -45,6 +45,14 @@ static int bytes_received = 0; static int bytes_received_done = 0; +static uv_buf_t alloc_cb(uv_tcp_t* tcp, size_t size) { + uv_buf_t buf; + buf.base = (char*)malloc(size); + buf.len = size; + return buf; +} + + static void close_cb(uv_handle_t* handle, int status) { ASSERT(handle != NULL); ASSERT(status == 0); @@ -152,19 +160,11 @@ static void connect_cb(uv_req_t* req, int status) { ASSERT(req != NULL); uv_req_init(req, (uv_handle_t*)tcp, read_cb); - r = uv_read_start(tcp, read_cb); + r = uv_read_start(tcp, alloc_cb, read_cb); ASSERT(r == 0); } -static uv_buf_t alloc_cb(uv_tcp_t* tcp, size_t size) { - uv_buf_t buf; - buf.base = (char*)malloc(size); - buf.len = size; - return buf; -} - - TEST_IMPL(tcp_writealot) { struct sockaddr_in addr = uv_ip4_addr("127.0.0.1", TEST_PORT); uv_tcp_t* client = (uv_tcp_t*)malloc(sizeof *client); @@ -178,7 +178,7 @@ TEST_IMPL(tcp_writealot) { ASSERT(send_buffer != NULL); - uv_init(alloc_cb); + uv_init(); r = uv_tcp_init(client, close_cb, NULL); ASSERT(r == 0); diff --git a/test/test-timer-again.c b/test/test-timer-again.c index 19e57cb2..0844bc3a 100644 --- a/test/test-timer-again.c +++ b/test/test-timer-again.c @@ -91,17 +91,10 @@ static void repeat_2_cb(uv_handle_t* handle, int status) { } -static uv_buf_t alloc_cb(uv_tcp_t* tcp, size_t size) { - uv_buf_t buf = {0, 0}; - FATAL("alloc should not be called"); - return buf; -} - - TEST_IMPL(timer_again) { int r; - uv_init(alloc_cb); + uv_init(); start_time = uv_now(); ASSERT(0 < start_time); diff --git a/test/test-timer.c b/test/test-timer.c index d68b49c4..049835ed 100644 --- a/test/test-timer.c +++ b/test/test-timer.c @@ -92,19 +92,12 @@ static void never_cb(uv_handle_t* handle, int status) { } -static uv_buf_t alloc_cb(uv_tcp_t* tcp, size_t size) { - uv_buf_t buf = {0, 0}; - FATAL("alloc should not be called"); - return buf; -} - - TEST_IMPL(timer) { uv_timer_t *once; uv_timer_t repeat, never; int i, r; - uv_init(alloc_cb); + uv_init(); start_time = uv_now(); ASSERT(0 < start_time); diff --git a/uv-unix.c b/uv-unix.c index edea3046..14a862ae 100644 --- a/uv-unix.c +++ b/uv-unix.c @@ -42,7 +42,6 @@ #endif static uv_err_t last_err; -static uv_alloc_cb alloc_cb; void uv__tcp_io(EV_P_ ev_io* watcher, int revents); @@ -187,10 +186,7 @@ int uv_close(uv_handle_t* handle) { } -void uv_init(uv_alloc_cb cb) { - assert(cb); - alloc_cb = cb; - +void uv_init() { // Initialize the default ev loop. #if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1060 ev_default_loop(EVBACKEND_KQUEUE); @@ -224,6 +220,7 @@ static void uv__handle_init(uv_handle_t* handle, uv_handle_type type, int uv_tcp_init(uv_tcp_t* tcp, uv_close_cb close_cb, void* data) { uv__handle_init((uv_handle_t*)tcp, UV_TCP, close_cb, data); + tcp->alloc_cb = NULL; tcp->connect_req = NULL; tcp->accepted_fd = -1; tcp->fd = -1; @@ -644,8 +641,8 @@ void uv__read(uv_tcp_t* tcp) { * tcp->read_cb is NULL or not? */ while (tcp->read_cb && uv_flag_is_set((uv_handle_t*)tcp, UV_READING)) { - assert(alloc_cb); - uv_buf_t buf = alloc_cb(tcp, 64 * 1024); + assert(tcp->alloc_cb); + uv_buf_t buf = tcp->alloc_cb(tcp, 64 * 1024); assert(buf.len > 0); assert(buf.base); @@ -918,7 +915,7 @@ int64_t uv_now() { } -int uv_read_start(uv_tcp_t* tcp, uv_read_cb cb) { +int uv_read_start(uv_tcp_t* tcp, uv_alloc_cb alloc_cb, uv_read_cb read_cb) { /* The UV_READING flag is irrelevant of the state of the tcp - it just * expresses the desired state of the user. */ @@ -929,7 +926,10 @@ int uv_read_start(uv_tcp_t* tcp, uv_read_cb cb) { * not start the IO watcher. */ assert(tcp->fd >= 0); - tcp->read_cb = cb; + assert(alloc_cb); + + tcp->read_cb = read_cb; + tcp->alloc_cb = alloc_cb; /* These should have been set by uv_tcp_init. */ assert(tcp->read_watcher.data == tcp); @@ -945,6 +945,7 @@ int uv_read_stop(uv_tcp_t* tcp) { ev_io_stop(EV_DEFAULT_UC_ &tcp->read_watcher); tcp->read_cb = NULL; + tcp->alloc_cb = NULL; return 0; } diff --git a/uv-unix.h b/uv-unix.h index cd52f447..31dc9ad6 100644 --- a/uv-unix.h +++ b/uv-unix.h @@ -57,6 +57,7 @@ typedef struct { #define UV_TCP_PRIVATE_FIELDS \ int delayed_error; \ uv_read_cb read_cb; \ + uv_alloc_cb alloc_cb; \ uv_accept_cb accept_cb; \ int accepted_fd; \ uv_req_t *connect_req; \ diff --git a/uv-win.c b/uv-win.c index fe988af9..705ec046 100644 --- a/uv-win.c +++ b/uv-win.c @@ -182,9 +182,6 @@ static uv_err_t uv_last_error_ = { UV_OK, ERROR_SUCCESS }; /* Error message string */ static char* uv_err_str_ = NULL; -/* Global alloc function */ -uv_alloc_cb uv_alloc_ = NULL; - /* Reference count that keeps the event loop alive */ static int uv_refs_ = 0; @@ -345,7 +342,7 @@ static void uv_get_extension_function(SOCKET socket, GUID guid, } -void uv_init(uv_alloc_cb alloc_cb) { +void uv_init() { const GUID wsaid_connectex = WSAID_CONNECTEX; const GUID wsaid_acceptex = WSAID_ACCEPTEX; const GUID wsaid_getacceptexsockaddrs = WSAID_GETACCEPTEXSOCKADDRS; @@ -357,8 +354,6 @@ void uv_init(uv_alloc_cb alloc_cb) { LARGE_INTEGER timer_frequency; SOCKET dummy; - uv_alloc_ = alloc_cb; - /* Initialize winsock */ errorno = WSAStartup(MAKEWORD(2, 2), &wsa_data); if (errorno != 0) { @@ -866,7 +861,7 @@ int uv_accept(uv_tcp_t* server, uv_tcp_t* client, } -int uv_read_start(uv_tcp_t* handle, uv_read_cb cb) { +int uv_read_start(uv_tcp_t* handle, uv_alloc_cb alloc_cb, uv_read_cb read_cb) { if (!(handle->flags & UV_HANDLE_CONNECTION)) { uv_set_sys_error(WSAEINVAL); return -1; @@ -883,7 +878,8 @@ int uv_read_start(uv_tcp_t* handle, uv_read_cb cb) { } handle->flags |= UV_HANDLE_READING; - handle->read_cb = cb; + handle->read_cb = read_cb; + handle->alloc_cb = alloc_cb; /* If reading was stopped and then started again, there could stell be a */ /* read request pending. */ @@ -1074,7 +1070,7 @@ static void uv_tcp_return_req(uv_tcp_t* handle, uv_req_t* req) { uv_close_error((uv_handle_t*)handle, uv_last_error_); } while (handle->flags & UV_HANDLE_READING) { - buf = uv_alloc_(handle, 65536); + buf = handle->alloc_cb(handle, 65536); assert(buf.len > 0); flags = 0; if (WSARecv(handle->socket, diff --git a/uv-win.h b/uv-win.h index f409a0d4..6ea16730 100644 --- a/uv-win.h +++ b/uv-win.h @@ -52,6 +52,7 @@ typedef struct uv_buf_t { int flags; #define uv_tcp_connection_fields \ + uv_alloc_cb alloc_cb; \ void* read_cb; \ struct uv_req_s read_req; \ unsigned int write_reqs_pending; \ diff --git a/uv.h b/uv.h index 78c4acc8..ce9dad9f 100644 --- a/uv.h +++ b/uv.h @@ -217,7 +217,7 @@ int uv_accept(uv_tcp_t* server, uv_tcp_t* client, * eof; it happens when libuv requested a buffer through the alloc callback * but then decided that it didn't need that buffer. */ -int uv_read_start(uv_tcp_t*, uv_read_cb cb); +int uv_read_start(uv_tcp_t*, uv_alloc_cb alloc_cb, uv_read_cb read_cb); int uv_read_stop(uv_tcp_t*); @@ -337,7 +337,7 @@ uv_err_t uv_last_error(); char* uv_strerror(uv_err_t err); const char* uv_err_name(uv_err_t err); -void uv_init(uv_alloc_cb alloc); +void uv_init(); int uv_run(); /*