69d634408d60fb1101c18a7971af6490ad3dc2b7
[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     std::lock_guard<Mutex> lock(PerProcess<Heap>::mutex());
69     
70     for (auto object : m_objectLog) {
71         if (isSmall(object)) {
72             SmallLine* line = SmallLine::get(object);
73             if (!line->deref(lock))
74                 continue;
75             deallocateSmallLine(lock, line);
76         } else {
77             ASSERT(isSmallOrMedium(object));
78             MediumLine* line = MediumLine::get(object);
79             if (!line->deref(lock))
80                 continue;
81             deallocateMediumLine(lock, line);
82         }
83     }
84     
85     m_objectLog.clear();
86 }
87
88 void Deallocator::deallocateSlowCase(void* object)
89 {
90     ASSERT(!deallocateFastCase(object));
91
92     if (!object)
93         return;
94
95     if (isSmallOrMedium(object)) {
96         processObjectLog();
97         m_objectLog.push(object);
98         return;
99     }
100
101     BeginTag* beginTag = LargeChunk::beginTag(object);
102     if (!beginTag->isXLarge())
103         return deallocateLarge(object);
104     
105     return deallocateXLarge(object);
106 }
107
108 void Deallocator::deallocateSmallLine(std::lock_guard<Mutex>& lock, SmallLine* line)
109 {
110     if (m_smallLineCache.size() == m_smallLineCache.capacity())
111         return PerProcess<Heap>::getFastCase()->deallocateSmallLine(lock, line);
112
113     m_smallLineCache.push(line);
114 }
115
116 SmallLine* Deallocator::allocateSmallLine()
117 {
118     if (!m_smallLineCache.size()) {
119         std::lock_guard<Mutex> lock(PerProcess<Heap>::mutex());
120         Heap* heap = PerProcess<Heap>::getFastCase();
121
122         while (m_smallLineCache.size() != m_smallLineCache.capacity())
123             m_smallLineCache.push(heap->allocateSmallLine(lock));
124     }
125
126     return m_smallLineCache.pop();
127 }
128
129 void Deallocator::deallocateMediumLine(std::lock_guard<Mutex>& lock, MediumLine* line)
130 {
131     if (m_mediumLineCache.size() == m_mediumLineCache.capacity())
132         return PerProcess<Heap>::getFastCase()->deallocateMediumLine(lock, line);
133
134     m_mediumLineCache.push(line);
135 }
136
137 MediumLine* Deallocator::allocateMediumLine()
138 {
139     if (!m_mediumLineCache.size()) {
140         std::lock_guard<Mutex> lock(PerProcess<Heap>::mutex());
141         Heap* heap = PerProcess<Heap>::getFastCase();
142
143         while (m_mediumLineCache.size() != m_mediumLineCache.capacity())
144             m_mediumLineCache.push(heap->allocateMediumLine(lock));
145     }
146
147     return m_mediumLineCache.pop();
148 }
149
150 } // namespace bmalloc