| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_MEMORY_DISCARDABLE_MEMORY_ALLOCATOR_H_ | 5 #ifndef BASE_MEMORY_DISCARDABLE_MEMORY_ASHMEM_ALLOCATOR_H_ |
| 6 #define BASE_MEMORY_DISCARDABLE_MEMORY_ALLOCATOR_H_ | 6 #define BASE_MEMORY_DISCARDABLE_MEMORY_ASHMEM_ALLOCATOR_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/scoped_vector.h" | 13 #include "base/memory/scoped_vector.h" |
| 14 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 15 #include "base/threading/thread_checker.h" | |
| 16 | 15 |
| 17 namespace base { | 16 namespace base { |
| 18 | |
| 19 class DiscardableMemory; | |
| 20 | |
| 21 namespace internal { | 17 namespace internal { |
| 22 | 18 |
| 23 // On Android ashmem is used to implement discardable memory. It is backed by a | 19 class AshmemRegion; |
| 24 // file (descriptor) thus is a limited resource. This allocator minimizes the | 20 |
| 25 // problem by allocating large ashmem regions internally and returning smaller | 21 // Internal class, whose instances are returned to the client of the allocator |
| 26 // chunks to the client. | 22 // (e.g. DiscardableMemoryAshmem), that mimicks the DiscardableMemory interface. |
| 23 class BASE_EXPORT_PRIVATE DiscardableAshmemChunk { |
| 24 public: |
| 25 ~DiscardableAshmemChunk(); |
| 26 |
| 27 // Returns whether the memory is still resident. |
| 28 bool Lock(); |
| 29 |
| 30 void Unlock(); |
| 31 |
| 32 void* Memory() const; |
| 33 |
| 34 private: |
| 35 friend class AshmemRegion; |
| 36 |
| 37 DiscardableAshmemChunk(AshmemRegion* ashmem_region, |
| 38 int fd, |
| 39 void* address, |
| 40 size_t offset, |
| 41 size_t size); |
| 42 |
| 43 AshmemRegion* const ashmem_region_; |
| 44 const int fd_; |
| 45 void* const address_; |
| 46 const size_t offset_; |
| 47 const size_t size_; |
| 48 bool locked_; |
| 49 |
| 50 DISALLOW_COPY_AND_ASSIGN(DiscardableAshmemChunk); |
| 51 }; |
| 52 |
| 53 // Ashmem regions are backed by a file (descriptor) therefore they are a limited |
| 54 // resource. This allocator minimizes the problem by allocating large ashmem |
| 55 // regions internally and returning smaller chunks to the client. |
| 27 // Allocated chunks are systematically aligned on a page boundary therefore this | 56 // Allocated chunks are systematically aligned on a page boundary therefore this |
| 28 // allocator should not be used for small allocations. | 57 // allocator should not be used for small allocations. |
| 29 // | 58 class BASE_EXPORT_PRIVATE DiscardableMemoryAshmemAllocator { |
| 30 // Threading: The allocator must be deleted on the thread it was constructed on | |
| 31 // although its Allocate() method can be invoked on any thread. See | |
| 32 // discardable_memory.h for DiscardableMemory's threading guarantees. | |
| 33 class BASE_EXPORT_PRIVATE DiscardableMemoryAllocator { | |
| 34 public: | 59 public: |
| 35 // Note that |name| is only used for debugging/measurement purposes. | 60 // Note that |name| is only used for debugging/measurement purposes. |
| 36 // |ashmem_region_size| is the size that will be used to create the underlying | 61 // |ashmem_region_size| is the size that will be used to create the underlying |
| 37 // ashmem regions and is expected to be greater or equal than 32 MBytes. | 62 // ashmem regions and is expected to be greater or equal than 32 MBytes. |
| 38 DiscardableMemoryAllocator(const std::string& name, | 63 DiscardableMemoryAshmemAllocator(const std::string& name, |
| 39 size_t ashmem_region_size); | 64 size_t ashmem_region_size); |
| 40 | 65 |
| 41 ~DiscardableMemoryAllocator(); | 66 ~DiscardableMemoryAshmemAllocator(); |
| 42 | 67 |
| 43 // Note that the allocator must outlive the returned DiscardableMemory | 68 // Note that the allocator must outlive the returned DiscardableAshmemChunk |
| 44 // instance. | 69 // instance. |
| 45 scoped_ptr<DiscardableMemory> Allocate(size_t size); | 70 scoped_ptr<DiscardableAshmemChunk> Allocate(size_t size); |
| 46 | 71 |
| 47 // Returns the size of the last ashmem region which was created. This is used | 72 // Returns the size of the last ashmem region which was created. This is used |
| 48 // for testing only. | 73 // for testing only. |
| 49 size_t last_ashmem_region_size() const; | 74 size_t last_ashmem_region_size() const; |
| 50 | 75 |
| 51 private: | 76 private: |
| 52 class AshmemRegion; | 77 friend class AshmemRegion; |
| 53 class DiscardableAshmemChunk; | |
| 54 | 78 |
| 55 void DeleteAshmemRegion_Locked(AshmemRegion* region); | 79 void DeleteAshmemRegion_Locked(AshmemRegion* region); |
| 56 | 80 |
| 57 ThreadChecker thread_checker_; | |
| 58 const std::string name_; | 81 const std::string name_; |
| 59 const size_t ashmem_region_size_; | 82 const size_t ashmem_region_size_; |
| 60 mutable Lock lock_; | 83 mutable Lock lock_; |
| 61 size_t last_ashmem_region_size_; | 84 size_t last_ashmem_region_size_; |
| 62 ScopedVector<AshmemRegion> ashmem_regions_; | 85 ScopedVector<AshmemRegion> ashmem_regions_; |
| 63 | 86 |
| 64 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryAllocator); | 87 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryAshmemAllocator); |
| 65 }; | 88 }; |
| 66 | 89 |
| 67 } // namespace internal | 90 } // namespace internal |
| 68 } // namespace base | 91 } // namespace base |
| 69 | 92 |
| 70 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_ALLOCATOR_H_ | 93 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_ASHMEM_ALLOCATOR_H_ |
| OLD | NEW |