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 <[email protected]>
  • Loading branch information
trevnorris authored and rvagg committed Jul 22, 2015
1 parent bec924f commit c735f73
Show file tree
Hide file tree
Showing 13 changed files with 2,527 additions and 1,255 deletions.
1,149 changes: 4 additions & 1,145 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,140 changes: 1,140 additions & 0 deletions lib/internal/buffer_old.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@
'lib/v8.js',
'lib/vm.js',
'lib/zlib.js',

'lib/internal/child_process.js',
'lib/internal/buffer_old.js',
'lib/internal/buffer_new.js',
'lib/internal/freelist.js',
'lib/internal/smalloc.js',
'lib/internal/socket_list.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
36 changes: 25 additions & 11 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 @@ -147,6 +148,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 @@ -166,23 +169,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 @@ -2838,6 +2835,18 @@ void SetupProcessObject(Environment* env,
READONLY_PROPERTY(process, "traceDeprecation", True(env->isolate()));
}

// --trace-sync-io
if (trace_sync_io) {
READONLY_PROPERTY(process, "traceSyncIO", True(env->isolate()));
// Don't env->set_trace_sync_io(true) because it will be enabled
// 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 @@ -3062,13 +3071,14 @@ static void PrintHelp() {
" -r, --require module to preload (option can be repeated)\n"
" --no-deprecation silence deprecation warnings\n"
" --throw-deprecation throw an exception anytime a deprecated "
"function is used\n"
" function is used\n"
" --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"
" --track-heap-objects track heap object allocations for heap "
"snapshots\n"
" snapshots\n"
" --v8-options print v8 command line options\n"
" --use-old-buffer Revert to old Buffer implementation\n"
#if defined(NODE_HAVE_I18N_SUPPORT)
" --icu-data-dir=dir set ICU data load path to dir\n"
" (overrides NODE_ICU_DATA)\n"
Expand Down Expand Up @@ -3206,6 +3216,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
Loading

0 comments on commit c735f73

Please sign in to comment.