[SCM] WebKit Debian packaging branch, webkit-1.2, updated. upstream/1.1.90-6072-g9a69373

yong.li at torchmobile.com yong.li at torchmobile.com
Thu Apr 8 00:54:28 UTC 2010


The following commit has been merged in the webkit-1.2 branch:
commit 0c527c95872c403d28ffa356590bc6b0a14936cd
Author: yong.li at torchmobile.com <yong.li at torchmobile.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Date:   Tue Jan 5 15:14:00 2010 +0000

    2010-01-04  Yong Li  <yoli at rim.com>
    
            Reviewed by Darin Adler.
    
            Let SharedBuffer use a group of memory segments internally.
            It will merge the segments into a flat consecutive buffer only when
            necessary.
            https://bugs.webkit.org/show_bug.cgi?id=33178
    
            * platform/SharedBuffer.cpp:
            (WebCore::allocateSegment):
            (WebCore::freeSegment):
            (WebCore::SharedBuffer::SharedBuffer):
            (WebCore::SharedBuffer::~SharedBuffer):
            (WebCore::SharedBuffer::adoptVector):
            (WebCore::SharedBuffer::size):
            (WebCore::SharedBuffer::data):
            (WebCore::SharedBuffer::append):
            (WebCore::SharedBuffer::clear):
            (WebCore::SharedBuffer::copy):
            (WebCore::SharedBuffer::buffer):
            (WebCore::SharedBuffer::getSomeData):
            * platform/SharedBuffer.h:
            * platform/cf/SharedBufferCF.cpp:
            (WebCore::SharedBuffer::maybeTransferPlatformData):
            * platform/haiku/SharedBufferHaiku.cpp:
            (WebCore::SharedBuffer::createWithContentsOfFile):
            * platform/qt/SharedBufferQt.cpp:
            (WebCore::SharedBuffer::createWithContentsOfFile):
            * platform/win/SharedBufferWin.cpp:
            (WebCore::SharedBuffer::createWithContentsOfFile):
    
    git-svn-id: http://svn.webkit.org/repository/webkit/trunk@52795 268f45cc-cd09-0410-ab3c-d52691b4dbfc

diff --git a/WebCore/ChangeLog b/WebCore/ChangeLog
index 4e2119a..fe3fe19 100644
--- a/WebCore/ChangeLog
+++ b/WebCore/ChangeLog
@@ -1,3 +1,35 @@
+2010-01-04  Yong Li  <yoli at rim.com>
+
+        Reviewed by Darin Adler.
+
+        Let SharedBuffer use a group of memory segments internally.
+        It will merge the segments into a flat consecutive buffer only when
+        necessary.
+        https://bugs.webkit.org/show_bug.cgi?id=33178
+
+        * platform/SharedBuffer.cpp:
+        (WebCore::allocateSegment):
+        (WebCore::freeSegment):
+        (WebCore::SharedBuffer::SharedBuffer):
+        (WebCore::SharedBuffer::~SharedBuffer):
+        (WebCore::SharedBuffer::adoptVector):
+        (WebCore::SharedBuffer::size):
+        (WebCore::SharedBuffer::data):
+        (WebCore::SharedBuffer::append):
+        (WebCore::SharedBuffer::clear):
+        (WebCore::SharedBuffer::copy):
+        (WebCore::SharedBuffer::buffer):
+        (WebCore::SharedBuffer::getSomeData):
+        * platform/SharedBuffer.h:
+        * platform/cf/SharedBufferCF.cpp:
+        (WebCore::SharedBuffer::maybeTransferPlatformData):
+        * platform/haiku/SharedBufferHaiku.cpp:
+        (WebCore::SharedBuffer::createWithContentsOfFile):
+        * platform/qt/SharedBufferQt.cpp:
+        (WebCore::SharedBuffer::createWithContentsOfFile):
+        * platform/win/SharedBufferWin.cpp:
+        (WebCore::SharedBuffer::createWithContentsOfFile):
+
 2010-01-05  Simon Hausmann  <simon.hausmann at nokia.com>
 
         Unreviewed trivial Symbian build fix
