Title: [178609] trunk/Source/bmalloc
Revision
178609
Author
[email protected]
Date
2015-01-16 16:01:22 -0800 (Fri, 16 Jan 2015)

Log Message

bmalloc: added some infrastructure for aligned allocation
https://bugs.webkit.org/show_bug.cgi?id=140572

Reviewed by Andreas Kling.

* bmalloc/Algorithm.h:
(bmalloc::isPowerOfTwo):
(bmalloc::roundUpToMultipleOf):
(bmalloc::roundDownToMultipleOf): Refactored some duplicate code to use our
isPowerOfTwo helper function.

* bmalloc/Allocator.cpp:
(bmalloc::Allocator::allocate):
* bmalloc/Allocator.h: Stubbed out an implementation of aligned allocation.
Doesn't do anything yet, but does correctly forward to system malloc
when bmalloc is disabled.

* bmalloc/Cache.cpp:
(bmalloc::Cache::allocateSlowCaseNullCache):
* bmalloc/Cache.h:
(bmalloc::Cache::allocate):
* bmalloc/bmalloc.h:
(bmalloc::api::memalign):
* bmalloc/mbmalloc.cpp: Stubbed out an API for aligned allocation.

Modified Paths

Diff

Modified: trunk/Source/bmalloc/ChangeLog (178608 => 178609)


--- trunk/Source/bmalloc/ChangeLog	2015-01-16 23:41:39 UTC (rev 178608)
+++ trunk/Source/bmalloc/ChangeLog	2015-01-17 00:01:22 UTC (rev 178609)
@@ -1,3 +1,30 @@
+2015-01-16  Geoffrey Garen  <[email protected]>
+
+        bmalloc: added some infrastructure for aligned allocation
+        https://bugs.webkit.org/show_bug.cgi?id=140572
+
+        Reviewed by Andreas Kling.
+
+        * bmalloc/Algorithm.h:
+        (bmalloc::isPowerOfTwo):
+        (bmalloc::roundUpToMultipleOf):
+        (bmalloc::roundDownToMultipleOf): Refactored some duplicate code to use our
+        isPowerOfTwo helper function.
+
+        * bmalloc/Allocator.cpp:
+        (bmalloc::Allocator::allocate):
+        * bmalloc/Allocator.h: Stubbed out an implementation of aligned allocation.
+        Doesn't do anything yet, but does correctly forward to system malloc
+        when bmalloc is disabled.
+
+        * bmalloc/Cache.cpp:
+        (bmalloc::Cache::allocateSlowCaseNullCache):
+        * bmalloc/Cache.h:
+        (bmalloc::Cache::allocate):
+        * bmalloc/bmalloc.h:
+        (bmalloc::api::memalign):
+        * bmalloc/mbmalloc.cpp: Stubbed out an API for aligned allocation.
+
 2015-01-13  Geoffrey Garen  <[email protected]>
 
         Consider alignment when allocating from a SegregatedFreeList

Modified: trunk/Source/bmalloc/bmalloc/Algorithm.h (178608 => 178609)


--- trunk/Source/bmalloc/bmalloc/Algorithm.h	2015-01-16 23:41:39 UTC (rev 178608)
+++ trunk/Source/bmalloc/bmalloc/Algorithm.h	2015-01-17 00:01:22 UTC (rev 178609)
@@ -62,21 +62,26 @@
     return !!(reinterpret_cast<uintptr_t>(value) & mask);
 }
 
+inline constexpr bool isPowerOfTwo(size_t size)
+{
+    return !(size & (size - 1));
+}
+
 template<typename T> inline T roundUpToMultipleOf(size_t divisor, T x)
 {
-    BASSERT(divisor && !(divisor & (divisor - 1)));
+    BASSERT(isPowerOfTwo(divisor));
     return reinterpret_cast<T>((reinterpret_cast<uintptr_t>(x) + (divisor - 1ul)) & ~(divisor - 1ul));
 }
 
 template<size_t divisor, typename T> inline constexpr T roundUpToMultipleOf(T x)
 {
-    static_assert(divisor && !(divisor & (divisor - 1)), "'divisor' must be a power of two.");
+    static_assert(isPowerOfTwo(divisor), "'divisor' must be a power of two.");
     return roundUpToMultipleOf(divisor, x);
 }
 
 template<size_t divisor, typename T> inline constexpr T roundDownToMultipleOf(T x)
 {
-    static_assert(divisor && !(divisor & (divisor - 1)), "'divisor' must be a power of two.");
+    static_assert(isPowerOfTwo(divisor), "'divisor' must be a power of two.");
     return reinterpret_cast<T>(mask(reinterpret_cast<uintptr_t>(x), ~(divisor - 1ul)));
 }
 
@@ -101,11 +106,6 @@
     return sizeof(T) * 8;
 }
 
-inline constexpr bool isPowerOfTwo(size_t size)
-{
-    return !(size & (size - 1));
-}
-
 } // namespace bmalloc
 
 #endif // Algorithm_h

Modified: trunk/Source/bmalloc/bmalloc/Allocator.cpp (178608 => 178609)


