Skip to content

Instantly share code, notes, and snippets.

@bnoordhuis
Created August 23, 2011 02:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bnoordhuis/13e681faf7feca9ab402 to your computer and use it in GitHub Desktop.
Save bnoordhuis/13e681faf7feca9ab402 to your computer and use it in GitHub Desktop.
diff --git a/test/benchmark-udp-packet-storm.c b/test/benchmark-udp-packet-storm.c
index 7eb213b..6b61b40 100644
--- a/test/benchmark-udp-packet-storm.c
+++ b/test/benchmark-udp-packet-storm.c
@@ -53,9 +53,7 @@ typedef struct {
static uv_buf_t alloc_cb(uv_handle_t* handle, size_t suggested_size) {
- static char slab[65536];
- ASSERT(suggested_size <= sizeof slab);
- return uv_buf_init(slab, sizeof slab);
+ return uv_buf_init(malloc(suggested_size), suggested_size);
}
@@ -89,17 +87,20 @@ static void recv_cb(uv_udp_t* handle,
struct sockaddr* addr,
unsigned flags) {
if (nread == 0)
- return;
+ goto out;
if (nread == -1) {
ASSERT(uv_last_error().code == UV_EINTR); /* FIXME change error code */
- return;
+ goto out;
}
ASSERT(addr->sa_family == AF_INET);
ASSERT(!memcmp(buf.base, EXPECTED, nread));
recv_cb_called++;
+
+out:
+ free(buf.base);
}
diff --git a/src/uv-unix.c b/src/uv-unix.c
index ff42cdb..1f96e8f 100644
--- a/src/uv-unix.c
+++ b/src/uv-unix.c
@@ -34,6 +34,7 @@
#include <assert.h>
#include <unistd.h>
#include <sys/types.h>
+#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/socket.h>
@@ -490,6 +491,7 @@ static void uv__udp_run_completed(uv_udp_t* handle) {
static void uv__udp_recvmsg(uv_udp_t* handle) {
struct sockaddr_storage peer;
struct msghdr h;
+ int remaining;
ssize_t nread;
uv_buf_t buf;
int flags;
@@ -498,8 +500,12 @@ static void uv__udp_recvmsg(uv_udp_t* handle) {
assert(handle->alloc_cb != NULL);
do {
- /* FIXME: hoist alloc_cb out the loop but for now follow uv__read() */
- buf = handle->alloc_cb((uv_handle_t*)handle, 64 * 1024);
+ remaining = 65536;
+
+ if (ioctl(handle->fd, FIONREAD, &remaining) == 0 && remaining == 0)
+ break;
+
+ buf = handle->alloc_cb((uv_handle_t*)handle, remaining);
assert(buf.len > 0);
assert(buf.base != NULL);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment