summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/wtf/typed_arrays/array_buffer.h
blob: 39f91ea147b7ad0c7c0c02dfbba61be1995948d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * Copyright (C) 2009 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_WTF_TYPED_ARRAYS_ARRAY_BUFFER_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_WTF_TYPED_ARRAYS_ARRAY_BUFFER_H_

#include "base/allocator/partition_allocator/oom.h"
#include "base/memory/scoped_refptr.h"
#include "third_party/blink/renderer/platform/wtf/assertions.h"
#include "third_party/blink/renderer/platform/wtf/hash_set.h"
#include "third_party/blink/renderer/platform/wtf/ref_counted.h"
#include "third_party/blink/renderer/platform/wtf/typed_arrays/array_buffer_contents.h"
#include "third_party/blink/renderer/platform/wtf/wtf_export.h"

namespace WTF {

class ArrayBuffer;
class ArrayBufferView;

class WTF_EXPORT ArrayBuffer : public RefCounted<ArrayBuffer> {
 public:
  static inline scoped_refptr<ArrayBuffer> Create(unsigned num_elements,
                                                  unsigned element_byte_size);
  static inline scoped_refptr<ArrayBuffer> Create(ArrayBuffer*);
  static inline scoped_refptr<ArrayBuffer> Create(const void* source,
                                                  size_t byte_length);
  static inline scoped_refptr<ArrayBuffer> Create(ArrayBufferContents&);

  static inline scoped_refptr<ArrayBuffer> CreateOrNull(
      unsigned num_elements,
      unsigned element_byte_size);

  // Only for use by DOMArrayBuffer::CreateUninitializedOrNull().
  static inline scoped_refptr<ArrayBuffer> CreateUninitializedOrNull(
      unsigned num_elements,
      unsigned element_byte_size);

  static inline scoped_refptr<ArrayBuffer> CreateShared(
      unsigned num_elements,
      unsigned element_byte_size);
  static inline scoped_refptr<ArrayBuffer> CreateShared(const void* source,
                                                        unsigned byte_length);

  inline void* Data();
  inline const void* Data() const;
  inline void* DataShared();
  inline const void* DataShared() const;
  inline void* DataMaybeShared();
  inline const void* DataMaybeShared() const;
  inline unsigned ByteLength() const;

  // Creates a new ArrayBuffer object with copy of bytes in this object
  // ranging from |begin| up to but not including |end|.
  inline scoped_refptr<ArrayBuffer> Slice(int begin, int end) const;
  inline scoped_refptr<ArrayBuffer> Slice(int begin) const;

  void AddView(ArrayBufferView*);
  void RemoveView(ArrayBufferView*);

  bool Transfer(ArrayBufferContents&);
  bool ShareContentsWith(ArrayBufferContents&);
  bool IsNeutered() const { return is_neutered_; }
  bool IsShared() const { return contents_.IsShared(); }

  ~ArrayBuffer() = default;

 protected:
  inline explicit ArrayBuffer(ArrayBufferContents&);

 private:
  static inline scoped_refptr<ArrayBuffer> Create(
      unsigned num_elements,
      unsigned element_byte_size,
      ArrayBufferContents::InitializationPolicy);
  static inline scoped_refptr<ArrayBuffer> CreateOrNull(
      unsigned num_elements,
      unsigned element_byte_size,
      ArrayBufferContents::InitializationPolicy);
  static inline scoped_refptr<ArrayBuffer> CreateShared(
      unsigned num_elements,
      unsigned element_byte_size,
      ArrayBufferContents::InitializationPolicy);

  inline scoped_refptr<ArrayBuffer> SliceImpl(unsigned begin,
                                              unsigned end) const;
  inline unsigned ClampIndex(int index) const;
  static inline unsigned ClampValue(int x, unsigned left, unsigned right);