diff --git a/WebCore/platform/SharedBuffer.cpp b/WebCore/platform/SharedBuffer.cpp
index 4a0d0f3..3179627 100644
--- a/WebCore/platform/SharedBuffer.cpp
+++ b/WebCore/platform/SharedBuffer.cpp
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
+ * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -30,28 +31,56 @@
 
 namespace WebCore {
 
+static const unsigned segmentSize = 0x1000;
+static const unsigned segmentPositionMask = 0x0FFF;
+
+static inline unsigned segmentIndex(unsigned position)
+{
+    return position / segmentSize;
+}
+
+static inline unsigned offsetInSegment(unsigned position)
+{
+    return position & segmentPositionMask;
+}
+
+static inline char* allocateSegment()
+{
+    return static_cast<char*>(fastMalloc(segmentSize));
+}
+
+static inline void freeSegment(char* p)
+{
+    fastFree(p);
+}
+
 SharedBuffer::SharedBuffer()
+    : m_size(0)
 {
 }
 
 SharedBuffer::SharedBuffer(const char* data, int size)
+    : m_size(0)
 {
-    m_buffer.append(data, size);
+    append(data, size);
 }
 
 SharedBuffer::SharedBuffer(const unsigned char* data, int size)
+    : m_size(0)
 {
-    m_buffer.append(data, size);
+    append(reinterpret_cast<const char*>(data), size);
 }
     
 SharedBuffer::~SharedBuffer()
 {
+    clear();
 }
 
 PassRefPtr<SharedBuffer> SharedBuffer::adoptVector(Vector<char>& vector)
 {
     RefPtr<SharedBuffer> buffer = create();
     buffer->m_buffer.swap(vector);
+    buffer->m_size = buffer->m_buffer.size();
     return buffer.release();
 }
 
@@ -71,7 +100,7 @@ unsigned SharedBuffer::size() const
     if (m_purgeableBuffer)
         return m_purgeableBuffer->size();
     
-    return m_buffer.size();
+    return m_size;
 }
 
 const char* SharedBuffer::data() const
@@ -82,29 +111,75 @@ const char* SharedBuffer::data() const
     if (m_purgeableBuffer)
         return m_purgeableBuffer->data();
     
-    return m_buffer.data();
+    return buffer().data();
 }
 
-void SharedBuffer::append(const char* data, int len)
+void SharedBuffer::append(const char* data, int length)
 {
     ASSERT(!m_purgeableBuffer);
 
     maybeTransferPlatformData();
     
-    m_buffer.append(data, len);
+    unsigned positionInSegment = offsetInSegment(m_size - m_buffer.size());
+    m_size += length;
+
+    if (m_size <= segmentSize) {
+        // No need to use segments for small resource data
+        m_buffer.append(data, length);
+        return;
+    }
+
+    char* segment;
+    if (!positionInSegment) {
+        segment = allocateSegment();
+        m_segments.append(segment);
+    } else
+        segment = m_segments.last() + positionInSegment;
+
+    unsigned segmentFreeSpace = segmentSize - positionInSegment;
+    unsigned bytesToCopy = length < segmentFreeSpace ? length : segmentFreeSpace;
+
+    for (;;) {
+        memcpy(segment, data, bytesToCopy);
+        if (length == bytesToCopy)
+            break;
+
+        length -= bytesToCopy;
+        data += bytesToCopy;
+        segment = allocateSegment();
+        m_segments.append(segment);
+        bytesToCopy = length < segmentSize ? length : segmentSize;
+    }
 }
 
 void SharedBuffer::clear()
 {
     clearPlatformData();
     
+    for (unsigned i = 0; i < m_segments.size(); ++i)
+        freeSegment(m_segments[i]);
+
+    m_segments.clear();
+    m_size = 0;
+
     m_buffer.clear();
     m_purgeableBuffer.clear();
 }
 
 PassRefPtr<SharedBuffer> SharedBuffer::copy() const
 {
-    return SharedBuffer::create(data(), size());
+    RefPtr<SharedBuffer> clone(adoptRef(new SharedBuffer));
+    if (m_purgeableBuffer || hasPlatformData()) {
+        clone->append(data(), size());
+        return clone;
+    }
+
+    clone->m_size = m_size;
+    clone->m_buffer.reserveCapacity(m_size);
+    clone->m_buffer.append(m_buffer.data(), m_buffer.size());
+    for (unsigned i = 0; i < m_segments.size(); ++i)
+        clone->m_buffer.append(m_segments[i], segmentSize);
+    return clone;
 }
 
 PurgeableBuffer* SharedBuffer::releasePurgeableBuffer()
