Index: base/memory/discardable_memory_allocator_android.cc |
diff --git a/base/memory/discardable_memory_allocator_android.cc b/base/memory/discardable_memory_allocator_android.cc |
index 077a441ac92ae0e247f83ca10f718f4df5137f99..ee886eebeb06e187d44d5dd3d98d43051337e4e5 100644 |
--- a/base/memory/discardable_memory_allocator_android.cc |
+++ b/base/memory/discardable_memory_allocator_android.cc |
@@ -18,10 +18,7 @@ |
#include "base/file_util.h" |
#include "base/files/scoped_file.h" |
#include "base/logging.h" |
-#include "base/memory/discardable_memory.h" |
#include "base/memory/scoped_vector.h" |
-#include "base/synchronization/lock.h" |
-#include "base/threading/thread_checker.h" |
#include "third_party/ashmem/ashmem.h" |
// The allocator consists of three parts (classes): |
@@ -30,10 +27,8 @@ |
// it owns). |
// - AshmemRegion: manages allocations and destructions inside a single large |
// (e.g. 32 MBytes) ashmem region. |
-// - DiscardableAshmemChunk: class implementing the DiscardableMemory interface |
-// whose instances are returned to the client. DiscardableAshmemChunk lets the |
-// client seamlessly operate on a subrange of the ashmem region managed by |
-// AshmemRegion. |
+// - DiscardableAshmemChunk: class mimicking the DiscardableMemory interface |
+// whose instances are returned to the client. |
namespace base { |
namespace { |
@@ -102,10 +97,8 @@ bool CloseAshmemRegion(int fd, size_t size, void* address) { |
return close(fd) == 0; |
} |
-DiscardableMemoryLockStatus LockAshmemRegion(int fd, size_t off, size_t size) { |
- const int result = ashmem_pin_region(fd, off, size); |
- return result == ASHMEM_WAS_PURGED ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED |
- : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS; |
+bool LockAshmemRegion(int fd, size_t off, size_t size) { |
+ return ashmem_pin_region(fd, off, size) != ASHMEM_WAS_PURGED; |
} |
bool UnlockAshmemRegion(int fd, size_t off, size_t size) { |
@@ -119,56 +112,7 @@ bool UnlockAshmemRegion(int fd, size_t off, size_t size) { |
namespace internal { |
-class DiscardableMemoryAllocator::DiscardableAshmemChunk |
- : public DiscardableMemory { |
- public: |
- // Note that |ashmem_region| must outlive |this|. |
- DiscardableAshmemChunk(AshmemRegion* ashmem_region, |
- int fd, |
- void* address, |
- size_t offset, |
- size_t size) |
- : ashmem_region_(ashmem_region), |
- fd_(fd), |
- address_(address), |
- offset_(offset), |
- size_(size), |
- locked_(true) { |
- } |
- |
- // Implemented below AshmemRegion since this requires the full definition of |
- // AshmemRegion. |
- virtual ~DiscardableAshmemChunk(); |
- |
- // DiscardableMemory: |
- virtual DiscardableMemoryLockStatus Lock() OVERRIDE { |
- DCHECK(!locked_); |
- locked_ = true; |
- return LockAshmemRegion(fd_, offset_, size_); |
- } |
- |
- virtual void Unlock() OVERRIDE { |
- DCHECK(locked_); |
- locked_ = false; |
- UnlockAshmemRegion(fd_, offset_, size_); |
- } |
- |
- virtual void* Memory() const OVERRIDE { |
- return address_; |
- } |
- |
- private: |
- AshmemRegion* const ashmem_region_; |
- const int fd_; |
- void* const address_; |
- const size_t offset_; |
- const size_t size_; |
- bool locked_; |
- |
- DISALLOW_COPY_AND_ASSIGN(DiscardableAshmemChunk); |
-}; |
- |
-class DiscardableMemoryAllocator::AshmemRegion { |
+class AshmemRegion { |
public: |
// Note that |allocator| must outlive |this|. |
static scoped_ptr<AshmemRegion> Create( |
@@ -189,21 +133,20 @@ class DiscardableMemoryAllocator::AshmemRegion { |
DCHECK(!highest_allocated_chunk_); |
} |
- // Returns a new instance of DiscardableMemory whose size is greater or equal |
- // than |actual_size| (which is expected to be greater or equal than |
+ // Returns a new instance of DiscardableAshmemChunk whose size is greater or |
+ // equal than |actual_size| (which is expected to be greater or equal than |
// |client_requested_size|). |
// Allocation works as follows: |
// 1) Reuse a previously freed chunk and return it if it succeeded. See |
- // ReuseFreeChunk_Locked() below for more information. |
+ // ReuseFreeChunk() below for more information. |
// 2) If no free chunk could be reused and the region is not big enough for |
// the requested size then NULL is returned. |
// 3) If there is enough room in the ashmem region then a new chunk is |
// returned. This new chunk starts at |offset_| which is the end of the |
// previously highest chunk in the region. |
- scoped_ptr<DiscardableMemory> Allocate_Locked(size_t client_requested_size, |
- size_t actual_size) { |
+ scoped_ptr<DiscardableAshmemChunk> Allocate(size_t client_requested_size, |
+ size_t actual_size) { |
DCHECK_LE(client_requested_size, actual_size); |
- allocator_->lock_.AssertAcquired(); |
// Check that the |highest_allocated_chunk_| field doesn't contain a stale |
// pointer. It should point to either a free chunk or a used chunk. |
@@ -213,14 +156,14 @@ class DiscardableMemoryAllocator::AshmemRegion { |
used_to_previous_chunk_map_.find(highest_allocated_chunk_) != |
used_to_previous_chunk_map_.end()); |
- scoped_ptr<DiscardableMemory> memory = ReuseFreeChunk_Locked( |
+ scoped_ptr<DiscardableAshmemChunk> memory = ReuseFreeChunk( |
client_requested_size, actual_size); |
if (memory) |
return memory.Pass(); |
if (size_ - offset_ < actual_size) { |
// This region does not have enough space left to hold the requested size. |
- return scoped_ptr<DiscardableMemory>(); |
+ return scoped_ptr<DiscardableAshmemChunk>(); |
} |
void* const address = static_cast<char*>(base_) + offset_; |
@@ -236,8 +179,7 @@ class DiscardableMemoryAllocator::AshmemRegion { |
} |
void OnChunkDeletion(void* chunk, size_t size) { |
- AutoLock auto_lock(allocator_->lock_); |
- MergeAndAddFreeChunk_Locked(chunk, size); |
+ MergeAndAddFreeChunk(chunk, size); |
// Note that |this| might be deleted beyond this point. |
} |
@@ -287,14 +229,13 @@ class DiscardableMemoryAllocator::AshmemRegion { |
} |
// Tries to reuse a previously freed chunk by doing a closest size match. |
- scoped_ptr<DiscardableMemory> ReuseFreeChunk_Locked( |
+ scoped_ptr<DiscardableAshmemChunk> ReuseFreeChunk( |
size_t client_requested_size, |
size_t actual_size) { |
- allocator_->lock_.AssertAcquired(); |
- const FreeChunk reused_chunk = RemoveFreeChunkFromIterator_Locked( |
+ const FreeChunk reused_chunk = RemoveFreeChunkFromIterator( |
free_chunks_.lower_bound(FreeChunk(actual_size))); |
if (reused_chunk.is_null()) |
- return scoped_ptr<DiscardableMemory>(); |
+ return scoped_ptr<DiscardableAshmemChunk>(); |
used_to_previous_chunk_map_.insert( |
std::make_pair(reused_chunk.start, reused_chunk.previous_chunk)); |
@@ -323,16 +264,16 @@ class DiscardableMemoryAllocator::AshmemRegion { |
const size_t new_chunk_size = reused_chunk.size - actual_size; |
// Note that merging is not needed here since there can't be contiguous |
// free chunks at this point. |
- AddFreeChunk_Locked( |
+ AddFreeChunk( |
FreeChunk(reused_chunk.start, new_chunk_start, new_chunk_size)); |
} |
const size_t offset = |
static_cast<char*>(reused_chunk.start) - static_cast<char*>(base_); |
LockAshmemRegion(fd_, offset, reused_chunk_size); |
- scoped_ptr<DiscardableMemory> memory( |
- new DiscardableAshmemChunk(this, fd_, reused_chunk.start, offset, |
- reused_chunk_size)); |
+ scoped_ptr<DiscardableAshmemChunk> memory( |
+ new DiscardableAshmemChunk( |
+ this, fd_, reused_chunk.start, offset, reused_chunk_size)); |
return memory.Pass(); |
} |
@@ -348,8 +289,7 @@ class DiscardableMemoryAllocator::AshmemRegion { |
// change in versions of kernel >=3.5 though. The fact that free chunks are |
// not immediately released is the reason why we are trying to minimize |
// fragmentation in order not to cause "artificial" memory pressure. |
- void MergeAndAddFreeChunk_Locked(void* chunk, size_t size) { |
- allocator_->lock_.AssertAcquired(); |
+ void MergeAndAddFreeChunk(void* chunk, size_t size) { |
size_t new_free_chunk_size = size; |
// Merge with the previous chunk. |
void* first_free_chunk = chunk; |
@@ -361,7 +301,7 @@ class DiscardableMemoryAllocator::AshmemRegion { |
used_to_previous_chunk_map_.erase(previous_chunk_it); |
if (previous_chunk) { |
- const FreeChunk free_chunk = RemoveFreeChunk_Locked(previous_chunk); |
+ const FreeChunk free_chunk = RemoveFreeChunk(previous_chunk); |
if (!free_chunk.is_null()) { |
new_free_chunk_size += free_chunk.size; |
first_free_chunk = previous_chunk; |
@@ -376,7 +316,7 @@ class DiscardableMemoryAllocator::AshmemRegion { |
// Merge with the next chunk if free and present. |
void* next_chunk = static_cast<char*>(chunk) + size; |
- const FreeChunk next_free_chunk = RemoveFreeChunk_Locked(next_chunk); |
+ const FreeChunk next_free_chunk = RemoveFreeChunk(next_chunk); |
if (!next_free_chunk.is_null()) { |
new_free_chunk_size += next_free_chunk.size; |
if (next_free_chunk.start == highest_allocated_chunk_) |
@@ -390,7 +330,7 @@ class DiscardableMemoryAllocator::AshmemRegion { |
const bool whole_ashmem_region_is_free = |
used_to_previous_chunk_map_.empty(); |
if (!whole_ashmem_region_is_free) { |
- AddFreeChunk_Locked( |
+ AddFreeChunk( |
FreeChunk(previous_chunk, first_free_chunk, new_free_chunk_size)); |
return; |
} |
@@ -402,11 +342,10 @@ class DiscardableMemoryAllocator::AshmemRegion { |
DCHECK(address_to_free_chunk_map_.empty()); |
DCHECK(used_to_previous_chunk_map_.empty()); |
highest_allocated_chunk_ = NULL; |
- allocator_->DeleteAshmemRegion_Locked(this); // Deletes |this|. |
+ allocator_->DeleteAshmemRegion(this); // Deletes |this|. |
} |
- void AddFreeChunk_Locked(const FreeChunk& free_chunk) { |
- allocator_->lock_.AssertAcquired(); |
+ void AddFreeChunk(const FreeChunk& free_chunk) { |
const std::multiset<FreeChunk>::iterator it = free_chunks_.insert( |
free_chunk); |
address_to_free_chunk_map_.insert(std::make_pair(free_chunk.start, it)); |
@@ -423,20 +362,18 @@ class DiscardableMemoryAllocator::AshmemRegion { |
// Finds and removes the free chunk, if any, whose start address is |
// |chunk_start|. Returns a copy of the unlinked free chunk or a free chunk |
// whose content is null if it was not found. |
- FreeChunk RemoveFreeChunk_Locked(void* chunk_start) { |
- allocator_->lock_.AssertAcquired(); |
+ FreeChunk RemoveFreeChunk(void* chunk_start) { |
const hash_map< |
void*, std::multiset<FreeChunk>::iterator>::iterator it = |
address_to_free_chunk_map_.find(chunk_start); |
if (it == address_to_free_chunk_map_.end()) |
return FreeChunk(); |
- return RemoveFreeChunkFromIterator_Locked(it->second); |
+ return RemoveFreeChunkFromIterator(it->second); |
} |
// Same as above but takes an iterator in. |
- FreeChunk RemoveFreeChunkFromIterator_Locked( |
+ FreeChunk RemoveFreeChunkFromIterator( |
std::multiset<FreeChunk>::iterator free_chunk_it) { |
- allocator_->lock_.AssertAcquired(); |
if (free_chunk_it == free_chunks_.end()) |
return FreeChunk(); |
DCHECK(free_chunk_it != free_chunks_.end()); |
@@ -473,12 +410,42 @@ class DiscardableMemoryAllocator::AshmemRegion { |
DISALLOW_COPY_AND_ASSIGN(AshmemRegion); |
}; |
-DiscardableMemoryAllocator::DiscardableAshmemChunk::~DiscardableAshmemChunk() { |
+DiscardableAshmemChunk::~DiscardableAshmemChunk() { |
if (locked_) |
UnlockAshmemRegion(fd_, offset_, size_); |
ashmem_region_->OnChunkDeletion(address_, size_); |
} |
+bool DiscardableAshmemChunk::Lock() { |
+ DCHECK(!locked_); |
+ locked_ = true; |
+ return LockAshmemRegion(fd_, offset_, size_); |
+} |
+ |
+void DiscardableAshmemChunk::Unlock() { |
+ DCHECK(locked_); |
+ locked_ = false; |
+ UnlockAshmemRegion(fd_, offset_, size_); |
+} |
+ |
+void* DiscardableAshmemChunk::Memory() const { |
+ return address_; |
+} |
+ |
+// Note that |ashmem_region| must outlive |this|. |
+DiscardableAshmemChunk::DiscardableAshmemChunk(AshmemRegion* ashmem_region, |
+ int fd, |
+ void* address, |
+ size_t offset, |
+ size_t size) |
+ : ashmem_region_(ashmem_region), |
+ fd_(fd), |
+ address_(address), |
+ offset_(offset), |
+ size_(size), |
+ locked_(true) { |
+} |
+ |
DiscardableMemoryAllocator::DiscardableMemoryAllocator( |
const std::string& name, |
size_t ashmem_region_size) |
@@ -490,25 +457,23 @@ DiscardableMemoryAllocator::DiscardableMemoryAllocator( |
} |
DiscardableMemoryAllocator::~DiscardableMemoryAllocator() { |
- DCHECK(thread_checker_.CalledOnValidThread()); |
DCHECK(ashmem_regions_.empty()); |
} |
-scoped_ptr<DiscardableMemory> DiscardableMemoryAllocator::Allocate( |
+scoped_ptr<DiscardableAshmemChunk> DiscardableMemoryAllocator::Allocate( |
size_t size) { |
const size_t aligned_size = AlignToNextPage(size); |
if (!aligned_size) |
- return scoped_ptr<DiscardableMemory>(); |
+ return scoped_ptr<DiscardableAshmemChunk>(); |
// TODO(pliard): make this function less naive by e.g. moving the free chunks |
// multiset to the allocator itself in order to decrease even more |
// fragmentation/speedup allocation. Note that there should not be more than a |
// couple (=5) of AshmemRegion instances in practice though. |
- AutoLock auto_lock(lock_); |
DCHECK_LE(ashmem_regions_.size(), 5U); |
for (ScopedVector<AshmemRegion>::iterator it = ashmem_regions_.begin(); |
it != ashmem_regions_.end(); ++it) { |
- scoped_ptr<DiscardableMemory> memory( |
- (*it)->Allocate_Locked(size, aligned_size)); |
+ scoped_ptr<DiscardableAshmemChunk> memory( |
+ (*it)->Allocate(size, aligned_size)); |
if (memory) |
return memory.Pass(); |
} |
@@ -525,20 +490,17 @@ scoped_ptr<DiscardableMemory> DiscardableMemoryAllocator::Allocate( |
continue; |
last_ashmem_region_size_ = region_size; |
ashmem_regions_.push_back(new_region.release()); |
- return ashmem_regions_.back()->Allocate_Locked(size, aligned_size); |
+ return ashmem_regions_.back()->Allocate(size, aligned_size); |
} |
// TODO(pliard): consider adding an histogram to see how often this happens. |
- return scoped_ptr<DiscardableMemory>(); |
+ return scoped_ptr<DiscardableAshmemChunk>(); |
} |
size_t DiscardableMemoryAllocator::last_ashmem_region_size() const { |
- AutoLock auto_lock(lock_); |
return last_ashmem_region_size_; |
} |
-void DiscardableMemoryAllocator::DeleteAshmemRegion_Locked( |
- AshmemRegion* region) { |
- lock_.AssertAcquired(); |
+void DiscardableMemoryAllocator::DeleteAshmemRegion(AshmemRegion* region) { |
// Note that there should not be more than a couple of ashmem region instances |
// in |ashmem_regions_|. |
DCHECK_LE(ashmem_regions_.size(), 5U); |