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