|
11 | 11 |
|
12 | 12 | namespace node { |
13 | 13 |
|
| 14 | +// It's a bit awkward to define this Buffer::New() overload here, but it |
| 15 | +// avoids a circular dependency with node_internals.h. |
| 16 | +namespace Buffer { |
| 17 | +v8::MaybeLocal<v8::Uint8Array> New(Environment* env, |
| 18 | + v8::Local<v8::ArrayBuffer> ab, |
| 19 | + size_t byte_offset, |
| 20 | + size_t length); |
| 21 | +} |
| 22 | + |
| 23 | +NoArrayBufferZeroFillScope::NoArrayBufferZeroFillScope( |
| 24 | + IsolateData* isolate_data) |
| 25 | + : node_allocator_(isolate_data->node_allocator()) { |
| 26 | + if (node_allocator_ != nullptr) node_allocator_->zero_fill_field()[0] = 0; |
| 27 | +} |
| 28 | + |
| 29 | +NoArrayBufferZeroFillScope::~NoArrayBufferZeroFillScope() { |
| 30 | + if (node_allocator_ != nullptr) node_allocator_->zero_fill_field()[0] = 1; |
| 31 | +} |
| 32 | + |
14 | 33 | AllocatedBuffer AllocatedBuffer::AllocateManaged( |
15 | 34 | Environment* env, |
16 | | - size_t size, |
17 | | - int flags) { |
18 | | - char* data = flags & ALLOCATE_MANAGED_UNCHECKED ? |
19 | | - env->AllocateUnchecked(size) : |
20 | | - env->Allocate(size); |
21 | | - if (data == nullptr) size = 0; |
22 | | - return AllocatedBuffer(env, uv_buf_init(data, size)); |
| 35 | + size_t size) { |
| 36 | + NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data()); |
| 37 | + std::unique_ptr<v8::BackingStore> bs = |
| 38 | + v8::ArrayBuffer::NewBackingStore(env->isolate(), size); |
| 39 | + return AllocatedBuffer(env, std::move(bs)); |
23 | 40 | } |
24 | 41 |
|
25 | | -inline AllocatedBuffer::AllocatedBuffer(Environment* env, uv_buf_t buf) |
26 | | - : env_(env), buffer_(buf) {} |
| 42 | +AllocatedBuffer::AllocatedBuffer( |
| 43 | + Environment* env, std::unique_ptr<v8::BackingStore> bs) |
| 44 | + : env_(env), backing_store_(std::move(bs)) {} |
| 45 | + |
| 46 | +AllocatedBuffer::AllocatedBuffer( |
| 47 | + Environment* env, uv_buf_t buffer) |
| 48 | + : env_(env) { |
| 49 | + if (buffer.base == nullptr) return; |
| 50 | + auto map = env->released_allocated_buffers(); |
| 51 | + auto it = map->find(buffer.base); |
| 52 | + CHECK_NE(it, map->end()); |
| 53 | + backing_store_ = std::move(it->second); |
| 54 | + map->erase(it); |
| 55 | +} |
27 | 56 |
|
28 | | -inline void AllocatedBuffer::Resize(size_t len) { |
29 | | - // The `len` check is to make sure we don't end up with `nullptr` as our base. |
30 | | - char* new_data = env_->Reallocate(buffer_.base, buffer_.len, |
31 | | - len > 0 ? len : 1); |
32 | | - CHECK_NOT_NULL(new_data); |
33 | | - buffer_ = uv_buf_init(new_data, len); |
| 57 | +void AllocatedBuffer::Resize(size_t len) { |
| 58 | + if (len == 0) { |
| 59 | + backing_store_ = v8::ArrayBuffer::NewBackingStore(env_->isolate(), 0); |
| 60 | + return; |
| 61 | + } |
| 62 | + NoArrayBufferZeroFillScope no_zero_fill_scope(env_->isolate_data()); |
| 63 | + backing_store_ = v8::BackingStore::Reallocate( |
| 64 | + env_->isolate(), std::move(backing_store_), len); |
34 | 65 | } |
35 | 66 |
|
36 | 67 | inline uv_buf_t AllocatedBuffer::release() { |
37 | | - uv_buf_t ret = buffer_; |
38 | | - buffer_ = uv_buf_init(nullptr, 0); |
| 68 | + if (data() == nullptr) return uv_buf_init(nullptr, 0); |
| 69 | + |
| 70 | + CHECK_NOT_NULL(env_); |
| 71 | + uv_buf_t ret = uv_buf_init(data(), size()); |
| 72 | + env_->released_allocated_buffers()->emplace( |
| 73 | + ret.base, std::move(backing_store_)); |
39 | 74 | return ret; |
40 | 75 | } |
41 | 76 |
|
42 | 77 | inline char* AllocatedBuffer::data() { |
43 | | - return buffer_.base; |
| 78 | + if (!backing_store_) return nullptr; |
| 79 | + return static_cast<char*>(backing_store_->Data()); |
44 | 80 | } |
45 | 81 |
|
46 | 82 | inline const char* AllocatedBuffer::data() const { |
47 | | - return buffer_.base; |
| 83 | + if (!backing_store_) return nullptr; |
| 84 | + return static_cast<char*>(backing_store_->Data()); |
48 | 85 | } |
49 | 86 |
|
50 | | -inline size_t AllocatedBuffer::size() const { |
51 | | - return buffer_.len; |
52 | | -} |
53 | | - |
54 | | -inline AllocatedBuffer::AllocatedBuffer(Environment* env) |
55 | | - : env_(env), buffer_(uv_buf_init(nullptr, 0)) {} |
56 | | - |
57 | | -inline AllocatedBuffer::AllocatedBuffer(AllocatedBuffer&& other) |
58 | | - : AllocatedBuffer() { |
59 | | - *this = std::move(other); |
60 | | -} |
61 | | - |
62 | | -inline AllocatedBuffer& AllocatedBuffer::operator=(AllocatedBuffer&& other) { |
63 | | - clear(); |
64 | | - env_ = other.env_; |
65 | | - buffer_ = other.release(); |
66 | | - return *this; |
67 | | -} |
68 | 87 |
|
69 | | -inline AllocatedBuffer::~AllocatedBuffer() { |
70 | | - clear(); |
| 88 | +inline size_t AllocatedBuffer::size() const { |
| 89 | + if (!backing_store_) return 0; |
| 90 | + return backing_store_->ByteLength(); |
71 | 91 | } |
72 | 92 |
|
73 | 93 | inline void AllocatedBuffer::clear() { |
74 | | - uv_buf_t buf = release(); |
75 | | - if (buf.base != nullptr) { |
76 | | - CHECK_NOT_NULL(env_); |
77 | | - env_->Free(buf.base, buf.len); |
78 | | - } |
| 94 | + backing_store_.reset(); |
79 | 95 | } |
80 | 96 |
|
81 | 97 | inline v8::MaybeLocal<v8::Object> AllocatedBuffer::ToBuffer() { |
82 | | - CHECK_NOT_NULL(env_); |
83 | | - v8::MaybeLocal<v8::Object> obj = Buffer::New(env_, data(), size(), false); |
84 | | - if (!obj.IsEmpty()) release(); |
85 | | - return obj; |
| 98 | + v8::Local<v8::ArrayBuffer> ab = ToArrayBuffer(); |
| 99 | + return Buffer::New(env_, ab, 0, ab->ByteLength()) |
| 100 | + .FromMaybe(v8::Local<v8::Uint8Array>()); |
86 | 101 | } |
87 | 102 |
|
88 | 103 | inline v8::Local<v8::ArrayBuffer> AllocatedBuffer::ToArrayBuffer() { |
89 | | - CHECK_NOT_NULL(env_); |
90 | | - uv_buf_t buf = release(); |
91 | | - auto callback = [](void* data, size_t length, void* deleter_data){ |
92 | | - CHECK_NOT_NULL(deleter_data); |
93 | | - |
94 | | - static_cast<v8::ArrayBuffer::Allocator*>(deleter_data) |
95 | | - ->Free(data, length); |
96 | | - }; |
97 | | - std::unique_ptr<v8::BackingStore> backing = |
98 | | - v8::ArrayBuffer::NewBackingStore(buf.base, |
99 | | - buf.len, |
100 | | - callback, |
101 | | - env_->isolate() |
102 | | - ->GetArrayBufferAllocator()); |
103 | | - return v8::ArrayBuffer::New(env_->isolate(), |
104 | | - std::move(backing)); |
| 104 | + return v8::ArrayBuffer::New(env_->isolate(), std::move(backing_store_)); |
105 | 105 | } |
106 | 106 |
|
107 | 107 | } // namespace node |
|
0 commit comments