Skip to content

Instantly share code, notes, and snippets.

@bnoordhuis
Created June 4, 2012 23:23
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/6920fe78d0acaa0d01fc to your computer and use it in GitHub Desktop.
Save bnoordhuis/6920fe78d0acaa0d01fc to your computer and use it in GitHub Desktop.
diff --git a/src/stream_wrap.cc b/src/stream_wrap.cc
index aaa5cc6..38aaed0 100644
--- a/src/stream_wrap.cc
+++ b/src/stream_wrap.cc
@@ -29,6 +29,7 @@
#include "req_wrap.h"
#include <stdlib.h> // abort()
+#include <string.h>
#include <limits.h> // INT_MAX
#define SLAB_SIZE (1024 * 1024)
@@ -154,6 +155,7 @@ Handle<Value> StreamWrap::ReadStop(const Arguments& args) {
uv_buf_t StreamWrap::OnAlloc(uv_handle_t* handle, size_t suggested_size) {
+ return uv_buf_init(new char[suggested_size], suggested_size);
StreamWrap* wrap = static_cast<StreamWrap*>(handle->data);
assert(wrap->stream_ == reinterpret_cast<uv_stream_t*>(handle));
char* buf = slab_allocator.Allocate(wrap->object_, suggested_size);
@@ -175,7 +177,8 @@ void StreamWrap::OnReadCommon(uv_stream_t* handle, ssize_t nread,
// If libuv reports an error or EOF it *may* give us a buffer back. In that
// case, return the space to the slab.
if (buf.base != NULL) {
- slab_allocator.Shrink(wrap->object_, buf.base, 0);
+ delete[] buf.base;
+ //slab_allocator.Shrink(wrap->object_, buf.base, 0);
}
SetErrno(uv_last_error(uv_default_loop()));
@@ -184,19 +187,34 @@ void StreamWrap::OnReadCommon(uv_stream_t* handle, ssize_t nread,
}
assert(buf.base != NULL);
+ /*
Local<Object> slab = slab_allocator.Shrink(wrap->object_,
buf.base,
nread);
+ */
if (nread == 0) return;
assert(static_cast<size_t>(nread) <= buf.len);
int argc = 3;
+ /*
Local<Value> argv[4] = {
slab,
Integer::NewFromUnsigned(buf.base - Buffer::Data(slab)),
Integer::NewFromUnsigned(nread)
};
+ */
+ Local<Value> arg = Integer::NewFromUnsigned(nread);
+ Local<Object> slab = Buffer::constructor_template
+ ->GetFunction()
+ ->NewInstance(1, &arg);
+ memcpy(Buffer::Data(slab), buf.base, nread);
+ delete[] buf.base;
+ Local<Value> argv[4] = {
+ slab,
+ Integer::NewFromUnsigned(0),
+ Integer::NewFromUnsigned(nread)
+ };
Local<Object> pending_obj;
if (pending == UV_TCP) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment