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 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/CheckedArithmetic.h>
34#include <wtf/FastMalloc.h>
35
36namespace WebCore {
37
38template<typename T>
39class AudioArray {
40 WTF_MAKE_FAST_ALLOCATED;
41public:
42 AudioArray() : m_allocation(0), m_alignedData(0), m_size(0) { }
43 explicit AudioArray(size_t n) : m_allocation(0), m_alignedData(0), m_size(0)
44 {
45 allocate(n);
46 }
47
48 ~AudioArray()
49 {
50 fastFree(m_allocation);
51 }
52
53 // It's OK to call allocate() multiple times, but data will *not* be copied from an initial allocation
54 // if re-allocated. Allocations are zero-initialized.
55 void allocate(Checked<size_t> n)
56 {
57 Checked<unsigned> initialSize = sizeof(T) * n;
58 const size_t alignment = 16;
59
60 if (m_allocation)
61 fastFree(m_allocation);
62
63 bool isAllocationGood = false;
64
65 while (!isAllocationGood) {
66 // Initially we try to allocate the exact size, but if it's not aligned
67 // then we'll have to reallocate and from then on allocate extra.
68 static size_t extraAllocationBytes = 0;
69
70 T* allocation = static_cast<T*>(fastMalloc((initialSize + extraAllocationBytes).unsafeGet()));
71 if (!allocation)
72 CRASH();
73 T* alignedData = alignedAddress(allocation, alignment);
74
75 if (alignedData == allocation || extraAllocationBytes == alignment) {
76 m_allocation = allocation;
77 m_alignedData = alignedData;
78 m_size = n.unsafeGet();
79 isAllocationGood = true;
80 zero();
81 } else {
82 extraAllocationBytes = alignment; // always allocate extra after the first alignment failure.
83 fastFree(allocation);
84 }
85 }
86 }
87
88 T* data() { return m_alignedData; }
89 const T* data() const { return m_alignedData; }
90 size_t size() const { return m_size; }
91
92 T& at(size_t i)
93 {
94 // Note that although it is a size_t, m_size is now guaranteed to be
95 // no greater than max unsigned. This guarantee is enforced in allocate().
96 ASSERT_WITH_SECURITY_IMPLICATION(i < size());
97 return data()[i];
98 }
99
100 T& operator[](size_t i) { return at(i); }
101
102 void zero()
103 {
104 // This multiplication is made safe by the check in allocate().
105 memset(this->data(), 0, sizeof(T) * this->size());
106 }
107
108 void zeroRange(unsigned start, unsigned end)
109 {
110 bool isSafe = (start <= end) && (end <= this->size());
111 ASSERT(isSafe);
112 if (!isSafe)
113 return;
114
115 // This expression cannot overflow because end - start cannot be
116 // greater than m_size, which is safe due to the check in allocate().
117 memset(this->data() + start, 0, sizeof(T) * (end - start));
118 }
119
120 void copyToRange(const T* sourceData, unsigned start, unsigned end)
121 {
122 bool isSafe = (start <= end) && (end <= this->size());
123 ASSERT(isSafe);
124 if (!isSafe)
125 return;
126
127 // This expression cannot overflow because end - start cannot be
128 // greater than m_size, which is safe due to the check in allocate().
129 memcpy(this->data() + start, sourceData, sizeof(T) * (end - start));
130 }
131
132private:
133 static T* alignedAddress(T* address, intptr_t alignment)
134 {
135 intptr_t value = reinterpret_cast<intptr_t>(address);
136 return reinterpret_cast<T*>((value + alignment - 1) & ~(alignment - 1));
137 }
138
139 T* m_allocation;
140 T* m_alignedData;
141 size_t m_size;
142};
143
144typedef AudioArray<float> AudioFloatArray;
145typedef AudioArray<double> AudioDoubleArray;
146
147} // WebCore
148
149#endif // AudioArray_h
150