2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
33 #include <wtf/FastMalloc.h>
34 #include <wtf/Vector.h>
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)
49 fastFree(m_allocation);
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)
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))
61 unsigned initialSize = sizeof(T) * n;
63 // 16-byte alignment for 128bit SIMD.
64 const size_t alignment = 16;
67 fastFree(m_allocation);
69 bool isAllocationGood = false;
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;
76 // Again, check for integer overflow.
77 if (initialSize + extraAllocationBytes < initialSize)
80 T* allocation = static_cast<T*>(fastMalloc(initialSize + extraAllocationBytes));
83 T* alignedData = alignedAddress(allocation, alignment);
85 if (alignedData == allocation || extraAllocationBytes == alignment) {
86 m_allocation = allocation;
87 m_alignedData = alignedData;
89 isAllocationGood = true;
92 extraAllocationBytes = alignment; // always allocate extra after the first alignment failure.
98 T* data() { return m_alignedData; }
99 const T* data() const { return m_alignedData; }
100 size_t size() const { return m_size; }
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().
110 T& operator[](size_t i) { return at(i); }
114 // This multiplication is made safe by the check in allocate().
115 memset(this->data(), 0, sizeof(T) * this->size());
118 void zeroRange(unsigned start, unsigned end)
120 bool isSafe = (start <= end) && (end <= this->size());
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));
130 void copyToRange(T* sourceData, unsigned start, unsigned end)
132 bool isSafe = (start <= end) && (end <= this->size());
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));
143 static T* alignedAddress(T* address, intptr_t alignment)
145 intptr_t value = reinterpret_cast<intptr_t>(address);
146 return reinterpret_cast<T*>((value + alignment - 1) & ~(alignment - 1));
154 typedef AudioArray<float> AudioFloatArray;
155 typedef AudioArray<double> AudioDoubleArray;
159 #endif // AudioArray_h