Merge MemoryPressureHandler{Mac,IOS}.mm
authorakling@apple.com <akling@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 14 Apr 2014 08:45:23 +0000 (08:45 +0000)
committerakling@apple.com <akling@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 14 Apr 2014 08:45:23 +0000 (08:45 +0000)
<https://webkit.org/b/131603>

Join the iOS and Mac platform implementations of MemoryPressureHandler
under the shared Cocoa banner. Each platform still has its own quirky
behavior, but this puts them in the same file so we can start sharing.

Reviewed by Darin Adler.

* WebCore.xcodeproj/project.pbxproj:
* platform/cocoa/MemoryPressureHandlerCocoa.mm: Renamed from Source/WebCore/platform/mac/MemoryPressureHandlerMac.mm.
(WebCore::MemoryPressureHandler::platformReleaseMemory):
(WebCore::MemoryPressureHandler::install):
(WebCore::MemoryPressureHandler::uninstall):
(WebCore::MemoryPressureHandler::holdOff):
(WebCore::MemoryPressureHandler::respondToMemoryPressure):
(WebCore::respondToMemoryPressureCallback):
(WebCore::MemoryPressureHandler::installMemoryReleaseBlock):
(WebCore::MemoryPressureHandler::setReceivedMemoryPressure):
(WebCore::MemoryPressureHandler::hasReceivedMemoryPressure):
(WebCore::MemoryPressureHandler::clearMemoryPressure):
(WebCore::MemoryPressureHandler::shouldWaitForMemoryClearMessage):
(WebCore::MemoryPressureHandler::respondToMemoryPressureIfNeeded):
* platform/ios/MemoryPressureHandlerIOS.mm: Removed.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@167219 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/WebCore.xcodeproj/project.pbxproj
Source/WebCore/platform/cocoa/MemoryPressureHandlerCocoa.mm [moved from Source/WebCore/platform/mac/MemoryPressureHandlerMac.mm with 68% similarity]
Source/WebCore/platform/ios/MemoryPressureHandlerIOS.mm [deleted file]

index 2e89f70..471394d 100644 (file)
@@ -1,3 +1,30 @@
+2014-04-14  Andreas Kling  <akling@apple.com>
+
+        Merge MemoryPressureHandler{Mac,IOS}.mm
+        <https://webkit.org/b/131603>
+
+        Join the iOS and Mac platform implementations of MemoryPressureHandler
+        under the shared Cocoa banner. Each platform still has its own quirky
+        behavior, but this puts them in the same file so we can start sharing.
+
+        Reviewed by Darin Adler.
+
+        * WebCore.xcodeproj/project.pbxproj:
+        * platform/cocoa/MemoryPressureHandlerCocoa.mm: Renamed from Source/WebCore/platform/mac/MemoryPressureHandlerMac.mm.
+        (WebCore::MemoryPressureHandler::platformReleaseMemory):
+        (WebCore::MemoryPressureHandler::install):
+        (WebCore::MemoryPressureHandler::uninstall):
+        (WebCore::MemoryPressureHandler::holdOff):
+        (WebCore::MemoryPressureHandler::respondToMemoryPressure):
+        (WebCore::respondToMemoryPressureCallback):
+        (WebCore::MemoryPressureHandler::installMemoryReleaseBlock):
+        (WebCore::MemoryPressureHandler::setReceivedMemoryPressure):
+        (WebCore::MemoryPressureHandler::hasReceivedMemoryPressure):
+        (WebCore::MemoryPressureHandler::clearMemoryPressure):
+        (WebCore::MemoryPressureHandler::shouldWaitForMemoryClearMessage):
+        (WebCore::MemoryPressureHandler::respondToMemoryPressureIfNeeded):
+        * platform/ios/MemoryPressureHandlerIOS.mm: Removed.
+
 2014-04-14  Benjamin Poulain  <benjamin@webkit.org>
 
         CSS JIT: compile the :nth-child() pseudo class
index a3fcdca..3fc6ac5 100644 (file)
                1F3C3BEA135CAF3C00B8C1AC /* MediaControls.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1F3C3BE8135CAF3C00B8C1AC /* MediaControls.cpp */; };
                1F3C3BEB135CAF3C00B8C1AC /* MediaControls.h in Headers */ = {isa = PBXBuildFile; fileRef = 1F3C3BE9135CAF3C00B8C1AC /* MediaControls.h */; };
                1F3F19531499CA7600A5AEA7 /* PODFreeListArena.h in Headers */ = {isa = PBXBuildFile; fileRef = 1F3F19521499CA7600A5AEA7 /* PODFreeListArena.h */; settings = {ATTRIBUTES = (Private, ); }; };
-               1F68D4AE14BB9DCA007B5C62 /* MemoryPressureHandlerIOS.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1F68D4AD14BB9DCA007B5C62 /* MemoryPressureHandlerIOS.mm */; };
                1F72BF0A187FD4490009BCB3 /* TileControllerMemoryHandlerIOS.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1F72BF08187FD4270009BCB3 /* TileControllerMemoryHandlerIOS.cpp */; };
                1F72BF0B187FD45C0009BCB3 /* TileControllerMemoryHandlerIOS.h in Headers */ = {isa = PBXBuildFile; fileRef = 1F72BF09187FD4270009BCB3 /* TileControllerMemoryHandlerIOS.h */; settings = {ATTRIBUTES = (Private, ); }; };
                1FAFBF1815A5FA6E00083A20 /* UTIUtilities.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1FAFBF1715A5FA5200083A20 /* UTIUtilities.mm */; };
                656D37480ADBA5DE00A4554D /* SubresourceLoader.h in Headers */ = {isa = PBXBuildFile; fileRef = 656D37300ADBA5DE00A4554D /* SubresourceLoader.h */; settings = {ATTRIBUTES = (Private, ); }; };
                6571DCC81385E6A400702DD0 /* MemoryPressureHandler.h in Headers */ = {isa = PBXBuildFile; fileRef = 657EDA071385CB97004E0645 /* MemoryPressureHandler.h */; settings = {ATTRIBUTES = (Private, ); }; };
                657EDA081385CB97004E0645 /* MemoryPressureHandler.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 657EDA061385CB97004E0645 /* MemoryPressureHandler.cpp */; };
-               657EDA0B1385CBD8004E0645 /* MemoryPressureHandlerMac.mm in Sources */ = {isa = PBXBuildFile; fileRef = 657EDA0A1385CBD8004E0645 /* MemoryPressureHandlerMac.mm */; };
                658436860AE01B7400E53753 /* FrameLoadRequest.h in Headers */ = {isa = PBXBuildFile; fileRef = 658436850AE01B7400E53753 /* FrameLoadRequest.h */; settings = {ATTRIBUTES = (Private, ); }; };
                6593923709AE4346002C531F /* URL.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6593923509AE4346002C531F /* URL.cpp */; };
                6593923809AE4346002C531F /* URL.h in Headers */ = {isa = PBXBuildFile; fileRef = 6593923609AE4346002C531F /* URL.h */; settings = {ATTRIBUTES = (Private, ); }; };
                AD726FED16DA1171003A4E6D /* JSCSSStyleDeclarationCustom.h in Headers */ = {isa = PBXBuildFile; fileRef = AD726FEA16D9F40B003A4E6D /* JSCSSStyleDeclarationCustom.h */; settings = {ATTRIBUTES = (Private, ); }; };
                AD726FEE16DA11BC003A4E6D /* JSStyleSheetCustom.h in Headers */ = {isa = PBXBuildFile; fileRef = AD726FEC16D9F4B9003A4E6D /* JSStyleSheetCustom.h */; settings = {ATTRIBUTES = (Private, ); }; };
                AD726FEF16DA11F5003A4E6D /* JSCSSRuleCustom.h in Headers */ = {isa = PBXBuildFile; fileRef = AD726FE916D9F40A003A4E6D /* JSCSSRuleCustom.h */; settings = {ATTRIBUTES = (Private, ); }; };
