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.
  • Loading branch information
trevnorris committed May 28, 2015
1 parent 266526c commit bcd8e41
Show file tree
Hide file tree
Showing 8 changed files with 624 additions and 223 deletions.
399 changes: 281 additions & 118 deletions lib/buffer.js

Large diffs are not rendered by default.

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
22 changes: 16 additions & 6 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,21 @@ ArrayBufferAllocator ArrayBufferAllocator::the_singleton;


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


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 +2845,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 +3078,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 +3215,9 @@ 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) {
using_old_buffer = true;

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

0 comments on commit bcd8e41

Please sign in to comment.