Docserver: Skip to version 3-45-2. There was a test instance already uploaded
[chromium-blink-merge.git] / base / memory / discardable_memory.h
blob5f83e333775b664c9f1e796e9b372c22c1768888
1 // Copyright (c) 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.
5 #ifndef BASE_MEMORY_DISCARDABLE_MEMORY_H_
6 #define BASE_MEMORY_DISCARDABLE_MEMORY_H_
8 #include <string>
9 #include <vector>
11 #include "base/base_export.h"
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/scoped_ptr.h"
16 namespace base {
18 enum DiscardableMemoryType {
19 DISCARDABLE_MEMORY_TYPE_NONE,
20 DISCARDABLE_MEMORY_TYPE_ASHMEM,
21 DISCARDABLE_MEMORY_TYPE_MACH,
22 DISCARDABLE_MEMORY_TYPE_EMULATED
25 enum DiscardableMemoryLockStatus {
26 DISCARDABLE_MEMORY_LOCK_STATUS_FAILED,
27 DISCARDABLE_MEMORY_LOCK_STATUS_PURGED,
28 DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS
31 // Platform abstraction for discardable memory. DiscardableMemory is used to
32 // cache large objects without worrying about blowing out memory, both on mobile
33 // devices where there is no swap, and desktop devices where unused free memory
34 // should be used to help the user experience. This is preferable to releasing
35 // memory in response to an OOM signal because it is simpler, though it has less
36 // flexibility as to which objects get discarded.
38 // Discardable memory has two states: locked and unlocked. While the memory is
39 // locked, it will not be discarded. Unlocking the memory allows the OS to
40 // reclaim it if needed. Locks do not nest.
42 // Notes:
43 // - The paging behavior of memory while it is locked is not specified. While
44 // mobile platforms will not swap it out, it may qualify for swapping
45 // on desktop platforms. It is not expected that this will matter, as the
46 // preferred pattern of usage for DiscardableMemory is to lock down the
47 // memory, use it as quickly as possible, and then unlock it.
48 // - Because of memory alignment, the amount of memory allocated can be
49 // larger than the requested memory size. It is not very efficient for
50 // small allocations.
51 // - A discardable memory instance is not thread safe. It is the
52 // responsibility of users of discardable memory to ensure there are no
53 // races.
55 // References:
56 // - Linux: http://lwn.net/Articles/452035/
57 // - Mac: http://trac.webkit.org/browser/trunk/Source/WebCore/platform/mac/PurgeableBufferMac.cpp
58 // the comment starting with "vm_object_purgable_control" at
59 // http://www.opensource.apple.com/source/xnu/xnu-792.13.8/osfmk/vm/vm_object.c
61 // Thread-safety: DiscardableMemory instances are not thread-safe.
62 class BASE_EXPORT DiscardableMemory {
63 public:
64 virtual ~DiscardableMemory() {}
66 // Gets the discardable memory type with a given name.
67 static DiscardableMemoryType GetNamedType(const std::string& name);
69 // Gets the name of a discardable memory type.
70 static const char* GetTypeName(DiscardableMemoryType type);
72 // Gets system supported discardable memory types. Default preferred type
73 // at the front of vector.
74 static void GetSupportedTypes(std::vector<DiscardableMemoryType>* types);
76 // Sets the preferred discardable memory type. This overrides the default
77 // preferred type. Can only be called once prior to GetPreferredType()
78 // or CreateLockedMemory(). Caller is responsible for correct ordering.
79 static void SetPreferredType(DiscardableMemoryType type);
81 // Gets the preferred discardable memory type.
82 static DiscardableMemoryType GetPreferredType();
84 // Create a DiscardableMemory instance with specified |type| and |size|.
85 static scoped_ptr<DiscardableMemory> CreateLockedMemoryWithType(
86 DiscardableMemoryType type, size_t size);
88 // Create a DiscardableMemory instance with preferred type and |size|.
89 static scoped_ptr<DiscardableMemory> CreateLockedMemory(size_t size);
91 // Discardable memory implementations might allow an elevated usage level
92 // while in frequent use. Call this to have the usage reduced to the base
93 // level. Returns true if there's no need to call this again until
94 // memory instances have been used. This indicates that all discardable
95 // memory implementations have reduced usage to the base level or below.
96 // Note: calling this too often or while discardable memory is in frequent
97 // use can hurt performance, whereas calling it too infrequently can result
98 // in memory bloat.
99 static bool ReduceMemoryUsage();
101 // Locks the memory so that it will not be purged by the system. Returns
102 // DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS on success. If the return value is
103 // DISCARDABLE_MEMORY_LOCK_STATUS_FAILED then this object should be
104 // discarded and a new one should be created. If the return value is
105 // DISCARDABLE_MEMORY_LOCK_STATUS_PURGED then the memory is present but any
106 // data that was in it is gone.
107 virtual DiscardableMemoryLockStatus Lock() WARN_UNUSED_RESULT = 0;
109 // Unlocks the memory so that it can be purged by the system. Must be called
110 // after every successful lock call.
111 virtual void Unlock() = 0;
113 // Returns the memory address held by this object. The object must be locked
114 // before calling this. Otherwise, this will cause a DCHECK error.
115 virtual void* Memory() const = 0;
117 // Testing utility calls.
119 // Purge all discardable memory in the system. This call has global effects
120 // across all running processes, so it should only be used for testing!
121 static void PurgeForTesting();
124 } // namespace base
126 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_H_