  ArrayBufferContents contents_;
  ArrayBufferView* first_view_;
  bool is_neutered_;
};

unsigned ArrayBuffer::ClampValue(int x, unsigned left, unsigned right) {
  DCHECK_LE(left, right);
  unsigned result;
  if (x < 0)
    result = left;
  else
    result = static_cast<unsigned>(x);

  if (result < left)
    result = left;
  if (right < result)
    result = right;
  return result;
}

scoped_refptr<ArrayBuffer> ArrayBuffer::Create(unsigned num_elements,
                                               unsigned element_byte_size) {
  return Create(num_elements, element_byte_size,
                ArrayBufferContents::kZeroInitialize);
}

scoped_refptr<ArrayBuffer> ArrayBuffer::Create(ArrayBuffer* other) {
  // TODO(binji): support creating a SharedArrayBuffer by copying another
  // ArrayBuffer?
  DCHECK(!other->IsShared());
  return ArrayBuffer::Create(other->Data(), other->ByteLength());
}

scoped_refptr<ArrayBuffer> ArrayBuffer::Create(const void* source,
                                               size_t byte_length) {
  ArrayBufferContents contents(byte_length, 1, ArrayBufferContents::kNotShared,
                               ArrayBufferContents::kDontInitialize);
  if (UNLIKELY(!contents.Data()))
    OOM_CRASH();
  scoped_refptr<ArrayBuffer> buffer = base::AdoptRef(new ArrayBuffer(contents));
  memcpy(buffer->Data(), source, byte_length);
  return buffer;
}

scoped_refptr<ArrayBuffer> ArrayBuffer::Create(ArrayBufferContents& contents) {
  CHECK(contents.DataMaybeShared());
  return base::AdoptRef(new ArrayBuffer(contents));
}

scoped_refptr<ArrayBuffer> ArrayBuffer::CreateOrNull(
    unsigned num_elements,
    unsigned element_byte_size) {
  return CreateOrNull(num_elements, element_byte_size,
                      ArrayBufferContents::kZeroInitialize);
}

scoped_refptr<ArrayBuffer> ArrayBuffer::CreateUninitializedOrNull(
    unsigned num_elements,
    unsigned element_byte_size) {
  return CreateOrNull(num_elements, element_byte_size,
                      ArrayBufferContents::kDontInitialize);
}

scoped_refptr<ArrayBuffer> ArrayBuffer::Create(
    unsigned num_elements,
    unsigned element_byte_size,
    ArrayBufferContents::InitializationPolicy policy) {
  ArrayBufferContents contents(num_elements, element_byte_size,
                               ArrayBufferContents::kNotShared, policy);
  if (UNLIKELY(!contents.Data()))
    OOM_CRASH();
  return base::AdoptRef(new ArrayBuffer(contents));
}

scoped_refptr<ArrayBuffer> ArrayBuffer::CreateOrNull(
    unsigned num_elements,
    unsigned element_byte_size,
    ArrayBufferContents::InitializationPolicy policy) {
  ArrayBufferContents contents(num_elements, element_byte_size,
                               ArrayBufferContents::kNotShared, policy);
  if (!contents.Data())
    return nullptr;
  return base::AdoptRef(new ArrayBuffer(contents));
}

scoped_refptr<ArrayBuffer> ArrayBuffer::CreateShared(
    unsigned num_elements,
    unsigned element_byte_size) {
  return CreateShared(num_elements, element_byte_size,
                      ArrayBufferContents::kZeroInitialize);
}

scoped_refptr<ArrayBuffer> ArrayBuffer::CreateShared(const void* source,
                                                     unsigned byte_length) {
  ArrayBufferContents contents(byte_length, 1, ArrayBufferContents::kShared,
                               ArrayBufferContents::kDontInitialize);
  CHECK(contents.DataShared());
  scoped_refptr<ArrayBuffer> buffer = base::AdoptRef(new ArrayBuffer(contents));
  memcpy(buffer->DataShared(), source, byte_length);
  return buffer;
}

scoped_refptr<ArrayBuffer> ArrayBuffer::CreateShared(
    unsigned num_elements,
    unsigned element_byte_size,
    ArrayBufferContents::InitializationPolicy policy) {
  ArrayBufferContents contents(num_elements, element_byte_size,
                               ArrayBufferContents::kShared, policy);
  CHECK(contents.DataShared());
  return base::AdoptRef(new ArrayBuffer(contents));
}

ArrayBuffer::ArrayBuffer(ArrayBufferContents& contents)
    : first_view_(nullptr), is_neutered_(false) {
  if (contents.IsShared())
    contents.ShareWith(contents_);
  else
    contents.Transfer(contents_);
}

void* ArrayBuffer::Data() {
  return contents_.Data();
}

const void* ArrayBuffer::Data() const {
  return contents_.Data();
}

void* ArrayBuffer::DataShared() {
  return contents_.DataShared();
}

const void* ArrayBuffer::DataShared() const {
  return contents_.DataShared();
}

void* ArrayBuffer::DataMaybeShared() {
  return contents_.DataMaybeShared();
}

const void* ArrayBuffer::DataMaybeShared() const {
  return contents_.DataMaybeShared();
}

unsigned ArrayBuffer::ByteLength() const {
  return contents_.DataLength();
}

scoped_refptr<ArrayBuffer> ArrayBuffer::Slice(int begin, int end) const {
  return SliceImpl(ClampIndex(begin), ClampIndex(end));
}

scoped_refptr<ArrayBuffer> ArrayBuffer::Slice(int begin) const {
  return SliceImpl(ClampIndex(begin), ByteLength());
}

scoped_refptr<ArrayBuffer> ArrayBuffer::SliceImpl(unsigned begin,
                                                  unsigned end) const {
  size_t size = static_cast<size_t>(begin <= end ? end - begin : 0);
  return ArrayBuffer::Create(static_cast<const char*>(Data()) + begin, size);
}

unsigned ArrayBuffer::ClampIndex(int index) const {
  unsigned current_length = ByteLength();
  if (index < 0)
    index = static_cast<int>(current_length + index);
  return ClampValue(index, 0, current_length);
}

}  // namespace WTF

using WTF::ArrayBuffer;

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_WTF_TYPED_ARRAYS_ARRAY_BUFFER_H_