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_ALLOCATOR_H_ |
6 #define BASE_MEMORY_DISCARDABLE_MEMORY_ALLOCATOR_H_ | 6 #define BASE_MEMORY_DISCARDABLE_MEMORY_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" | |
15 #include "base/threading/thread_checker.h" | |
16 | 14 |
17 namespace base { | 15 namespace base { |
16 namespace internal { | |
18 | 17 |
19 class DiscardableMemory; | 18 // WARNING: The classes below are *NOT* thread-safe! This means that it's the |
19 // responsibility of the client (e.g. DiscardableMemoryAshmem) to synchronize | |
20 // operations on a same allocator instance. | |
20 | 21 |
21 namespace internal { | 22 class AshmemRegion; |
23 | |
24 // Internal class, whose instances are returned to the client of the allocator | |
25 // (e.g. DiscardableMemoryAshmem), that mimicks the DiscardableMemory interface. | |
26 class DiscardableAshmemChunk { | |
27 public: | |
28 // WARNING: This notifies the allocator instance from which this instance was | |
29 // constructed, i.e. see the thread-safety warning above. | |
30 ~DiscardableAshmemChunk(); | |
31 | |
32 // Returns whether the memory is still resident. | |
33 bool Lock(); | |
34 | |
35 void Unlock(); | |
36 | |
37 void* Memory() const; | |
38 | |
39 private: | |
40 friend class AshmemRegion; | |
41 | |
42 DiscardableAshmemChunk(AshmemRegion* ashmem_region, | |
43 int fd, | |
44 void* address, | |
45 size_t offset, | |
46 size_t size); | |
47 | |
48 AshmemRegion* const ashmem_region_; | |
49 const int fd_; | |
50 void* const address_; | |
51 const size_t offset_; | |
52 const size_t size_; | |
53 bool locked_; | |
54 | |
55 DISALLOW_COPY_AND_ASSIGN(DiscardableAshmemChunk); | |
56 }; | |
22 | 57 |
23 // On Android ashmem is used to implement discardable memory. It is backed by a | 58 // On Android ashmem is used to implement discardable memory. It is backed by a |
reveman
2014/04/26 00:13:37
nit: Please remove "Android" from here if this cod
Philippe
2014/04/28 12:23:14
Done.
| |
24 // file (descriptor) thus is a limited resource. This allocator minimizes the | 59 // file (descriptor) thus is a limited resource. This allocator minimizes the |
25 // problem by allocating large ashmem regions internally and returning smaller | 60 // problem by allocating large ashmem regions internally and returning smaller |
26 // chunks to the client. | 61 // chunks to the client. |
27 // Allocated chunks are systematically aligned on a page boundary therefore this | 62 // Allocated chunks are systematically aligned on a page boundary therefore this |
28 // allocator should not be used for small allocations. | 63 // allocator should not be used for small allocations. |
29 // | |
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 { | 64 class BASE_EXPORT_PRIVATE DiscardableMemoryAllocator { |
reveman
2014/04/26 00:13:37
What's the correct name for this class and these f
Philippe
2014/04/28 12:23:14
I had suggested in my previous comment to keep the
reveman
2014/04/28 15:39:16
Sorry I missed that. I think it makes sense as par
Philippe
2014/04/28 15:53:45
No problem, I was worried about increasing the dif
| |
34 public: | 65 public: |
35 // Note that |name| is only used for debugging/measurement purposes. | 66 // 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 | 67 // |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. | 68 // ashmem regions and is expected to be greater or equal than 32 MBytes. |
38 DiscardableMemoryAllocator(const std::string& name, | 69 DiscardableMemoryAllocator(const std::string& name, |
39 size_t ashmem_region_size); | 70 size_t ashmem_region_size); |
40 | 71 |
41 ~DiscardableMemoryAllocator(); | 72 ~DiscardableMemoryAllocator(); |
42 | 73 |
43 // Note that the allocator must outlive the returned DiscardableMemory | 74 // Note that the allocator must outlive the returned DiscardableAshmemChunk |
44 // instance. | 75 // instance. |
45 scoped_ptr<DiscardableMemory> Allocate(size_t size); | 76 scoped_ptr<DiscardableAshmemChunk> Allocate(size_t size); |
46 | 77 |
47 // Returns the size of the last ashmem region which was created. This is used | 78 // Returns the size of the last ashmem region which was created. This is used |
48 // for testing only. | 79 // for testing only. |
49 size_t last_ashmem_region_size() const; | 80 size_t last_ashmem_region_size() const; |
50 | 81 |
51 private: | 82 private: |
52 class AshmemRegion; | 83 friend class AshmemRegion; |
53 class DiscardableAshmemChunk; | |
54 | 84 |
55 void DeleteAshmemRegion_Locked(AshmemRegion* region); | 85 void DeleteAshmemRegion(AshmemRegion* region); |
56 | 86 |
57 ThreadChecker thread_checker_; | |
58 const std::string name_; | 87 const std::string name_; |
59 const size_t ashmem_region_size_; | 88 const size_t ashmem_region_size_; |
60 mutable Lock lock_; | |
61 size_t last_ashmem_region_size_; | 89 size_t last_ashmem_region_size_; |
62 ScopedVector<AshmemRegion> ashmem_regions_; | 90 ScopedVector<AshmemRegion> ashmem_regions_; |
63 | 91 |
64 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryAllocator); | 92 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryAllocator); |
65 }; | 93 }; |
66 | 94 |
67 } // namespace internal | 95 } // namespace internal |
68 } // namespace base | 96 } // namespace base |
69 | 97 |
70 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_ALLOCATOR_H_ | 98 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_ALLOCATOR_H_ |
OLD | NEW |