initial import
[vuplus_webkit] / Source / WebCore / platform / audio / AudioArray.h
1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #ifndef AudioArray_h
30 #define AudioArray_h
31
32 #include <string.h>
33 #include <wtf/FastMalloc.h>
34 #include <wtf/Vector.h>
35
36 namespace WebCore {
37
38 template<typename T>
39 class AudioArray {
40 public:
41     AudioArray() : m_allocation(0), m_alignedData(0), m_size(0) { }
42     explicit AudioArray(size_t n) : m_allocation(0), m_alignedData(0), m_size(0)
43     {
44         allocate(n);
45     }
46
47     ~AudioArray()
48     {
49         fastFree(m_allocation);
50     }
51
52     // It's OK to call allocate() multiple times, but data will *not* be copied from an initial allocation
53     // if re-allocated. Allocations are zero-initialized.
54     void allocate(size_t n)
55     {
56         // Although n is a size_t, its true limit is max unsigned because we use unsigned in zeroRange()
57         // and copyToRange(). Also check for integer overflow.
58         if (n > std::numeric_limits<unsigned>::max() / sizeof(T))
59             CRASH();
60       
61         unsigned initialSize = sizeof(T) * n;
62
63         // 16-byte alignment for 128bit SIMD.
64         const size_t alignment = 16;
65
66         if (m_allocation)
67             fastFree(m_allocation);
68         
69         bool isAllocationGood = false;
70         
71         while (!isAllocationGood) {
72             // Initially we try to allocate the exact size, but if it's not aligned
73             // then we'll have to reallocate and from then on allocate extra.
74             static size_t extraAllocationBytes = 0;
75
76             // Again, check for integer overflow.
77             if (initialSize + extraAllocationBytes < initialSize)
78                 CRASH();
79
80             T* allocation = static_cast<T*>(fastMalloc(initialSize + extraAllocationBytes));
81             if (!allocation)
82                 CRASH();
83             T* alignedData = alignedAddress(allocation, alignment);
84
85             if (alignedData == allocation || extraAllocationBytes == alignment) {
86                 m_allocation = allocation;
87                 m_alignedData = alignedData;
88                 m_size = n;
89                 isAllocationGood = true;
90                 zero();
91             } else {
92                 extraAllocationBytes = alignment; // always allocate extra after the first alignment failure.
93                 fastFree(allocation);
94             }
95         }
96     }
97
98     T* data() { return m_alignedData; }
99     const T* data() const { return m_alignedData; }
100     size_t size() const { return m_size; }
101
102     T& at(size_t i)
103     {
104         // Note that although it is a size_t, m_size is now guaranteed to be
105         // no greater than max unsigned. This guarantee is enforced in allocate().
106         ASSERT(i < size());
107         return data()[i];
108     }
109
110     T& operator[](size_t i) { return at(i); }
111
112     void zero()
113     {
114         // This multiplication is made safe by the check in allocate().
115         memset(this->data(), 0, sizeof(T) * this->size());
116     }
117
118     void zeroRange(unsigned start, unsigned end)
119     {
120         bool isSafe = (start <= end) && (end <= this->size());
121         ASSERT(isSafe);
122         if (!isSafe)
123             return;
124
125         // This expression cannot overflow because end - start cannot be
126         // greater than m_size, which is safe due to the check in allocate().
127         memset(this->data() + start, 0, sizeof(T) * (end - start));
128     }
129
130     void copyToRange(T* sourceData, unsigned start, unsigned end)
131     {
132         bool isSafe = (start <= end) && (end <= this->size());
133         ASSERT(isSafe);
134         if (!isSafe)
135             return;
136
137         // This expression cannot overflow because end - start cannot be
138         // greater than m_size, which is safe due to the check in allocate().
139         memcpy(this->data() + start, sourceData, sizeof(T) * (end - start));
140     }
141
142 private:
143     static T* alignedAddress(T* address, intptr_t alignment)
144     {
145         intptr_t value = reinterpret_cast<intptr_t>(address);
146         return reinterpret_cast<T*>((value + alignment - 1) & ~(alignment - 1));
147     }
148
149     T* m_allocation;
150     T* m_alignedData;
151     size_t m_size;
152 };
153
154 typedef AudioArray<float> AudioFloatArray;
155 typedef AudioArray<double> AudioDoubleArray;
156
157 } // WebCore
158
159 #endif // AudioArray_h