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 | |
53 DiscardableMemoryShmemAllocator* g_allocator = nullptr; | 14 DiscardableMemoryShmemAllocator* g_allocator = nullptr; |
54 | 15 |
55 } // namespace | 16 } // namespace |
56 | 17 |
57 // static | 18 // static |
58 void DiscardableMemoryShmemAllocator::SetInstance( | 19 void DiscardableMemoryShmemAllocator::SetInstance( |
59 DiscardableMemoryShmemAllocator* allocator) { | 20 DiscardableMemoryShmemAllocator* allocator) { |
60 DCHECK(allocator); | 21 DCHECK(allocator); |
61 | 22 |
62 // Make sure this function is only called once before the first call | 23 // Make sure this function is only called once before the first call |
63 // to GetInstance(). | 24 // to GetInstance(). |
64 DCHECK(!g_allocator); | 25 DCHECK(!g_allocator); |
65 | 26 |
66 g_allocator = allocator; | 27 g_allocator = allocator; |
67 } | 28 } |
68 | 29 |
69 // static | 30 // static |
70 DiscardableMemoryShmemAllocator* | 31 DiscardableMemoryShmemAllocator* |
71 DiscardableMemoryShmemAllocator::GetInstance() { | 32 DiscardableMemoryShmemAllocator::GetInstance() { |
72 if (!g_allocator) | 33 DCHECK(g_allocator); |
73 g_allocator = g_default_allocator.Pointer(); | |
74 | |
75 return g_allocator; | 34 return g_allocator; |
76 } | 35 } |
77 | 36 |
78 } // namespace base | 37 } // namespace base |
OLD | NEW |