Title: [92408] trunk/Source/WebCore
Revision
92408
Author
[email protected]
Date
2011-08-04 14:07:22 -0700 (Thu, 04 Aug 2011)

Log Message

Make sure that AudioArray is 16-byte aligned
https://bugs.webkit.org/show_bug.cgi?id=65651

Reviewed by Kenneth Russell.

No new tests - this does not change _javascript_ API.

* platform/audio/AudioArray.h:
(WebCore::AudioArray::AudioArray):
(WebCore::AudioArray::~AudioArray):
(WebCore::AudioArray::allocate):
(WebCore::AudioArray::data):
(WebCore::AudioArray::size):
(WebCore::AudioArray::at):
(WebCore::AudioArray::operator[]):
(WebCore::AudioArray::alignedAddress):
* platform/audio/Biquad.cpp:
(WebCore::Biquad::Biquad):
* platform/audio/ReverbConvolverStage.cpp:
(WebCore::ReverbConvolverStage::ReverbConvolverStage):
* webaudio/DelayDSPKernel.cpp:
(WebCore::DelayDSPKernel::DelayDSPKernel):
* webaudio/RealtimeAnalyser.cpp:
(WebCore::RealtimeAnalyser::setFftSize):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (92407 => 92408)


--- trunk/Source/WebCore/ChangeLog	2011-08-04 20:38:37 UTC (rev 92407)
+++ trunk/Source/WebCore/ChangeLog	2011-08-04 21:07:22 UTC (rev 92408)
@@ -1,3 +1,30 @@
+2011-08-04  Chris Rogers  <[email protected]>
+
+        Make sure that AudioArray is 16-byte aligned
+        https://bugs.webkit.org/show_bug.cgi?id=65651
+
+        Reviewed by Kenneth Russell.
+
+        No new tests - this does not change _javascript_ API.
+
+        * platform/audio/AudioArray.h:
+        (WebCore::AudioArray::AudioArray):
+        (WebCore::AudioArray::~AudioArray):
+        (WebCore::AudioArray::allocate):
+        (WebCore::AudioArray::data):
+        (WebCore::AudioArray::size):
+        (WebCore::AudioArray::at):
+        (WebCore::AudioArray::operator[]):
+        (WebCore::AudioArray::alignedAddress):
+        * platform/audio/Biquad.cpp:
+        (WebCore::Biquad::Biquad):
+        * platform/audio/ReverbConvolverStage.cpp:
+        (WebCore::ReverbConvolverStage::ReverbConvolverStage):
+        * webaudio/DelayDSPKernel.cpp:
+        (WebCore::DelayDSPKernel::DelayDSPKernel):
+        * webaudio/RealtimeAnalyser.cpp:
+        (WebCore::RealtimeAnalyser::setFftSize):
+
 2011-08-04  Brady Eidson  <[email protected]>
 
         <rdar://problem/9882581>, <rdar://problem/9868015>, and https://bugs.webkit.org/show_bug.cgi?id=65712

Modified: trunk/Source/WebCore/platform/audio/AudioArray.h (92407 => 92408)


--- trunk/Source/WebCore/platform/audio/AudioArray.h	2011-08-04 20:38:37 UTC (rev 92407)
+++ trunk/Source/WebCore/platform/audio/AudioArray.h	2011-08-04 21:07:22 UTC (rev 92408)
@@ -30,16 +30,70 @@
 #define AudioArray_h
 
 #include <string.h>
