Commit 5c1ded78 authored by Andreas Haas's avatar Andreas Haas Committed by Commit Bot

[arraybuffer] Remove the ArrayPiece class

ArrayPiece was only used by DOMArrayPiece, so I moved the implementation
to DOMArrayPiece and deleted ArrayPiece itself.

R=haraken@chromium.org
CC=ulan@chromium.org

Bug: chromium:1008840
Change-Id: If82cf00e2afac02d3e72882654751f596e1cd9b8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2066792Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Commit-Queue: Andreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#743846}
parent 4dc2ee07
...@@ -12,8 +12,6 @@ blink_core_sources("typed_arrays") { ...@@ -12,8 +12,6 @@ blink_core_sources("typed_arrays") {
"array_buffer/array_buffer_contents.h", "array_buffer/array_buffer_contents.h",
"array_buffer/array_buffer_view.cc", "array_buffer/array_buffer_view.cc",
"array_buffer/array_buffer_view.h", "array_buffer/array_buffer_view.h",
"array_buffer/array_piece.cc",
"array_buffer/array_piece.h",
"array_buffer/typed_array.h", "array_buffer/typed_array.h",
"array_buffer_view_helpers.h", "array_buffer_view_helpers.h",
"dom_array_buffer.cc", "dom_array_buffer.cc",
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/core/typed_arrays/array_buffer/array_piece.h"
#include "third_party/blink/renderer/core/typed_arrays/array_buffer/array_buffer.h"
#include "third_party/blink/renderer/core/typed_arrays/array_buffer/array_buffer_view.h"
#include "third_party/blink/renderer/platform/wtf/assertions.h"
#include "third_party/blink/renderer/platform/wtf/std_lib_extras.h"
namespace blink {
ArrayPiece::ArrayPiece() {
InitNull();
}
ArrayPiece::ArrayPiece(ArrayBuffer* buffer) {
InitWithArrayBuffer(buffer);
}
ArrayPiece::ArrayPiece(ArrayBufferView* buffer) {
InitWithArrayBufferView(buffer);
}
bool ArrayPiece::IsNull() const {
return is_null_;
}
bool ArrayPiece::IsDetached() const {
return is_detached_;
}
void* ArrayPiece::Data() const {
DCHECK(!IsNull());
return data_;
}
unsigned char* ArrayPiece::Bytes() const {
return static_cast<unsigned char*>(Data());
}
size_t ArrayPiece::ByteLengthAsSizeT() const {
DCHECK(!IsNull());
return byte_length_;
}
void ArrayPiece::InitWithArrayBuffer(ArrayBuffer* buffer) {
if (buffer) {
InitWithData(buffer->Data(), buffer->ByteLengthAsSizeT());
is_detached_ = buffer->IsDetached();
} else {
InitNull();
}
}
void ArrayPiece::InitWithArrayBufferView(ArrayBufferView* buffer) {
if (buffer) {
InitWithData(buffer->BaseAddress(), buffer->ByteLengthAsSizeT());
is_detached_ = buffer->Buffer() ? buffer->Buffer()->IsDetached() : true;
} else {
InitNull();
}
}
void ArrayPiece::InitWithData(void* data, size_t byte_length) {
byte_length_ = byte_length;
data_ = data;
is_null_ = false;
is_detached_ = false;
}
void ArrayPiece::InitNull() {
byte_length_ = 0;
data_ = nullptr;
is_null_ = true;
is_detached_ = false;
}
} // namespace blink
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_TYPED_ARRAYS_ARRAY_BUFFER_ARRAY_PIECE_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_TYPED_ARRAYS_ARRAY_BUFFER_ARRAY_PIECE_H_
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
namespace blink {
class ArrayBuffer;
class ArrayBufferView;
// This class is for passing around un-owned bytes as a pointer + length.
// It supports implicit conversion from several other data types.
//
// ArrayPiece has the concept of being "null". This is different from an empty
// byte range. It is invalid to call methods other than isNull() on such
// instances.
//
// IMPORTANT: The data contained by ArrayPiece is NOT OWNED, so caution must be
// taken to ensure it is kept alive.
class CORE_EXPORT ArrayPiece {
DISALLOW_NEW();
public:
// Constructs a "null" ArrayPiece object.
ArrayPiece();
// Constructs an ArrayPiece from the given ArrayBuffer. If the input is a
// nullptr, then the constructed instance will be isNull().
ArrayPiece(ArrayBuffer*);
ArrayPiece(ArrayBufferView*);
bool IsNull() const;
bool IsDetached() const;
void* Data() const;
unsigned char* Bytes() const;
size_t ByteLengthAsSizeT() const;
protected:
void InitWithArrayBuffer(ArrayBuffer*);
void InitWithArrayBufferView(ArrayBufferView*);
void InitWithData(void* data, size_t byte_length);
private:
void InitNull();
void* data_;
size_t byte_length_;
bool is_null_;
bool is_detached_;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_CORE_TYPED_ARRAYS_ARRAY_BUFFER_ARRAY_PIECE_H_
...@@ -9,19 +9,81 @@ ...@@ -9,19 +9,81 @@
namespace blink { namespace blink {
DOMArrayPiece::DOMArrayPiece( DOMArrayPiece::DOMArrayPiece(
const ArrayBufferOrArrayBufferView& array_buffer_or_view, const ArrayBufferOrArrayBufferView& array_buffer_or_view) {
InitWithUnionOption option) {
if (array_buffer_or_view.IsArrayBuffer()) { if (array_buffer_or_view.IsArrayBuffer()) {
DOMArrayBuffer* array_buffer = array_buffer_or_view.GetAsArrayBuffer(); DOMArrayBuffer* array_buffer = array_buffer_or_view.GetAsArrayBuffer();
InitWithArrayBuffer(array_buffer->Buffer()); InitWithArrayBuffer(array_buffer);
} else if (array_buffer_or_view.IsArrayBufferView()) { } else if (array_buffer_or_view.IsArrayBufferView()) {
DOMArrayBufferView* array_buffer_view = DOMArrayBufferView* array_buffer_view =
array_buffer_or_view.GetAsArrayBufferView().View(); array_buffer_or_view.GetAsArrayBufferView().View();
InitWithArrayBufferView(array_buffer_view->View()); InitWithArrayBufferView(array_buffer_view);
} else if (array_buffer_or_view.IsNull() && }
option == kAllowNullPointToNullWithZeroSize) { }
InitWithData(nullptr, 0); ///////////////////////////////////////////////////////
} // Otherwise, leave the obejct as null. DOMArrayPiece::DOMArrayPiece() {
InitNull();
}
DOMArrayPiece::DOMArrayPiece(DOMArrayBuffer* buffer) {
InitWithArrayBuffer(buffer);
}
DOMArrayPiece::DOMArrayPiece(DOMArrayBufferView* buffer) {
InitWithArrayBufferView(buffer);
}
bool DOMArrayPiece::IsNull() const {
return is_null_;
}
bool DOMArrayPiece::IsDetached() const {
return is_detached_;
}
void* DOMArrayPiece::Data() const {
DCHECK(!IsNull());
return data_;
}
unsigned char* DOMArrayPiece::Bytes() const {
return static_cast<unsigned char*>(Data());
}
size_t DOMArrayPiece::ByteLengthAsSizeT() const {
DCHECK(!IsNull());
return byte_length_;
}
void DOMArrayPiece::InitWithArrayBuffer(DOMArrayBuffer* buffer) {
if (buffer) {
InitWithData(buffer->Data(), buffer->ByteLengthAsSizeT());
is_detached_ = buffer->IsDetached();
} else {
InitNull();
}
}
void DOMArrayPiece::InitWithArrayBufferView(DOMArrayBufferView* buffer) {
if (buffer) {
InitWithData(buffer->BaseAddress(), buffer->byteLengthAsSizeT());
is_detached_ = buffer->buffer() ? buffer->buffer()->IsDetached() : true;
} else {
InitNull();
}
}
void DOMArrayPiece::InitWithData(void* data, size_t byte_length) {
byte_length_ = byte_length;
data_ = data;
is_null_ = false;
is_detached_ = false;
}
void DOMArrayPiece::InitNull() {
byte_length_ = 0;
data_ = nullptr;
is_null_ = true;
is_detached_ = false;
} }
} // namespace blink } // namespace blink
...@@ -5,51 +5,54 @@ ...@@ -5,51 +5,54 @@
#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_TYPED_ARRAYS_DOM_ARRAY_PIECE_H_ #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_TYPED_ARRAYS_DOM_ARRAY_PIECE_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_TYPED_ARRAYS_DOM_ARRAY_PIECE_H_ #define THIRD_PARTY_BLINK_RENDERER_CORE_TYPED_ARRAYS_DOM_ARRAY_PIECE_H_
#include "third_party/blink/renderer/core/typed_arrays/array_buffer/array_piece.h" #include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/typed_arrays/dom_array_buffer.h" #include "third_party/blink/renderer/core/typed_arrays/dom_array_buffer.h"
#include "third_party/blink/renderer/core/typed_arrays/dom_array_buffer_view.h"
namespace blink { namespace blink {
class ArrayBufferOrArrayBufferView; class ArrayBufferOrArrayBufferView;
class DOMArrayBufferView;
// This class is for passing around un-owned bytes as a pointer + length. // This class is for passing around un-owned bytes as a pointer + length.
// It supports implicit conversion from several other data types. // It supports implicit conversion from several other data types.
// //
// ArrayPiece has the concept of being "null". This is different from an empty // DOMArrayPiece has the concept of being "null". This is different from an
// byte range. It is invalid to call methods other than isNull() on such // empty byte range. It is invalid to call methods other than isNull() on such
// instances. // instances.
// //
// IMPORTANT: The data contained by ArrayPiece is NOT OWNED, so caution must be // IMPORTANT: The data contained by DOMArrayPiece is NOT OWNED, so caution must
// taken to ensure it is kept alive. // be taken to ensure it is kept alive.
class CORE_EXPORT DOMArrayPiece : public ArrayPiece { class CORE_EXPORT DOMArrayPiece {
DISALLOW_NEW(); DISALLOW_NEW();
public: public:
enum InitWithUnionOption { DOMArrayPiece();
// Initialize this object as "null" when initialized with an union which DOMArrayPiece(DOMArrayBuffer* buffer);
// holds null. DOMArrayPiece(DOMArrayBufferView* view);
kTreatNullAsNull, DOMArrayPiece(const ArrayBufferOrArrayBufferView&);
// Initialize this object so this points to null pointer with zero size
// when initialized with an union which holds null.
kAllowNullPointToNullWithZeroSize,
};
DOMArrayPiece() = default;
DOMArrayPiece(DOMArrayBuffer* buffer) : ArrayPiece(buffer->Buffer()) {}
DOMArrayPiece(DOMArrayBufferView* view) : ArrayPiece(view->View()) {}
DOMArrayPiece(const ArrayBufferOrArrayBufferView&,
InitWithUnionOption = kTreatNullAsNull);
bool operator==(const DOMArrayBuffer& other) const { bool operator==(const DOMArrayBuffer& other) const {
return ByteLengthAsSizeT() == other.ByteLengthAsSizeT() && return ByteLengthAsSizeT() == other.ByteLengthAsSizeT() &&
memcmp(Data(), other.Data(), ByteLengthAsSizeT()) == 0; memcmp(Data(), other.Data(), ByteLengthAsSizeT()) == 0;
} }
bool operator==(const DOMArrayBufferView& other) const { bool IsNull() const;
return ByteLengthAsSizeT() == other.byteLengthAsSizeT() && bool IsDetached() const;
memcmp(Data(), other.BaseAddress(), ByteLengthAsSizeT()) == 0; void* Data() const;
} unsigned char* Bytes() const;
size_t ByteLengthAsSizeT() const;
private:
void InitWithArrayBuffer(DOMArrayBuffer*);
void InitWithArrayBufferView(DOMArrayBufferView*);
void InitWithData(void* data, size_t byte_length);
void InitNull();
void* data_;
size_t byte_length_;
bool is_null_;
bool is_detached_;
}; };
} // namespace blink } // namespace blink
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment