v8/src/detachable-vector.h
Peter Marshall 329f694678 [cleanup] Replace List with std::vector in api.
The members of HandleScopeImplementer are copied with memcpy when
the isolate is transferred to another thread. List contained some
primitives which allowed us to manually free the backing store, which
was needed in order to ensure that threads would not hold on to
old pointers and use them later. With std::vector, we can't do that.

Here we change the HandleScopeImplementer to instead use a custom
structure DetachableVector, which contains a std::vector but allows
manual detaching and freeing of the backing store. This allows us to
maintain the old behavior.

Bug: v8:6333
Cq-Include-Trybots: master.tryserver.chromium.linux:linux_chromium_rel_ng
Change-Id: I6361d161cdb19878ba19ed51d6ba2fae99e8cdc0
Reviewed-on: https://chromium-review.googlesource.com/660125
Reviewed-by: Yang Guo <yangguo@chromium.org>
Commit-Queue: Peter Marshall <petermarshall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#48197}
2017-09-28 09:32:18 +00:00

74 lines
1.9 KiB
C++

// Copyright 2017 the V8 project 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 V8_DETACHABLE_VECTOR_H_
#define V8_DETACHABLE_VECTOR_H_
#include <vector>
namespace v8 {
namespace internal {
// This class wraps a std::vector and provides a few of the common member
// functions for accessing the data. It acts as a lazy wrapper of the vector,
// not initiliazing the backing store until push_back() is first called. Two
// extra methods are also provided: free() and detach(), which allow for manual
// control of the backing store. This is currently required for use in the
// HandleScopeImplementer. Any other class should just use a std::vector
// directly.
template <typename T>
class DetachableVector {
public:
DetachableVector() : vector_(nullptr) {}
~DetachableVector() { delete vector_; }
void push_back(const T& value) {
ensureAttached();
vector_->push_back(value);
}
// Free the backing store and clear our reference to it.
void free() {
delete vector_;
vector_ = nullptr;
}
// Clear our reference to the backing store. Does not delete it!
void detach() { vector_ = nullptr; }
T& at(typename std::vector<T>::size_type i) const { return vector_->at(i); }
T& back() const { return vector_->back(); }
T& front() const { return vector_->front(); }
void pop_back() { vector_->pop_back(); }
typename std::vector<T>::size_type size() const {
if (vector_) return vector_->size();
return 0;
}
bool empty() const {
if (vector_) return vector_->empty();
return true;
}
private:
std::vector<T>* vector_;
// Attach a vector backing store if not present.
void ensureAttached() {
if (vector_ == nullptr) {
vector_ = new std::vector<T>();
}
}
};
} // namespace internal
} // namespace v8
#endif // V8_DETACHABLE_VECTOR_H_