Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2585)

Unified Diff: base/memory/discardable_memory.h

Issue 17106004: Add discardable memory emulation for non-android/mac platforms (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/memory/discardable_memory.h
diff --git a/base/memory/discardable_memory.h b/base/memory/discardable_memory.h
index d6e91b979d5a46fdd50e6ef07ab4f52a904bc042..f50caf8947b5d8fffc7e18bec7c71bc235420616 100644
--- a/base/memory/discardable_memory.h
+++ b/base/memory/discardable_memory.h
@@ -8,6 +8,7 @@
#include "base/base_export.h"
#include "base/basictypes.h"
#include "base/compiler_specific.h"
+#include "base/threading/non_thread_safe.h"
namespace base {
@@ -17,6 +18,12 @@ enum LockDiscardableMemoryStatus {
DISCARDABLE_MEMORY_SUCCESS = 1
};
+#if defined(OS_MACOSX) || defined(OS_ANDROID)
+#define DISCARDABLE_MEMORY_ALWAYS_SUPPORTED_NATIVELY
+#else
+#define DISCARDABLE_MEMORY_NEVER_SUPPORTED_NATIVELY
+#endif
+
// Platform abstraction for discardable memory. DiscardableMemory is used to
// cache large objects without worrying about blowing out memory, both on mobile
// devices where there is no swap, and desktop devices where unused free memory
@@ -37,13 +44,17 @@ enum LockDiscardableMemoryStatus {
// - Because of memory alignment, the amount of memory allocated can be
// larger than the requested memory size. It is not very efficient for
// small allocations.
+// - A discardable memory instance is not thread safe. If the need to pass
+// ownership of a discardable memory instance from one thread to another,
+// NonThreadSafe::DetachFromThread could be used, but this is unnecessary
+// at the moment.
//
// References:
// - Linux: http://lwn.net/Articles/452035/
// - Mac: http://trac.webkit.org/browser/trunk/Source/WebCore/platform/mac/PurgeableBufferMac.cpp
// the comment starting with "vm_object_purgable_control" at
// http://www.opensource.apple.com/source/xnu/xnu-792.13.8/osfmk/vm/vm_object.c
-class BASE_EXPORT DiscardableMemory {
+class BASE_EXPORT DiscardableMemory : public NonThreadSafe {
public:
DiscardableMemory();
@@ -51,8 +62,9 @@ class BASE_EXPORT DiscardableMemory {
// The opened file will also be closed after this.
~DiscardableMemory();
- // Check whether the system supports discardable memory.
- static bool Supported();
+ // Check whether the system supports discardable memory natively. Returns
+ // false if the support is emulated.
+ static bool SupportedNatively();
// Initialize the DiscardableMemory object. On success, this function returns
// true and the memory is locked. This should only be called once.
@@ -72,6 +84,8 @@ class BASE_EXPORT DiscardableMemory {
// after every successful lock call.
void Unlock();
+ bool is_locked() const { return is_locked_; }
+
// Return the memory address held by this object. The object must be locked
// before calling this. Otherwise, this will cause a DCHECK error.
void* Memory() const;
@@ -87,6 +101,15 @@ class BASE_EXPORT DiscardableMemory {
static void PurgeForTesting();
private:
+#if !defined(DISCARDABLE_MEMORY_ALWAYS_SUPPORTED_NATIVELY)
+ friend class DiscardableMemoryProvider;
+ friend class DiscardableMemoryProviderTest;
+
+ // Used by the DiscardableMemoryProvider to allocate and free memory.
+ bool Allocate();
+ void Deallocate();
+#endif
+
#if defined(OS_ANDROID)
// Maps the discardable memory into the caller's address space.
// Returns true on success, false otherwise.

Powered by Google App Engine
This is Rietveld 408576698