Skip to content

Instantly share code, notes, and snippets.

@ry
Created August 18, 2011 00:39
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 ry/1153020 to your computer and use it in GitHub Desktop.
Save ry/1153020 to your computer and use it in GitHub Desktop.
commit 04b760db2fa108ac08418eb65b341e2d913989c5
Author: Ryan Dahl <ry@tinyclouds.org>
Date: Wed Aug 17 17:38:39 2011 -0700
Add uv_buf_init() constructor
diff --git a/include/uv.h b/include/uv.h
index 0d7bc85..0df13f8 100644
--- a/include/uv.h
+++ b/include/uv.h
@@ -284,6 +284,15 @@ int uv_is_active(uv_handle_t* handle);
void uv_close(uv_handle_t* handle, uv_close_cb close_cb);
+/*
+ * Construtor for uv_buf_t.
+ * Due to platform differences the user cannot rely on the ordering of the
+ * base and len members of the uv_buf_t struct. The user is responsible for
+ * freeing base after the uv_buf_t is done. Return struct passed by value.
+ */
+uv_buf_t uv_buf_init(char* base, size_t len);
+
+
#define UV_STREAM_FIELDS \
/* number of bytes queued for writing */ \
size_t write_queue_size; \
diff --git a/src/uv-common.c b/src/uv-common.c
index ae859e8..2428fdd 100644
--- a/src/uv-common.c
+++ b/src/uv-common.c
@@ -43,6 +43,14 @@ uv_counters_t* uv_counters() {
}
+uv_buf_t uv_buf_init(char* base, size_t len) {
+ uv_buf_t buf;
+ buf.base = base;
+ buf.len = len;
+ return buf;
+}
+
+
const char* uv_err_name(uv_err_t err) {
switch (err.code) {
case UV_UNKNOWN: return "UNKNOWN";
diff --git a/test/echo-server.c b/test/echo-server.c
index f75d388..bf8ffa6 100644
--- a/test/echo-server.c
+++ b/test/echo-server.c
@@ -111,8 +111,7 @@ static void after_read(uv_stream_t* handle, ssize_t nread, uv_buf_t buf) {
wr = (write_req_t*) malloc(sizeof *wr);
- wr->buf.base = buf.base;
- wr->buf.len = nread;
+ wr->buf = uv_buf_init(buf.base, nread);
if (uv_write(&wr->req, handle, &wr->buf, 1, after_write)) {
FATAL("uv_write failed");
}
@@ -125,10 +124,7 @@ static void on_close(uv_handle_t* peer) {
static uv_buf_t echo_alloc(uv_stream_t* handle, size_t suggested_size) {
- uv_buf_t buf;
- buf.base = (char*) malloc(suggested_size);
- buf.len = suggested_size;
- return buf;
+ return uv_buf_init(malloc(suggested_size), suggested_size);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment