bmalloc
[WebKit-https.git] / Source / bmalloc / bmalloc / Deallocator.cpp
1 /*
2  * Copyright (C) 2014 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 "BAssert.h"
27 #include "BeginTag.h"
28 #include "LargeChunk.h"
29 #include "Deallocator.h"
30 #include "Heap.h"
31 #include "Inline.h"
32 #include "PerProcess.h"
33 #include "SmallChunk.h"
34 #include <algorithm>
35 #include <sys/mman.h>
36
37 using namespace std;
38
39 namespace bmalloc {
40
41 Deallocator::Deallocator()
42     : m_objectLog()
43     , m_smallLineCache()
44     , m_mediumLineCache()
45 {
46 }
47
48 Deallocator::~Deallocator()
49 {
50     std::lock_guard<Mutex> lock(PerProcess<Heap>::mutex());
51     processObjectLog();
52 }
53
54 void Deallocator::deallocateLarge(void* object)
55 {
56     std::lock_guard<Mutex> lock(PerProcess<Heap>::mutex());
57     PerProcess<Heap>::getFastCase()->deallocateLarge(lock, object);
58 }
59
60 void Deallocator::deallocateXLarge(void* object)
61 {
62     std::lock_guard<Mutex> lock(PerProcess<Heap>::mutex());
63     PerProcess<Heap>::getFastCase()->deallocateXLarge(lock, object);
64 }
65
66 void Deallocator::processObjectLog()
67 {
68     for (auto object : m_objectLog) {
69         if (isSmall(object)) {
70             SmallLine* line = SmallLine::get(object);
71             if (!line->deref())
72                 continue;
73             deallocateSmallLine(line);
74         } else {
75             ASSERT(isSmallOrMedium(object));
76             MediumLine* line = MediumLine::get(object);
77             if (!line->deref())
78                 continue;
79             deallocateMediumLine(line);
80         }
81     }
82     
83     m_objectLog.clear();
84 }
85
86 void Deallocator::deallocateSlowCase(void* object)
87 {
88     ASSERT(!deallocateFastCase(object));
89
90     if (!object)
91         return;
92
93     if (isSmallOrMedium(object)) {
94         std::unique_lock<Mutex> lock(PerProcess<Heap>::mutex(), std::defer_lock);
95         bool didLock;
96         if (m_objectLog.size() == m_objectLog.capacity()) {
97             didLock = true;
98             lock.lock();
99         } else
100             didLock = lock.try_lock();
101         
102         if (didLock)
103             processObjectLog();
104         
105         m_objectLog.push(object);
106         return;
107     }
108
109     BeginTag* beginTag = LargeChunk::beginTag(object);
110     if (!beginTag->isXLarge())
111         return deallocateLarge(object);
112     
113     return deallocateXLarge(object);
114 }
115
116 void Deallocator::deallocateSmallLine(SmallLine* line)
117 {
118     if (m_smallLineCache.size() == m_smallLineCache.capacity())
119         return PerProcess<Heap>::getFastCase()->deallocateSmallLine(line);
120
121     m_smallLineCache.push(line);
122 }
123
124 SmallLine* Deallocator::allocateSmallLine()
125 {
126     if (!m_smallLineCache.size()) {
127         std::lock_guard<Mutex> lock(PerProcess<Heap>::mutex());
128         Heap* heap = PerProcess<Heap>::getFastCase();
129
130         while (m_smallLineCache.size() != m_smallLineCache.capacity())
131             m_smallLineCache.push(heap->allocateSmallLine(lock));
132     }
133
134     return m_smallLineCache.pop();
135 }
136
137 void Deallocator::deallocateMediumLine(MediumLine* line)
138 {
139     if (m_mediumLineCache.size() == m_mediumLineCache.capacity())
140         return PerProcess<Heap>::getFastCase()->deallocateMediumLine(line);
141
142     m_mediumLineCache.push(line);
143 }
144
145 MediumLine* Deallocator::allocateMediumLine()
146 {
147     if (!m_mediumLineCache.size()) {
148         std::lock_guard<Mutex> lock(PerProcess<Heap>::mutex());
149         Heap* heap = PerProcess<Heap>::getFastCase();
150
151         while (m_mediumLineCache.size() != m_mediumLineCache.capacity())
152             m_mediumLineCache.push(heap->allocateMediumLine(lock));
153     }
154
155     return m_mediumLineCache.pop();
156 }
157
158 } // namespace bmalloc