+               ADB6B29818FB90240081963E /* MemoryPressureHandlerCocoa.mm in Sources */ = {isa = PBXBuildFile; fileRef = ADB6B29718FB90240081963E /* MemoryPressureHandlerCocoa.mm */; };
                ADDF1AD71257CD9A0003A759 /* RenderSVGPath.h in Headers */ = {isa = PBXBuildFile; fileRef = ADDF1AD51257CD9A0003A759 /* RenderSVGPath.h */; };
                ADEC78F818EE5308001315C2 /* JSElementCustom.h in Headers */ = {isa = PBXBuildFile; fileRef = ADEC78F718EE5308001315C2 /* JSElementCustom.h */; settings = {ATTRIBUTES = (Private, ); }; };
                B10B6980140C174000BC1C26 /* WebVTTToken.h in Headers */ = {isa = PBXBuildFile; fileRef = B10B697D140C174000BC1C26 /* WebVTTToken.h */; };
                1F3C3BE8135CAF3C00B8C1AC /* MediaControls.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MediaControls.cpp; sourceTree = "<group>"; };
                1F3C3BE9135CAF3C00B8C1AC /* MediaControls.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MediaControls.h; sourceTree = "<group>"; };
                1F3F19521499CA7600A5AEA7 /* PODFreeListArena.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PODFreeListArena.h; sourceTree = "<group>"; };
-               1F68D4AD14BB9DCA007B5C62 /* MemoryPressureHandlerIOS.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = MemoryPressureHandlerIOS.mm; path = ios/MemoryPressureHandlerIOS.mm; sourceTree = "<group>"; };
                1F72BF08187FD4270009BCB3 /* TileControllerMemoryHandlerIOS.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = TileControllerMemoryHandlerIOS.cpp; path = ios/TileControllerMemoryHandlerIOS.cpp; sourceTree = "<group>"; };
                1F72BF09187FD4270009BCB3 /* TileControllerMemoryHandlerIOS.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TileControllerMemoryHandlerIOS.h; path = ios/TileControllerMemoryHandlerIOS.h; sourceTree = "<group>"; };
                1FAFBF1615A5FA5200083A20 /* UTIUtilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UTIUtilities.h; sourceTree = "<group>"; };
                656D37300ADBA5DE00A4554D /* SubresourceLoader.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = SubresourceLoader.h; sourceTree = "<group>"; };
                657EDA061385CB97004E0645 /* MemoryPressureHandler.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MemoryPressureHandler.cpp; sourceTree = "<group>"; };
                657EDA071385CB97004E0645 /* MemoryPressureHandler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MemoryPressureHandler.h; sourceTree = "<group>"; };
-               657EDA0A1385CBD8004E0645 /* MemoryPressureHandlerMac.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MemoryPressureHandlerMac.mm; sourceTree = "<group>"; };
                658436850AE01B7400E53753 /* FrameLoadRequest.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = FrameLoadRequest.h; sourceTree = "<group>"; };
                6593923509AE4346002C531F /* URL.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = URL.cpp; sourceTree = "<group>"; };
                6593923609AE4346002C531F /* URL.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = URL.h; sourceTree = "<group>"; };
                AD726FE916D9F40A003A4E6D /* JSCSSRuleCustom.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSCSSRuleCustom.h; sourceTree = "<group>"; };
                AD726FEA16D9F40B003A4E6D /* JSCSSStyleDeclarationCustom.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSCSSStyleDeclarationCustom.h; sourceTree = "<group>"; };
                AD726FEC16D9F4B9003A4E6D /* JSStyleSheetCustom.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSStyleSheetCustom.h; sourceTree = "<group>"; };
+               ADB6B29718FB90240081963E /* MemoryPressureHandlerCocoa.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MemoryPressureHandlerCocoa.mm; sourceTree = "<group>"; };
                ADDF1AD41257CD9A0003A759 /* RenderSVGPath.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RenderSVGPath.cpp; sourceTree = "<group>"; };
                ADDF1AD51257CD9A0003A759 /* RenderSVGPath.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RenderSVGPath.h; sourceTree = "<group>"; };
                ADE11F4A18D8311B0078983B /* ElementDescendantIterator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ElementDescendantIterator.h; sourceTree = "<group>"; };
                                1402645D0AFDC19B005919E2 /* LoggingMac.mm */,
                                CD641EBD1819B35900EE4C41 /* MediaTimeMac.cpp */,
                                CD641EBE1819B35900EE4C41 /* MediaTimeMac.h */,
