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 authored and rvagg committed Aug 4, 2015

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 23be6ca commit 63da0df
Showing 13 changed files with 2,521 additions and 1,252 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
@@ -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',
1 change: 1 addition & 0 deletions src/env.h
Original file line number Diff line number Diff line change
@@ -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) \
25 changes: 16 additions & 9 deletions src/node.cc
Original file line number Diff line number Diff line change
@@ -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"
@@ -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
@@ -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);
}


@@ -2836,6 +2833,11 @@ void SetupProcessObject(Environment* env,
READONLY_PROPERTY(process, "traceDeprecation", True(env->isolate()));
}

// --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;
@@ -3067,6 +3069,7 @@ static void PrintHelp() {
" --track-heap-objects track heap object allocations for heap "
"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"
@@ -3204,6 +3207,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.
Loading

0 comments on commit 63da0df

Please sign in to comment.