Infer appropriate GNU_STACK alignment for a shared library.
[chromium-blink-merge.git] / base / memory / discardable_memory_ashmem_allocator.h
blob996dde924967d1ee71619cc5cf912b223586b2a7
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_
8 #include <string>
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"
16 namespace base {
17 namespace internal {
19 class AshmemRegion;
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();
27 // Returns whether the memory is still resident.
28 bool Lock();
30 void Unlock();
32 void* Memory() const;
34 private:
35 friend class AshmemRegion;
37 DiscardableAshmemChunk(AshmemRegion* ashmem_region,
38 int fd,
39 void* address,
40 size_t offset,
41 size_t size);
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_;
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 {
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);
66 ~DiscardableMemoryAshmemAllocator();
68 // Note that the allocator must outlive the returned DiscardableAshmemChunk
69 // instance.
70 scoped_ptr<DiscardableAshmemChunk> Allocate(size_t size);
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;
76 private:
77 friend class AshmemRegion;
79 void DeleteAshmemRegion_Locked(AshmemRegion* region);
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_;
87 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryAshmemAllocator);
90 } // namespace internal
91 } // namespace base
93 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_ASHMEM_ALLOCATOR_H_