Skip to content

Commit

Permalink
buffer: implement Uint8Array backed Buffer
Browse files Browse the repository at this point in the history
With V8 4.4 removing the external array data API currently used by
Buffer, the new implementation uses the Uint8Array to back Buffer.

Buffers now have a maximum size of Smi::kMaxLength, as defined by V8.
Which is ~2 GB on 64 bit and ~1 GB on 32 bit.

The flag --use-old-buffer allows using the old Buffer implementation.
This flag will be removed once V8 4.4 has landed.

The two JS Buffer implementations have been split into two files for
simplicity.

Use getter to return expected .parent/.offset values for backwards
compatibility.

PR-URL: #1825
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
  • Loading branch information
trevnorris committed Jun 2, 2015
1 parent b12d267 commit 65cd82a
Show file tree
Hide file tree
Showing 13 changed files with 2,527 additions and 1,252 deletions.
1,150 changes: 4 additions & 1,146 deletions lib/buffer.js

Large diffs are not rendered by default.

1,020 changes: 1,020 additions & 0 deletions lib/internal/buffer_new.js

Large diffs are not rendered by default.

1,149 changes: 1,149 additions & 0 deletions lib/internal/buffer_old.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
'lib/vm.js',
'lib/zlib.js',

'lib/internal/buffer_old.js',
'lib/internal/buffer_new.js',
'lib/internal/freelist.js',
'lib/internal/smalloc.js',
'lib/internal/repl.js',
Expand Down
1 change: 1 addition & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ namespace node {
V(async_hooks_post_function, v8::Function) \
V(binding_cache_object, v8::Object) \
V(buffer_constructor_function, v8::Function) \
V(buffer_prototype_object, v8::Object) \
V(context, v8::Context) \
V(domain_array, v8::Array) \
V(fs_stats_constructor_function, v8::Function) \
Expand Down
25 changes: 16 additions & 9 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "node_http_parser.h"
#include "node_javascript.h"
#include "node_version.h"
#include "node_internals.h"

#if defined HAVE_PERFCTR
#include "node_counters.h"
Expand Down Expand Up @@ -146,6 +147,8 @@ static uv_async_t dispatch_debug_messages_async;
static Isolate* node_isolate = nullptr;
static v8::Platform* default_platform;

bool using_old_buffer = false;

class ArrayBufferAllocator : public ArrayBuffer::Allocator {
public:
// Impose an upper limit to avoid out of memory errors that bring down
Expand All @@ -165,23 +168,17 @@ ArrayBufferAllocator ArrayBufferAllocator::the_singleton;


void* ArrayBufferAllocator::Allocate(size_t length) {
if (length > kMaxLength)
return nullptr;
char* data = new char[length];
memset(data, 0, length);
return data;
return calloc(length, 1);
}


void* ArrayBufferAllocator::AllocateUninitialized(size_t length) {
if (length > kMaxLength)
return nullptr;
return new char[length];
return malloc(length);
}


void ArrayBufferAllocator::Free(void* data, size_t length) {
delete[] static_cast<char*>(data);
free(data);
}


Expand Down Expand Up @@ -2844,6 +2841,11 @@ void SetupProcessObject(Environment* env,
// after LoadEnvironment() has run.
}

// --use-old_buffer
if (using_old_buffer) {
READONLY_PROPERTY(process, "useOldBuffer", True(env->isolate()));
}

size_t exec_path_len = 2 * PATH_MAX;
char* exec_path = new char[exec_path_len];
Local<String> exec_path_value;
Expand Down Expand Up @@ -3072,6 +3074,7 @@ static void PrintHelp() {
" --trace-deprecation show stack traces on deprecations\n"
" --trace-sync-io show stack trace when use of sync IO\n"
" is detected after the first tick\n"
" --use-old-buffer Revert to old Buffer implementation\n"
" --v8-options print v8 command line options\n"
#if defined(NODE_HAVE_I18N_SUPPORT)
" --icu-data-dir=dir set ICU data load path to dir\n"
Expand Down Expand Up @@ -3208,6 +3211,10 @@ static void ParseArgs(int* argc,
#endif
} else if (strcmp(arg, "--expose-internals") == 0 ||
strcmp(arg, "--expose_internals") == 0) {
} else if (strcmp(arg, "--use-old-buffer") == 0 ||
strcmp(arg, "--use_old_buffer") == 0) {
using_old_buffer = true;

// consumed in js
} else {
// V8 option. Pass through as-is.
Expand Down

0 comments on commit 65cd82a

Please sign in to comment.