Commit 69031573 authored by Sigurdur Asgeirsson's avatar Sigurdur Asgeirsson Committed by Commit Bot

PM: Eliminate or localize and test reinterpret_cast use.

Change-Id: I9bf7d0b0f5cbd33ded4ff55bd3b11bdf3c380376
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2507509Reviewed-by: default avatarChris Hamilton <chrisha@chromium.org>
Commit-Queue: Sigurður Ásgeirsson <siggi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#822188}
parent bd6beb55
...@@ -77,15 +77,11 @@ class ExecutionContextImpl : public ExecutionContext, ...@@ -77,15 +77,11 @@ class ExecutionContextImpl : public ExecutionContext,
const FrameNode* GetFrameNode() const override { const FrameNode* GetFrameNode() const override {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (std::is_same<FrameNodeImpl, NodeImplType>::value)
return reinterpret_cast<const FrameNode*>(node_);
return nullptr; return nullptr;
} }
const WorkerNode* GetWorkerNode() const override { const WorkerNode* GetWorkerNode() const override {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (std::is_same<WorkerNodeImpl, NodeImplType>::value)
return reinterpret_cast<const WorkerNodeImpl*>(node_);
return nullptr; return nullptr;
} }
...@@ -120,6 +116,11 @@ class FrameExecutionContext ...@@ -120,6 +116,11 @@ class FrameExecutionContext
return blink::ExecutionContextToken(node_->frame_token()); return blink::ExecutionContextToken(node_->frame_token());
} }
const FrameNode* GetFrameNode() const override {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return node_;
}
protected: protected:
friend class NodeAttachedDataImpl<FrameExecutionContext>; friend class NodeAttachedDataImpl<FrameExecutionContext>;
explicit FrameExecutionContext(const FrameNodeImpl* frame_node) explicit FrameExecutionContext(const FrameNodeImpl* frame_node)
...@@ -143,6 +144,11 @@ class WorkerExecutionContext ...@@ -143,6 +144,11 @@ class WorkerExecutionContext
return ToExecutionContextToken(node_->worker_token()); return ToExecutionContextToken(node_->worker_token());
} }
const WorkerNode* GetWorkerNode() const override {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return node_;
}
protected: protected:
friend class NodeAttachedDataImpl<WorkerExecutionContext>; friend class NodeAttachedDataImpl<WorkerExecutionContext>;
explicit WorkerExecutionContext(const WorkerNodeImpl* worker_node) explicit WorkerExecutionContext(const WorkerNodeImpl* worker_node)
......
...@@ -10,6 +10,14 @@ ...@@ -10,6 +10,14 @@
namespace performance_manager { namespace performance_manager {
namespace execution_context_priority { namespace execution_context_priority {
// static
MaxVoteAggregator::StampedVote*
MaxVoteAggregator::StampedVote::FromAcceptedVote(AcceptedVote* accepted_vote) {
static_assert(offsetof(StampedVote, vote) == 0,
"AcceptedVote is expected to be at offset 0 of StampedVote");
return reinterpret_cast<StampedVote*>(accepted_vote);
}
MaxVoteAggregator::MaxVoteAggregator() : factory_(this) {} MaxVoteAggregator::MaxVoteAggregator() : factory_(this) {}
MaxVoteAggregator::~MaxVoteAggregator() = default; MaxVoteAggregator::~MaxVoteAggregator() = default;
...@@ -138,9 +146,7 @@ bool MaxVoteAggregator::VoteData::RemoveVote(size_t index) { ...@@ -138,9 +146,7 @@ bool MaxVoteAggregator::VoteData::RemoveVote(size_t index) {
} }
size_t MaxVoteAggregator::VoteData::GetVoteIndex(AcceptedVote* vote) { size_t MaxVoteAggregator::VoteData::GetVoteIndex(AcceptedVote* vote) {
static_assert(offsetof(StampedVote, vote) == 0, StampedVote* stamped_vote = StampedVote::FromAcceptedVote(vote);
"AcceptedVote is expected to be at offset 0 of StampedVote");
StampedVote* stamped_vote = reinterpret_cast<StampedVote*>(vote);
DCHECK_NE(0u, votes_.size()); DCHECK_NE(0u, votes_.size());
DCHECK_LE(votes_.data(), stamped_vote); DCHECK_LE(votes_.data(), stamped_vote);
DCHECK_LT(stamped_vote, votes_.data() + votes_.size()); DCHECK_LT(stamped_vote, votes_.data() + votes_.size());
......
...@@ -15,8 +15,10 @@ namespace execution_context_priority { ...@@ -15,8 +15,10 @@ namespace execution_context_priority {
class MaxVoteAggregatorTestAccess { class MaxVoteAggregatorTestAccess {
public: public:
using VoteData = MaxVoteAggregator::VoteData; using VoteData = MaxVoteAggregator::VoteData;
using StampedVote = MaxVoteAggregator::StampedVote;
}; };
using VoteData = MaxVoteAggregatorTestAccess::VoteData; using VoteData = MaxVoteAggregatorTestAccess::VoteData;
using StampedVote = MaxVoteAggregatorTestAccess::StampedVote;
namespace { namespace {
...@@ -76,6 +78,12 @@ class FakeVoteConsumer : public test::DummyVoteConsumer { ...@@ -76,6 +78,12 @@ class FakeVoteConsumer : public test::DummyVoteConsumer {
} // namespace } // namespace
TEST(MaxVoteAggregatorTest, StampedVoteCast) {
StampedVote stamped_vote;
EXPECT_EQ(&stamped_vote, StampedVote::FromAcceptedVote(&stamped_vote.vote));
}
TEST(MaxVoteAggregatorTest, VoteDataHeapStressTest) { TEST(MaxVoteAggregatorTest, VoteDataHeapStressTest) {
// Build a simple consumer/voter chain so that we generate an actual VoterId. // Build a simple consumer/voter chain so that we generate an actual VoterId.
FakeVoteConsumer consumer; FakeVoteConsumer consumer;
......
...@@ -61,6 +61,10 @@ class MaxVoteAggregator : public VoteConsumer { ...@@ -61,6 +61,10 @@ class MaxVoteAggregator : public VoteConsumer {
return vote_id > rhs.vote_id; return vote_id > rhs.vote_id;
} }
// Given an AcceptedVote that's embedded in a StampedVote::vote, retrieve
// the embedding StampedVote instance.
static StampedVote* FromAcceptedVote(AcceptedVote* accepted_vote);
// IntrusiveHeap contract. We actually don't need HeapHandles, as we already // IntrusiveHeap contract. We actually don't need HeapHandles, as we already
// know the positions of the elements in the heap directly, as they are // know the positions of the elements in the heap directly, as they are
// tracked with explicit back pointers. // tracked with explicit back pointers.
......
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