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