@@ -113,6 +188,54 @@ PurgeableBuffer* SharedBuffer::releasePurgeableBuffer()
     return m_purgeableBuffer.release(); 
 }
 
+const Vector<char>& SharedBuffer::buffer() const
+{
+    unsigned bufferSize = m_buffer.size();
+    if (m_size > bufferSize) {
+        m_buffer.resize(m_size);
+        char* destination = m_buffer.data() + bufferSize;
+        unsigned bytesLeft = m_size - bufferSize;
+        for (unsigned i = 0; i < m_segments.size(); ++i) {
+            unsigned bytesToCopy = bytesLeft < segmentSize ? bytesLeft : segmentSize;
+            memcpy(destination, m_segments[i], bytesToCopy);
+            destination += bytesToCopy;
+            bytesLeft -= bytesToCopy;
+            freeSegment(m_segments[i]);
+        }
+        m_segments.clear();
+    }
+    return m_buffer;
+}
+
+unsigned SharedBuffer::getSomeData(const char*& someData, unsigned position) const
+{
+    if (hasPlatformData() || m_purgeableBuffer) {
+        someData = data() + position;
+        return size() - position;
+    }
+
+    if (position >= m_size) {
+        someData = 0;
+        return 0;
+    }
+
+    unsigned consecutiveSize = m_buffer.size();
+    if (position < consecutiveSize) {
+        someData = m_buffer.data() + position;
+        return consecutiveSize - position;
+    }
+ 
+    position -= consecutiveSize;
+    unsigned segmentedSize = m_size - consecutiveSize;
+    unsigned segments = m_segments.size();
+    unsigned segment = segmentIndex(position);
+    ASSERT(segment < segments);
+
+    unsigned positionInSegment = offsetInSegment(position);
+    someData = m_segments[segment] + positionInSegment;
+    return segment == segments - 1 ? segmentedSize - position : segmentSize - positionInSegment;
+}
+
 #if !PLATFORM(CF)
 
 inline void SharedBuffer::clearPlatformData()
diff --git a/WebCore/platform/SharedBuffer.h b/WebCore/platform/SharedBuffer.h
index 3404a0c..750a59a 100644
--- a/WebCore/platform/SharedBuffer.h
+++ b/WebCore/platform/SharedBuffer.h
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2006 Apple Computer, Inc.  All rights reserved.
+ * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -73,11 +74,19 @@ public:
     static PassRefPtr<SharedBuffer> wrapCFData(CFDataRef);
 #endif
 
+    // Calling this function will force internal segmented buffers
+    // to be merged into a flat buffer. Use getSomeData() whenever possible
+    // for better performance.
     const char* data() const;
+
     unsigned size() const;
-    const Vector<char> &buffer() { return m_buffer; }
 
-    bool isEmpty() const { return size() == 0; }
+    // Calling this function will force internal segmented buffers
+    // to be merged into a flat buffer. Use getSomeData() whenever possible
+    // for better performance.
+    const Vector<char>& buffer() const;
+
+    bool isEmpty() const { return size(); }
 
     void append(const char*, int);
     void clear();
@@ -90,7 +99,21 @@ public:
 
     // Ensure this buffer has no other clients before calling this.
     PurgeableBuffer* releasePurgeableBuffer();
