Support for unpacked ARM packed relocations.
[chromium-blink-merge.git] / base / memory / discardable_memory.h
blob9189b5eb027901a0e6e22d87853cd928b0c9fb4b
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_MAC,
22 DISCARDABLE_MEMORY_TYPE_EMULATED,
23 DISCARDABLE_MEMORY_TYPE_MALLOC
26 enum DiscardableMemoryLockStatus {
27 DISCARDABLE_MEMORY_LOCK_STATUS_FAILED,
28 DISCARDABLE_MEMORY_LOCK_STATUS_PURGED,
29 DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS
32 // Platform abstraction for discardable memory. DiscardableMemory is used to
33 // cache large objects without worrying about blowing out memory, both on mobile
34 // devices where there is no swap, and desktop devices where unused free memory
35 // should be used to help the user experience. This is preferable to releasing
36 // memory in response to an OOM signal because it is simpler, though it has less
37 // flexibility as to which objects get discarded.
39 // Discardable memory has two states: locked and unlocked. While the memory is
40 // locked, it will not be discarded. Unlocking the memory allows the OS to
41 // reclaim it if needed. Locks do not nest.
43 // Notes:
44 // - The paging behavior of memory while it is locked is not specified. While
45 // mobile platforms will not swap it out, it may qualify for swapping
46 // on desktop platforms. It is not expected that this will matter, as the
47 // preferred pattern of usage for DiscardableMemory is to lock down the
48 // memory, use it as quickly as possible, and then unlock it.
49 // - Because of memory alignment, the amount of memory allocated can be
50 // larger than the requested memory size. It is not very efficient for
51 // small allocations.
52 // - A discardable memory instance is not thread safe. It is the
53 // responsibility of users of discardable memory to ensure there are no
54 // races.
56 // References:
57 // - Linux: http://lwn.net/Articles/452035/
58 // - Mac: http://trac.webkit.org/browser/trunk/Source/WebCore/platform/mac/PurgeableBufferMac.cpp
59 // the comment starting with "vm_object_purgable_control" at
60 // http://www.opensource.apple.com/source/xnu/xnu-792.13.8/osfmk/vm/vm_object.c
62 // Thread-safety: DiscardableMemory instances are not thread-safe.
63 class BASE_EXPORT DiscardableMemory {
64 public:
65 virtual ~DiscardableMemory() {}
67 // Call this on a thread with a MessageLoop current to allow discardable
68 // memory implementations to respond to memory pressure signals.
69 static void RegisterMemoryPressureListeners();
71 // Call this to prevent discardable memory implementations from responding
72 // to memory pressure signals.
73 static void UnregisterMemoryPressureListeners();
75 // Gets the discardable memory type with a given name.
76 static DiscardableMemoryType GetNamedType(const std::string& name);
78 // Gets the name of a discardable memory type.
79 static const char* GetTypeName(DiscardableMemoryType type);
81 // Gets system supported discardable memory types. Default preferred type
82 // at the front of vector.
83 static void GetSupportedTypes(std::vector<DiscardableMemoryType>* types);
85 // Sets the preferred discardable memory type. This overrides the default
86 // preferred type. Can only be called once prior to GetPreferredType()
87 // or CreateLockedMemory(). Caller is responsible for correct ordering.
88 static void SetPreferredType(DiscardableMemoryType type);
90 // Gets the preferred discardable memory type.
91 static DiscardableMemoryType GetPreferredType();
93 // Create a DiscardableMemory instance with specified |type| and |size|.
94 static scoped_ptr<DiscardableMemory> CreateLockedMemoryWithType(
95 DiscardableMemoryType type, size_t size);
97 // Create a DiscardableMemory instance with preferred type and |size|.
98 static scoped_ptr<DiscardableMemory> CreateLockedMemory(size_t size);
100 // Locks the memory so that it will not be purged by the system. Returns
101 // DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS on success. If the return value is
102 // DISCARDABLE_MEMORY_LOCK_STATUS_FAILED then this object should be
103 // discarded and a new one should be created. If the return value is
104 // DISCARDABLE_MEMORY_LOCK_STATUS_PURGED then the memory is present but any
105 // data that was in it is gone.
106 virtual DiscardableMemoryLockStatus Lock() WARN_UNUSED_RESULT = 0;
108 // Unlocks the memory so that it can be purged by the system. Must be called
109 // after every successful lock call.
110 virtual void Unlock() = 0;
112 // Returns the memory address held by this object. The object must be locked
113 // before calling this. Otherwise, this will cause a DCHECK error.
114 virtual void* Memory() const = 0;
116 // Testing utility calls.
118 // Purge all discardable memory in the system. This call has global effects
119 // across all running processes, so it should only be used for testing!
120 static void PurgeForTesting();
123 } // namespace base
125 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_H_