Skip to content

Commit 63da0df

Browse files
trevnorrisrvagg
authored andcommitted
buffer: implement Uint8Array backed Buffer
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]>
1 parent 23be6ca commit 63da0df

13 files changed

+2521
-1252
lines changed

lib/buffer.js

+4-1,145
Large diffs are not rendered by default.

lib/internal/buffer_new.js

+1,020
Large diffs are not rendered by default.

lib/internal/buffer_old.js

+1,140
Large diffs are not rendered by default.

node.gyp

+2-1
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@
6969
'lib/v8.js',
7070
'lib/vm.js',
7171
'lib/zlib.js',
72-
7372
'lib/internal/child_process.js',
73+
'lib/internal/buffer_old.js',
74+
'lib/internal/buffer_new.js',
7475
'lib/internal/freelist.js',
7576
'lib/internal/smalloc.js',
7677
'lib/internal/socket_list.js',

src/env.h

+1
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ namespace node {
231231
V(async_hooks_post_function, v8::Function) \
232232
V(binding_cache_object, v8::Object) \
233233
V(buffer_constructor_function, v8::Function) \
234+
V(buffer_prototype_object, v8::Object) \
234235
V(context, v8::Context) \
235236
V(domain_array, v8::Array) \
236237
V(fs_stats_constructor_function, v8::Function) \

src/node.cc

+16-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "node_http_parser.h"
66
#include "node_javascript.h"
77
#include "node_version.h"
8+
#include "node_internals.h"
89

910
#if defined HAVE_PERFCTR
1011
#include "node_counters.h"
@@ -147,6 +148,8 @@ static uv_async_t dispatch_debug_messages_async;
147148
static Isolate* node_isolate = nullptr;
148149
static v8::Platform* default_platform;
149150

151+
bool using_old_buffer = false;
152+
150153
class ArrayBufferAllocator : public ArrayBuffer::Allocator {
151154
public:
152155
// Impose an upper limit to avoid out of memory errors that bring down
@@ -166,23 +169,17 @@ ArrayBufferAllocator ArrayBufferAllocator::the_singleton;
166169

167170

168171
void* ArrayBufferAllocator::Allocate(size_t length) {
169-
if (length > kMaxLength)
170-
return nullptr;
171-
char* data = new char[length];
172-
memset(data, 0, length);
173-
return data;
172+
return calloc(length, 1);
174173
}
175174

176175

177176
void* ArrayBufferAllocator::AllocateUninitialized(size_t length) {
178-
if (length > kMaxLength)
179-
return nullptr;
180-
return new char[length];
177+
return malloc(length);
181178
}
182179

183180

184181
void ArrayBufferAllocator::Free(void* data, size_t length) {
185-
delete[] static_cast<char*>(data);
182+
free(data);
186183
}
187184

188185

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

2836+
// --use-old_buffer
2837+
if (using_old_buffer) {
2838+
READONLY_PROPERTY(process, "useOldBuffer", True(env->isolate()));
2839+
}
2840+
28392841
size_t exec_path_len = 2 * PATH_MAX;
28402842
char* exec_path = new char[exec_path_len];
28412843
Local<String> exec_path_value;
@@ -3067,6 +3069,7 @@ static void PrintHelp() {
30673069
" --track-heap-objects track heap object allocations for heap "
30683070
"snapshots\n"
30693071
" --v8-options print v8 command line options\n"
3072+
" --use-old-buffer Revert to old Buffer implementation\n"
30703073
#if defined(NODE_HAVE_I18N_SUPPORT)
30713074
" --icu-data-dir=dir set ICU data load path to dir\n"
30723075
" (overrides NODE_ICU_DATA)\n"
@@ -3204,6 +3207,10 @@ static void ParseArgs(int* argc,
32043207
#endif
32053208
} else if (strcmp(arg, "--expose-internals") == 0 ||
32063209
strcmp(arg, "--expose_internals") == 0) {
3210+
} else if (strcmp(arg, "--use-old-buffer") == 0 ||
3211+
strcmp(arg, "--use_old_buffer") == 0) {
3212+
using_old_buffer = true;
3213+
32073214
// consumed in js
32083215
} else {
32093216
// V8 option. Pass through as-is.

0 commit comments

Comments
 (0)