Merge "[MIPS] Ensure correct buffer alignment for doubles."
diff --git a/Source/WebCore/platform/graphics/android/utils/LinearAllocator.cpp b/Source/WebCore/platform/graphics/android/utils/LinearAllocator.cpp
index b945944..636c30c 100644
--- a/Source/WebCore/platform/graphics/android/utils/LinearAllocator.cpp
+++ b/Source/WebCore/platform/graphics/android/utils/LinearAllocator.cpp
@@ -44,7 +44,14 @@
 // Must be smaller than INITIAL_PAGE_SIZE
 #define MAX_WASTE_SIZE ((size_t)1024)
 
-#define ALIGN(x) (x + (x % sizeof(int)))
+#if CPU(MIPS)
+#define ALIGN_SZ (sizeof(double))
+#else
+#define ALIGN_SZ (sizeof(int))
+#endif
+
+#define ALIGN(x) ((x + ALIGN_SZ - 1 ) & ~(ALIGN_SZ - 1))
+#define ALIGN_PTR(p) ((void*)(ALIGN((unsigned int)p)))
 
 #if LOG_NDEBUG
 #define ADD_ALLOCATION(size)
@@ -123,7 +130,7 @@
 
 void* LinearAllocator::start(Page* p)
 {
-    return ((char*)p) + sizeof(Page);
+    return ALIGN_PTR(((char*)p) + sizeof(Page));
 }
 
 void* LinearAllocator::end(Page* p)
@@ -178,6 +185,7 @@
 void LinearAllocator::rewindIfLastAlloc(void* ptr, size_t allocSize)
 {
     // Don't bother rewinding across pages
+    allocSize = ALIGN(allocSize);
     if (ptr >= start(m_currentPage) && ptr < end(m_currentPage)
             && ptr == ((char*)m_next - allocSize)) {
         m_totalAllocated -= allocSize;
@@ -188,7 +196,7 @@
 
 LinearAllocator::Page* LinearAllocator::newPage(size_t pageSize)
 {
-    pageSize += sizeof(LinearAllocator::Page);
+    pageSize = ALIGN(pageSize + sizeof(LinearAllocator::Page));
     ADD_ALLOCATION(pageSize);
     m_totalAllocated += pageSize;
     m_pageCount++;