[webkit-dev] Fix for Vector::m_inlineBuffer alignment?

Paul Pedriana ppedriana at gmail.com
Wed Sep 3 15:05:34 PDT 2008


I see that JavaScriptCore/wtf/Vector.h has this:

    // FIXME: Nothing guarantees this buffer is appropriately aligned to 
hold objects of type T.
    char m_inlineBuffer[m_inlineBufferSize];

And I've heard reports about people having alignment crashes on some 
hardware. Something like the code below could rectify this in a portable 
way. This could be made into a patch as-is, though the first part of the 
code really belongs in a separate PlatformDefs.h-style header. I'm 
wondering if WebKit has a central place for such a thing that I'm not 
aware of.

    // Portable facilities to detect and set alignment
    #if defined(__GNUC__) || defined(__MWERKS__)
        #define WTF_ALIGN_OF(type)   __alignof__(type)
        #define WTF_PREFIX_ALIGN(n)
        #define WTF_POSTFIX_ALIGN(n) __attribute__((aligned(n)))
    #elif defined(_MSC_VER)
        #define WTF_ALIGN_OF(type)   __alignof(type)
        #define WTF_PREFIX_ALIGN(n)  __declspec(align(n))  // n must be 
a literal integer, it cannot be a general constant expression.
        #define WTF_POSTFIX_ALIGN(n)
    #else
        #error need alignment control
    #endif

    // Portable aliasing support.
    #if defined(__GNUC__) && (((__GNUC__ * 100) + __GNUC_MINOR__) >= 303)
        typedef char __attribute__((__may_alias__)) aligned_buffer_char;
    #else
        typedef char aligned_buffer_char;
    #endif

    // Portable aligned char buffer.
    // VC++ can't compile__declspec(align(__alignof(T)), so we solve 
this with template specialization.
    template <size_t size, size_t alignment>
    struct aligned_buffer { aligned_buffer_char buffer[size]; };

    template<size_t size>
    struct aligned_buffer<size, 2> { WTF_PREFIX_ALIGN(2)  
aligned_buffer_char buffer[size] WTF_POSTFIX_ALIGN(2); };

    template<size_t size>
    struct aligned_buffer<size, 4> { WTF_PREFIX_ALIGN(4)  
aligned_buffer_char buffer[size] WTF_POSTFIX_ALIGN(4); };

    template<size_t size>
    struct aligned_buffer<size, 8> { WTF_PREFIX_ALIGN(8)  
aligned_buffer_char buffer[size] WTF_POSTFIX_ALIGN(8); };

    template<size_t size>
    struct aligned_buffer<size, 16> { WTF_PREFIX_ALIGN(16) 
aligned_buffer_char buffer[size] WTF_POSTFIX_ALIGN(16); };

    template<size_t size>
    struct aligned_buffer<size, 32> { WTF_PREFIX_ALIGN(32) 
aligned_buffer_char buffer[size] WTF_POSTFIX_ALIGN(32); };

    template<size_t size>
    struct aligned_buffer<size, 64> { WTF_PREFIX_ALIGN(64) 
aligned_buffer_char buffer[size] WTF_POSTFIX_ALIGN(64); };



    template<typename T, size_t inlineCapacity>
    class VectorBuffer : private VectorBufferBase<T> {
        .
        .
        .

-        T* inlineBuffer() { return reinterpret_cast<T*>(&m_inlineBuffer); }
-        // FIXME: Nothing guarantees this buffer is appropriately 
aligned to hold objects of type T.
-        char m_inlineBuffer[m_inlineBufferSize];


+        T* inlineBuffer() { return 
reinterpret_cast<T*>(m_inlineBuffer.buffer); }
+        aligned_buffer<m_inlineBufferSize, WTF_ALIGN_OF(T)> m_inlineBuffer;
    };












More information about the webkit-dev mailing list