- Revision
- 177317
- Author
- [email protected]
- Date
- 2014-12-15 15:40:06 -0800 (Mon, 15 Dec 2014)
Log Message
Safari crashes when you set Malloc environment variables
https://bugs.webkit.org/show_bug.cgi?id=139656
Reviewed by Michael Saboff.
I forgot to cover the realloc() case. Whoops. (OoPS?)
This time around, I ran the full MallocBench test suite in Malloc=1
mode, and it passed.
* bmalloc/Allocator.cpp:
(bmalloc::Allocator::reallocate):
* bmalloc/Allocator.h: Pushed realloc() logic down into the allocator.
It needs to be down there so that we can do the short-circuiting check
for whether bmalloc is enabled first.
Also added the check.
* bmalloc/Cache.cpp:
(bmalloc::Cache::scavenge):
(bmalloc::Cache::Cache):
(bmalloc::Cache::reallocateSlowCaseNullCache):
* bmalloc/Cache.h:
(bmalloc::Cache::deallocator):
(bmalloc::Cache::reallocate): Ditto.
* bmalloc/bmalloc.h:
(bmalloc::api::free):
(bmalloc::api::realloc): Ditto.
(bmalloc::api::scavenge): Pushed this down into Cache to match the
surrounding functions.
Modified Paths
Diff
Modified: trunk/Source/bmalloc/ChangeLog (177316 => 177317)
--- trunk/Source/bmalloc/ChangeLog 2014-12-15 23:24:12 UTC (rev 177316)
+++ trunk/Source/bmalloc/ChangeLog 2014-12-15 23:40:06 UTC (rev 177317)
@@ -1,3 +1,38 @@
+2014-12-15 Geoffrey Garen <[email protected]>
+
+ Safari crashes when you set Malloc environment variables
+ https://bugs.webkit.org/show_bug.cgi?id=139656
+
+ Reviewed by Michael Saboff.
+
+ I forgot to cover the realloc() case. Whoops. (OoPS?)
+
+ This time around, I ran the full MallocBench test suite in Malloc=1
+ mode, and it passed.
+
+ * bmalloc/Allocator.cpp:
+ (bmalloc::Allocator::reallocate):
+ * bmalloc/Allocator.h: Pushed realloc() logic down into the allocator.
+ It needs to be down there so that we can do the short-circuiting check
+ for whether bmalloc is enabled first.
+
+ Also added the check.
+
+ * bmalloc/Cache.cpp:
+ (bmalloc::Cache::scavenge):
+ (bmalloc::Cache::Cache):
+ (bmalloc::Cache::reallocateSlowCaseNullCache):
+ * bmalloc/Cache.h:
+ (bmalloc::Cache::deallocator):
+ (bmalloc::Cache::reallocate): Ditto.
+
+ * bmalloc/bmalloc.h:
+ (bmalloc::api::free):
+ (bmalloc::api::realloc): Ditto.
+
+ (bmalloc::api::scavenge): Pushed this down into Cache to match the
+ surrounding functions.
+
2014-12-11 Geoffrey Garen <[email protected]>
bmalloc should support system memory analysis tools (part 2)
Modified: trunk/Source/bmalloc/bmalloc/Allocator.cpp (177316 => 177317)
--- trunk/Source/bmalloc/bmalloc/Allocator.cpp 2014-12-15 23:24:12 UTC (rev 177316)
+++ trunk/Source/bmalloc/bmalloc/Allocator.cpp 2014-12-15 23:40:06 UTC (rev 177317)
@@ -27,8 +27,10 @@
#include "BAssert.h"
#include "Deallocator.h"
#include "Heap.h"
+#include "LargeChunk.h"
#include "PerProcess.h"
#include "Sizes.h"
+#include "XLargeChunk.h"
#include <algorithm>
#include <cstdlib>
@@ -49,6 +51,45 @@
scavenge();
}
+void* Allocator::reallocate(void* object, size_t newSize)
+{
+ if (!m_isBmallocEnabled)
+ return realloc(object, newSize);
+
+ void* result = allocate(newSize);
+ if (!object)
+ return result;
+
+ size_t oldSize = 0;
+ switch (objectType(object)) {
+ case Small: {
+ SmallPage* page = SmallPage::get(SmallLine::get(object));
+ oldSize = objectSize(page->sizeClass());
+ break;
+ }
+ case Medium: {
+ MediumPage* page = MediumPage::get(MediumLine::get(object));
+ oldSize = objectSize(page->sizeClass());
+ break;
+ }
+ case Large: {
+ BeginTag* beginTag = LargeChunk::beginTag(object);
+ oldSize = beginTag->size();
+ break;
+ }
+ case XLarge: {
+ XLargeChunk* chunk = XLargeChunk::get(object);
+ oldSize = chunk->size();
+ break;
+ }
+ }
+
+ size_t copySize = std::min(oldSize, newSize);
+ memcpy(result, object, copySize);
+ m_deallocator.deallocate(object);
+ return result;
+}
+
void Allocator::scavenge()
{
for (unsigned short i = alignment; i <= mediumMax; i += alignment) {
Modified: trunk/Source/bmalloc/bmalloc/Allocator.h (177316 => 177317)
--- trunk/Source/bmalloc/bmalloc/Allocator.h 2014-12-15 23:24:12 UTC (rev 177316)
+++ trunk/Source/bmalloc/bmalloc/Allocator.h 2014-12-15 23:40:06 UTC (rev 177317)
@@ -42,6 +42,8 @@
~Allocator();
void* allocate(size_t);
+ void* reallocate(void*, size_t);
+
void scavenge();
private:
Modified: trunk/Source/bmalloc/bmalloc/Cache.cpp (177316 => 177317)
--- trunk/Source/bmalloc/bmalloc/Cache.cpp 2014-12-15 23:24:12 UTC (rev 177316)
+++ trunk/Source/bmalloc/bmalloc/Cache.cpp 2014-12-15 23:40:06 UTC (rev 177317)
@@ -40,17 +40,24 @@
vmDeallocate(p, vmSize(size));
}
+void Cache::scavenge()
+{
+ Cache* cache = PerThread<Cache>::getFastCase();
+ if (!cache)
+ return;
+
+ cache->allocator().scavenge();
+ cache->deallocator().scavenge();
+
+ std::unique_lock<StaticMutex> lock(PerProcess<Heap>::mutex());
+ PerProcess<Heap>::get()->scavenge(lock, std::chrono::milliseconds(0));
+}
+
Cache::Cache()
: m_deallocator(PerProcess<Heap>::get())
, m_allocator(PerProcess<Heap>::get(), m_deallocator)
{
}
-
-void Cache::scavenge()
-{
- m_allocator.scavenge();
- m_deallocator.scavenge();
-}
NO_INLINE void* Cache::allocateSlowCaseNullCache(size_t size)
{
@@ -62,4 +69,9 @@
PerThread<Cache>::getSlowCase()->deallocator().deallocate(object);
}
+NO_INLINE void* Cache::reallocateSlowCaseNullCache(void* object, size_t newSize)
+{
+ return PerThread<Cache>::getSlowCase()->allocator().reallocate(object, newSize);
+}
+
} // namespace bmalloc
Modified: trunk/Source/bmalloc/bmalloc/Cache.h (177316 => 177317)
--- trunk/Source/bmalloc/bmalloc/Cache.h 2014-12-15 23:24:12 UTC (rev 177316)
+++ trunk/Source/bmalloc/bmalloc/Cache.h 2014-12-15 23:40:06 UTC (rev 177317)
@@ -41,17 +41,18 @@
static void* allocate(size_t);
static void deallocate(void*);
+ static void* reallocate(void*, size_t);
+ static void scavenge();
Cache();
Allocator& allocator() { return m_allocator; }
Deallocator& deallocator() { return m_deallocator; }
-
- void scavenge();
private:
static void* allocateSlowCaseNullCache(size_t);
static void deallocateSlowCaseNullCache(void*);
+ static void* reallocateSlowCaseNullCache(void*, size_t);
Deallocator m_deallocator;
Allocator m_allocator;
@@ -73,6 +74,14 @@
return cache->deallocator().deallocate(object);
}
+inline void* Cache::reallocate(void* object, size_t newSize)
+{
+ Cache* cache = PerThread<Cache>::getFastCase();
+ if (!cache)
+ return reallocateSlowCaseNullCache(object, newSize);
+ return cache->allocator().reallocate(object, newSize);
+}
+
} // namespace bmalloc
#endif // Cache_h
Modified: trunk/Source/bmalloc/bmalloc/bmalloc.h (177316 => 177317)
--- trunk/Source/bmalloc/bmalloc/bmalloc.h 2014-12-15 23:24:12 UTC (rev 177316)
+++ trunk/Source/bmalloc/bmalloc/bmalloc.h 2014-12-15 23:40:06 UTC (rev 177317)
@@ -24,11 +24,6 @@
*/
#include "Cache.h"
-#include "Heap.h"
-#include "LargeChunk.h"
-#include "PerProcess.h"
-#include "XLargeChunk.h"
-#include "Sizes.h"
namespace bmalloc {
namespace api {
@@ -40,51 +35,17 @@
inline void free(void* object)
{
- return Cache::deallocate(object);
+ Cache::deallocate(object);
}
inline void* realloc(void* object, size_t newSize)
{
- void* result = Cache::allocate(newSize);
- if (!object)
- return result;
-
- size_t oldSize = 0;
- switch(objectType(object)) {
- case Small: {
- SmallPage* page = SmallPage::get(SmallLine::get(object));
- oldSize = objectSize(page->sizeClass());
- break;
- }
- case Medium: {
- MediumPage* page = MediumPage::get(MediumLine::get(object));
- oldSize = objectSize(page->sizeClass());
- break;
- }
- case Large: {
- BeginTag* beginTag = LargeChunk::beginTag(object);
- oldSize = beginTag->size();
- break;
- }
- case XLarge: {
- XLargeChunk* chunk = XLargeChunk::get(object);
- oldSize = chunk->size();
- break;
- }
- }
-
- size_t copySize = std::min(oldSize, newSize);
- memcpy(result, object, copySize);
- Cache::deallocate(object);
- return result;
+ return Cache::reallocate(object, newSize);
}
inline void scavenge()
{
- PerThread<Cache>::get()->scavenge();
-
- std::unique_lock<StaticMutex> lock(PerProcess<Heap>::mutex());
- PerProcess<Heap>::get()->scavenge(lock, std::chrono::milliseconds(0));
+ Cache::scavenge();
}
} // namespace api