Commit 2cc373d6 authored by reveman's avatar reveman Committed by Commit bot

base: Rename DiscardableMemory::Memory() to ::data().

This make the API more consistent with RefCountedMemory and
other discardable memory interfaces such as SkDiscardableMemory.

Also improves readability by replacing memory->Memory() calls
with memory->data() calls.

A handy method is also added to simplify calling data() with a
reinterpret_cast. This is also consistent with RefCountedMemory.

BUG=

Review URL: https://codereview.chromium.org/1094433002

Cr-Commit-Position: refs/heads/master@{#325509}
parent ad74a7e3
......@@ -52,8 +52,13 @@ class BASE_EXPORT DiscardableMemory {
virtual void Unlock() = 0;
// Returns the memory address held by this object. The object must be locked
// before calling this. Otherwise, this will cause a DCHECK error.
virtual void* Memory() const = 0;
// before calling this.
virtual void* data() const = 0;
// Handy method to simplify calling data() with a reinterpret_cast.
template<typename T> const T* data_as() const {
return reinterpret_cast<const T*>(data());
}
};
} // namespace base
......
......@@ -13,15 +13,15 @@ namespace {
class DiscardableMemoryImpl : public DiscardableMemory {
public:
explicit DiscardableMemoryImpl(size_t size) : memory_(new uint8_t[size]) {}
explicit DiscardableMemoryImpl(size_t size) : data_(new uint8_t[size]) {}
// Overridden from DiscardableMemory:
bool Lock() override { return false; }
void Unlock() override {}
void* Memory() const override { return memory_.get(); }
void* data() const override { return data_.get(); }
private:
scoped_ptr<uint8_t[]> memory_;
scoped_ptr<uint8_t[]> data_;
};
} // namespace
......
......@@ -59,7 +59,7 @@ class DiscardableMemoryImpl : public base::DiscardableMemory {
manager_->UnlockSpan(span_.get());
is_locked_ = false;
}
void* Memory() const override {
void* data() const override {
DCHECK(is_locked_);
return reinterpret_cast<void*>(span_->start() * base::GetPageSize());
}
......
......@@ -60,7 +60,7 @@ IN_PROC_BROWSER_TEST_F(ChildDiscardableSharedMemoryManagerBrowserTest,
kSize, &memory));
ASSERT_TRUE(memory);
void* addr = memory->Memory();
void* addr = memory->data();
ASSERT_NE(nullptr, addr);
PostTaskToInProcessRendererAndWait(
......@@ -96,7 +96,7 @@ IN_PROC_BROWSER_TEST_F(ChildDiscardableSharedMemoryManagerBrowserTest,
&ChildDiscardableSharedMemoryManagerBrowserTest::AllocateLockedMemory,
kLargeSize, &memory));
ASSERT_TRUE(memory);
void* addr = memory->Memory();
void* addr = memory->data();
ASSERT_NE(nullptr, addr);
PostTaskToInProcessRendererAndWait(base::Bind(
&ChildDiscardableSharedMemoryManagerBrowserTest::UnlockMemory,
......
......@@ -28,7 +28,7 @@ void WebDiscardableMemoryImpl::unlock() {
}
void* WebDiscardableMemoryImpl::data() {
return discardable_->Memory();
return discardable_->data();
}
WebDiscardableMemoryImpl::WebDiscardableMemoryImpl(
......
......@@ -51,7 +51,7 @@ class DiscardableMemoryImpl : public base::DiscardableMemory {
shared_memory_->Unlock(0, 0);
is_locked_ = false;
}
void* Memory() const override {
void* data() const override {
DCHECK(is_locked_);
return shared_memory_->memory();
}
......
......@@ -18,7 +18,7 @@ class DiscardableMemoryAllocator::OwnedMemoryChunk {
OwnedMemoryChunk(size_t size, DiscardableMemoryAllocator* allocator)
: is_locked_(true),
size_(size),
memory_(new uint8_t[size]),
data_(new uint8_t[size]),
allocator_(allocator),
weak_factory_(this) {}
~OwnedMemoryChunk() {}
......@@ -37,9 +37,9 @@ class DiscardableMemoryAllocator::OwnedMemoryChunk {
bool is_locked() const { return is_locked_; }
size_t size() const { return size_; }
void* Memory() const {
void* data() const {
DCHECK(is_locked_);
return memory_.get();
return data_.get();
}
base::WeakPtr<OwnedMemoryChunk> GetWeakPtr() {
......@@ -49,7 +49,7 @@ class DiscardableMemoryAllocator::OwnedMemoryChunk {
private:
bool is_locked_;
size_t size_;
scoped_ptr<uint8_t[]> memory_;
scoped_ptr<uint8_t[]> data_;
DiscardableMemoryAllocator* allocator_;
std::list<OwnedMemoryChunk*>::iterator unlocked_position_;
......@@ -89,9 +89,9 @@ class DiscardableMemoryChunkImpl : public base::DiscardableMemory {
memory_chunk_->Unlock();
}
void* Memory() const override {
void* data() const override {
if (memory_chunk_)
return memory_chunk_->Memory();
return memory_chunk_->data();
return nullptr;
}
......
......@@ -63,9 +63,9 @@ TEST(DiscardableMemoryAllocator, DontDiscardLiveChunks) {
allocator.AllocateLockedDiscardableMemory(kAlmostOneMegabyte);
// These accesses will fail if the underlying weak ptr has been deallocated.
EXPECT_NE(nullptr, chunk_one->Memory());
EXPECT_NE(nullptr, chunk_two->Memory());
EXPECT_NE(nullptr, chunk_three->Memory());
EXPECT_NE(nullptr, chunk_one->data());
EXPECT_NE(nullptr, chunk_two->data());
EXPECT_NE(nullptr, chunk_three->data());
chunk_one->Unlock();
chunk_two->Unlock();
......
......@@ -14,7 +14,7 @@ bool SkDiscardableMemoryChrome::lock() {
}
void* SkDiscardableMemoryChrome::data() {
return discardable_->Memory();
return discardable_->data();
}
void SkDiscardableMemoryChrome::unlock() {
......
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