-    
+
+    // Return the number of consecutive bytes after "position". "data"
+    // points to the first byte.
+    // Return 0 when no more data left.
+    // When extracting all data with getSomeData(), the caller should
+    // repeat calling it until it returns 0.
+    // Usage:
+    //      const char* segment;
+    //      unsigned pos = 0;
+    //      while (unsigned length = sharedBuffer->getSomeData(segment, pos)) {
+    //          // Use the data. for example: decoder->decode(segment, length);
+    //          pos += length;
+    //      }
+    unsigned getSomeData(const char*& data, unsigned position = 0) const;
+
 private:
     SharedBuffer();
     SharedBuffer(const char*, int);
@@ -100,7 +123,9 @@ private:
     void maybeTransferPlatformData();
     bool hasPlatformData() const;
     
-    Vector<char> m_buffer;
+    unsigned m_size;
+    mutable Vector<char> m_buffer;
+    mutable Vector<char*> m_segments;
     OwnPtr<PurgeableBuffer> m_purgeableBuffer;
 #if PLATFORM(CF)
     SharedBuffer(CFDataRef);
@@ -110,4 +135,4 @@ private:
     
 }
 
-#endif
+#endif // SharedBuffer_h
diff --git a/WebCore/platform/cf/SharedBufferCF.cpp b/WebCore/platform/cf/SharedBufferCF.cpp
index 26fc07f..c96bfcc 100644
--- a/WebCore/platform/cf/SharedBufferCF.cpp
+++ b/WebCore/platform/cf/SharedBufferCF.cpp
@@ -76,9 +76,9 @@ void SharedBuffer::maybeTransferPlatformData()
     if (!m_cfData)
         return;
     
-    ASSERT(m_buffer.size() == 0);
+    ASSERT(m_size == 0);
         
-    m_buffer.append((const char*)CFDataGetBytePtr(m_cfData.get()), CFDataGetLength(m_cfData.get()));
+    append((const char*)CFDataGetBytePtr(m_cfData.get()), CFDataGetLength(m_cfData.get()));
         
     m_cfData = 0;
 }
diff --git a/WebCore/platform/haiku/SharedBufferHaiku.cpp b/WebCore/platform/haiku/SharedBufferHaiku.cpp
index 113cd2e..abe9e2d 100644
--- a/WebCore/platform/haiku/SharedBufferHaiku.cpp
+++ b/WebCore/platform/haiku/SharedBufferHaiku.cpp
@@ -47,6 +47,7 @@ PassRefPtr<SharedBuffer> SharedBuffer::createWithContentsOfFile(const String& fi
     result->m_buffer.resize(size);
     if (result->m_buffer.size() != size)
         return 0;
+    result->m_size = size;
 
     file.Read(result->m_buffer.data(), result->m_buffer.size());
     return result.release();
diff --git a/WebCore/platform/qt/SharedBufferQt.cpp b/WebCore/platform/qt/SharedBufferQt.cpp
index 8d62226..029d9d6 100644
--- a/WebCore/platform/qt/SharedBufferQt.cpp
+++ b/WebCore/platform/qt/SharedBufferQt.cpp
@@ -45,6 +45,8 @@ PassRefPtr<SharedBuffer> SharedBuffer::createWithContentsOfFile(const String& fi
     if (result->m_buffer.size() != file.size())
         return 0;
 
+    result->m_size = result->m_buffer.size();
+
     file.read(result->m_buffer.data(), result->m_buffer.size());
     return result.release();
 }
diff --git a/WebCore/platform/win/SharedBufferWin.cpp b/WebCore/platform/win/SharedBufferWin.cpp
index 1839c99..a95d590 100644
--- a/WebCore/platform/win/SharedBufferWin.cpp
+++ b/WebCore/platform/win/SharedBufferWin.cpp
@@ -57,6 +57,8 @@ PassRefPtr<SharedBuffer> SharedBuffer::createWithContentsOfFile(const String& fi
         goto exit;
     }
 
+    result->m_size = result->m_buffer.size();
+
     if (fread(result->m_buffer.data(), 1, fileStat.st_size, fileDescriptor) != fileStat.st_size)
         LOG_ERROR("Failed to fully read contents of file %s - errno(%i)", filePath.ascii().data(), errno);
 

-- 
WebKit Debian packaging



More information about the Pkg-webkit-commits mailing list