<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[210746] trunk/Source/bmalloc</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/210746">210746</a></dd>
<dt>Author</dt> <dd>ggaren@apple.com</dd>
<dt>Date</dt> <dd>2017-01-13 15:39:09 -0800 (Fri, 13 Jan 2017)</dd>
</dl>

<h3>Log Message</h3>
<pre>bmalloc: Use a separate zone when using system malloc
https://bugs.webkit.org/show_bug.cgi?id=167014

Reviewed by Filip Pizlo.

Harris asked for this so he could separate Safari and WebKit memory use
when doing memory analysis.

This patch adds an explicit DebugHeap class that contains all our
code for specialized allocation with debugging.

* bmalloc.xcodeproj/project.pbxproj:

* bmalloc/Allocator.cpp:
(bmalloc::Allocator::Allocator):
(bmalloc::Allocator::tryAllocate):
(bmalloc::Allocator::allocateImpl):
(bmalloc::Allocator::reallocate):
(bmalloc::Allocator::allocateSlowCase):
* bmalloc/Allocator.h: Forward to DebugHeap instead of inlining all the
code. This is required for our new interface, and it is also a nice
simplification that moves some not-very-important code out of the way.

* bmalloc/Deallocator.cpp:
(bmalloc::Deallocator::Deallocator):
(bmalloc::Deallocator::scavenge):
(bmalloc::Deallocator::deallocateSlowCase):
* bmalloc/Deallocator.h: Ditto.

* bmalloc/DebugHeap.cpp: Added.
(bmalloc::DebugHeap::DebugHeap):
(bmalloc::DebugHeap::malloc):
(bmalloc::DebugHeap::memalign):
(bmalloc::DebugHeap::realloc):
(bmalloc::DebugHeap::free):
* bmalloc/DebugHeap.h: Added. New class for overriding normal heap
behavior. Right now, it just adds a malloc zone and then forwards to
system malloc -- but we can add lots more kinds of debug heaps in the
future if we find them useful.

* bmalloc/Environment.cpp:
(bmalloc::Environment::Environment):
(bmalloc::Environment::computeIsDebugHeapEnabled):
(bmalloc::Environment::computeIsBmallocEnabled): Deleted.
* bmalloc/Environment.h:
(bmalloc::Environment::isDebugHeapEnabled):
(bmalloc::Environment::isBmallocEnabled): Deleted. Renamed everything to
reflect our new use of DebugHeap.

* bmalloc/Heap.cpp:
(bmalloc::Heap::Heap):
* bmalloc/Heap.h:
(bmalloc::Heap::debugHeap): Updated to use DebugHeap.
(bmalloc::Heap::environment): Deleted. 

