OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 #include "base/memory/discardable_memory_shmem_allocator.h" | 5 #include "base/memory/discardable_memory_shmem_allocator.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/discardable_shared_memory.h" | 9 #include "base/memory/discardable_shared_memory.h" |
10 | 10 |
11 namespace base { | 11 namespace base { |
12 namespace { | 12 namespace { |
13 | 13 |
| 14 class DiscardableMemoryShmemChunkImpl : public DiscardableMemoryShmemChunk { |
| 15 public: |
| 16 explicit DiscardableMemoryShmemChunkImpl( |
| 17 scoped_ptr<DiscardableSharedMemory> shared_memory) |
| 18 : shared_memory_(shared_memory.Pass()) {} |
| 19 |
| 20 // Overridden from DiscardableMemoryShmemChunk: |
| 21 bool Lock() override { return false; } |
| 22 void Unlock() override { |
| 23 shared_memory_->Unlock(0, 0); |
| 24 shared_memory_.reset(); |
| 25 } |
| 26 void* Memory() const override { return shared_memory_->memory(); } |
| 27 |
| 28 private: |
| 29 scoped_ptr<DiscardableSharedMemory> shared_memory_; |
| 30 |
| 31 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryShmemChunkImpl); |
| 32 }; |
| 33 |
| 34 // Default allocator implementation that allocates in-process |
| 35 // DiscardableSharedMemory instances. |
| 36 class DiscardableMemoryShmemAllocatorImpl |
| 37 : public DiscardableMemoryShmemAllocator { |
| 38 public: |
| 39 // Overridden from DiscardableMemoryShmemAllocator: |
| 40 scoped_ptr<DiscardableMemoryShmemChunk> |
| 41 AllocateLockedDiscardableMemory(size_t size) override { |
| 42 scoped_ptr<DiscardableSharedMemory> memory(new DiscardableSharedMemory); |
| 43 if (!memory->CreateAndMap(size)) |
| 44 return nullptr; |
| 45 |
| 46 return make_scoped_ptr(new DiscardableMemoryShmemChunkImpl(memory.Pass())); |
| 47 } |
| 48 }; |
| 49 |
| 50 LazyInstance<DiscardableMemoryShmemAllocatorImpl>::Leaky g_default_allocator = |
| 51 LAZY_INSTANCE_INITIALIZER; |
| 52 |
14 DiscardableMemoryShmemAllocator* g_allocator = nullptr; | 53 DiscardableMemoryShmemAllocator* g_allocator = nullptr; |
15 | 54 |
16 } // namespace | 55 } // namespace |
17 | 56 |
18 // static | 57 // static |
19 void DiscardableMemoryShmemAllocator::SetInstance( | 58 void DiscardableMemoryShmemAllocator::SetInstance( |
20 DiscardableMemoryShmemAllocator* allocator) { | 59 DiscardableMemoryShmemAllocator* allocator) { |
21 DCHECK(allocator); | 60 DCHECK(allocator); |
22 | 61 |
23 // Make sure this function is only called once before the first call | 62 // Make sure this function is only called once before the first call |
24 // to GetInstance(). | 63 // to GetInstance(). |
25 DCHECK(!g_allocator); | 64 DCHECK(!g_allocator); |
26 | 65 |
27 g_allocator = allocator; | 66 g_allocator = allocator; |
28 } | 67 } |
29 | 68 |
30 // static | 69 // static |
31 DiscardableMemoryShmemAllocator* | 70 DiscardableMemoryShmemAllocator* |
32 DiscardableMemoryShmemAllocator::GetInstance() { | 71 DiscardableMemoryShmemAllocator::GetInstance() { |
33 DCHECK(g_allocator); | 72 if (!g_allocator) |
| 73 g_allocator = g_default_allocator.Pointer(); |
| 74 |
34 return g_allocator; | 75 return g_allocator; |
35 } | 76 } |
36 | 77 |
37 } // namespace base | 78 } // namespace base |
OLD | NEW |