[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 | |
| 5 | #include "gin/array_buffer.h" |
| 6 | |
| 7 | #include <stdlib.h> |
| 8 | |
| 9 | namespace gin { |
| 10 | |
| 11 | COMPILE_ASSERT(V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT == 2, |
| 12 | array_buffers_must_have_two_internal_fields); |
| 13 | |
| 14 | static const int kBufferViewPrivateIndex = 0; |
| 15 | |
| 16 | // ArrayBufferAllocator ------------------------------------------------------- |
| 17 | |
| 18 | void* ArrayBufferAllocator::Allocate(size_t length) { |
| 19 | return calloc(1, length); |
| 20 | } |
| 21 | |
| 22 | void* ArrayBufferAllocator::AllocateUninitialized(size_t length) { |
| 23 | return malloc(length); |
| 24 | } |
| 25 | |
| 26 | void ArrayBufferAllocator::Free(void* data, size_t length) { |
| 27 | free(data); |
| 28 | } |
| 29 | |
| 30 | ArrayBufferAllocator* ArrayBufferAllocator::SharedInstance() { |
| 31 | static ArrayBufferAllocator* instance = new ArrayBufferAllocator(); |
| 32 | return instance; |
| 33 | } |
| 34 | |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 35 | // ArrayBuffer::Private ------------------------------------------------------- |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 36 | |
| 37 | // This class exists to solve a tricky lifetime problem. The V8 API doesn't |
| 38 | // want to expose a direct view into the memory behind an array buffer because |
| 39 | // V8 might deallocate that memory during garbage collection. Instead, the V8 |
| 40 | // API forces us to externalize the buffer and take ownership of the memory. |
| 41 | // In order to know when to free the memory, we need to figure out both when |
| 42 | // we're done with it and when V8 is done with it. |
| 43 | // |
| 44 | // 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] | 45 | // the array buffer takes a reference to the ArrayBuffer::Private object that |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 46 | // actually owns the memory. To determine when V8 is done with the memory, we |
| 47 | // open a weak handle to the ArrayBuffer object. When we receive the weak |
| 48 | // callback, we know the object is about to be garbage collected and we can |
| 49 | // drop V8's implied reference to the memory. |
| 50 | // |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 51 | // The final subtlety is that we need every ArrayBuffer into the same array |
| 52 | // buffer to AddRef the same ArrayBuffer::Private. To make that work, we store |
| 53 | // a pointer to the ArrayBuffer::Private object in an internal field of the |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 54 | // ArrayBuffer object. |
| 55 | // |
[email protected] | 855ab43 | 2013-11-18 17:09:36 | [diff] [blame] | 56 | class ArrayBuffer::Private : public base::RefCounted<ArrayBuffer::Private> { |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 57 | public: |
| 58 | static scoped_refptr<Private> From(v8::Isolate* isolate, |
| 59 | v8::Handle<v8::ArrayBuffer> array); |
| 60 | |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 61 | void* buffer() const { return buffer_; } |
| 62 | size_t length() const { return length_; } |
| 63 | |
| 64 | private: |
[email protected] | 855ab43 | 2013-11-18 17:09:36 | [diff] [blame] | 65 | friend class base::RefCounted<Private>; |
| 66 | |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 67 | Private(v8::Isolate* isolate, v8::Handle<v8::ArrayBuffer> array); |
| 68 | ~Private(); |
| 69 | |
| 70 | static void WeakCallback( |
| 71 | const v8::WeakCallbackData<v8::ArrayBuffer, Private>& data); |
| 72 | |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 73 | v8::Persistent<v8::ArrayBuffer> array_buffer_; |
[email protected] | 855ab43 | 2013-11-18 17:09:36 | [diff] [blame] | 74 | scoped_refptr<Private> self_reference_; |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 75 | void* buffer_; |
| 76 | size_t length_; |
| 77 | }; |
| 78 | |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 79 | scoped_refptr<ArrayBuffer::Private> ArrayBuffer::Private::From( |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 80 | v8::Isolate* isolate, v8::Handle<v8::ArrayBuffer> array) { |
| 81 | if (array->IsExternal()) { |
| 82 | return make_scoped_refptr(static_cast<Private*>( |
| 83 | array->GetAlignedPointerFromInternalField(kBufferViewPrivateIndex))); |
| 84 | } |
| 85 | return make_scoped_refptr(new Private(isolate, array)); |
| 86 | } |
| 87 | |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 88 | ArrayBuffer::Private::Private(v8::Isolate* isolate, |
| 89 | v8::Handle<v8::ArrayBuffer> array) |
[email protected] | 855ab43 | 2013-11-18 17:09:36 | [diff] [blame] | 90 | : array_buffer_(isolate, array) { |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 91 | // Take ownership of the array buffer. |
| 92 | v8::ArrayBuffer::Contents contents = array->Externalize(); |
| 93 | buffer_ = contents.Data(); |
| 94 | length_ = contents.ByteLength(); |
| 95 | |
| 96 | array->SetAlignedPointerInInternalField(kBufferViewPrivateIndex, this); |
| 97 | |
[email protected] | 855ab43 | 2013-11-18 17:09:36 | [diff] [blame] | 98 | self_reference_ = this; // Cleared in WeakCallback. |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 99 | array_buffer_.SetWeak(this, WeakCallback); |
| 100 | } |
| 101 | |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 102 | ArrayBuffer::Private::~Private() { |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 103 | ArrayBufferAllocator::SharedInstance()->Free(buffer_, length_); |
| 104 | } |
| 105 | |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 106 | void ArrayBuffer::Private::WeakCallback( |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 107 | const v8::WeakCallbackData<v8::ArrayBuffer, Private>& data) { |
| 108 | Private* parameter = data.GetParameter(); |
| 109 | parameter->array_buffer_.Reset(); |
[email protected] | 93f9f360 | 2013-11-21 18:38:51 | [diff] [blame] | 110 | parameter->self_reference_ = NULL; |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 111 | } |
| 112 | |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 113 | // ArrayBuffer ---------------------------------------------------------------- |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 114 | |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 115 | ArrayBuffer::ArrayBuffer() |
| 116 | : bytes_(0), |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 117 | num_bytes_(0) { |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 118 | } |
| 119 | |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 120 | ArrayBuffer::ArrayBuffer(v8::Isolate* isolate, |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 121 | v8::Handle<v8::ArrayBuffer> array) { |
| 122 | private_ = ArrayBuffer::Private::From(isolate, array); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 123 | bytes_ = private_->buffer(); |
| 124 | num_bytes_ = private_->length(); |
| 125 | } |
| 126 | |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 127 | ArrayBuffer::~ArrayBuffer() { |
| 128 | } |
| 129 | |
| 130 | // Converter<ArrayBuffer> ----------------------------------------------------- |
| 131 | |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 132 | bool Converter<ArrayBuffer>::FromV8(v8::Isolate* isolate, |
| 133 | v8::Handle<v8::Value> val, |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 134 | ArrayBuffer* out) { |
| 135 | if (!val->IsArrayBuffer()) |
| 136 | return false; |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 137 | *out = ArrayBuffer(isolate, v8::Handle<v8::ArrayBuffer>::Cast(val)); |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 138 | return true; |
| 139 | } |
| 140 | |
| 141 | // ArrayBufferView ------------------------------------------------------------ |
| 142 | |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 143 | ArrayBufferView::ArrayBufferView() |
| 144 | : offset_(0), |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 145 | num_bytes_(0) { |
| 146 | } |
| 147 | |
| 148 | ArrayBufferView::ArrayBufferView(v8::Isolate* isolate, |
| 149 | v8::Handle<v8::ArrayBufferView> view) |
| 150 | : array_buffer_(isolate, view->Buffer()), |
| 151 | offset_(view->ByteOffset()), |
| 152 | num_bytes_(view->ByteLength()) { |
| 153 | } |
| 154 | |
| 155 | ArrayBufferView::~ArrayBufferView() { |
| 156 | } |
| 157 | |
| 158 | // Converter<ArrayBufferView> ------------------------------------------------- |
| 159 | |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 160 | bool Converter<ArrayBufferView>::FromV8(v8::Isolate* isolate, |
| 161 | v8::Handle<v8::Value> val, |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 162 | ArrayBufferView* out) { |
| 163 | if (!val->IsArrayBufferView()) |
| 164 | return false; |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 165 | *out = ArrayBufferView(isolate, v8::Handle<v8::ArrayBufferView>::Cast(val)); |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 166 | return true; |
| 167 | } |
| 168 | |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 169 | } // namespace gin |