-                               657EDA0A1385CBD8004E0645 /* MemoryPressureHandlerMac.mm */,
                                BC772C5D0C4EB3440083285F /* MIMETypeRegistryMac.mm */,
                                93500F3113FDE3BE0099EC24 /* NSScrollerImpDetails.h */,
                                52F52E1014A0134F00ACC397 /* NSScrollerImpDetails.mm */,
                                26C17A3D1491D2D400D12BA2 /* FileSystemIOS.mm */,
                                265541371489811C000DFC5D /* KeyEventCodesIOS.h */,
                                265541381489811C000DFC5D /* KeyEventIOS.mm */,
-                               1F68D4AD14BB9DCA007B5C62 /* MemoryPressureHandlerIOS.mm */,
                                E45390180EAFCACA003695C8 /* MIMETypeRegistryIOS.mm */,
                                E45390190EAFCACA003695C8 /* PasteboardIOS.mm */,
                                26601EBD14B3B9AD0012C0FE /* PlatformEventFactoryIOS.h */,
                                5D8C4DBE1428222C0026CE72 /* DisplaySleepDisablerCocoa.h */,
                                A5C974CF11485FF10066F2AB /* KeyEventCocoa.h */,
                                A5C974D011485FF10066F2AB /* KeyEventCocoa.mm */,
+                               ADB6B29718FB90240081963E /* MemoryPressureHandlerCocoa.mm */,
                                7CC564B918BAC720001B9652 /* TelephoneNumberDetectorCocoa.cpp */,
                        );
                        path = cocoa;
                                0705853817FE044F005F2BCB /* MediaTrackConstraintSet.cpp in Sources */,
                                BCB16C170979C3BD00467741 /* MemoryCache.cpp in Sources */,
                                657EDA081385CB97004E0645 /* MemoryPressureHandler.cpp in Sources */,
-                               1F68D4AE14BB9DCA007B5C62 /* MemoryPressureHandlerIOS.mm in Sources */,
-                               657EDA0B1385CBD8004E0645 /* MemoryPressureHandlerMac.mm in Sources */,
                                93309DF9099E64920056E581 /* MergeIdenticalElementsCommand.cpp in Sources */,
                                E1ADECCF0E76AD8B004A1A5E /* MessageChannel.cpp in Sources */,
                                75793E830D0CE0B3007FC0AC /* MessageEvent.cpp in Sources */,
                                E4C91A101802343900A17F6D /* TextPaintStyle.cpp in Sources */,
                                93F19A9D08245E59001E9ABC /* TextResourceDecoder.cpp in Sources */,
                                376DCCE113B4F966002EBEFC /* TextRun.cpp in Sources */,
+                               ADB6B29818FB90240081963E /* MemoryPressureHandlerCocoa.mm in Sources */,
                                B2C3DA4A0D006C1D00EF6F26 /* TextStream.cpp in Sources */,
                                9759E93F14EF1CF80026A2DD /* TextTrack.cpp in Sources */,
                                9759E94214EF1CF80026A2DD /* TextTrackCue.cpp in Sources */,
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2011, 2012 Apple Inc. All Rights Reserved.
+ * Copyright (C) 2011-2014 Apple Inc. All Rights Reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
 #import "config.h"
 #import "MemoryPressureHandler.h"
 
-#if !PLATFORM(IOS)
-
 #import "IOSurfacePool.h"
 #import "GCController.h"
 #import "LayerPool.h"
