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") {
"array_buffer/array_buffer_contents.h",
"array_buffer/array_buffer_view.cc",
"array_buffer/array_buffer_view.h",
"array_buffer/array_piece.cc",
"array_buffer/array_piece.h",
"array_buffer/typed_array.h",
"array_buffer_view_helpers.h",
"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 @@
namespace blink {
DOMArrayPiece::DOMArrayPiece(
const ArrayBufferOrArrayBufferView& array_buffer_or_view,
InitWithUnionOption option) {
const ArrayBufferOrArrayBufferView& array_buffer_or_view) {
if (array_buffer_or_view.IsArrayBuffer()) {
DOMArrayBuffer* array_buffer = array_buffer_or_view.GetAsArrayBuffer();
InitWithArrayBuffer(array_buffer->Buffer());
InitWithArrayBuffer(array_buffer);
} else if (array_buffer_or_view.IsArrayBufferView()) {
DOMArrayBufferView* array_buffer_view =
array_buffer_or_view.GetAsArrayBufferView().View();
InitWithArrayBufferView(array_buffer_view->View());
} else if (array_buffer_or_view.IsNull() &&
option == kAllowNullPointToNullWithZeroSize) {
InitWithData(nullptr, 0);
} // Otherwise, leave the obejct as null.
InitWithArrayBufferView(array_buffer_view);
}
}
///////////////////////////////////////////////////////
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
......@@ -5,51 +5,54 @@
#ifndef 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_view.h"
namespace blink {
class ArrayBufferOrArrayBufferView;
class DOMArrayBufferView;
// 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
// DOMArrayPiece 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 DOMArrayPiece : public ArrayPiece {
// IMPORTANT: The data contained by DOMArrayPiece is NOT OWNED, so caution must
// be taken to ensure it is kept alive.
class CORE_EXPORT DOMArrayPiece {
DISALLOW_NEW();
public:
enum InitWithUnionOption {
// Initialize this object as "null" when initialized with an union which
// holds null.
kTreatNullAsNull,
// 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);
DOMArrayPiece();
DOMArrayPiece(DOMArrayBuffer* buffer);
DOMArrayPiece(DOMArrayBufferView* view);
DOMArrayPiece(const ArrayBufferOrArrayBufferView&);
bool operator==(const DOMArrayBuffer& other) const {
return ByteLengthAsSizeT() == other.ByteLengthAsSizeT() &&
memcmp(Data(), other.Data(), ByteLengthAsSizeT()) == 0;
}
bool operator==(const DOMArrayBufferView& other) const {
return ByteLengthAsSizeT() == other.byteLengthAsSizeT() &&
memcmp(Data(), other.BaseAddress(), ByteLengthAsSizeT()) == 0;
}
bool IsNull() const;
bool IsDetached() const;
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
......
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