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_ALLOCATOR_H_ | |
6 #define BASE_MEMORY_DISCARDABLE_MEMORY_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 #include "base/threading/thread_checker.h" | |
16 | |
17 namespace base { | |
18 | |
19 class DiscardableMemory; | |
20 | |
21 namespace internal { | |
22 | |
23 // On Android ashmem is used to implement discardable memory. It is backed by a | |
24 // file (descriptor) thus is a limited resource. This allocator minimizes the | |
25 // problem by allocating large ashmem regions internally and returning smaller | |
26 // chunks to the client. | |
27 // Allocated chunks are systematically aligned on a page boundary therefore this | |
28 // 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 { | |
34 public: | |
35 // Exposed for testing. | |
36 enum { | |
37 kMinAshmemRegionSize = 32 * 1024 * 1024, | |
willchan no longer on Chromium
2013/11/28 06:16:37
When the allocation size is larger than this, is t
Philippe
2013/11/28 16:42:53
Yeah, you're right. I slightly modified the heuris
| |
38 }; | |
39 | |
40 // Note that |name| is only used for debugging/measurement purposes. | |
41 explicit DiscardableMemoryAllocator(const std::string& name); | |
42 ~DiscardableMemoryAllocator(); | |
43 | |
44 // Note that the allocator must outlive the returned DiscardableMemory | |
45 // instance. | |
46 scoped_ptr<DiscardableMemory> Allocate(size_t size); | |
47 | |
48 private: | |
49 class AshmemRegion; | |
50 class DiscardableAshmemChunk; | |
51 | |
52 void DeleteAshmemRegion_Locked(AshmemRegion* region); | |
53 | |
54 base::ThreadChecker thread_checker_; | |
55 const std::string name_; | |
56 base::Lock lock_; | |
57 ScopedVector<AshmemRegion> ashmem_regions_; | |
58 | |
59 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryAllocator); | |
60 }; | |
61 | |
62 } // namespace internal | |
63 } // namespace base | |
64 | |
65 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_ALLOCATOR_H_ | |
OLD | NEW |