+#import "Logging.h"
 #import "WebCoreSystemInterface.h"
 #import <malloc/malloc.h>
 #import <notify.h>
 #import <wtf/CurrentTime.h>
 
+#if PLATFORM(IOS)
+#import "SystemMemory.h"
+#import "WebCoreThread.h"
+#import <libkern/OSAtomic.h>
+#endif
+
 namespace WebCore {
 
+void MemoryPressureHandler::platformReleaseMemory(bool)
+{
+#if PLATFORM(MAC)
+    LayerPool::sharedPool()->drain();
+#endif
+#if USE(IOSURFACE)
+    IOSurfacePool::sharedPool().discardAllSurfaces();
+#endif
+}
+
+#if PLATFORM(MAC)
+
 static dispatch_source_t _cache_event_source = 0;
 static dispatch_source_t _timer_event_source = 0;
 static int _notifyToken;
@@ -140,12 +157,79 @@ void MemoryPressureHandler::respondToMemoryPressure()
     holdOff(std::max(holdOffTime, s_minimumHoldOffTime));
 }
 
-void MemoryPressureHandler::platformReleaseMemory(bool)
+#else // !PLATFORM(MAC)
+
+static void respondToMemoryPressureCallback(CFRunLoopObserverRef observer, CFRunLoopActivity /*activity*/, void* /*info*/)
 {
-    LayerPool::sharedPool()->drain();
-    IOSurfacePool::sharedPool().discardAllSurfaces();
+    memoryPressureHandler().respondToMemoryPressureIfNeeded();
+    CFRunLoopObserverInvalidate(observer);
+    CFRelease(observer);
 }
 
-} // namespace WebCore
+void MemoryPressureHandler::installMemoryReleaseBlock(void (^releaseMemoryBlock)(), bool clearPressureOnMemoryRelease)
+{
+    if (m_installed)
+        return;
+    m_releaseMemoryBlock = Block_copy(releaseMemoryBlock);
+    m_clearPressureOnMemoryRelease = clearPressureOnMemoryRelease;
+    m_installed = true;
+}
 
