diff --git a/include/uv-unix.h b/include/uv-unix.h index e9edb11d..38c18537 100644 --- a/include/uv-unix.h +++ b/include/uv-unix.h @@ -199,9 +199,9 @@ typedef struct { #define UV_WRITE_PRIVATE_FIELDS \ void* queue[2]; \ - int write_index; \ + unsigned int write_index; \ uv_buf_t* bufs; \ - int bufcnt; \ + unsigned int nbufs; \ int error; \ uv_buf_t bufsml[4]; \ diff --git a/include/uv.h b/include/uv.h index 6cbeb6f3..fcef08bb 100644 --- a/include/uv.h +++ b/include/uv.h @@ -654,8 +654,11 @@ UV_EXTERN int uv_read2_start(uv_stream_t*, uv_alloc_cb alloc_cb, * uv_write(&req2, stream, b, 2); * */ -UV_EXTERN int uv_write(uv_write_t* req, uv_stream_t* handle, - uv_buf_t bufs[], int bufcnt, uv_write_cb cb); +UV_EXTERN int uv_write(uv_write_t* req, + uv_stream_t* handle, + const uv_buf_t bufs[], + unsigned int nbufs, + uv_write_cb cb); /* * Extended write function for sending handles over a pipe. The pipe must be @@ -664,8 +667,12 @@ UV_EXTERN int uv_write(uv_write_t* req, uv_stream_t* handle, * (listening or connected state). Bound sockets or pipes will be assumed to * be servers. */ -UV_EXTERN int uv_write2(uv_write_t* req, uv_stream_t* handle, uv_buf_t bufs[], - int bufcnt, uv_stream_t* send_handle, uv_write_cb cb); +UV_EXTERN int uv_write2(uv_write_t* req, + uv_stream_t* handle, + const uv_buf_t bufs[], + unsigned int nbufs, + uv_stream_t* send_handle, + uv_write_cb cb); /* uv_write_t is a subclass of uv_req_t */ struct uv_write_s { diff --git a/src/unix/stream.c b/src/unix/stream.c index aa49c21b..c8e9f6d1 100644 --- a/src/unix/stream.c +++ b/src/unix/stream.c @@ -93,15 +93,15 @@ static int uv__open_cloexec(const char* path, int flags) { } -static size_t uv__buf_count(uv_buf_t bufs[], int bufcnt) { - size_t total = 0; - int i; +static size_t uv_count_bufs(const uv_buf_t bufs[], unsigned int nbufs) { + unsigned int i; + size_t bytes; - for (i = 0; i < bufcnt; i++) { - total += bufs[i].len; - } + bytes = 0; + for (i = 0; i < nbufs; i++) + bytes += bufs[i].len; - return total; + return bytes; } @@ -656,8 +656,8 @@ static size_t uv__write_req_size(uv_write_t* req) { size_t size; assert(req->bufs != NULL); - size = uv__buf_count(req->bufs + req->write_index, - req->bufcnt - req->write_index); + size = uv_count_bufs(req->bufs + req->write_index, + req->nbufs - req->write_index); assert(req->handle->write_queue_size >= size); return size; @@ -742,7 +742,7 @@ start: */ assert(sizeof(uv_buf_t) == sizeof(struct iovec)); iov = (struct iovec*) &(req->bufs[req->write_index]); - iovcnt = req->bufcnt - req->write_index; + iovcnt = req->nbufs - req->write_index; iovmax = uv__getiovmax(); @@ -819,7 +819,7 @@ start: uv_buf_t* buf = &(req->bufs[req->write_index]); size_t len = buf->len; - assert(req->write_index < req->bufcnt); + assert(req->write_index < req->nbufs); if ((size_t)n < len) { buf->base += n; @@ -849,7 +849,7 @@ start: assert(stream->write_queue_size >= len); stream->write_queue_size -= len; - if (req->write_index == req->bufcnt) { + if (req->write_index == req->nbufs) { /* Then we're done! */ assert(n == 0); uv__write_req_finish(req); @@ -1207,13 +1207,13 @@ static void uv__stream_connect(uv_stream_t* stream) { int uv_write2(uv_write_t* req, uv_stream_t* stream, - uv_buf_t bufs[], - int bufcnt, + const uv_buf_t bufs[], + unsigned int nbufs, uv_stream_t* send_handle, uv_write_cb cb) { int empty_queue; - assert(bufcnt > 0); + assert(nbufs > 0); assert((stream->type == UV_TCP || stream->type == UV_NAMED_PIPE || stream->type == UV_TTY) && @@ -1252,15 +1252,17 @@ int uv_write2(uv_write_t* req, req->send_handle = send_handle; QUEUE_INIT(&req->queue); - if (bufcnt <= (int) ARRAY_SIZE(req->bufsml)) - req->bufs = req->bufsml; - else - req->bufs = malloc(sizeof(uv_buf_t) * bufcnt); + req->bufs = req->bufsml; + if (nbufs > ARRAY_SIZE(req->bufsml)) + req->bufs = malloc(nbufs * sizeof(bufs[0])); - memcpy(req->bufs, bufs, bufcnt * sizeof(uv_buf_t)); - req->bufcnt = bufcnt; + if (req->bufs == NULL) + return -ENOMEM; + + memcpy(req->bufs, bufs, nbufs * sizeof(bufs[0])); + req->nbufs = nbufs; req->write_index = 0; - stream->write_queue_size += uv__buf_count(bufs, bufcnt); + stream->write_queue_size += uv_count_bufs(bufs, nbufs); /* Append the request to write_queue. */ QUEUE_INSERT_TAIL(&stream->write_queue, &req->queue); @@ -1292,9 +1294,12 @@ int uv_write2(uv_write_t* req, /* The buffers to be written must remain valid until the callback is called. * This is not required for the uv_buf_t array. */ -int uv_write(uv_write_t* req, uv_stream_t* stream, uv_buf_t bufs[], int bufcnt, - uv_write_cb cb) { - return uv_write2(req, stream, bufs, bufcnt, NULL, cb); +int uv_write(uv_write_t* req, + uv_stream_t* handle, + const uv_buf_t bufs[], + unsigned int nbufs, + uv_write_cb cb) { + return uv_write2(req, handle, bufs, nbufs, NULL, cb); } diff --git a/src/win/internal.h b/src/win/internal.h index 3b38311f..6a4fb46b 100644 --- a/src/win/internal.h +++ b/src/win/internal.h @@ -106,7 +106,7 @@ int uv_tcp_accept(uv_tcp_t* server, uv_tcp_t* client); int uv_tcp_read_start(uv_tcp_t* handle, uv_alloc_cb alloc_cb, uv_read_cb read_cb); int uv_tcp_write(uv_loop_t* loop, uv_write_t* req, uv_tcp_t* handle, - uv_buf_t bufs[], int bufcnt, uv_write_cb cb); + const uv_buf_t bufs[], unsigned int nbufs, uv_write_cb cb); void uv_process_tcp_read_req(uv_loop_t* loop, uv_tcp_t* handle, uv_req_t* req); void uv_process_tcp_write_req(uv_loop_t* loop, uv_tcp_t* handle, @@ -150,9 +150,10 @@ int uv_pipe_read_start(uv_pipe_t* handle, uv_alloc_cb alloc_cb, int uv_pipe_read2_start(uv_pipe_t* handle, uv_alloc_cb alloc_cb, uv_read2_cb read_cb); int uv_pipe_write(uv_loop_t* loop, uv_write_t* req, uv_pipe_t* handle, - uv_buf_t bufs[], int bufcnt, uv_write_cb cb); + const uv_buf_t bufs[], unsigned int nbufs, uv_write_cb cb); int uv_pipe_write2(uv_loop_t* loop, uv_write_t* req, uv_pipe_t* handle, - uv_buf_t bufs[], int bufcnt, uv_stream_t* send_handle, uv_write_cb cb); + const uv_buf_t bufs[], unsigned int nbufs, uv_stream_t* send_handle, + uv_write_cb cb); void uv_process_pipe_read_req(uv_loop_t* loop, uv_pipe_t* handle, uv_req_t* req); @@ -179,7 +180,7 @@ int uv_tty_read_start(uv_tty_t* handle, uv_alloc_cb alloc_cb, uv_read_cb read_cb); int uv_tty_read_stop(uv_tty_t* handle); int uv_tty_write(uv_loop_t* loop, uv_write_t* req, uv_tty_t* handle, - uv_buf_t bufs[], int bufcnt, uv_write_cb cb); + const uv_buf_t bufs[], unsigned int nbufs, uv_write_cb cb); void uv_tty_close(uv_tty_t* handle); void uv_process_tty_read_req(uv_loop_t* loop, uv_tty_t* handle, diff --git a/src/win/pipe.c b/src/win/pipe.c index 09b59cc6..bf98b776 100644 --- a/src/win/pipe.c +++ b/src/win/pipe.c @@ -1030,16 +1030,20 @@ static void uv_queue_non_overlapped_write(uv_pipe_t* handle) { } -static int uv_pipe_write_impl(uv_loop_t* loop, uv_write_t* req, - uv_pipe_t* handle, uv_buf_t bufs[], int bufcnt, - uv_stream_t* send_handle, uv_write_cb cb) { +static int uv_pipe_write_impl(uv_loop_t* loop, + uv_write_t* req, + uv_pipe_t* handle, + const uv_buf_t bufs[], + unsigned int nbufs, + uv_stream_t* send_handle, + uv_write_cb cb) { int err; int result; uv_tcp_t* tcp_send_handle; uv_write_t* ipc_header_req; uv_ipc_frame_uv_stream ipc_frame; - if (bufcnt != 1 && (bufcnt != 0 || !send_handle)) { + if (nbufs != 1 && (nbufs != 0 || !send_handle)) { return ERROR_NOT_SUPPORTED; } @@ -1081,7 +1085,7 @@ static int uv_pipe_write_impl(uv_loop_t* loop, uv_write_t* req, } } - if (bufcnt == 1) { + if (nbufs == 1) { ipc_frame.header.flags |= UV_IPC_RAW_DATA; ipc_frame.header.raw_data_length = bufs[0].len; } @@ -1189,7 +1193,7 @@ static int uv_pipe_write_impl(uv_loop_t* loop, uv_write_t* req, } /* Request queued by the kernel. */ - req->queued_bytes = uv_count_bufs(bufs, bufcnt); + req->queued_bytes = uv_count_bufs(bufs, nbufs); handle->write_queue_size += req->queued_bytes; } else if (handle->flags & UV_HANDLE_BLOCKING_WRITES) { /* Using overlapped IO, but wait for completion before returning */ @@ -1245,7 +1249,7 @@ static int uv_pipe_write_impl(uv_loop_t* loop, uv_write_t* req, req->queued_bytes = 0; } else { /* Request queued by the kernel. */ - req->queued_bytes = uv_count_bufs(bufs, bufcnt); + req->queued_bytes = uv_count_bufs(bufs, nbufs); handle->write_queue_size += req->queued_bytes; } @@ -1270,19 +1274,28 @@ static int uv_pipe_write_impl(uv_loop_t* loop, uv_write_t* req, } -int uv_pipe_write(uv_loop_t* loop, uv_write_t* req, uv_pipe_t* handle, - uv_buf_t bufs[], int bufcnt, uv_write_cb cb) { - return uv_pipe_write_impl(loop, req, handle, bufs, bufcnt, NULL, cb); +int uv_pipe_write(uv_loop_t* loop, + uv_write_t* req, + uv_pipe_t* handle, + const uv_buf_t bufs[], + unsigned int nbufs, + uv_write_cb cb) { + return uv_pipe_write_impl(loop, req, handle, bufs, nbufs, NULL, cb); } -int uv_pipe_write2(uv_loop_t* loop, uv_write_t* req, uv_pipe_t* handle, - uv_buf_t bufs[], int bufcnt, uv_stream_t* send_handle, uv_write_cb cb) { +int uv_pipe_write2(uv_loop_t* loop, + uv_write_t* req, + uv_pipe_t* handle, + const uv_buf_t bufs[], + unsigned int nbufs, + uv_stream_t* send_handle, + uv_write_cb cb) { if (!handle->ipc) { return WSAEINVAL; } - return uv_pipe_write_impl(loop, req, handle, bufs, bufcnt, send_handle, cb); + return uv_pipe_write_impl(loop, req, handle, bufs, nbufs, send_handle, cb); } diff --git a/src/win/stream-inl.h b/src/win/stream-inl.h index 3cde668a..e4bf0863 100644 --- a/src/win/stream-inl.h +++ b/src/win/stream-inl.h @@ -53,13 +53,13 @@ INLINE static void uv_connection_init(uv_stream_t* handle) { } -INLINE static size_t uv_count_bufs(uv_buf_t bufs[], int count) { - size_t bytes = 0; - int i; +INLINE static size_t uv_count_bufs(const uv_buf_t bufs[], unsigned int nbufs) { + unsigned int i; + size_t bytes; - for (i = 0; i < count; i++) { - bytes += (size_t)bufs[i].len; - } + bytes = 0; + for (i = 0; i < nbufs; i++) + bytes += (size_t) bufs[i].len; return bytes; } diff --git a/src/win/stream.c b/src/win/stream.c index 2351dd37..e77ce1c6 100644 --- a/src/win/stream.c +++ b/src/win/stream.c @@ -139,8 +139,11 @@ int uv_read_stop(uv_stream_t* handle) { } -int uv_write(uv_write_t* req, uv_stream_t* handle, uv_buf_t bufs[], int bufcnt, - uv_write_cb cb) { +int uv_write(uv_write_t* req, + uv_stream_t* handle, + const uv_buf_t bufs[], + unsigned int nbufs, + uv_write_cb cb) { uv_loop_t* loop = handle->loop; int err; @@ -151,13 +154,13 @@ int uv_write(uv_write_t* req, uv_stream_t* handle, uv_buf_t bufs[], int bufcnt, err = ERROR_INVALID_PARAMETER; switch (handle->type) { case UV_TCP: - err = uv_tcp_write(loop, req, (uv_tcp_t*) handle, bufs, bufcnt, cb); + err = uv_tcp_write(loop, req, (uv_tcp_t*) handle, bufs, nbufs, cb); break; case UV_NAMED_PIPE: - err = uv_pipe_write(loop, req, (uv_pipe_t*) handle, bufs, bufcnt, cb); + err = uv_pipe_write(loop, req, (uv_pipe_t*) handle, bufs, nbufs, cb); break; case UV_TTY: - err = uv_tty_write(loop, req, (uv_tty_t*) handle, bufs, bufcnt, cb); + err = uv_tty_write(loop, req, (uv_tty_t*) handle, bufs, nbufs, cb); break; default: assert(0); @@ -167,8 +170,12 @@ int uv_write(uv_write_t* req, uv_stream_t* handle, uv_buf_t bufs[], int bufcnt, } -int uv_write2(uv_write_t* req, uv_stream_t* handle, uv_buf_t bufs[], int bufcnt, - uv_stream_t* send_handle, uv_write_cb cb) { +int uv_write2(uv_write_t* req, + uv_stream_t* handle, + const uv_buf_t bufs[], + unsigned int nbufs, + uv_stream_t* send_handle, + uv_write_cb cb) { uv_loop_t* loop = handle->loop; int err; @@ -179,7 +186,13 @@ int uv_write2(uv_write_t* req, uv_stream_t* handle, uv_buf_t bufs[], int bufcnt, err = ERROR_INVALID_PARAMETER; switch (handle->type) { case UV_NAMED_PIPE: - err = uv_pipe_write2(loop, req, (uv_pipe_t*) handle, bufs, bufcnt, send_handle, cb); + err = uv_pipe_write2(loop, + req, + (uv_pipe_t*) handle, + bufs, + nbufs, + send_handle, + cb); break; default: assert(0); diff --git a/src/win/tcp.c b/src/win/tcp.c index 64323ca3..05f00edc 100644 --- a/src/win/tcp.c +++ b/src/win/tcp.c @@ -769,8 +769,12 @@ int uv_tcp_getpeername(uv_tcp_t* handle, struct sockaddr* name, } -int uv_tcp_write(uv_loop_t* loop, uv_write_t* req, uv_tcp_t* handle, - uv_buf_t bufs[], int bufcnt, uv_write_cb cb) { +int uv_tcp_write(uv_loop_t* loop, + uv_write_t* req, + uv_tcp_t* handle, + const uv_buf_t bufs[], + unsigned int nbufs, + uv_write_cb cb) { int result; DWORD bytes; @@ -792,8 +796,8 @@ int uv_tcp_write(uv_loop_t* loop, uv_write_t* req, uv_tcp_t* handle, } result = WSASend(handle->socket, - (WSABUF*)bufs, - bufcnt, + (WSABUF*) bufs, + nbufs, &bytes, 0, &req->overlapped, @@ -808,7 +812,7 @@ int uv_tcp_write(uv_loop_t* loop, uv_write_t* req, uv_tcp_t* handle, uv_insert_pending_req(loop, (uv_req_t*) req); } else if (UV_SUCCEEDED_WITH_IOCP(result == 0)) { /* Request queued by the kernel. */ - req->queued_bytes = uv_count_bufs(bufs, bufcnt); + req->queued_bytes = uv_count_bufs(bufs, nbufs); handle->reqs_pending++; handle->write_reqs_pending++; REGISTER_HANDLE_REQ(loop, handle, req); diff --git a/src/win/tty.c b/src/win/tty.c index 60c30148..a2785558 100644 --- a/src/win/tty.c +++ b/src/win/tty.c @@ -1318,13 +1318,15 @@ static int uv_tty_restore_state(uv_tty_t* handle, } -static int uv_tty_write_bufs(uv_tty_t* handle, uv_buf_t bufs[], int bufcnt, - DWORD* error) { +static int uv_tty_write_bufs(uv_tty_t* handle, + const uv_buf_t bufs[], + unsigned int nbufs, + DWORD* error) { /* We can only write 8k characters at a time. Windows can't handle */ /* much more characters in a single console write anyway. */ WCHAR utf16_buf[8192]; DWORD utf16_buf_used = 0; - int i; + unsigned int i; #define FLUSH_TEXT() \ do { \ @@ -1347,7 +1349,7 @@ static int uv_tty_write_bufs(uv_tty_t* handle, uv_buf_t bufs[], int bufcnt, EnterCriticalSection(&uv_tty_output_lock); - for (i = 0; i < bufcnt; i++) { + for (i = 0; i < nbufs; i++) { uv_buf_t buf = bufs[i]; unsigned int j; @@ -1745,8 +1747,12 @@ static int uv_tty_write_bufs(uv_tty_t* handle, uv_buf_t bufs[], int bufcnt, } -int uv_tty_write(uv_loop_t* loop, uv_write_t* req, uv_tty_t* handle, - uv_buf_t bufs[], int bufcnt, uv_write_cb cb) { +int uv_tty_write(uv_loop_t* loop, + uv_write_t* req, + uv_tty_t* handle, + const uv_buf_t bufs[], + unsigned int nbufs, + uv_write_cb cb) { DWORD error; uv_req_init(loop, (uv_req_t*) req); @@ -1760,7 +1766,7 @@ int uv_tty_write(uv_loop_t* loop, uv_write_t* req, uv_tty_t* handle, req->queued_bytes = 0; - if (!uv_tty_write_bufs(handle, bufs, bufcnt, &error)) { + if (!uv_tty_write_bufs(handle, bufs, nbufs, &error)) { SET_REQ_SUCCESS(req); } else { SET_REQ_ERROR(req, error);