| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef BASE_MEMORY_DISCARDABLE_MEMORY_ASHMEM_ALLOCATOR_H_ | |
| 6 #define BASE_MEMORY_DISCARDABLE_MEMORY_ASHMEM_ALLOCATOR_H_ | |
| 7 | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/base_export.h" | |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/memory/scoped_vector.h" | |
| 14 #include "base/synchronization/lock.h" | |
| 15 | |
| 16 namespace base { | |
| 17 namespace internal { | |
| 18 | |
| 19 class AshmemRegion; | |
| 20 | |
| 21 // Internal class, whose instances are returned to the client of the allocator | |
| 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. | |
| 56 // Allocated chunks are systematically aligned on a page boundary therefore this | |
| 57 // allocator should not be used for small allocations. | |
| 58 class BASE_EXPORT_PRIVATE DiscardableMemoryAshmemAllocator { | |
| 59 public: | |
| 60 // Note that |name| is only used for debugging/measurement purposes. | |
| 61 // |ashmem_region_size| is the size that will be used to create the underlying | |
| 62 // ashmem regions and is expected to be greater or equal than 32 MBytes. | |
| 63 DiscardableMemoryAshmemAllocator(const std::string& name, | |
| 64 size_t ashmem_region_size); | |
| 65 | |
| 66 ~DiscardableMemoryAshmemAllocator(); | |
| 67 | |
| 68 // Note that the allocator must outlive the returned DiscardableAshmemChunk | |
| 69 // instance. | |
| 70 scoped_ptr<DiscardableAshmemChunk> Allocate(size_t size); | |
| 71 | |
| 72 // Returns the size of the last ashmem region which was created. This is used | |
| 73 // for testing only. | |
| 74 size_t last_ashmem_region_size() const; | |
| 75 | |
| 76 private: | |
| 77 friend class AshmemRegion; | |
| 78 | |
| 79 void DeleteAshmemRegion_Locked(AshmemRegion* region); | |
| 80 | |
| 81 const std::string name_; | |
| 82 const size_t ashmem_region_size_; | |
| 83 mutable Lock lock_; | |
| 84 size_t last_ashmem_region_size_; | |
| 85 ScopedVector<AshmemRegion> ashmem_regions_; | |
| 86 | |
| 87 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryAshmemAllocator); | |
| 88 }; | |
| 89 | |
| 90 } // namespace internal | |
| 91 } // namespace base | |
| 92 | |
| 93 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_ASHMEM_ALLOCATOR_H_ | |
| OLD | NEW |