-#endif // !PLATFORM(IOS)
+void MemoryPressureHandler::setReceivedMemoryPressure(MemoryPressureReason reason)
+{
+    OSAtomicTestAndSet(0, &m_receivedMemoryPressure);
+
+    {
+        MutexLocker locker(m_observerMutex);
+        if (!m_observer) {
+            m_observer = CFRunLoopObserverCreate(NULL, kCFRunLoopBeforeWaiting | kCFRunLoopExit, NO /* don't repeat */,
+                0, WebCore::respondToMemoryPressureCallback, NULL);
+            CFRunLoopAddObserver(WebThreadRunLoop(), m_observer, kCFRunLoopCommonModes);
+            CFRunLoopWakeUp(WebThreadRunLoop());
+        }
+        m_memoryPressureReason |= reason;
+    }
+}
+
+bool MemoryPressureHandler::hasReceivedMemoryPressure()
+{
+    return OSAtomicOr32(0, &m_receivedMemoryPressure);
+}
+
+void MemoryPressureHandler::clearMemoryPressure()
+{
+    OSAtomicTestAndClear(0, &m_receivedMemoryPressure);
+
+    {
+        MutexLocker locker(m_observerMutex);
+        m_memoryPressureReason = MemoryPressureReasonNone;
+    }
+}
+
+bool MemoryPressureHandler::shouldWaitForMemoryClearMessage()
+{
+    MutexLocker locker(m_observerMutex);
+    return m_memoryPressureReason & MemoryPressureReasonVMStatus;
+}
+
+void MemoryPressureHandler::respondToMemoryPressureIfNeeded()
+{
+    ASSERT(WebThreadIsLockedOrDisabled());
+
+    {
+        MutexLocker locker(m_observerMutex);
+        m_observer = 0;
+    }
+
+    if (hasReceivedMemoryPressure()) {
+        ASSERT(m_releaseMemoryBlock);
+        LOG(MemoryPressure, "Handle memory pressure at %s", __PRETTY_FUNCTION__);
+        m_releaseMemoryBlock();
+        if (m_clearPressureOnMemoryRelease)
+            clearMemoryPressure();
+    }
+}
+
+#endif
+
+} // namespace WebCore
diff --git a/Source/WebCore/platform/ios/MemoryPressureHandlerIOS.mm b/Source/WebCore/platform/ios/MemoryPressureHandlerIOS.mm
deleted file mode 100644 (file)
index 6a7d949..0000000
+++ /dev/null
@@ -1,115 +0,0 @@
-/*
- * Copyright (C) 2012 Apple Inc. All Rights Reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
- * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#import "config.h"
-#import "MemoryPressureHandler.h"
-
-#import "IOSurfacePool.h"
-#import "Logging.h"
-#import "SystemMemory.h"
-#import "WebCoreThread.h"
-#import <libkern/OSAtomic.h>
-
-namespace WebCore {
-
-static void respondToMemoryPressureCallback(CFRunLoopObserverRef observer, CFRunLoopActivity /*activity*/, void* /*info*/)
-{
-    memoryPressureHandler().respondToMemoryPressureIfNeeded();
-    CFRunLoopObserverInvalidate(observer);
-    CFRelease(observer);
-}
-
-void MemoryPressureHandler::installMemoryReleaseBlock(void (^releaseMemoryBlock)(), bool clearPressureOnMemoryRelease)
-{
-    if (m_installed)
-        return;
-    m_releaseMemoryBlock = Block_copy(releaseMemoryBlock);
-    m_clearPressureOnMemoryRelease = clearPressureOnMemoryRelease;
-    m_installed = true;
-}
-
-void MemoryPressureHandler::setReceivedMemoryPressure(MemoryPressureReason reason)
-{
-    OSAtomicTestAndSet(0, &m_receivedMemoryPressure);
-
-    {
-        MutexLocker locker(m_observerMutex);
-        if (!m_observer) {
-            m_observer = CFRunLoopObserverCreate(NULL, kCFRunLoopBeforeWaiting | kCFRunLoopExit, NO /* don't repeat */,
-                0, WebCore::respondToMemoryPressureCallback, NULL);
-            CFRunLoopAddObserver(WebThreadRunLoop(), m_observer, kCFRunLoopCommonModes);
-            CFRunLoopWakeUp(WebThreadRunLoop());
-        }
-        m_memoryPressureReason |= reason;
-    }
-}
-
-bool MemoryPressureHandler::hasReceivedMemoryPressure()
-{
-    return OSAtomicOr32(0, &m_receivedMemoryPressure);
-}
-
-void MemoryPressureHandler::clearMemoryPressure()
-{
-    OSAtomicTestAndClear(0, &m_receivedMemoryPressure);
-
-    {
-        MutexLocker locker(m_observerMutex);
-        m_memoryPressureReason = MemoryPressureReasonNone;
-    }
-}
-
-bool MemoryPressureHandler::shouldWaitForMemoryClearMessage()
-{
-    MutexLocker locker(m_observerMutex);
-    return m_memoryPressureReason & MemoryPressureReasonVMStatus;
-}
-
-void MemoryPressureHandler::respondToMemoryPressureIfNeeded()
-{
-    ASSERT(WebThreadIsLockedOrDisabled());
-
-    {
-        MutexLocker locker(m_observerMutex);
-        m_observer = 0;
-    }
-
-    if (hasReceivedMemoryPressure()) {
-        ASSERT(m_releaseMemoryBlock);
-        LOG(MemoryPressure, "Handle memory pressure at %s", __PRETTY_FUNCTION__);
-        m_releaseMemoryBlock();
-        if (m_clearPressureOnMemoryRelease)
-            clearMemoryPressure();
-    }
-}
-
-void MemoryPressureHandler::platformReleaseMemory(bool)
-{
-#if USE(IOSURFACE)
-    IOSurfacePool::sharedPool().discardAllSurfaces();
-#endif
-}
-
-} // namespace WebCore