1/*
2 * Copyright (C) 2016-2018 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "DebugHeap.h"
27
28#include "Algorithm.h"
29#include "BAssert.h"
30#include "BPlatform.h"
31#include "VMAllocate.h"
32#include <cstdlib>
33#include <thread>
34
35namespace bmalloc {
36
37DebugHeap* debugHeapCache { nullptr };
38
39DEFINE_STATIC_PER_PROCESS_STORAGE(DebugHeap);
40
41#if BOS(DARWIN)
42
43DebugHeap::DebugHeap(std::lock_guard<Mutex>&)
44 : m_zone(malloc_create_zone(0, 0))
45 , m_pageSize(vmPageSize())
46{
47 malloc_set_zone_name(m_zone, "WebKit Using System Malloc");
48}
49
50void* DebugHeap::malloc(size_t size, bool crashOnFailure)
51{
52 void* result = malloc_zone_malloc(m_zone, size);
53 if (!result && crashOnFailure)
54 BCRASH();
55 return result;
56}
57
58void* DebugHeap::memalign(size_t alignment, size_t size, bool crashOnFailure)
59{
60 void* result = malloc_zone_memalign(m_zone, alignment, size);
61 if (!result && crashOnFailure)
62 BCRASH();
63 return result;
64}
65
66void* DebugHeap::realloc(void* object, size_t size, bool crashOnFailure)
67{
68 void* result = malloc_zone_realloc(m_zone, object, size);
69 if (!result && crashOnFailure)
70 BCRASH();
71 return result;
72}
73
74void DebugHeap::free(void* object)
75{
76 malloc_zone_free(m_zone, object);
77}
78
79void DebugHeap::scavenge()
80{
81 // Currently |goal| does not affect on the behavior of malloc_zone_pressure_relief if (1) we only scavenge one zone and (2) it is not nanomalloc.
82 constexpr size_t goal = 0;
83 malloc_zone_pressure_relief(m_zone, goal);
84}
85
86void DebugHeap::dump()
87{
88 constexpr bool verbose = true;
89 malloc_zone_print(m_zone, verbose);
90}
91
92#else
93
94DebugHeap::DebugHeap(std::lock_guard<Mutex>&)
95 : m_pageSize(vmPageSize())
96{
97}
98
99void* DebugHeap::malloc(size_t size, bool crashOnFailure)
100{
101 void* result = ::malloc(size);
102 if (!result && crashOnFailure)
103 BCRASH();
104 return result;
105}
106
107void* DebugHeap::memalign(size_t alignment, size_t size, bool crashOnFailure)
108{
109 void* result;
110 if (posix_memalign(&result, alignment, size)) {
111 if (crashOnFailure)
112 BCRASH();
113 return nullptr;
114 }
115 return result;
116}
117
118void* DebugHeap::realloc(void* object, size_t size, bool crashOnFailure)
119{
120 void* result = ::realloc(object, size);
121 if (!result && crashOnFailure)
122 BCRASH();
123 return result;
124}
125
126void DebugHeap::free(void* object)
127{
128 ::free(object);
129}
130
131void DebugHeap::scavenge()
132{
133}
134
135void DebugHeap::dump()
136{
137}
138
139#endif
140
141// FIXME: This looks an awful lot like the code in wtf/Gigacage.cpp for large allocation.
142// https://bugs.webkit.org/show_bug.cgi?id=175086
143
144void* DebugHeap::memalignLarge(size_t alignment, size_t size)
145{
146 alignment = roundUpToMultipleOf(m_pageSize, alignment);
147 size = roundUpToMultipleOf(m_pageSize, size);
148 void* result = tryVMAllocate(alignment, size);
149 if (!result)
150 return nullptr;
151 {
152 std::lock_guard<Mutex> locker(mutex());
153 m_sizeMap[result] = size;
154 }
155 return result;
156}
157
158void DebugHeap::freeLarge(void* base)
159{
160 if (!base)
161 return;
162
163 size_t size;
164 {
165 std::lock_guard<Mutex> locker(mutex());
166 size = m_sizeMap[base];
167 size_t numErased = m_sizeMap.erase(base);
168 RELEASE_BASSERT(numErased == 1);
169 }
170
171 vmDeallocate(base, size);
172}
173
174} // namespace bmalloc
175