1 // Copyright 2014 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.
5 #ifndef BASE_MEMORY_DISCARDABLE_MEMORY_ASHMEM_ALLOCATOR_H_
6 #define BASE_MEMORY_DISCARDABLE_MEMORY_ASHMEM_ALLOCATOR_H_
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"
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
{
25 ~DiscardableAshmemChunk();
27 // Returns whether the memory is still resident.
35 friend class AshmemRegion
;
37 DiscardableAshmemChunk(AshmemRegion
* ashmem_region
,
43 AshmemRegion
* const ashmem_region_
;
50 DISALLOW_COPY_AND_ASSIGN(DiscardableAshmemChunk
);
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
{
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
);
66 ~DiscardableMemoryAshmemAllocator();
68 // Note that the allocator must outlive the returned DiscardableAshmemChunk
70 scoped_ptr
<DiscardableAshmemChunk
> Allocate(size_t size
);
72 // Returns the size of the last ashmem region which was created. This is used
74 size_t last_ashmem_region_size() const;
77 friend class AshmemRegion
;
79 void DeleteAshmemRegion_Locked(AshmemRegion
* region
);
81 const std::string name_
;
82 const size_t ashmem_region_size_
;
84 size_t last_ashmem_region_size_
;
85 ScopedVector
<AshmemRegion
> ashmem_regions_
;
87 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryAshmemAllocator
);
90 } // namespace internal
93 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_ASHMEM_ALLOCATOR_H_