* bmalloc/bmalloc.h:
(bmalloc::api::isEnabled): Updated to use DebugHeap.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourcebmallocCMakeListstxt">trunk/Source/bmalloc/CMakeLists.txt</a></li>
<li><a href="#trunkSourcebmallocChangeLog">trunk/Source/bmalloc/ChangeLog</a></li>
<li><a href="#trunkSourcebmallocbmallocAllocatorcpp">trunk/Source/bmalloc/bmalloc/Allocator.cpp</a></li>
<li><a href="#trunkSourcebmallocbmallocAllocatorh">trunk/Source/bmalloc/bmalloc/Allocator.h</a></li>
<li><a href="#trunkSourcebmallocbmallocDeallocatorcpp">trunk/Source/bmalloc/bmalloc/Deallocator.cpp</a></li>
<li><a href="#trunkSourcebmallocbmallocDeallocatorh">trunk/Source/bmalloc/bmalloc/Deallocator.h</a></li>
<li><a href="#trunkSourcebmallocbmallocEnvironmentcpp">trunk/Source/bmalloc/bmalloc/Environment.cpp</a></li>
<li><a href="#trunkSourcebmallocbmallocEnvironmenth">trunk/Source/bmalloc/bmalloc/Environment.h</a></li>
<li><a href="#trunkSourcebmallocbmallocHeapcpp">trunk/Source/bmalloc/bmalloc/Heap.cpp</a></li>
<li><a href="#trunkSourcebmallocbmallocHeaph">trunk/Source/bmalloc/bmalloc/Heap.h</a></li>
<li><a href="#trunkSourcebmallocbmallocbmalloch">trunk/Source/bmalloc/bmalloc/bmalloc.h</a></li>
<li><a href="#trunkSourcebmallocbmallocxcodeprojprojectpbxproj">trunk/Source/bmalloc/bmalloc.xcodeproj/project.pbxproj</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkSourcebmallocbmallocDebugHeapcpp">trunk/Source/bmalloc/bmalloc/DebugHeap.cpp</a></li>
<li><a href="#trunkSourcebmallocbmallocDebugHeaph">trunk/Source/bmalloc/bmalloc/DebugHeap.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourcebmallocCMakeListstxt"></a>
<div class="modfile"><h4>Modified: trunk/Source/bmalloc/CMakeLists.txt (210745 => 210746)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/bmalloc/CMakeLists.txt        2017-01-13 23:30:45 UTC (rev 210745)
+++ trunk/Source/bmalloc/CMakeLists.txt        2017-01-13 23:39:09 UTC (rev 210746)
</span><span class="lines">@@ -8,6 +8,7 @@
</span><span class="cx">     bmalloc/Allocator.cpp
</span><span class="cx">     bmalloc/Cache.cpp
</span><span class="cx">     bmalloc/Deallocator.cpp
</span><ins>+    bmalloc/DebugHeap.cpp
</ins><span class="cx">     bmalloc/Environment.cpp
</span><span class="cx">     bmalloc/Heap.cpp
</span><span class="cx">     bmalloc/LargeMap.cpp
</span></span></pre></div>
<a id="trunkSourcebmallocChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/bmalloc/ChangeLog (210745 => 210746)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/bmalloc/ChangeLog        2017-01-13 23:30:45 UTC (rev 210745)
+++ trunk/Source/bmalloc/ChangeLog        2017-01-13 23:39:09 UTC (rev 210746)
</span><span class="lines">@@ -1,3 +1,63 @@
</span><ins>+2017-01-13  Geoffrey Garen  &lt;ggaren@apple.com&gt;
+
+        bmalloc: Use a separate zone when using system malloc
+        https://bugs.webkit.org/show_bug.cgi?id=167014
+
+        Reviewed by Filip Pizlo.
+
+        Harris asked for this so he could separate Safari and WebKit memory use
+        when doing memory analysis.
+
+        This patch adds an explicit DebugHeap class that contains all our
+        code for specialized allocation with debugging.
+
+        * bmalloc.xcodeproj/project.pbxproj:
+
+        * bmalloc/Allocator.cpp:
+        (bmalloc::Allocator::Allocator):
+        (bmalloc::Allocator::tryAllocate):
+        (bmalloc::Allocator::allocateImpl):
+        (bmalloc::Allocator::reallocate):
+        (bmalloc::Allocator::allocateSlowCase):
+        * bmalloc/Allocator.h: Forward to DebugHeap instead of inlining all the
+        code. This is required for our new interface, and it is also a nice
+        simplification that moves some not-very-important code out of the way.
+
+        * bmalloc/Deallocator.cpp:
+        (bmalloc::Deallocator::Deallocator):
+        (bmalloc::Deallocator::scavenge):
+        (bmalloc::Deallocator::deallocateSlowCase):
+        * bmalloc/Deallocator.h: Ditto.
+
+        * bmalloc/DebugHeap.cpp: Added.
+        (bmalloc::DebugHeap::DebugHeap):
+        (bmalloc::DebugHeap::malloc):
+        (bmalloc::DebugHeap::memalign):
+        (bmalloc::DebugHeap::realloc):
+        (bmalloc::DebugHeap::free):
+        * bmalloc/DebugHeap.h: Added. New class for overriding normal heap
+        behavior. Right now, it just adds a malloc zone and then forwards to
+        system malloc -- but we can add lots more kinds of debug heaps in the
+        future if we find them useful.
+
+        * bmalloc/Environment.cpp:
+        (bmalloc::Environment::Environment):
+        (bmalloc::Environment::computeIsDebugHeapEnabled):
+        (bmalloc::Environment::computeIsBmallocEnabled): Deleted.
+        * bmalloc/Environment.h:
+        (bmalloc::Environment::isDebugHeapEnabled):
+        (bmalloc::Environment::isBmallocEnabled): Deleted. Renamed everything to
+        reflect our new use of DebugHeap.
+
+        * bmalloc/Heap.cpp:
+        (bmalloc::Heap::Heap):
+        * bmalloc/Heap.h:
+        (bmalloc::Heap::debugHeap): Updated to use DebugHeap.
+        (bmalloc::Heap::environment): Deleted. 
+
+        * bmalloc/bmalloc.h:
+        (bmalloc::api::isEnabled): Updated to use DebugHeap.
+
</ins><span class="cx"> 2016-12-15  Myles C. Maxfield  &lt;mmaxfield@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Sort Xcode project files
</span></span></pre></div>
<a id="trunkSourcebmallocbmallocAllocatorcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/bmalloc/bmalloc/Allocator.cpp (210745 => 210746)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/bmalloc/bmalloc/Allocator.cpp        2017-01-13 23:30:45 UTC (rev 210745)
+++ trunk/Source/bmalloc/bmalloc/Allocator.cpp        2017-01-13 23:39:09 UTC (rev 210746)
</span><span class="lines">@@ -27,6 +27,7 @@
</span><span class="cx"> #include &quot;BAssert.h&quot;
</span><span class="cx"> #include &quot;Chunk.h&quot;
</span><span class="cx"> #include &quot;Deallocator.h&quot;
</span><ins>+#include &quot;DebugHeap.h&quot;
</ins><span class="cx"> #include &quot;Heap.h&quot;
</span><span class="cx"> #include &quot;PerProcess.h&quot;
</span><span class="cx"> #include &quot;Sizes.h&quot;
</span><span class="lines">@@ -38,7 +39,7 @@
</span><span class="cx"> namespace bmalloc {
</span><span class="cx"> 
</span><span class="cx"> Allocator::Allocator(Heap* heap, Deallocator&amp; deallocator)
</span><del>-    : m_isBmallocEnabled(heap-&gt;environment().isBmallocEnabled())
</del><ins>+    : m_debugHeap(heap-&gt;debugHeap())
</ins><span class="cx">     , m_deallocator(deallocator)
</span><span class="cx"> {
</span><span class="cx">     for (size_t sizeClass = 0; sizeClass &lt; sizeClassCount; ++sizeClass)
</span><span class="lines">@@ -52,8 +53,8 @@
</span><span class="cx"> 
</span><span class="cx"> void* Allocator::tryAllocate(size_t size)
</span><span class="cx"> {
</span><del>-    if (!m_isBmallocEnabled)
-        return malloc(size);
</del><ins>+    if (m_debugHeap)
+        return m_debugHeap-&gt;malloc(size);
</ins><span class="cx"> 
</span><span class="cx">     if (size &lt;= smallMax)
</span><span class="cx">         return allocate(size);
</span><span class="lines">@@ -78,15 +79,8 @@
</span><span class="cx"> {
</span><span class="cx">     BASSERT(isPowerOfTwo(alignment));
</span><span class="cx"> 
</span><del>-    if (!m_isBmallocEnabled) {
-        void* result = nullptr;
-        if (posix_memalign(&amp;result, alignment, size)) {
-            if (crashOnFailure)
-                BCRASH();
-            return nullptr;
-        }
-        return result;
-    }
</del><ins>+    if (m_debugHeap)
+        return m_debugHeap-&gt;memalign(alignment, size, crashOnFailure);
</ins><span class="cx"> 
</span><span class="cx">     if (!size)
</span><span class="cx">         size = alignment;
</span><span class="lines">@@ -103,12 +97,8 @@
</span><span class="cx"> 
</span><span class="cx"> void* Allocator::reallocate(void* object, size_t newSize)
</span><span class="cx"> {
</span><del>-    if (!m_isBmallocEnabled) {
-        void* result = realloc(object, newSize);
-        if (!result)
-            BCRASH();
-        return result;
-    }
</del><ins>+    if (m_debugHeap)
+        return m_debugHeap-&gt;realloc(object, newSize);
</ins><span class="cx"> 
</span><span class="cx">     size_t oldSize = 0;
</span><span class="cx">     switch (objectType(object)) {
</span><span class="lines">@@ -193,12 +183,8 @@
</span><span class="cx"> 
</span><span class="cx"> void* Allocator::allocateSlowCase(size_t size)
</span><span class="cx"> {
</span><del>-    if (!m_isBmallocEnabled) {
-        void* result = malloc(size);
-        if (!result)
-            BCRASH();
-        return result;
-    }
</del><ins>+    if (m_debugHeap)
+        return m_debugHeap-&gt;malloc(size);
</ins><span class="cx"> 
</span><span class="cx">     if (size &lt;= maskSizeClassMax) {
</span><span class="cx">         size_t sizeClass = bmalloc::maskSizeClass(size);
</span></span></pre></div>
<a id="trunkSourcebmallocbmallocAllocatorh"></a>
<div class="modfile"><h4>Modified: trunk/Source/bmalloc/bmalloc/Allocator.h (210745 => 210746)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/bmalloc/bmalloc/Allocator.h        2017-01-13 23:30:45 UTC (rev 210745)
+++ trunk/Source/bmalloc/bmalloc/Allocator.h        2017-01-13 23:39:09 UTC (rev 210746)
</span><span class="lines">@@ -32,6 +32,7 @@
</span><span class="cx"> namespace bmalloc {
</span><span class="cx"> 
</span><span class="cx"> class Deallocator;
</span><ins>+class DebugHeap;
</ins><span class="cx"> class Heap;
</span><span class="cx"> 
</span><span class="cx"> // Per-cache object allocator.
</span><span class="lines">@@ -64,7 +65,7 @@
</span><span class="cx">     std::array&lt;BumpAllocator, sizeClassCount&gt; m_bumpAllocators;
</span><span class="cx">     std::array&lt;BumpRangeCache, sizeClassCount&gt; m_bumpRangeCaches;
</span><span class="cx"> 
</span><del>-    bool m_isBmallocEnabled;
</del><ins>+    DebugHeap* m_debugHeap;
</ins><span class="cx">     Deallocator&amp; m_deallocator;
</span><span class="cx"> };
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourcebmallocbmallocDeallocatorcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/bmalloc/bmalloc/Deallocator.cpp (210745 => 210746)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/bmalloc/bmalloc/Deallocator.cpp        2017-01-13 23:30:45 UTC (rev 210745)
+++ trunk/Source/bmalloc/bmalloc/Deallocator.cpp        2017-01-13 23:39:09 UTC (rev 210746)
</span><span class="lines">@@ -26,6 +26,7 @@
</span><span class="cx"> #include &quot;BAssert.h&quot;
</span><span class="cx"> #include &quot;Chunk.h&quot;
</span><span class="cx"> #include &quot;Deallocator.h&quot;
</span><ins>+#include &quot;DebugHeap.h&quot;
</ins><span class="cx"> #include &quot;Heap.h&quot;
</span><span class="cx"> #include &quot;Inline.h&quot;
</span><span class="cx"> #include &quot;Object.h&quot;
</span><span class="lines">@@ -39,9 +40,9 @@
</span><span class="cx"> namespace bmalloc {
</span><span class="cx"> 
</span><span class="cx"> Deallocator::Deallocator(Heap* heap)
</span><del>-    : m_isBmallocEnabled(heap-&gt;environment().isBmallocEnabled())
</del><ins>+    : m_debugHeap(heap-&gt;debugHeap())
</ins><span class="cx"> {
</span><del>-    if (!m_isBmallocEnabled) {
</del><ins>+    if (m_debugHeap) {
</ins><span class="cx">         // Fill the object log in order to disable the fast path.
</span><span class="cx">         while (m_objectLog.size() != m_objectLog.capacity())
</span><span class="cx">             m_objectLog.push(nullptr);
</span><span class="lines">@@ -55,8 +56,10 @@
</span><span class="cx">     
</span><span class="cx"> void Deallocator::scavenge()
</span><span class="cx"> {
</span><del>-    if (m_isBmallocEnabled)
-        processObjectLog();
</del><ins>+    if (m_debugHeap)
+        return;
+
+    processObjectLog();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void Deallocator::processObjectLog(std::lock_guard&lt;StaticMutex&gt;&amp; lock)
</span><span class="lines">@@ -77,10 +80,8 @@
</span><span class="cx"> 
</span><span class="cx"> void Deallocator::deallocateSlowCase(void* object)
</span><span class="cx"> {
</span><del>-    if (!m_isBmallocEnabled) {
-        free(object);
-        return;
-    }
</del><ins>+    if (m_debugHeap)
+        return m_debugHeap-&gt;free(object);
</ins><span class="cx"> 
</span><span class="cx">     if (!object)
</span><span class="cx">         return;
</span></span></pre></div>
<a id="trunkSourcebmallocbmallocDeallocatorh"></a>
<div class="modfile"><h4>Modified: trunk/Source/bmalloc/bmalloc/Deallocator.h (210745 => 210746)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/bmalloc/bmalloc/Deallocator.h        2017-01-13 23:30:45 UTC (rev 210745)
+++ trunk/Source/bmalloc/bmalloc/Deallocator.h        2017-01-13 23:39:09 UTC (rev 210746)
</span><span class="lines">@@ -31,6 +31,7 @@
</span><span class="cx"> 
</span><span class="cx"> namespace bmalloc {
</span><span class="cx"> 
</span><ins>+class DebugHeap;
</ins><span class="cx"> class Heap;
</span><span class="cx"> class StaticMutex;
</span><span class="cx"> 
</span><span class="lines">@@ -52,7 +53,7 @@
</span><span class="cx">     void deallocateSlowCase(void*);
</span><span class="cx"> 
</span><span class="cx">     FixedVector&lt;void*, deallocatorLogCapacity&gt; m_objectLog;
</span><del>-    bool m_isBmallocEnabled;
</del><ins>+    DebugHeap* m_debugHeap;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> inline bool Deallocator::deallocateFastCase(void* object)
</span></span></pre></div>
<a id="trunkSourcebmallocbmallocDebugHeapcpp"></a>
<div class="addfile"><h4>Added: trunk/Source/bmalloc/bmalloc/DebugHeap.cpp (0 => 210746)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/bmalloc/bmalloc/DebugHeap.cpp                                (rev 0)
+++ trunk/Source/bmalloc/bmalloc/DebugHeap.cpp        2017-01-13 23:39:09 UTC (rev 210746)
</span><span class="lines">@@ -0,0 +1,110 @@
</span><ins>+/*
+ * Copyright (C) 2016 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. 
+ */
+
+#include &quot;DebugHeap.h&quot;
+#include &quot;BAssert.h&quot;
+#include &quot;BPlatform.h&quot;
+#include &lt;thread&gt;
+
+namespace bmalloc {
+    
+#if BOS(DARWIN)
+
+DebugHeap::DebugHeap(std::lock_guard&lt;StaticMutex&gt;&amp;)
+    : m_zone(malloc_create_zone(0, 0))
+{
+    malloc_set_zone_name(m_zone, &quot;WebKit Using System Malloc&quot;);
+}
+
+void* DebugHeap::malloc(size_t size)
+{
+    void* result = malloc_zone_malloc(m_zone, size);
+    if (!result)
+        BCRASH();
+    return result;
+}
+
+void* DebugHeap::memalign(size_t alignment, size_t size, bool crashOnFailure)
+{
+    void* result = malloc_zone_memalign(m_zone, alignment, size);
+    if (!result &amp;&amp; crashOnFailure)
+        BCRASH();
+    return result;
+}
+
+void* DebugHeap::realloc(void* object, size_t size)
+{
+    void* result = malloc_zone_realloc(m_zone, object, size);
+    if (!result)
+        BCRASH();
+    return result;
+}
+
+void DebugHeap::free(void* object)
+{
+    malloc_zone_free(m_zone, object);
+}
+
+#else
+
+DebugHeap::DebugHeap(std::lock_guard&lt;StaticMutex&gt;&amp;)
+{
+}
+
+void* DebugHeap::malloc(size_t size)
+{
+    void* result = ::malloc(size);
+    if (!result)
+        BCRASH();
+    return result;
+}
+
+void* DebugHeap::memalign(size_t alignment, size_t size, bool crashOnFailure)
+{
+    void* result;
+    if (posix_memalign(&amp;result, alignment, size)) {
+        if (crashOnFailure)
+            BCRASH();
+        return nullptr;
+    }
+    return result;
+}
+
+void* DebugHeap::realloc(void* object, size_t size)
+{
+    void* result = ::realloc(object, size);
+    if (!result)
+        BCRASH();
+    return result;
+}
+
+void DebugHeap::free(void* object)
+{
+    ::free(object);
+}
+    
+#endif
+
+} // namespace bmalloc
</ins></span></pre></div>
<a id="trunkSourcebmallocbmallocDebugHeaph"></a>
<div class="addfile"><h4>Added: trunk/Source/bmalloc/bmalloc/DebugHeap.h (0 => 210746)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/bmalloc/bmalloc/DebugHeap.h                                (rev 0)
+++ trunk/Source/bmalloc/bmalloc/DebugHeap.h        2017-01-13 23:39:09 UTC (rev 210746)
</span><span class="lines">@@ -0,0 +1,51 @@
</span><ins>+/*
+ * Copyright (C) 2016 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. 
+ */
+
+#pragma once
+
+#include &quot;StaticMutex.h&quot;
+#include &lt;mutex&gt;
+#if BOS(DARWIN)
+#include &lt;malloc/malloc.h&gt;
+#endif
+
+namespace bmalloc {
+    
+class DebugHeap {
+public:
+    DebugHeap(std::lock_guard&lt;StaticMutex&gt;&amp;);
+    
+    void* malloc(size_t);
+    void* memalign(size_t alignment, size_t, bool crashOnFailure);
+    void* realloc(void*, size_t);
+    void free(void*);
+
+private:
+#if BOS(DARWIN)
+    malloc_zone_t* m_zone;
+#endif
+};
+
+} // namespace bmalloc
</ins></span></pre></div>
<a id="trunkSourcebmallocbmallocEnvironmentcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/bmalloc/bmalloc/Environment.cpp (210745 => 210746)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/bmalloc/bmalloc/Environment.cpp        2017-01-13 23:30:45 UTC (rev 210745)
+++ trunk/Source/bmalloc/bmalloc/Environment.cpp        2017-01-13 23:39:09 UTC (rev 210746)
</span><span class="lines">@@ -108,19 +108,19 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> Environment::Environment()
</span><del>-    : m_isBmallocEnabled(computeIsBmallocEnabled())
</del><ins>+    : m_isDebugHeapEnabled(computeIsDebugHeapEnabled())
</ins><span class="cx"> {
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-bool Environment::computeIsBmallocEnabled()
</del><ins>+bool Environment::computeIsDebugHeapEnabled()
</ins><span class="cx"> {
</span><span class="cx">     if (isMallocEnvironmentVariableSet())
</span><del>-        return false;
</del><ins>+        return true;
</ins><span class="cx">     if (isLibgmallocEnabled())
</span><del>-        return false;
</del><ins>+        return true;
</ins><span class="cx">     if (isSanitizerEnabled())
</span><del>-        return false;
-    return true;
</del><ins>+        return true;
+    return false;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> } // namespace bmalloc
</span></span></pre></div>
<a id="trunkSourcebmallocbmallocEnvironmenth"></a>
<div class="modfile"><h4>Modified: trunk/Source/bmalloc/bmalloc/Environment.h (210745 => 210746)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/bmalloc/bmalloc/Environment.h        2017-01-13 23:30:45 UTC (rev 210745)
+++ trunk/Source/bmalloc/bmalloc/Environment.h        2017-01-13 23:39:09 UTC (rev 210746)
</span><span class="lines">@@ -32,12 +32,12 @@
</span><span class="cx"> public:
</span><span class="cx">     Environment();
</span><span class="cx">     
</span><del>-    bool isBmallocEnabled() { return m_isBmallocEnabled; }
</del><ins>+    bool isDebugHeapEnabled() { return m_isDebugHeapEnabled; }
</ins><span class="cx"> 
</span><span class="cx"> private:
</span><del>-    bool computeIsBmallocEnabled();
</del><ins>+    bool computeIsDebugHeapEnabled();
</ins><span class="cx"> 
</span><del>-    bool m_isBmallocEnabled;
</del><ins>+    bool m_isDebugHeapEnabled;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> } // namespace bmalloc
</span></span></pre></div>
<a id="trunkSourcebmallocbmallocHeapcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/bmalloc/bmalloc/Heap.cpp (210745 => 210746)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/bmalloc/bmalloc/Heap.cpp        2017-01-13 23:30:45 UTC (rev 210745)
+++ trunk/Source/bmalloc/bmalloc/Heap.cpp        2017-01-13 23:39:09 UTC (rev 210746)
</span><span class="lines">@@ -26,6 +26,7 @@
</span><span class="cx"> #include &quot;Heap.h&quot;
</span><span class="cx"> #include &quot;BumpAllocator.h&quot;
</span><span class="cx"> #include &quot;Chunk.h&quot;
</span><ins>+#include &quot;DebugHeap.h&quot;
</ins><span class="cx"> #include &quot;PerProcess.h&quot;
</span><span class="cx"> #include &quot;SmallLine.h&quot;
</span><span class="cx"> #include &quot;SmallPage.h&quot;
</span><span class="lines">@@ -37,6 +38,7 @@
</span><span class="cx">     : m_vmPageSizePhysical(vmPageSizePhysical())
</span><span class="cx">     , m_isAllocatingPages(false)
</span><span class="cx">     , m_scavenger(*this, &amp;Heap::concurrentScavenge)
</span><ins>+    , m_debugHeap(nullptr)
</ins><span class="cx"> {
</span><span class="cx">     RELEASE_BASSERT(vmPageSizePhysical() &gt;= smallPageSize);
</span><span class="cx">     RELEASE_BASSERT(vmPageSize() &gt;= vmPageSizePhysical());
</span><span class="lines">@@ -43,6 +45,9 @@
</span><span class="cx"> 
</span><span class="cx">     initializeLineMetadata();
</span><span class="cx">     initializePageMetadata();
</span><ins>+    
+    if (m_environment.isDebugHeapEnabled())
+        m_debugHeap = PerProcess&lt;DebugHeap&gt;::get();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void Heap::initializeLineMetadata()
</span></span></pre></div>
<a id="trunkSourcebmallocbmallocHeaph"></a>
<div class="modfile"><h4>Modified: trunk/Source/bmalloc/bmalloc/Heap.h (210745 => 210746)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/bmalloc/bmalloc/Heap.h        2017-01-13 23:30:45 UTC (rev 210745)
+++ trunk/Source/bmalloc/bmalloc/Heap.h        2017-01-13 23:39:09 UTC (rev 210746)
</span><span class="lines">@@ -46,6 +46,7 @@
</span><span class="cx"> 
</span><span class="cx"> class BeginTag;
</span><span class="cx"> class BumpAllocator;
</span><ins>+class DebugHeap;
</ins><span class="cx"> class EndTag;
</span><span class="cx"> 
</span><span class="cx"> class Heap {
</span><span class="lines">@@ -52,7 +53,7 @@
</span><span class="cx"> public:
</span><span class="cx">     Heap(std::lock_guard&lt;StaticMutex&gt;&amp;);
</span><span class="cx">     
</span><del>-    Environment&amp; environment() { return m_environment; }
</del><ins>+    DebugHeap* debugHeap() { return m_debugHeap; }
</ins><span class="cx"> 
</span><span class="cx">     void allocateSmallBumpRanges(std::lock_guard&lt;StaticMutex&gt;&amp;, size_t sizeClass, BumpAllocator&amp;, BumpRangeCache&amp;);
</span><span class="cx">     void derefSmallLine(std::lock_guard&lt;StaticMutex&gt;&amp;, Object);
</span><span class="lines">@@ -116,6 +117,7 @@
</span><span class="cx">     AsyncTask&lt;Heap, decltype(&amp;Heap::concurrentScavenge)&gt; m_scavenger;
</span><span class="cx"> 
</span><span class="cx">     Environment m_environment;
</span><ins>+    DebugHeap* m_debugHeap;
</ins><span class="cx"> 
</span><span class="cx">     VMHeap m_vmHeap;
</span><span class="cx"> };
</span></span></pre></div>
<a id="trunkSourcebmallocbmallocbmalloch"></a>
<div class="modfile"><h4>Modified: trunk/Source/bmalloc/bmalloc/bmalloc.h (210745 => 210746)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/bmalloc/bmalloc/bmalloc.h        2017-01-13 23:30:45 UTC (rev 210745)
+++ trunk/Source/bmalloc/bmalloc/bmalloc.h        2017-01-13 23:39:09 UTC (rev 210746)
</span><span class="lines">@@ -82,7 +82,7 @@
</span><span class="cx"> inline bool isEnabled()
</span><span class="cx"> {
</span><span class="cx">     std::unique_lock&lt;StaticMutex&gt; lock(PerProcess&lt;Heap&gt;::mutex());
</span><del>-    return PerProcess&lt;Heap&gt;::getFastCase()-&gt;environment().isBmallocEnabled();
</del><ins>+    return !PerProcess&lt;Heap&gt;::getFastCase()-&gt;debugHeap();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> } // namespace api
</span></span></pre></div>
<a id="trunkSourcebmallocbmallocxcodeprojprojectpbxproj"></a>
<div class="modfile"><h4>Modified: trunk/Source/bmalloc/bmalloc.xcodeproj/project.pbxproj (210745 => 210746)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/bmalloc/bmalloc.xcodeproj/project.pbxproj        2017-01-13 23:30:45 UTC (rev 210745)
+++ trunk/Source/bmalloc/bmalloc.xcodeproj/project.pbxproj        2017-01-13 23:39:09 UTC (rev 210746)
</span><span class="lines">@@ -12,6 +12,8 @@
</span><span class="cx">                 140FA00319CE429C00FFD3C8 /* BumpRange.h in Headers */ = {isa = PBXBuildFile; fileRef = 140FA00219CE429C00FFD3C8 /* BumpRange.h */; settings = {ATTRIBUTES = (Private, ); }; };
</span><span class="cx">                 140FA00519CE4B6800FFD3C8 /* LineMetadata.h in Headers */ = {isa = PBXBuildFile; fileRef = 140FA00419CE4B6800FFD3C8 /* LineMetadata.h */; settings = {ATTRIBUTES = (Private, ); }; };
</span><span class="cx">                 141D9B001C8E51C0000ABBA0 /* List.h in Headers */ = {isa = PBXBuildFile; fileRef = 141D9AFF1C8E51C0000ABBA0 /* List.h */; settings = {ATTRIBUTES = (Private, ); }; };
</span><ins>+                142B44361E2839E7001DA6E9 /* DebugHeap.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 142B44341E2839E7001DA6E9 /* DebugHeap.cpp */; };
+                142B44371E2839E7001DA6E9 /* DebugHeap.h in Headers */ = {isa = PBXBuildFile; fileRef = 142B44351E2839E7001DA6E9 /* DebugHeap.h */; };
</ins><span class="cx">                 143CB81C19022BC900B16A45 /* StaticMutex.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 143CB81A19022BC900B16A45 /* StaticMutex.cpp */; };
</span><span class="cx">                 143CB81D19022BC900B16A45 /* StaticMutex.h in Headers */ = {isa = PBXBuildFile; fileRef = 143CB81B19022BC900B16A45 /* StaticMutex.h */; settings = {ATTRIBUTES = (Private, ); }; };
</span><span class="cx">                 1440AFCB1A95261100837FAA /* Zone.h in Headers */ = {isa = PBXBuildFile; fileRef = 1440AFCA1A95261100837FAA /* Zone.h */; settings = {ATTRIBUTES = (Private, ); }; };
</span><span class="lines">@@ -81,6 +83,8 @@
</span><span class="cx">                 1417F65218BA88A00076FA3F /* AsyncTask.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AsyncTask.h; path = bmalloc/AsyncTask.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 141D9AFF1C8E51C0000ABBA0 /* List.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = List.h; path = bmalloc/List.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 1421A87718EE462A00B4DD68 /* Algorithm.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = Algorithm.h; path = bmalloc/Algorithm.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><ins>+                142B44341E2839E7001DA6E9 /* DebugHeap.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = DebugHeap.cpp; path = bmalloc/DebugHeap.cpp; sourceTree = &quot;&lt;group&gt;&quot;; };
+                142B44351E2839E7001DA6E9 /* DebugHeap.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = DebugHeap.h; path = bmalloc/DebugHeap.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</ins><span class="cx">                 143CB81A19022BC900B16A45 /* StaticMutex.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = StaticMutex.cpp; path = bmalloc/StaticMutex.cpp; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 143CB81B19022BC900B16A45 /* StaticMutex.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = StaticMutex.h; path = bmalloc/StaticMutex.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 143E29ED18CAE90500FE8A0F /* SmallPage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SmallPage.h; path = bmalloc/SmallPage.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="lines">@@ -229,6 +233,8 @@
</span><span class="cx">                         children = (
</span><span class="cx">                                 140FA00219CE429C00FFD3C8 /* BumpRange.h */,
</span><span class="cx">                                 147DC6E21CA5B70B00724E8D /* Chunk.h */,
</span><ins>+                                142B44341E2839E7001DA6E9 /* DebugHeap.cpp */,
+                                142B44351E2839E7001DA6E9 /* DebugHeap.h */,
</ins><span class="cx">                                 14895D8F1A3A319C0006235D /* Environment.cpp */,
</span><span class="cx">                                 14895D901A3A319C0006235D /* Environment.h */,
</span><span class="cx">                                 14DA320E18875D9F007269E0 /* Heap.cpp */,
</span><span class="lines">@@ -321,6 +327,7 @@
</span><span class="cx">                                 14DD78CA18F48D7500950702 /* Mutex.h in Headers */,
</span><span class="cx">                                 144BE11F1CA346520099C8C0 /* Object.h in Headers */,
</span><span class="cx">                                 14DD789318F48D0F00950702 /* ObjectType.h in Headers */,
</span><ins>+                                142B44371E2839E7001DA6E9 /* DebugHeap.h in Headers */,
</ins><span class="cx">                                 14DD78CB18F48D7500950702 /* PerProcess.h in Headers */,
</span><span class="cx">                                 14DD78CC18F48D7500950702 /* PerThread.h in Headers */,
</span><span class="cx">                                 14DD78CD18F48D7500950702 /* Range.h in Headers */,
</span><span class="lines">@@ -422,6 +429,7 @@
</span><span class="cx">                                 14F271C718EA3990008C152F /* Heap.cpp in Sources */,
</span><span class="cx">                                 144C07F41C7B70260051BB6A /* LargeMap.cpp in Sources */,
</span><span class="cx">                                 4426E2801C838EE0008EB042 /* Logging.cpp in Sources */,
</span><ins>+                                142B44361E2839E7001DA6E9 /* DebugHeap.cpp in Sources */,
</ins><span class="cx">                                 14F271C818EA3990008C152F /* ObjectType.cpp in Sources */,
</span><span class="cx">                                 143CB81C19022BC900B16A45 /* StaticMutex.cpp in Sources */,
</span><span class="cx">                                 14F271C918EA3990008C152F /* VMHeap.cpp in Sources */,
</span></span></pre>
</div>
</div>

</body>
</html>