140fdb01e28952acd80025c5bda9591fa67276c3
[platform/framework/web/crosswalk.git] / src / base / memory / discardable_memory_emulated.cc
1 // Copyright 2013 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.
4
5 #include "base/memory/discardable_memory_emulated.h"
6
7 #include "base/lazy_instance.h"
8 #include "base/memory/discardable_memory_manager.h"
9
10 namespace base {
11 namespace {
12
13 // This is admittedly pretty magical. It's approximately enough memory for four
14 // 2560x1600 images.
15 const size_t kEmulatedMemoryLimit = 64 * 1024 * 1024;
16 const size_t kEmulatedBytesToKeepUnderModeratePressure =
17     kEmulatedMemoryLimit / 4;
18
19 struct SharedState {
20   SharedState()
21       : manager(kEmulatedMemoryLimit,
22                 kEmulatedBytesToKeepUnderModeratePressure) {}
23
24   internal::DiscardableMemoryManager manager;
25 };
26 LazyInstance<SharedState>::Leaky g_shared_state = LAZY_INSTANCE_INITIALIZER;
27
28 }  // namespace
29
30 namespace internal {
31
32 DiscardableMemoryEmulated::DiscardableMemoryEmulated(size_t bytes)
33     : bytes_(bytes),
34       is_locked_(false) {
35   g_shared_state.Pointer()->manager.Register(this, bytes);
36 }
37
38 DiscardableMemoryEmulated::~DiscardableMemoryEmulated() {
39   if (is_locked_)
40     Unlock();
41   g_shared_state.Pointer()->manager.Unregister(this);
42 }
43
44 // static
45 void DiscardableMemoryEmulated::RegisterMemoryPressureListeners() {
46   g_shared_state.Pointer()->manager.RegisterMemoryPressureListener();
47 }
48
49 // static
50 void DiscardableMemoryEmulated::UnregisterMemoryPressureListeners() {
51   g_shared_state.Pointer()->manager.UnregisterMemoryPressureListener();
52 }
53
54 // static
55 void DiscardableMemoryEmulated::PurgeForTesting() {
56   g_shared_state.Pointer()->manager.PurgeAll();
57 }
58
59 bool DiscardableMemoryEmulated::Initialize() {
60   return Lock() != DISCARDABLE_MEMORY_LOCK_STATUS_FAILED;
61 }
62
63 DiscardableMemoryLockStatus DiscardableMemoryEmulated::Lock() {
64   DCHECK(!is_locked_);
65
66   bool purged = false;
67   if (!g_shared_state.Pointer()->manager.AcquireLock(this, &purged))
68     return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED;
69
70   is_locked_ = true;
71   return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED
72                 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS;
73 }
74
75 void DiscardableMemoryEmulated::Unlock() {
76   DCHECK(is_locked_);
77   g_shared_state.Pointer()->manager.ReleaseLock(this);
78   is_locked_ = false;
79 }
80
81 void* DiscardableMemoryEmulated::Memory() const {
82   DCHECK(is_locked_);
83   DCHECK(memory_);
84   return memory_.get();
85 }
86
87 bool DiscardableMemoryEmulated::AllocateAndAcquireLock() {
88   if (memory_)
89     return true;
90
91   memory_.reset(new uint8[bytes_]);
92   return false;
93 }
94
95 void DiscardableMemoryEmulated::Purge() {
96   memory_.reset();
97 }
98
99 }  // namespace internal
100 }  // namespace base