diff --git a/src/net.cc b/src/net.cc index 07c8f743f8..5c3bbb6635 100644 --- a/src/net.cc +++ b/src/net.cc @@ -374,29 +374,6 @@ Connection::ForceClose (const Arguments& args) return Undefined(); } -static void -free_buf (oi_buf *b) -{ - V8::AdjustAmountOfExternalAllocatedMemory(-b->len); - free(b); -} - -static oi_buf * -new_buf (size_t size) -{ - size_t total = sizeof(oi_buf) + size; - void *p = malloc(total); - if (p == NULL) return NULL; - - oi_buf *b = static_cast(p); - b->base = static_cast(p) + sizeof(oi_buf); - - b->len = size; - b->release = free_buf; - V8::AdjustAmountOfExternalAllocatedMemory(total); - - return b; -} Handle Connection::Send (const Arguments& args) @@ -422,7 +399,7 @@ Connection::Send (const Arguments& args) enum encoding enc = ParseEncoding(args[1]); Local s = args[0]->ToString(); size_t len = s->Utf8Length(); - oi_buf *buf = new_buf(len); + oi_buf *buf = node::buf_new(len); switch (enc) { case RAW: case ASCII: @@ -441,7 +418,7 @@ Connection::Send (const Arguments& args) } else if (args[0]->IsArray()) { Handle array = Handle::Cast(args[0]); size_t len = array->Length(); - oi_buf *buf = new_buf(len); + oi_buf *buf = node::buf_new(len); for (size_t i = 0; i < len; i++) { Local int_value = array->Get(Integer::New(i)); buf->base[i] = int_value->IntegerValue(); diff --git a/src/node.cc b/src/node.cc index 5b0c2ab591..bdac7400e4 100644 --- a/src/node.cc +++ b/src/node.cc @@ -97,6 +97,30 @@ ObjectWrap::InformV8ofAllocation (ObjectWrap *obj) v8::V8::AdjustAmountOfExternalAllocatedMemory(obj->size()); } +static void +buf_free (oi_buf *b) +{ + V8::AdjustAmountOfExternalAllocatedMemory(-b->len); + free(b); +} + +oi_buf * +node::buf_new (size_t size) +{ + size_t total = sizeof(oi_buf) + size; + void *p = malloc(total); + if (p == NULL) return NULL; + + oi_buf *b = static_cast(p); + b->base = static_cast(p) + sizeof(oi_buf); + + b->len = size; + b->release = buf_free; + V8::AdjustAmountOfExternalAllocatedMemory(total); + + return b; +} + // Extracts a C string from a V8 Utf8Value. const char* ToCString(const v8::String::Utf8Value& value) diff --git a/src/node.h b/src/node.h index 5311b479f5..1a03b051c8 100644 --- a/src/node.h +++ b/src/node.h @@ -4,6 +4,7 @@ #include #include #include +#include namespace node { @@ -29,6 +30,7 @@ do { \ enum encoding {ASCII, UTF8, RAW}; enum encoding ParseEncoding (v8::Handle encoding_v); void FatalException (v8::TryCatch &try_catch); +oi_buf * buf_new (size_t size); class ObjectWrap { public: diff --git a/src/process.cc b/src/process.cc index 6962405172..d43c550ad6 100644 --- a/src/process.cc +++ b/src/process.cc @@ -71,30 +71,6 @@ Process::PIDGetter (Local _, const AccessorInfo& info) return scope.Close(pid); } -static void -free_buf (oi_buf *b) -{ - V8::AdjustAmountOfExternalAllocatedMemory(-b->len); - free(b); -} - -static oi_buf * -new_buf (size_t size) -{ - size_t total = sizeof(oi_buf) + size; - void *p = malloc(total); - if (p == NULL) return NULL; - - oi_buf *b = static_cast(p); - b->base = static_cast(p) + sizeof(oi_buf); - - b->len = size; - b->release = free_buf; - V8::AdjustAmountOfExternalAllocatedMemory(total); - - return b; -} - Handle Process::Write (const Arguments& args) { @@ -117,7 +93,7 @@ Process::Write (const Arguments& args) enum encoding enc = ParseEncoding(args[1]); Local s = args[0]->ToString(); len = s->Utf8Length(); - buf = new_buf(len); + buf = node::buf_new(len); switch (enc) { case RAW: case ASCII: @@ -135,7 +111,7 @@ Process::Write (const Arguments& args) } else if (args[0]->IsArray()) { Handle array = Handle::Cast(args[0]); len = array->Length(); - buf = new_buf(len); + buf = node::buf_new(len); for (size_t i = 0; i < len; i++) { Local int_value = array->Get(Integer::New(i)); buf->base[i] = int_value->IntegerValue();