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 #include "base/memory/discardable_memory_shmem_allocator.h"
7 #include "base/lazy_instance.h"
8 #include "base/logging.h"
9 #include "base/memory/discardable_shared_memory.h"
14 // Default allocator implementation that allocates in-process
15 // DiscardableSharedMemory instances.
16 class DiscardableMemoryShmemAllocatorImpl
17 : public DiscardableMemoryShmemAllocator
{
19 // Overridden from DiscardableMemoryShmemAllocator:
20 virtual scoped_ptr
<DiscardableSharedMemory
>
21 AllocateLockedDiscardableSharedMemory(size_t size
) override
{
22 scoped_ptr
<DiscardableSharedMemory
> memory(new DiscardableSharedMemory
);
23 if (!memory
->CreateAndMap(size
))
24 return scoped_ptr
<DiscardableSharedMemory
>();
30 LazyInstance
<DiscardableMemoryShmemAllocatorImpl
>::Leaky g_default_allocator
=
31 LAZY_INSTANCE_INITIALIZER
;
33 DiscardableMemoryShmemAllocator
* g_allocator
= nullptr;
38 void DiscardableMemoryShmemAllocator::SetInstance(
39 DiscardableMemoryShmemAllocator
* allocator
) {
42 // Make sure this function is only called once before the first call
46 g_allocator
= allocator
;
50 DiscardableMemoryShmemAllocator
*
51 DiscardableMemoryShmemAllocator::GetInstance() {
53 g_allocator
= g_default_allocator
.Pointer();