Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / wtf / PartitionAlloc.h
index 70e03c4..b135477 100644 (file)
@@ -191,11 +191,8 @@ static const size_t kMaxFreeableSpans = 16;
 // These two byte values match tcmalloc.
 static const unsigned char kUninitializedByte = 0xAB;
 static const unsigned char kFreedByte = 0xCD;
-#if CPU(64BIT)
-static const uintptr_t kCookieValue = 0xDEADBEEFDEADBEEFul;
-#else
-static const uintptr_t kCookieValue = 0xDEADBEEFu;
-#endif
+static const uint32_t kCookieValue = 0xDEADBEEFu;
+static const size_t kCookieSize = 16; // Handles alignment up to XMM instructions on Intel.
 #endif
 
 struct PartitionBucket;
@@ -330,8 +327,8 @@ ALWAYS_INLINE size_t partitionCookieSizeAdjustAdd(size_t size)
 {
 #ifndef NDEBUG
     // Add space for cookies, checking for integer overflow.
-    ASSERT(size + (2 * sizeof(uintptr_t)) > size);
-    size += 2 * sizeof(uintptr_t);
+    ASSERT(size + (2 * kCookieSize) > size);
+    size += 2 * kCookieSize;
 #endif
     return size;
 }
@@ -340,8 +337,8 @@ ALWAYS_INLINE size_t partitionCookieSizeAdjustSubtract(size_t size)
 {
 #ifndef NDEBUG
     // Remove space for cookies.
-    ASSERT(size >= 2 * sizeof(uintptr_t));
-    size -= 2 * sizeof(uintptr_t);
+    ASSERT(size >= 2 * kCookieSize);
+    size -= 2 * kCookieSize;
 #endif
     return size;
 }
@@ -350,11 +347,29 @@ ALWAYS_INLINE void* partitionCookieFreePointerAdjust(void* ptr)
 {
 #ifndef NDEBUG
     // The value given to the application is actually just after the cookie.
-    ptr = static_cast<uintptr_t*>(ptr) - 1;
+    ptr = static_cast<char*>(ptr) - kCookieSize;
 #endif
     return ptr;
 }
 
+ALWAYS_INLINE void partitionCookieWriteValue(void* ptr)
+{
+#ifndef NDEBUG
+    uint32_t* cookiePtr = reinterpret_cast<uint32_t*>(ptr);
+    for (size_t i = 0; i < kCookieSize / sizeof(kCookieValue); ++i, ++cookiePtr)
+        *cookiePtr = kCookieValue;
+#endif
+}
+
+ALWAYS_INLINE void partitionCookieCheckValue(void* ptr)
+{
+#ifndef NDEBUG
+    uint32_t* cookiePtr = reinterpret_cast<uint32_t*>(ptr);
+    for (size_t i = 0; i < kCookieSize / sizeof(kCookieValue); ++i, ++cookiePtr)
+        ASSERT(*cookiePtr == kCookieValue);
+#endif
+}
+
 ALWAYS_INLINE char* partitionSuperPageToMetadataArea(char* ptr)
 {
     uintptr_t pointerAsUint = reinterpret_cast<uintptr_t>(ptr);
@@ -437,11 +452,10 @@ ALWAYS_INLINE void* partitionBucketAlloc(PartitionRootBase* root, int flags, siz
     page = partitionPointerToPage(ret);
     size_t bucketSize = page->bucket->slotSize;
     memset(ret, kUninitializedByte, bucketSize);
-    *(static_cast<uintptr_t*>(ret)) = kCookieValue;
-    void* retEnd = static_cast<char*>(ret) + bucketSize;
-    *(static_cast<uintptr_t*>(retEnd) - 1) = kCookieValue;
+    partitionCookieWriteValue(ret);
+    partitionCookieWriteValue(reinterpret_cast<char*>(ret) + bucketSize - kCookieSize);
     // The value given to the application is actually just after the cookie.
-    ret = static_cast<uintptr_t*>(ret) + 1;
+    ret = static_cast<char*>(ret) + kCookieSize;
 #endif
     return ret;
 }
@@ -468,9 +482,8 @@ ALWAYS_INLINE void partitionFreeWithPage(void* ptr, PartitionPage* page)
     // If these asserts fire, you probably corrupted memory.
 #ifndef NDEBUG
     size_t bucketSize = page->bucket->slotSize;
-    void* ptrEnd = static_cast<char*>(ptr) + bucketSize;
-    ASSERT(*(static_cast<uintptr_t*>(ptr)) == kCookieValue);
-    ASSERT(*(static_cast<uintptr_t*>(ptrEnd) - 1) == kCookieValue);
+    partitionCookieCheckValue(ptr);
+    partitionCookieCheckValue(reinterpret_cast<char*>(ptr) + bucketSize - kCookieSize);
     memset(ptr, kFreedByte, bucketSize);
 #endif
     ASSERT(page->numAllocatedSlots);