[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Bill Budge | 07c6ea6 | 2017-12-22 19:16:11 | [diff] [blame] | 5 | #include "gin/array_buffer.h" |
| 6 | |
avi | 90e658dd | 2015-12-21 07:16:19 | [diff] [blame] | 7 | #include <stddef.h> |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 8 | #include <stdlib.h> |
| 9 | |
Eric Holk | b2fa95d | 2017-07-17 18:51:36 | [diff] [blame] | 10 | #include "base/allocator/partition_allocator/page_allocator.h" |
[email protected] | 73dcce9 | 2014-02-20 08:24:04 | [diff] [blame] | 11 | #include "base/logging.h" |
Alexei Filippov | 2eea119 | 2018-03-01 18:01:03 | [diff] [blame] | 12 | #include "base/partition_alloc_buildflags.h" |
Eric Holk | b2fa95d | 2017-07-17 18:51:36 | [diff] [blame] | 13 | #include "build/build_config.h" |
[email protected] | 73dcce9 | 2014-02-20 08:24:04 | [diff] [blame] | 14 | #include "gin/per_isolate_data.h" |
| 15 | |
Eric Holk | b2fa95d | 2017-07-17 18:51:36 | [diff] [blame] | 16 | #if defined(OS_POSIX) |
| 17 | #include <sys/mman.h> |
| 18 | |
| 19 | #ifndef MAP_ANONYMOUS |
| 20 | #define MAP_ANONYMOUS MAP_ANON |
| 21 | #endif |
Bill Budge | 07c6ea6 | 2017-12-22 19:16:11 | [diff] [blame] | 22 | #endif // defined(OS_POSIX) |
Eric Holk | b2fa95d | 2017-07-17 18:51:36 | [diff] [blame] | 23 | |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 24 | namespace gin { |
| 25 | |
[email protected] | 73dcce9 | 2014-02-20 08:24:04 | [diff] [blame] | 26 | namespace { |
| 27 | |
| 28 | gin::WrapperInfo g_array_buffer_wrapper_info = {gin::kEmbedderNativeGin}; |
| 29 | |
| 30 | } // namespace |
| 31 | |
anujk.sharma | c1b6156b | 2015-01-20 21:47:36 | [diff] [blame] | 32 | static_assert(V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT == 2, |
| 33 | "array buffers must have two internal fields"); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 34 | |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 35 | // ArrayBufferAllocator ------------------------------------------------------- |
| 36 | |
| 37 | void* ArrayBufferAllocator::Allocate(size_t length) { |
Bill Budge | 07c6ea6 | 2017-12-22 19:16:11 | [diff] [blame] | 38 | // TODO(bbudge) Use partition allocator for malloc/calloc allocations. |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 39 | return calloc(1, length); |
| 40 | } |
| 41 | |
| 42 | void* ArrayBufferAllocator::AllocateUninitialized(size_t length) { |
| 43 | return malloc(length); |
| 44 | } |
| 45 | |
| 46 | void ArrayBufferAllocator::Free(void* data, size_t length) { |
| 47 | free(data); |
| 48 | } |
| 49 | |
| 50 | ArrayBufferAllocator* ArrayBufferAllocator::SharedInstance() { |
| 51 | static ArrayBufferAllocator* instance = new ArrayBufferAllocator(); |
| 52 | return instance; |
| 53 | } |
| 54 | |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 55 | // ArrayBuffer::Private ------------------------------------------------------- |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 56 | |
| 57 | // This class exists to solve a tricky lifetime problem. The V8 API doesn't |
| 58 | // want to expose a direct view into the memory behind an array buffer because |
| 59 | // V8 might deallocate that memory during garbage collection. Instead, the V8 |
| 60 | // API forces us to externalize the buffer and take ownership of the memory. |
| 61 | // In order to know when to free the memory, we need to figure out both when |
| 62 | // we're done with it and when V8 is done with it. |
| 63 | // |
| 64 | // To determine whether we're done with the memory, every view we have into |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 65 | // the array buffer takes a reference to the ArrayBuffer::Private object that |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 66 | // actually owns the memory. To determine when V8 is done with the memory, we |
| 67 | // open a weak handle to the ArrayBuffer object. When we receive the weak |
| 68 | // callback, we know the object is about to be garbage collected and we can |
| 69 | // drop V8's implied reference to the memory. |
| 70 | // |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 71 | // The final subtlety is that we need every ArrayBuffer into the same array |
| 72 | // buffer to AddRef the same ArrayBuffer::Private. To make that work, we store |
| 73 | // a pointer to the ArrayBuffer::Private object in an internal field of the |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 74 | // ArrayBuffer object. |
| 75 | // |
[email protected] | 855ab43 | 2013-11-18 17:09:36 | [diff] [blame] | 76 | class ArrayBuffer::Private : public base::RefCounted<ArrayBuffer::Private> { |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 77 | public: |
| 78 | static scoped_refptr<Private> From(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 79 | v8::Local<v8::ArrayBuffer> array); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 80 | |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 81 | void* buffer() const { return buffer_; } |
| 82 | size_t length() const { return length_; } |
| 83 | |
| 84 | private: |
[email protected] | 855ab43 | 2013-11-18 17:09:36 | [diff] [blame] | 85 | friend class base::RefCounted<Private>; |
Stephan Herhut | 3c2d9527 | 2018-09-24 18:51:54 | [diff] [blame] | 86 | using DataDeleter = void (*)(void* data, size_t length, void* info); |
[email protected] | 855ab43 | 2013-11-18 17:09:36 | [diff] [blame] | 87 | |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 88 | Private(v8::Isolate* isolate, v8::Local<v8::ArrayBuffer> array); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 89 | ~Private(); |
| 90 | |
dcarney | 99ade908 | 2015-04-22 09:55:42 | [diff] [blame] | 91 | static void FirstWeakCallback(const v8::WeakCallbackInfo<Private>& data); |
| 92 | static void SecondWeakCallback(const v8::WeakCallbackInfo<Private>& data); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 93 | |
dcarney | 99ade908 | 2015-04-22 09:55:42 | [diff] [blame] | 94 | v8::Global<v8::ArrayBuffer> array_buffer_; |
[email protected] | 855ab43 | 2013-11-18 17:09:36 | [diff] [blame] | 95 | scoped_refptr<Private> self_reference_; |
[email protected] | 73dcce9 | 2014-02-20 08:24:04 | [diff] [blame] | 96 | v8::Isolate* isolate_; |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 97 | void* buffer_; |
| 98 | size_t length_; |
Stephan Herhut | 3c2d9527 | 2018-09-24 18:51:54 | [diff] [blame] | 99 | DataDeleter deleter_; |
| 100 | void* deleter_data_; |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 101 | }; |
| 102 | |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 103 | scoped_refptr<ArrayBuffer::Private> ArrayBuffer::Private::From( |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 104 | v8::Isolate* isolate, v8::Local<v8::ArrayBuffer> array) { |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 105 | if (array->IsExternal()) { |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 106 | CHECK_EQ(WrapperInfo::From(v8::Local<v8::Object>::Cast(array)), |
[email protected] | 73dcce9 | 2014-02-20 08:24:04 | [diff] [blame] | 107 | &g_array_buffer_wrapper_info) |
| 108 | << "Cannot mix blink and gin ArrayBuffers"; |
kylechar | da69d88 | 2017-10-04 05:49:52 | [diff] [blame] | 109 | return base::WrapRefCounted(static_cast<Private*>( |
[email protected] | 73dcce9 | 2014-02-20 08:24:04 | [diff] [blame] | 110 | array->GetAlignedPointerFromInternalField(kEncodedValueIndex))); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 111 | } |
kylechar | da69d88 | 2017-10-04 05:49:52 | [diff] [blame] | 112 | return base::WrapRefCounted(new Private(isolate, array)); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 113 | } |
| 114 | |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 115 | ArrayBuffer::Private::Private(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 116 | v8::Local<v8::ArrayBuffer> array) |
[email protected] | 73dcce9 | 2014-02-20 08:24:04 | [diff] [blame] | 117 | : array_buffer_(isolate, array), isolate_(isolate) { |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 118 | // Take ownership of the array buffer. |
[email protected] | 73dcce9 | 2014-02-20 08:24:04 | [diff] [blame] | 119 | CHECK(!array->IsExternal()); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 120 | v8::ArrayBuffer::Contents contents = array->Externalize(); |
| 121 | buffer_ = contents.Data(); |
| 122 | length_ = contents.ByteLength(); |
Stephan Herhut | 3c2d9527 | 2018-09-24 18:51:54 | [diff] [blame] | 123 | deleter_ = contents.Deleter(); |
| 124 | deleter_data_ = contents.DeleterData(); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 125 | |
[email protected] | 73dcce9 | 2014-02-20 08:24:04 | [diff] [blame] | 126 | array->SetAlignedPointerInInternalField(kWrapperInfoIndex, |
| 127 | &g_array_buffer_wrapper_info); |
| 128 | array->SetAlignedPointerInInternalField(kEncodedValueIndex, this); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 129 | |
dcarney | 99ade908 | 2015-04-22 09:55:42 | [diff] [blame] | 130 | self_reference_ = this; // Cleared in SecondWeakCallback. |
| 131 | array_buffer_.SetWeak(this, FirstWeakCallback, |
| 132 | v8::WeakCallbackType::kParameter); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 133 | } |
| 134 | |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 135 | ArrayBuffer::Private::~Private() { |
Stephan Herhut | 3c2d9527 | 2018-09-24 18:51:54 | [diff] [blame] | 136 | deleter_(buffer_, length_, deleter_data_); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 137 | } |
| 138 | |
dcarney | 99ade908 | 2015-04-22 09:55:42 | [diff] [blame] | 139 | void ArrayBuffer::Private::FirstWeakCallback( |
| 140 | const v8::WeakCallbackInfo<Private>& data) { |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 141 | Private* parameter = data.GetParameter(); |
| 142 | parameter->array_buffer_.Reset(); |
dcarney | 99ade908 | 2015-04-22 09:55:42 | [diff] [blame] | 143 | data.SetSecondPassCallback(SecondWeakCallback); |
| 144 | } |
| 145 | |
| 146 | void ArrayBuffer::Private::SecondWeakCallback( |
| 147 | const v8::WeakCallbackInfo<Private>& data) { |
| 148 | Private* parameter = data.GetParameter(); |
[email protected] | 93f9f360 | 2013-11-21 18:38:51 | [diff] [blame] | 149 | parameter->self_reference_ = NULL; |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 150 | } |
| 151 | |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 152 | // ArrayBuffer ---------------------------------------------------------------- |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 153 | |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 154 | ArrayBuffer::ArrayBuffer() |
| 155 | : bytes_(0), |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 156 | num_bytes_(0) { |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 157 | } |
| 158 | |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 159 | ArrayBuffer::ArrayBuffer(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 160 | v8::Local<v8::ArrayBuffer> array) { |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 161 | private_ = ArrayBuffer::Private::From(isolate, array); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 162 | bytes_ = private_->buffer(); |
| 163 | num_bytes_ = private_->length(); |
| 164 | } |
| 165 | |
Chris Watkins | 756035a | 2017-12-01 03:03:27 | [diff] [blame] | 166 | ArrayBuffer::~ArrayBuffer() = default; |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 167 | |
Chris Watkins | 756035a | 2017-12-01 03:03:27 | [diff] [blame] | 168 | ArrayBuffer& ArrayBuffer::operator=(const ArrayBuffer& other) = default; |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 169 | |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 170 | // Converter<ArrayBuffer> ----------------------------------------------------- |
| 171 | |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 172 | bool Converter<ArrayBuffer>::FromV8(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 173 | v8::Local<v8::Value> val, |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 174 | ArrayBuffer* out) { |
| 175 | if (!val->IsArrayBuffer()) |
| 176 | return false; |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 177 | *out = ArrayBuffer(isolate, v8::Local<v8::ArrayBuffer>::Cast(val)); |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 178 | return true; |
| 179 | } |
| 180 | |
| 181 | // ArrayBufferView ------------------------------------------------------------ |
| 182 | |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 183 | ArrayBufferView::ArrayBufferView() |
| 184 | : offset_(0), |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 185 | num_bytes_(0) { |
| 186 | } |
| 187 | |
| 188 | ArrayBufferView::ArrayBufferView(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 189 | v8::Local<v8::ArrayBufferView> view) |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 190 | : array_buffer_(isolate, view->Buffer()), |
| 191 | offset_(view->ByteOffset()), |
| 192 | num_bytes_(view->ByteLength()) { |
| 193 | } |
| 194 | |
Chris Watkins | 756035a | 2017-12-01 03:03:27 | [diff] [blame] | 195 | ArrayBufferView::~ArrayBufferView() = default; |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 196 | |
Chris Watkins | 756035a | 2017-12-01 03:03:27 | [diff] [blame] | 197 | ArrayBufferView& ArrayBufferView::operator=(const ArrayBufferView& other) = |
| 198 | default; |
[email protected] | dfc613d | 2014-05-16 13:16:52 | [diff] [blame] | 199 | |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 200 | // Converter<ArrayBufferView> ------------------------------------------------- |
| 201 | |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 202 | bool Converter<ArrayBufferView>::FromV8(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 203 | v8::Local<v8::Value> val, |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 204 | ArrayBufferView* out) { |
| 205 | if (!val->IsArrayBufferView()) |
| 206 | return false; |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 207 | *out = ArrayBufferView(isolate, v8::Local<v8::ArrayBufferView>::Cast(val)); |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 208 | return true; |
| 209 | } |
| 210 | |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 211 | } // namespace gin |