+#include <wtf/FastMalloc.h>
 #include <wtf/Vector.h>
 
 namespace WebCore {
 
 template<typename T>
-class AudioArray : public Vector<T> {
+class AudioArray {
 public:
-    AudioArray() : Vector<T>(0) { }
-    explicit AudioArray(size_t n) : Vector<T>(n, 0) { }
+    AudioArray() : m_allocation(0), m_alignedData(0), m_size(0) { }
+    explicit AudioArray(size_t n) : m_allocation(0), m_alignedData(0), m_size(0)
+    {
+        allocate(n);
+    }
 
+    ~AudioArray()
+    {
+        fastFree(m_allocation);
+    }
+
+    // It's OK to call allocate() multiple times, but data will *not* be copied from an initial allocation
+    // if re-allocated. Allocations are zero-initialized.
+    void allocate(size_t n)
+    {
+        // 16-byte alignment for 128bit SIMD.
+        const size_t alignment = 16;
+
+        if (m_allocation)
+            fastFree(m_allocation);
+        
+        bool isAllocationGood = false;
+        
+        while (!isAllocationGood) {
+            // Initially we try to allocate the exact size, but if it's not aligned
+            // then we'll have to reallocate and from then on allocate extra.
+            static size_t extraAllocationBytes = 0;
+
+            T* allocation = static_cast<T*>(fastMalloc(sizeof(T) * n + extraAllocationBytes));
+            T* alignedData = alignedAddress(allocation, alignment);
+
+            if (alignedData == allocation || extraAllocationBytes == alignment) {
+                m_allocation = allocation;
+                m_alignedData = alignedData;
+                m_size = n;
+                isAllocationGood = true;
+                zero();
+            } else {
+                extraAllocationBytes = alignment; // always allocate extra after the first alignment failure.
+                fastFree(allocation);
+            }
+        }
+    }
+
+    T* data() { return m_alignedData; }
+    const T* data() const { return m_alignedData; }
+    size_t size() const { return m_size; }
+
+    T& at(size_t i)
+    {
+        ASSERT(i < size());
+        return data()[i];
+    }
+
+    T& operator[](size_t i) { return at(i); }
+
     void zero() { memset(this->data(), 0, sizeof(T) * this->size()); }
 
     void zeroRange(unsigned start, unsigned end)
@@ -61,6 +115,17 @@
 
         memcpy(this->data() + start, sourceData, sizeof(T) * (end - start));
     }
+
+private:
+    static T* alignedAddress(T* address, intptr_t alignment)
+    {
+        intptr_t value = reinterpret_cast<intptr_t>(address);
+        return reinterpret_cast<T*>((value + alignment - 1) & ~(alignment - 1));
+    }
+
+    T* m_allocation;
+    T* m_alignedData;
+    size_t m_size;
 };
 
 typedef AudioArray<float> AudioFloatArray;

Modified: trunk/Source/WebCore/platform/audio/Biquad.cpp (92407 => 92408)


--- trunk/Source/WebCore/platform/audio/Biquad.cpp	2011-08-04 20:38:37 UTC (rev 92407)
+++ trunk/Source/WebCore/platform/audio/Biquad.cpp	2011-08-04 21:07:22 UTC (rev 92408)
@@ -48,8 +48,8 @@
 {
 #if OS(DARWIN)
     // Allocate two samples more for filter history
-    m_inputBuffer.resize(kBufferSize + 2);
-    m_outputBuffer.resize(kBufferSize + 2);
+    m_inputBuffer.allocate(kBufferSize + 2);
+    m_outputBuffer.allocate(kBufferSize + 2);
 #endif
 
     // Initialize as pass-thru (straight-wire, no filter effect)

Modified: trunk/Source/WebCore/platform/audio/ReverbConvolverStage.cpp (92407 => 92408)


--- trunk/Source/WebCore/platform/audio/ReverbConvolverStage.cpp	2011-08-04 20:38:37 UTC (rev 92407)
+++ trunk/Source/WebCore/platform/audio/ReverbConvolverStage.cpp	2011-08-04 21:07:22 UTC (rev 92408)
@@ -56,7 +56,7 @@
     
     m_fftKernel.doPaddedFFT(impulseResponse + stageOffset, stageLength);
     m_convolver = adoptPtr(new FFTConvolver(fftSize));
-    m_temporaryBuffer.resize(renderSliceSize);
+    m_temporaryBuffer.allocate(renderSliceSize);
 
     // The convolution stage at offset stageOffset needs to have a corresponding delay to cancel out the offset.
     size_t totalDelay = stageOffset + reverbTotalLatency;
@@ -78,7 +78,7 @@
     m_preReadWriteIndex = 0;
     m_framesProcessed = 0; // total frames processed so far
 
-    m_preDelayBuffer.resize(m_preDelayLength < fftSize ? fftSize : m_preDelayLength);
+    m_preDelayBuffer.allocate(m_preDelayLength < fftSize ? fftSize : m_preDelayLength);
 }
 
 void ReverbConvolverStage::processInBackground(ReverbConvolver* convolver, size_t framesToProcess)

Modified: trunk/Source/WebCore/webaudio/DelayDSPKernel.cpp (92407 => 92408)


--- trunk/Source/WebCore/webaudio/DelayDSPKernel.cpp	2011-08-04 20:38:37 UTC (rev 92407)
+++ trunk/Source/WebCore/webaudio/DelayDSPKernel.cpp	2011-08-04 21:07:22 UTC (rev 92408)
@@ -48,7 +48,7 @@
     if (!processor)
         return;
 
-    m_buffer.resize(static_cast<size_t>(processor->sampleRate() * DefaultMaxDelayTime));
+    m_buffer.allocate(static_cast<size_t>(processor->sampleRate() * DefaultMaxDelayTime));
     m_buffer.zero();
 
     m_smoothingRate = AudioUtilities::discreteTimeConstantForSampleRate(SmoothingTimeConstant, processor->sampleRate());
@@ -69,7 +69,7 @@
     if (!bufferLength)
         return;
     
-    m_buffer.resize(bufferLength);
+    m_buffer.allocate(bufferLength);
     m_buffer.zero();
 
     m_smoothingRate = AudioUtilities::discreteTimeConstantForSampleRate(SmoothingTimeConstant, sampleRate);

Modified: trunk/Source/WebCore/webaudio/RealtimeAnalyser.cpp (92407 => 92408)


--- trunk/Source/WebCore/webaudio/RealtimeAnalyser.cpp	2011-08-04 20:38:37 UTC (rev 92407)
+++ trunk/Source/WebCore/webaudio/RealtimeAnalyser.cpp	2011-08-04 21:07:22 UTC (rev 92408)
@@ -90,7 +90,7 @@
 
     if (m_fftSize != size) {
         m_analysisFrame = adoptPtr(new FFTFrame(m_fftSize));
-        m_magnitudeBuffer.resize(size);
+        m_magnitudeBuffer.allocate(size);
         m_fftSize = size;
     }
 }
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to