Commit f366e7dd authored by Andreas Haas's avatar Andreas Haas Committed by Commit Bot

[arraybuffer] Organize views in a hash_set

This is a first step of getting rid of blink::ArrayBuffer.

blink::ArrayBuffer serves two purposes by now:
1) Managing a list of ArrayBufferViews;
2) Managing the detached flag;

With this CL I encapsulate the list of ArrayBufferViews. In follow-up
CLs I then want to pass the list and an ArrayBufferContents object
to the ArrayBufferView instead of the ArrayBuffer object. This will
bring us one step closer to removing blink::ArrayBuffer.

Update: ArrayBuffer seems to be very involved. It is the connecting
data structure between DOMArrayBuffer, ArrayBufferContents, and
ArrayBufferView. Even though it is not used anymore outside these
classes, it is not obvious if we can replace it within these classes.
I still think that refactoring the organization of views here is
good.

R=haraken@chromium.org

Bug: chromium:1008840
Change-Id: I533f5496b7c51312e77a0138ac8249f9f0e55ffd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1911207Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Commit-Queue: Andreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#714880}
parent 2c012459
......@@ -30,6 +30,13 @@
namespace blink {
ArrayBuffer::ArrayBuffer(ArrayBufferContents& contents) : is_detached_(false) {
if (contents.IsShared())
contents.ShareWith(contents_);
else
contents.Transfer(contents_);
}
bool ArrayBuffer::Transfer(ArrayBufferContents& result) {
DCHECK(!IsShared());
scoped_refptr<ArrayBuffer> keep_alive(this);
......@@ -40,19 +47,19 @@ bool ArrayBuffer::Transfer(ArrayBufferContents& result) {
}
bool all_views_are_detachable = true;
for (ArrayBufferView* i = first_view_; i; i = i->next_view_) {
if (!i->IsDetachable())
for (auto* view : views_) {
if (!view->IsDetachable()) {
all_views_are_detachable = false;
}
}
if (all_views_are_detachable) {
contents_.Transfer(result);
while (first_view_) {
ArrayBufferView* current = first_view_;
RemoveView(current);
current->Detach();
for (auto* view : views_) {
view->Detach();
}
views_.clear();
is_detached_ = true;
} else {
......@@ -96,23 +103,12 @@ bool ArrayBuffer::ShareNonSharedForInternalUse(ArrayBufferContents& result) {
}
void ArrayBuffer::AddView(ArrayBufferView* view) {
view->buffer_ = this;
view->prev_view_ = nullptr;
view->next_view_ = first_view_;
if (first_view_)
first_view_->prev_view_ = view;
first_view_ = view;
views_.insert(view);
}
void ArrayBuffer::RemoveView(ArrayBufferView* view) {
DCHECK_EQ(this, view->buffer_.get());
if (view->next_view_)
view->next_view_->prev_view_ = view->prev_view_;
if (view->prev_view_)
view->prev_view_->next_view_ = view->next_view_;
if (first_view_ == view)
first_view_ = view->next_view_;
view->prev_view_ = view->next_view_ = nullptr;
DCHECK(views_.Contains(view));
views_.erase(view);
}
} // namespace blink
......@@ -94,7 +94,7 @@ class CORE_EXPORT ArrayBuffer : public RefCounted<ArrayBuffer> {
~ArrayBuffer() = default;
protected:
inline explicit ArrayBuffer(ArrayBufferContents&);
explicit ArrayBuffer(ArrayBufferContents&);
private:
static inline scoped_refptr<ArrayBuffer> Create(
......@@ -113,7 +113,7 @@ class CORE_EXPORT ArrayBuffer : public RefCounted<ArrayBuffer> {
inline unsigned ClampIndex(unsigned index) const;
ArrayBufferContents contents_;
ArrayBufferView* first_view_;
HashSet<ArrayBufferView*> views_;
bool is_detached_;
};
......@@ -207,14 +207,6 @@ scoped_refptr<ArrayBuffer> ArrayBuffer::CreateShared(
return base::AdoptRef(new ArrayBuffer(contents));
}
ArrayBuffer::ArrayBuffer(ArrayBufferContents& contents)
: first_view_(nullptr), is_detached_(false) {
if (contents.IsShared())
contents.ShareWith(contents_);
else
contents.Transfer(contents_);
}
void* ArrayBuffer::Data() {
return contents_.Data();
}
......
......@@ -33,9 +33,7 @@ ArrayBufferView::ArrayBufferView(scoped_refptr<ArrayBuffer> buffer,
unsigned byte_offset)
: byte_offset_(byte_offset),
is_detachable_(true),
buffer_(std::move(buffer)),
prev_view_(nullptr),
next_view_(nullptr) {
buffer_(std::move(buffer)) {
base_address_ =
buffer_ ? (static_cast<char*>(buffer_->DataMaybeShared()) + byte_offset_)
: nullptr;
......
......@@ -110,8 +110,6 @@ class CORE_EXPORT ArrayBufferView : public RefCounted<ArrayBufferView> {
private:
friend class ArrayBuffer;
scoped_refptr<ArrayBuffer> buffer_;
ArrayBufferView* prev_view_;
ArrayBufferView* next_view_;
};
bool ArrayBufferView::SetImpl(ArrayBufferView* array, unsigned byte_offset) {
......
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