Chromium Code Reviews| 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_ALLOCATION_ASHMEM_FACTORY_H_ |
| 6 #define BASE_MEMORY_DISCARDABLE_MEMORY_ALLOCATOR_H_ | 6 #define BASE_MEMORY_DISCARDABLE_MEMORY_ALLOCATION_ASHMEM_FACTORY_H_ |
|
reveman
2014/03/20 19:33:44
Let's make sure the type is always the last suffix
Philippe
2014/03/21 10:17:44
Yeah. I think it's still ok though to have the Fac
| |
| 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/discardable_memory_allocation.h" | |
| 12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/scoped_vector.h" | 14 #include "base/memory/scoped_vector.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 // On Android/ChromeOS ashmem is used to implement discardable memory. It is |
|
reveman
2014/03/20 19:33:44
I don't think the Android, ChromeOS or any other s
Philippe
2014/03/21 10:17:44
Done.
| |
| 24 // file (descriptor) thus is a limited resource. This allocator minimizes the | 20 // backed by a file (descriptor) therefore it is a limited resource. This |
| 25 // problem by allocating large ashmem regions internally and returning smaller | 21 // factory class (which is in fact more an allocator) minimizes the problem by |
| 26 // chunks to the client. | 22 // allocating large ashmem regions internally and returning smaller chunks to |
| 23 // the client. | |
| 27 // Allocated chunks are systematically aligned on a page boundary therefore this | 24 // Allocated chunks are systematically aligned on a page boundary therefore this |
| 28 // allocator should not be used for small allocations. | 25 // allocator should not be used for small allocations. |
| 29 // | 26 // |
| 30 // Threading: The allocator must be deleted on the thread it was constructed on | 27 // WARNING: this class is not thread-safe! |
| 31 // although its Allocate() method can be invoked on any thread. See | 28 class BASE_EXPORT_PRIVATE DiscardableMemoryAllocationAshmemFactory : |
|
reveman
2014/03/20 19:33:44
I much prefer DiscardableMemoryAllocationFactoryAs
Philippe
2014/03/21 10:17:44
Yeah, done.
| |
| 32 // discardable_memory.h for DiscardableMemory's threading guarantees. | 29 public DiscardableMemoryAllocation::Factory { |
| 33 class BASE_EXPORT_PRIVATE DiscardableMemoryAllocator { | |
| 34 public: | 30 public: |
| 35 // Note that |name| is only used for debugging/measurement purposes. | 31 // 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 | 32 // |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. | 33 // ashmem regions and is expected to be greater or equal than 32 MBytes. |
| 38 DiscardableMemoryAllocator(const std::string& name, | 34 DiscardableMemoryAllocationAshmemFactory(const std::string& name, |
| 39 size_t ashmem_region_size); | 35 size_t ashmem_region_size); |
| 40 | 36 |
| 41 ~DiscardableMemoryAllocator(); | 37 virtual ~DiscardableMemoryAllocationAshmemFactory(); |
| 42 | 38 |
| 43 // Note that the allocator must outlive the returned DiscardableMemory | 39 // Note that the factory must outlive the returned DiscardableMemoryAllocation |
| 44 // instance. | 40 // instance. |
| 45 scoped_ptr<DiscardableMemory> Allocate(size_t size); | 41 // DiscardableMemoryAllocation::Factory: |
| 42 virtual scoped_ptr<DiscardableMemoryAllocation> CreateLockedAllocation( | |
| 43 size_t size) OVERRIDE; | |
| 46 | 44 |
| 47 // Returns the size of the last ashmem region which was created. This is used | 45 // Returns the size of the last ashmem region which was created. This is used |
| 48 // for testing only. | 46 // for testing only. |
| 49 size_t last_ashmem_region_size() const; | 47 size_t last_ashmem_region_size() const; |
| 50 | 48 |
| 51 private: | 49 private: |
| 52 class AshmemRegion; | 50 class AshmemRegion; |
| 53 class DiscardableAshmemChunk; | 51 class DiscardableAshmemChunk; |
| 54 | 52 |
| 55 void DeleteAshmemRegion_Locked(AshmemRegion* region); | 53 void DeleteAshmemRegion(AshmemRegion* region); |
| 56 | 54 |
| 57 ThreadChecker thread_checker_; | |
| 58 const std::string name_; | 55 const std::string name_; |
| 59 const size_t ashmem_region_size_; | 56 const size_t ashmem_region_size_; |
| 60 mutable Lock lock_; | |
| 61 size_t last_ashmem_region_size_; | 57 size_t last_ashmem_region_size_; |
| 62 ScopedVector<AshmemRegion> ashmem_regions_; | 58 ScopedVector<AshmemRegion> ashmem_regions_; |
| 63 | 59 |
| 64 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryAllocator); | 60 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryAllocationAshmemFactory); |
| 65 }; | 61 }; |
| 66 | 62 |
| 67 } // namespace internal | 63 } // namespace internal |
| 68 } // namespace base | 64 } // namespace base |
| 69 | 65 |
| 70 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_ALLOCATOR_H_ | 66 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_ALLOCATION_ASHMEM_FACTORY_H_ |
| OLD | NEW |