Title: [220154] trunk/Source/bmalloc
Revision
220154
Author
[email protected]
Date
2017-08-02 14:50:33 -0700 (Wed, 02 Aug 2017)

Log Message

If Gigacage is disabled, bmalloc should service large aligned memory allocation requests through vmAllocate
https://bugs.webkit.org/show_bug.cgi?id=175085

Reviewed by Saam Barati.
        
This fixes a problem where if we used gmalloc, WebAssembly memory allocations would still use
bmalloc's large allocator.
        
We want to use the page allocator for those "large" allocations when the Gigacage is disabled.

* bmalloc/DebugHeap.cpp:
(bmalloc::DebugHeap::DebugHeap):
(bmalloc::DebugHeap::memalignLarge):
(bmalloc::DebugHeap::freeLarge):
* bmalloc/DebugHeap.h:
* bmalloc/Heap.cpp:
(bmalloc::Heap::tryAllocateLarge):
(bmalloc::Heap::deallocateLarge):

Modified Paths

Diff

Modified: trunk/Source/bmalloc/ChangeLog (220153 => 220154)


--- trunk/Source/bmalloc/ChangeLog	2017-08-02 21:50:13 UTC (rev 220153)
+++ trunk/Source/bmalloc/ChangeLog	2017-08-02 21:50:33 UTC (rev 220154)
@@ -1,5 +1,26 @@
 2017-08-02  Filip Pizlo  <[email protected]>
 
+        If Gigacage is disabled, bmalloc should service large aligned memory allocation requests through vmAllocate
+        https://bugs.webkit.org/show_bug.cgi?id=175085
+
+        Reviewed by Saam Barati.
+        
+        This fixes a problem where if we used gmalloc, WebAssembly memory allocations would still use
+        bmalloc's large allocator.
+        
+        We want to use the page allocator for those "large" allocations when the Gigacage is disabled.
+
+        * bmalloc/DebugHeap.cpp:
+        (bmalloc::DebugHeap::DebugHeap):
+        (bmalloc::DebugHeap::memalignLarge):
+        (bmalloc::DebugHeap::freeLarge):
+        * bmalloc/DebugHeap.h:
+        * bmalloc/Heap.cpp:
+        (bmalloc::Heap::tryAllocateLarge):
+        (bmalloc::Heap::deallocateLarge):
+
+2017-08-02  Filip Pizlo  <[email protected]>
+
         We should be OK with the gigacage being disabled on gmalloc
         https://bugs.webkit.org/show_bug.cgi?id=175082
 

Modified: trunk/Source/bmalloc/bmalloc/DebugHeap.cpp (220153 => 220154)


--- trunk/Source/bmalloc/bmalloc/DebugHeap.cpp	2017-08-02 21:50:13 UTC (rev 220153)
+++ trunk/Source/bmalloc/bmalloc/DebugHeap.cpp	2017-08-02 21:50:33 UTC (rev 220154)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016 Apple Inc. All rights reserved.
+ * Copyright (C) 2016-2017 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -24,8 +24,11 @@
  */
 
 #include "DebugHeap.h"
+
+#include "Algorithm.h"
 #include "BAssert.h"
 #include "BPlatform.h"
+#include "VMAllocate.h"
 #include <cstdlib>
 #include <thread>
 
@@ -35,6 +38,7 @@
 
 DebugHeap::DebugHeap(std::lock_guard<StaticMutex>&)
     : m_zone(malloc_create_zone(0, 0))
+    , m_pageSize(vmPageSize())
 {
     malloc_set_zone_name(m_zone, "WebKit Using System Malloc");
 }
@@ -108,4 +112,39 @@
     
 #endif
 
+// FIXME: This looks an awful lot like the code in wtf/Gigacage.cpp for large allocation.
+// https://bugs.webkit.org/show_bug.cgi?id=175086
+
+void* DebugHeap::memalignLarge(size_t alignment, size_t size, AllocationKind allocationKind)
+{
+    alignment = roundUpToMultipleOf(m_pageSize, alignment);
+    size = roundUpToMultipleOf(m_pageSize, size);
+    void* result = tryVMAllocate(alignment, size);
+    if (!result)
+        return nullptr;
+    if (allocationKind == AllocationKind::Virtual)
+        vmDeallocatePhysicalPages(result, size);
+    {
+        std::lock_guard<std::mutex> locker(m_lock);
+        m_sizeMap[result] = size;
+    }
+    return result;
+}
+
+void DebugHeap::freeLarge(void* base, AllocationKind)
+{
+    if (!base)
+        return;
+    
+    size_t size;
+    {
+        std::lock_guard<std::mutex> locker(m_lock);
+        size = m_sizeMap[base];
+        size_t numErased = m_sizeMap.erase(base);
+        RELEASE_BASSERT(numErased == 1);
+    }
+    
+    vmDeallocate(base, size);
+}
+
 } // namespace bmalloc

Modified: trunk/Source/bmalloc/bmalloc/DebugHeap.h (220153 => 220154)


--- trunk/Source/bmalloc/bmalloc/DebugHeap.h	2017-08-02 21:50:13 UTC (rev 220153)
+++ trunk/Source/bmalloc/bmalloc/DebugHeap.h	2017-08-02 21:50:33 UTC (rev 220154)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016 Apple Inc. All rights reserved.
+ * Copyright (C) 2016-2017 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -25,8 +25,11 @@
 
 #pragma once
 
+#include "AllocationKind.h"
 #include "StaticMutex.h"
 #include <mutex>
+#include <unordered_map>
+
 #if BOS(DARWIN)
 #include <malloc/malloc.h>
 #endif
@@ -41,11 +44,19 @@
     void* memalign(size_t alignment, size_t, bool crashOnFailure);
     void* realloc(void*, size_t);
     void free(void*);
+    
+    void* memalignLarge(size_t alignment, size_t, AllocationKind);
+    void freeLarge(void* base, AllocationKind);
 
 private:
 #if BOS(DARWIN)
     malloc_zone_t* m_zone;
 #endif
+    
+    // This is the debug heap. We can use whatever data structures we like. It doesn't matter.
+    size_t m_pageSize;
+    std::mutex m_lock;
+    std::unordered_map<void*, size_t> m_sizeMap;
 };
 
 } // namespace bmalloc

Modified: trunk/Source/bmalloc/bmalloc/Heap.cpp (220153 => 220154)


--- trunk/Source/bmalloc/bmalloc/Heap.cpp	2017-08-02 21:50:13 UTC (rev 220153)
+++ trunk/Source/bmalloc/bmalloc/Heap.cpp	2017-08-02 21:50:33 UTC (rev 220154)
@@ -499,7 +499,10 @@
 void* Heap::tryAllocateLarge(std::lock_guard<StaticMutex>&, size_t alignment, size_t size, AllocationKind allocationKind)
 {
     BASSERT(isPowerOfTwo(alignment));
-
+    
+    if (m_debugHeap)
+        return m_debugHeap->memalignLarge(alignment, size, allocationKind);
+    
     m_isGrowing = true;
     
     size_t roundedSize = size ? roundUpToMultipleOf(largeAlignment, size) : largeAlignment;
@@ -559,6 +562,9 @@
 
 void Heap::deallocateLarge(std::lock_guard<StaticMutex>&, void* object, AllocationKind allocationKind)
 {
+    if (m_debugHeap)
+        return m_debugHeap->freeLarge(object, allocationKind);
+
     size_t size = m_largeAllocated.remove(object);
     m_largeFree.add(LargeRange(object, size, allocationKind == AllocationKind::Physical ? size : 0));
     scheduleScavenger(size);
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to