--- trunk/Source/bmalloc/bmalloc/Allocator.cpp	2015-01-16 23:41:39 UTC (rev 178608)
+++ trunk/Source/bmalloc/bmalloc/Allocator.cpp	2015-01-17 00:01:22 UTC (rev 178609)
@@ -51,6 +51,18 @@
     scavenge();
 }
 
+void* Allocator::allocate(size_t alignment, size_t size)
+{
+    if (!m_isBmallocEnabled) {
+        void* result = nullptr;
+        posix_memalign(&result, alignment, size);
+        return result;
+    }
+    
+    BASSERT(isPowerOfTwo(alignment));
+    return nullptr;
+}
+
 void* Allocator::reallocate(void* object, size_t newSize)
 {
     if (!m_isBmallocEnabled)

Modified: trunk/Source/bmalloc/bmalloc/Allocator.h (178608 => 178609)


--- trunk/Source/bmalloc/bmalloc/Allocator.h	2015-01-16 23:41:39 UTC (rev 178608)
+++ trunk/Source/bmalloc/bmalloc/Allocator.h	2015-01-17 00:01:22 UTC (rev 178609)
@@ -42,6 +42,7 @@
     ~Allocator();
 
     void* allocate(size_t);
+    void* allocate(size_t alignment, size_t);
     void* reallocate(void*, size_t);
 
     void scavenge();

Modified: trunk/Source/bmalloc/bmalloc/Cache.cpp (178608 => 178609)


--- trunk/Source/bmalloc/bmalloc/Cache.cpp	2015-01-16 23:41:39 UTC (rev 178608)
+++ trunk/Source/bmalloc/bmalloc/Cache.cpp	2015-01-17 00:01:22 UTC (rev 178609)
@@ -64,6 +64,11 @@
     return PerThread<Cache>::getSlowCase()->allocator().allocate(size);
 }
 
+NO_INLINE void* Cache::allocateSlowCaseNullCache(size_t alignment, size_t size)
+{
+    return PerThread<Cache>::getSlowCase()->allocator().allocate(alignment, size);
+}
+
 NO_INLINE void Cache::deallocateSlowCaseNullCache(void* object)
 {
     PerThread<Cache>::getSlowCase()->deallocator().deallocate(object);

Modified: trunk/Source/bmalloc/bmalloc/Cache.h (178608 => 178609)


--- trunk/Source/bmalloc/bmalloc/Cache.h	2015-01-16 23:41:39 UTC (rev 178608)
+++ trunk/Source/bmalloc/bmalloc/Cache.h	2015-01-17 00:01:22 UTC (rev 178609)
@@ -40,6 +40,7 @@
     void operator delete(void*, size_t);
 
     static void* allocate(size_t);
+    static void* allocate(size_t alignment, size_t);
     static void deallocate(void*);
     static void* reallocate(void*, size_t);
     static void scavenge();
@@ -51,6 +52,7 @@
 
 private:
     static void* allocateSlowCaseNullCache(size_t);
+    static void* allocateSlowCaseNullCache(size_t alignment, size_t);
     static void deallocateSlowCaseNullCache(void*);
     static void* reallocateSlowCaseNullCache(void*, size_t);
 
@@ -66,6 +68,14 @@
     return cache->allocator().allocate(size);
 }
 
+inline void* Cache::allocate(size_t alignment, size_t size)
+{
+    Cache* cache = PerThread<Cache>::getFastCase();
+    if (!cache)
+        return allocateSlowCaseNullCache(alignment, size);
+    return cache->allocator().allocate(alignment, size);
+}
+
 inline void Cache::deallocate(void* object)
 {
     Cache* cache = PerThread<Cache>::getFastCase();

Modified: trunk/Source/bmalloc/bmalloc/bmalloc.h (178608 => 178609)


--- trunk/Source/bmalloc/bmalloc/bmalloc.h	2015-01-16 23:41:39 UTC (rev 178608)
+++ trunk/Source/bmalloc/bmalloc/bmalloc.h	2015-01-17 00:01:22 UTC (rev 178609)
@@ -33,6 +33,11 @@
     return Cache::allocate(size);
 }
 
+inline void* memalign(size_t alignment, size_t size)
+{
+    return Cache::allocate(alignment, size);
+}
+
 inline void free(void* object)
 {
     Cache::deallocate(object);

Modified: trunk/Source/bmalloc/bmalloc/mbmalloc.cpp (178608 => 178609)


--- trunk/Source/bmalloc/bmalloc/mbmalloc.cpp	2015-01-16 23:41:39 UTC (rev 178608)
+++ trunk/Source/bmalloc/bmalloc/mbmalloc.cpp	2015-01-17 00:01:22 UTC (rev 178609)
@@ -30,6 +30,7 @@
 extern "C" {
 
 EXPORT void* mbmalloc(size_t);
+EXPORT void* mbmemalign(size_t, size_t);
 EXPORT void mbfree(void*, size_t);
 EXPORT void* mbrealloc(void*, size_t, size_t);
 EXPORT void mbscavenge();
@@ -39,6 +40,11 @@
     return bmalloc::api::malloc(size);
 }
 
+void* mbmemalign(size_t alignment, size_t size)
+{
+    return bmalloc::api::memalign(alignment, size);
+}
+
 void mbfree(void* p, size_t)
 {
     bmalloc::api::free(p);
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to