Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(339)

Side by Side Diff: gin/array_buffer.cc

Issue 74753002: Introduce gin_shell (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix more header nits Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « gin/array_buffer.h ('k') | gin/context_holder.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "gin/array_buffer.h" 5 #include "gin/array_buffer.h"
6 6
7 #include <stdlib.h> 7 #include <stdlib.h>
8 8
9 namespace gin { 9 namespace gin {
10 10
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 // actually owns the memory. To determine when V8 is done with the memory, we 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 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 48 // callback, we know the object is about to be garbage collected and we can
49 // drop V8's implied reference to the memory. 49 // drop V8's implied reference to the memory.
50 // 50 //
51 // The final subtlety is that we need every ArrayBuffer into the same array 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 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 53 // a pointer to the ArrayBuffer::Private object in an internal field of the
54 // ArrayBuffer object. 54 // ArrayBuffer object.
55 // 55 //
56 class ArrayBuffer::Private { 56 class ArrayBuffer::Private : public base::RefCounted<ArrayBuffer::Private> {
57 public: 57 public:
58 static scoped_refptr<Private> From(v8::Isolate* isolate, 58 static scoped_refptr<Private> From(v8::Isolate* isolate,
59 v8::Handle<v8::ArrayBuffer> array); 59 v8::Handle<v8::ArrayBuffer> array);
60 60
61 void AddRef();
62 void Release();
63
64 void* buffer() const { return buffer_; } 61 void* buffer() const { return buffer_; }
65 size_t length() const { return length_; } 62 size_t length() const { return length_; }
66 63
67 private: 64 private:
65 friend class base::RefCounted<Private>;
66
68 Private(v8::Isolate* isolate, v8::Handle<v8::ArrayBuffer> array); 67 Private(v8::Isolate* isolate, v8::Handle<v8::ArrayBuffer> array);
69 ~Private(); 68 ~Private();
70 69
71 static void WeakCallback( 70 static void WeakCallback(
72 const v8::WeakCallbackData<v8::ArrayBuffer, Private>& data); 71 const v8::WeakCallbackData<v8::ArrayBuffer, Private>& data);
73 72
74 size_t ref_count_;
75 v8::Persistent<v8::ArrayBuffer> array_buffer_; 73 v8::Persistent<v8::ArrayBuffer> array_buffer_;
74 scoped_refptr<Private> self_reference_;
76 void* buffer_; 75 void* buffer_;
77 size_t length_; 76 size_t length_;
78 }; 77 };
79 78
80 scoped_refptr<ArrayBuffer::Private> ArrayBuffer::Private::From( 79 scoped_refptr<ArrayBuffer::Private> ArrayBuffer::Private::From(
81 v8::Isolate* isolate, v8::Handle<v8::ArrayBuffer> array) { 80 v8::Isolate* isolate, v8::Handle<v8::ArrayBuffer> array) {
82 if (array->IsExternal()) { 81 if (array->IsExternal()) {
83 return make_scoped_refptr(static_cast<Private*>( 82 return make_scoped_refptr(static_cast<Private*>(
84 array->GetAlignedPointerFromInternalField(kBufferViewPrivateIndex))); 83 array->GetAlignedPointerFromInternalField(kBufferViewPrivateIndex)));
85 } 84 }
86 return make_scoped_refptr(new Private(isolate, array)); 85 return make_scoped_refptr(new Private(isolate, array));
87 } 86 }
88 87
89 void ArrayBuffer::Private::AddRef() {
90 ++ref_count_;
91 }
92
93 void ArrayBuffer::Private::Release() {
94 if (--ref_count_)
95 return;
96 delete this;
97 }
98
99 ArrayBuffer::Private::Private(v8::Isolate* isolate, 88 ArrayBuffer::Private::Private(v8::Isolate* isolate,
100 v8::Handle<v8::ArrayBuffer> array) 89 v8::Handle<v8::ArrayBuffer> array)
101 : ref_count_(0), 90 : array_buffer_(isolate, array) {
102 array_buffer_(isolate, array) {
103 // Take ownership of the array buffer. 91 // Take ownership of the array buffer.
104 v8::ArrayBuffer::Contents contents = array->Externalize(); 92 v8::ArrayBuffer::Contents contents = array->Externalize();
105 buffer_ = contents.Data(); 93 buffer_ = contents.Data();
106 length_ = contents.ByteLength(); 94 length_ = contents.ByteLength();
107 95
108 array->SetAlignedPointerInInternalField(kBufferViewPrivateIndex, this); 96 array->SetAlignedPointerInInternalField(kBufferViewPrivateIndex, this);
109 97
110 AddRef(); // Balanced in WeakCallback. 98 self_reference_ = this; // Cleared in WeakCallback.
111 array_buffer_.SetWeak(this, WeakCallback); 99 array_buffer_.SetWeak(this, WeakCallback);
112 } 100 }
113 101
114 ArrayBuffer::Private::~Private() { 102 ArrayBuffer::Private::~Private() {
115 ArrayBufferAllocator::SharedInstance()->Free(buffer_, length_); 103 ArrayBufferAllocator::SharedInstance()->Free(buffer_, length_);
116 } 104 }
117 105
118 void ArrayBuffer::Private::WeakCallback( 106 void ArrayBuffer::Private::WeakCallback(
119 const v8::WeakCallbackData<v8::ArrayBuffer, Private>& data) { 107 const v8::WeakCallbackData<v8::ArrayBuffer, Private>& data) {
120 Private* parameter = data.GetParameter(); 108 Private* parameter = data.GetParameter();
121 parameter->array_buffer_.Reset(); 109 parameter->array_buffer_.Reset();
122 parameter->Release(); // Balanced in ArrayBuffer::Private::Private. 110 // parameter->self_reference_.clear();
123 } 111 }
124 112
125 // ArrayBuffer ---------------------------------------------------------------- 113 // ArrayBuffer ----------------------------------------------------------------
126 114
127 ArrayBuffer::ArrayBuffer(v8::Isolate* isolate) 115 ArrayBuffer::ArrayBuffer(v8::Isolate* isolate)
128 : isolate_(isolate), 116 : isolate_(isolate),
129 bytes_(0), 117 bytes_(0),
130 num_bytes_(0) { 118 num_bytes_(0) {
131 } 119 }
132 120
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 bool Converter<ArrayBufferView>::FromV8(v8::Handle<v8::Value> val, 162 bool Converter<ArrayBufferView>::FromV8(v8::Handle<v8::Value> val,
175 ArrayBufferView* out) { 163 ArrayBufferView* out) {
176 if (!val->IsArrayBufferView()) 164 if (!val->IsArrayBufferView())
177 return false; 165 return false;
178 *out = ArrayBufferView(out->isolate(), 166 *out = ArrayBufferView(out->isolate(),
179 v8::Handle<v8::ArrayBufferView>::Cast(val)); 167 v8::Handle<v8::ArrayBufferView>::Cast(val));
180 return true; 168 return true;
181 } 169 }
182 170
183 } // namespace gin 171 } // namespace gin
OLDNEW
« no previous file with comments | « gin/array_buffer.h ('k') | gin/context_holder.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698