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. 4 5 #ifndef BASE_MEMORY_DISCARDABLE_MEMORY_H_ 6 #define BASE_MEMORY_DISCARDABLE_MEMORY_H_ 7 8 #include "base/base_export.h" 9 #include "base/basictypes.h" 10 #include "base/compiler_specific.h" 11 #include "base/memory/scoped_ptr.h" 12 13 namespace base { 14 15 enum LockDiscardableMemoryStatus { 16 DISCARDABLE_MEMORY_FAILED = -1, 17 DISCARDABLE_MEMORY_PURGED = 0, 18 DISCARDABLE_MEMORY_SUCCESS = 1 19 }; 20 21 // Platform abstraction for discardable memory. DiscardableMemory is used to 22 // cache large objects without worrying about blowing out memory, both on mobile 23 // devices where there is no swap, and desktop devices where unused free memory 24 // should be used to help the user experience. This is preferable to releasing 25 // memory in response to an OOM signal because it is simpler, though it has less 26 // flexibility as to which objects get discarded. 27 // 28 // Discardable memory has two states: locked and unlocked. While the memory is 29 // locked, it will not be discarded. Unlocking the memory allows the OS to 30 // reclaim it if needed. Locks do not nest. 31 // 32 // Notes: 33 // - The paging behavior of memory while it is locked is not specified. While 34 // mobile platforms will not swap it out, it may qualify for swapping 35 // on desktop platforms. It is not expected that this will matter, as the 36 // preferred pattern of usage for DiscardableMemory is to lock down the 37 // memory, use it as quickly as possible, and then unlock it. 38 // - Because of memory alignment, the amount of memory allocated can be 39 // larger than the requested memory size. It is not very efficient for 40 // small allocations. 41 // - A discardable memory instance is not thread safe. It is the 42 // responsibility of users of discardable memory to ensure there are no 43 // races. 44 // 45 // References: 46 // - Linux: http://lwn.net/Articles/452035/ 47 // - Mac: http://trac.webkit.org/browser/trunk/Source/WebCore/platform/mac/PurgeableBufferMac.cpp 48 // the comment starting with "vm_object_purgable_control" at 49 // http://www.opensource.apple.com/source/xnu/xnu-792.13.8/osfmk/vm/vm_object.c 50 // 51 // Thread-safety: DiscardableMemory instances are not thread-safe. 52 class BASE_EXPORT DiscardableMemory { 53 public: 54 virtual ~DiscardableMemory() {} 55 56 // Check whether the system supports discardable memory natively. Returns 57 // false if the support is emulated. 58 static bool SupportedNatively(); 59 60 static scoped_ptr<DiscardableMemory> CreateLockedMemory(size_t size); 61 62 // Locks the memory so that it will not be purged by the system. Returns 63 // DISCARDABLE_MEMORY_SUCCESS on success. If the return value is 64 // DISCARDABLE_MEMORY_FAILED then this object should be discarded and 65 // a new one should be created. If the return value is 66 // DISCARDABLE_MEMORY_PURGED then the memory is present but any data that 67 // was in it is gone. 68 virtual LockDiscardableMemoryStatus Lock() WARN_UNUSED_RESULT = 0; 69 70 // Unlocks the memory so that it can be purged by the system. Must be called 71 // after every successful lock call. 72 virtual void Unlock() = 0; 73 74 // Returns the memory address held by this object. The object must be locked 75 // before calling this. Otherwise, this will cause a DCHECK error. 76 virtual void* Memory() const = 0; 77 78 // Testing utility calls. 79 80 // Check whether a purge of all discardable memory in the system is supported. 81 // Use only for testing! 82 static bool PurgeForTestingSupported(); 83 84 // Purge all discardable memory in the system. This call has global effects 85 // across all running processes, so it should only be used for testing! 86 static void PurgeForTesting(); 87 }; 88 89 } // namespace base 90 91 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_H_ 92