Add uv_buf_init() constructor

This commit is contained in:
Ryan Dahl 2011-08-17 17:38:39 -07:00
parent cc5aa51fdf
commit e5a938f1f4
3 changed files with 19 additions and 6 deletions

View File

@ -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; \

View File

@ -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";

View File

@ -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);
}