| 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 shared_memory_->Lock(0, 0); } |
| 22 void Unlock() override { shared_memory_->Unlock(0, 0); } |
| 23 void* Memory() const override { return shared_memory_->memory(); } |
| 24 bool IsMemoryResident() const override { |
| 25 return shared_memory_->IsMemoryResident(); |
| 26 } |
| 27 |
| 28 private: |
| 29 scoped_ptr<DiscardableSharedMemory> shared_memory_; |
| 30 |
| 31 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryShmemChunkImpl); |
| 32 }; |
| 33 |
| 14 // Default allocator implementation that allocates in-process | 34 // Default allocator implementation that allocates in-process |
| 15 // DiscardableSharedMemory instances. | 35 // DiscardableSharedMemory instances. |
| 16 class DiscardableMemoryShmemAllocatorImpl | 36 class DiscardableMemoryShmemAllocatorImpl |
| 17 : public DiscardableMemoryShmemAllocator { | 37 : public DiscardableMemoryShmemAllocator { |
| 18 public: | 38 public: |
| 19 // Overridden from DiscardableMemoryShmemAllocator: | 39 // Overridden from DiscardableMemoryShmemAllocator: |
| 20 scoped_ptr<DiscardableSharedMemory> AllocateLockedDiscardableSharedMemory( | 40 scoped_ptr<DiscardableMemoryShmemChunk> |
| 21 size_t size) override { | 41 AllocateLockedDiscardableMemory(size_t size) override { |
| 22 scoped_ptr<DiscardableSharedMemory> memory(new DiscardableSharedMemory); | 42 scoped_ptr<DiscardableSharedMemory> memory(new DiscardableSharedMemory); |
| 23 if (!memory->CreateAndMap(size)) | 43 if (!memory->CreateAndMap(size)) |
| 24 return scoped_ptr<DiscardableSharedMemory>(); | 44 return nullptr; |
| 25 | 45 |
| 26 return memory.Pass(); | 46 return make_scoped_ptr(new DiscardableMemoryShmemChunkImpl(memory.Pass())); |
| 27 } | 47 } |
| 28 }; | 48 }; |
| 29 | 49 |
| 30 LazyInstance<DiscardableMemoryShmemAllocatorImpl>::Leaky g_default_allocator = | 50 LazyInstance<DiscardableMemoryShmemAllocatorImpl>::Leaky g_default_allocator = |
| 31 LAZY_INSTANCE_INITIALIZER; | 51 LAZY_INSTANCE_INITIALIZER; |
| 32 | 52 |
| 33 DiscardableMemoryShmemAllocator* g_allocator = nullptr; | 53 DiscardableMemoryShmemAllocator* g_allocator = nullptr; |
| 34 | 54 |
| 35 } // namespace | 55 } // namespace |
| 36 | 56 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 49 // static | 69 // static |
| 50 DiscardableMemoryShmemAllocator* | 70 DiscardableMemoryShmemAllocator* |
| 51 DiscardableMemoryShmemAllocator::GetInstance() { | 71 DiscardableMemoryShmemAllocator::GetInstance() { |
| 52 if (!g_allocator) | 72 if (!g_allocator) |
| 53 g_allocator = g_default_allocator.Pointer(); | 73 g_allocator = g_default_allocator.Pointer(); |
| 54 | 74 |
| 55 return g_allocator; | 75 return g_allocator; |
| 56 } | 76 } |
| 57 | 77 |
| 58 } // namespace base | 78 } // namespace base |
| OLD | NEW |