Skip to content

Commit

Permalink
buffer: removing duplicate code
Browse files Browse the repository at this point in the history
- using an overload of Alloc that does the same that was being done
  inside `Buffer::New`

The overload we now call inside `smalloc.cc` takes care of the same as
the code that was removed:

    if (length == 0)
      return Alloc(env, obj, nullptr, length, type);

    char* data = static_cast<char*>(malloc(length));
    if (data == nullptr) {
      FatalError("node::smalloc::Alloc(v8::Handle<v8::Object>, size_t,"
                  " v8::ExternalArrayType)", "Out Of Memory");
    }

    Alloc(env, obj, data, length, type);

PR-URL: #1144
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
  • Loading branch information
thlorenz authored and bnoordhuis committed Mar 19, 2015
1 parent 1514b82 commit 813a536
Showing 1 changed file with 1 addition and 12 deletions.
13 changes: 1 addition & 12 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,7 @@ Local<Object> New(Environment* env, size_t length) {
Local<Value> arg = Uint32::NewFromUnsigned(env->isolate(), length);
Local<Object> obj = env->buffer_constructor_function()->NewInstance(1, &arg);

// TODO(trevnorris): done like this to handle HasInstance since only checks
// if external array data has been set, but would like to use a better
// approach if v8 provided one.
char* data;
if (length > 0) {
data = static_cast<char*>(malloc(length));
if (data == nullptr)
FatalError("node::Buffer::New(size_t)", "Out Of Memory");
} else {
data = nullptr;
}
smalloc::Alloc(env, obj, data, length);
smalloc::Alloc(env, obj, length);

return scope.Escape(obj);
}
Expand Down

0 comments on commit 813a536

Please sign in to comment.