2 * Copyright (C) 2009 Apple 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
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef ArrayBufferView_h
27 #define ArrayBufferView_h
29 #include <wtf/ArrayBuffer.h>
33 #include <wtf/PassRefPtr.h>
34 #include <wtf/RefCounted.h>
35 #include <wtf/RefPtr.h>
39 class WTF_EXPORT_PRIVATE_RTTI ArrayBufferView : public RefCounted<ArrayBufferView> {
53 virtual ViewType getType() const = 0;
55 PassRefPtr<ArrayBuffer> buffer() const
60 void* baseAddress() const
65 unsigned byteOffset() const
70 virtual unsigned byteLength() const = 0;
72 WTF_EXPORT_PRIVATE_NO_RTTI virtual ~ArrayBufferView();
75 WTF_EXPORT_PRIVATE_NO_RTTI ArrayBufferView(PassRefPtr<ArrayBuffer>, unsigned byteOffset);
77 inline bool setImpl(ArrayBufferView*, unsigned byteOffset);
79 inline bool setRangeImpl(const char* data, size_t dataByteLength, unsigned byteOffset);
81 inline bool zeroRangeImpl(unsigned byteOffset, size_t rangeByteLength);
83 static inline void calculateOffsetAndLength(int start, int end, unsigned arraySize,
84 unsigned* offset, unsigned* length);
86 // Helper to verify that a given sub-range of an ArrayBuffer is
89 static bool verifySubRange(PassRefPtr<ArrayBuffer> buffer,
95 if (sizeof(T) > 1 && byteOffset % sizeof(T))
97 if (byteOffset > buffer->byteLength())
99 unsigned remainingElements = (buffer->byteLength() - byteOffset) / sizeof(T);
100 if (numElements > remainingElements)
105 // Input offset is in number of elements from this array's view;
106 // output offset is in number of bytes from the underlying buffer's view.
107 template <typename T>
108 static void clampOffsetAndNumElements(PassRefPtr<ArrayBuffer> buffer,
109 unsigned arrayByteOffset,
111 unsigned *numElements)
113 unsigned maxOffset = (UINT_MAX - arrayByteOffset) / sizeof(T);
114 if (*offset > maxOffset) {
115 *offset = buffer->byteLength();
119 *offset = arrayByteOffset + *offset * sizeof(T);
120 *offset = std::min(buffer->byteLength(), *offset);
121 unsigned remainingElements = (buffer->byteLength() - *offset) / sizeof(T);
122 *numElements = std::min(remainingElements, *numElements);
125 WTF_EXPORT_PRIVATE_NO_RTTI virtual void neuter();
127 // This is the address of the ArrayBuffer's storage, plus the byte offset.
130 unsigned m_byteOffset;
133 friend class ArrayBuffer;
134 RefPtr<ArrayBuffer> m_buffer;
135 ArrayBufferView* m_prevView;
136 ArrayBufferView* m_nextView;
139 bool ArrayBufferView::setImpl(ArrayBufferView* array, unsigned byteOffset)
141 if (byteOffset > byteLength()
142 || byteOffset + array->byteLength() > byteLength()
143 || byteOffset + array->byteLength() < byteOffset) {
144 // Out of range offset or overflow
148 char* base = static_cast<char*>(baseAddress());
149 memmove(base + byteOffset, array->baseAddress(), array->byteLength());
153 bool ArrayBufferView::setRangeImpl(const char* data, size_t dataByteLength, unsigned byteOffset)
155 if (byteOffset > byteLength()
156 || byteOffset + dataByteLength > byteLength()
157 || byteOffset + dataByteLength < byteOffset) {
158 // Out of range offset or overflow
162 char* base = static_cast<char*>(baseAddress());
163 memmove(base + byteOffset, data, dataByteLength);
167 bool ArrayBufferView::zeroRangeImpl(unsigned byteOffset, size_t rangeByteLength)
169 if (byteOffset > byteLength()
170 || byteOffset + rangeByteLength > byteLength()
171 || byteOffset + rangeByteLength < byteOffset) {
172 // Out of range offset or overflow
176 char* base = static_cast<char*>(baseAddress());
177 memset(base + byteOffset, 0, rangeByteLength);
181 void ArrayBufferView::calculateOffsetAndLength(int start, int end, unsigned arraySize,
182 unsigned* offset, unsigned* length)
192 if (static_cast<unsigned>(end) > arraySize)
196 *offset = static_cast<unsigned>(start);
197 *length = static_cast<unsigned>(end - start);
202 using WTF::ArrayBufferView;
204 #endif // ArrayBufferView_h