blob: 43dd8da348fa6a3a1125523339f7fd2a9c5ebfd3 [file] [log] [blame]
[email protected]a22998a2013-11-10 05:00:501// 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
9namespace gin {
10
11COMPILE_ASSERT(V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT == 2,
12 array_buffers_must_have_two_internal_fields);
13
14static const int kBufferViewPrivateIndex = 0;
15
16// ArrayBufferAllocator -------------------------------------------------------
17
18void* ArrayBufferAllocator::Allocate(size_t length) {
19 return calloc(1, length);
20}
21
22void* ArrayBufferAllocator::AllocateUninitialized(size_t length) {
23 return malloc(length);
24}
25
26void ArrayBufferAllocator::Free(void* data, size_t length) {
27 free(data);
28}
29
30ArrayBufferAllocator* ArrayBufferAllocator::SharedInstance() {
31 static ArrayBufferAllocator* instance = new ArrayBufferAllocator();
32 return instance;
33}
34
[email protected]e87f3122013-11-12 00:41:2735// ArrayBuffer::Private -------------------------------------------------------
[email protected]a22998a2013-11-10 05:00:5036
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]e87f3122013-11-12 00:41:2745// the array buffer takes a reference to the ArrayBuffer::Private object that
[email protected]a22998a2013-11-10 05:00:5046// 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]e87f3122013-11-12 00:41:2751// 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]a22998a2013-11-10 05:00:5054// ArrayBuffer object.
55//
[email protected]e87f3122013-11-12 00:41:2756class ArrayBuffer::Private {
[email protected]a22998a2013-11-10 05:00:5057 public:
58 static scoped_refptr<Private> From(v8::Isolate* isolate,
59 v8::Handle<v8::ArrayBuffer> array);
60
61 void AddRef();
62 void Release();
63
64 void* buffer() const { return buffer_; }
65 size_t length() const { return length_; }
66
67 private:
68 Private(v8::Isolate* isolate, v8::Handle<v8::ArrayBuffer> array);
69 ~Private();
70
71 static void WeakCallback(
72 const v8::WeakCallbackData<v8::ArrayBuffer, Private>& data);
73
74 size_t ref_count_;
75 v8::Persistent<v8::ArrayBuffer> array_buffer_;
76 void* buffer_;
77 size_t length_;
78};
79
[email protected]e87f3122013-11-12 00:41:2780scoped_refptr<ArrayBuffer::Private> ArrayBuffer::Private::From(
[email protected]a22998a2013-11-10 05:00:5081 v8::Isolate* isolate, v8::Handle<v8::ArrayBuffer> array) {
82 if (array->IsExternal()) {
83 return make_scoped_refptr(static_cast<Private*>(
84 array->GetAlignedPointerFromInternalField(kBufferViewPrivateIndex)));
85 }
86 return make_scoped_refptr(new Private(isolate, array));
87}
88
[email protected]e87f3122013-11-12 00:41:2789void ArrayBuffer::Private::AddRef() {
[email protected]a22998a2013-11-10 05:00:5090 ++ref_count_;
91}
92
[email protected]e87f3122013-11-12 00:41:2793void ArrayBuffer::Private::Release() {
[email protected]a22998a2013-11-10 05:00:5094 if (--ref_count_)
95 return;
96 delete this;
97}
98
[email protected]e87f3122013-11-12 00:41:2799ArrayBuffer::Private::Private(v8::Isolate* isolate,
100 v8::Handle<v8::ArrayBuffer> array)
[email protected]a22998a2013-11-10 05:00:50101 : ref_count_(0),
102 array_buffer_(isolate, array) {
103 // Take ownership of the array buffer.
104 v8::ArrayBuffer::Contents contents = array->Externalize();
105 buffer_ = contents.Data();
106 length_ = contents.ByteLength();
107
108 array->SetAlignedPointerInInternalField(kBufferViewPrivateIndex, this);
109
110 AddRef(); // Balanced in WeakCallback.
111 array_buffer_.SetWeak(this, WeakCallback);
112}
113
[email protected]e87f3122013-11-12 00:41:27114ArrayBuffer::Private::~Private() {
[email protected]a22998a2013-11-10 05:00:50115 ArrayBufferAllocator::SharedInstance()->Free(buffer_, length_);
116}
117
[email protected]e87f3122013-11-12 00:41:27118void ArrayBuffer::Private::WeakCallback(
[email protected]a22998a2013-11-10 05:00:50119 const v8::WeakCallbackData<v8::ArrayBuffer, Private>& data) {
120 Private* parameter = data.GetParameter();
121 parameter->array_buffer_.Reset();
[email protected]e87f3122013-11-12 00:41:27122 parameter->Release(); // Balanced in ArrayBuffer::Private::Private.
[email protected]a22998a2013-11-10 05:00:50123}
124
[email protected]e87f3122013-11-12 00:41:27125// ArrayBuffer ----------------------------------------------------------------
[email protected]a22998a2013-11-10 05:00:50126
[email protected]e87f3122013-11-12 00:41:27127ArrayBuffer::ArrayBuffer(v8::Isolate* isolate)
128 : isolate_(isolate),
129 bytes_(0),
130 num_bytes_(0) {
[email protected]a22998a2013-11-10 05:00:50131}
132
[email protected]e87f3122013-11-12 00:41:27133ArrayBuffer::ArrayBuffer(v8::Isolate* isolate,
134 v8::Handle<v8::ArrayBuffer> array)
135 : isolate_(isolate) {
136 private_ = ArrayBuffer::Private::From(isolate_, array);
[email protected]a22998a2013-11-10 05:00:50137 bytes_ = private_->buffer();
138 num_bytes_ = private_->length();
139}
140
[email protected]e87f3122013-11-12 00:41:27141ArrayBuffer::~ArrayBuffer() {
142}
143
144// Converter<ArrayBuffer> -----------------------------------------------------
145
146bool Converter<ArrayBuffer>::FromV8(v8::Handle<v8::Value> val,
147 ArrayBuffer* out) {
148 if (!val->IsArrayBuffer())
149 return false;
150 *out = ArrayBuffer(out->isolate(), v8::Handle<v8::ArrayBuffer>::Cast(val));
151 return true;
152}
153
154// ArrayBufferView ------------------------------------------------------------
155
156ArrayBufferView::ArrayBufferView(v8::Isolate* isolate)
157 : array_buffer_(isolate),
158 offset_(0),
159 num_bytes_(0) {
160}
161
162ArrayBufferView::ArrayBufferView(v8::Isolate* isolate,
163 v8::Handle<v8::ArrayBufferView> view)
164 : array_buffer_(isolate, view->Buffer()),
165 offset_(view->ByteOffset()),
166 num_bytes_(view->ByteLength()) {
167}
168
169ArrayBufferView::~ArrayBufferView() {
170}
171
172// Converter<ArrayBufferView> -------------------------------------------------
173
174bool Converter<ArrayBufferView>::FromV8(v8::Handle<v8::Value> val,
175 ArrayBufferView* out) {
176 if (!val->IsArrayBufferView())
177 return false;
178 *out = ArrayBufferView(out->isolate(),
179 v8::Handle<v8::ArrayBufferView>::Cast(val));
180 return true;
181}
182
[email protected]a22998a2013-11-10 05:00:50183} // namespace gin