blob: 451c1b6ed99c1afb659761a7558f113a4684578d [file] [log] [blame]
cmarrin@apple.com24616122009-09-08 13:01:34 +00001/*
2 * Copyright (C) 2009 Apple 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 * 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.
12 *
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.
24 */
25
kbr@google.com3706f9b2010-05-14 22:37:05 +000026#ifndef ArrayBufferView_h
27#define ArrayBufferView_h
cmarrin@apple.com24616122009-09-08 13:01:34 +000028
eric@webkit.org3979f2d2012-03-07 08:50:54 +000029#include <wtf/ArrayBuffer.h>
kbr@google.comaf472002010-08-05 18:33:48 +000030
31#include <algorithm>
antonm@chromium.org44bc4672010-04-14 12:12:28 +000032#include <limits.h>
cmarrin@apple.com24616122009-09-08 13:01:34 +000033#include <wtf/PassRefPtr.h>
34#include <wtf/RefCounted.h>
35#include <wtf/RefPtr.h>
cmarrin@apple.com24616122009-09-08 13:01:34 +000036
oliver@apple.comd05cffe2011-11-15 01:52:20 +000037namespace WTF {
adamk@chromium.org04718132011-11-09 01:51:35 +000038
kevino@webkit.org342534c2012-02-29 17:44:34 +000039class WTF_EXPORT_PRIVATE_RTTI ArrayBufferView : public RefCounted<ArrayBufferView> {
cmarrin@apple.coma4ae3282009-11-16 21:30:15 +000040 public:
commit-queue@webkit.orgd8d6c692012-07-27 00:25:04 +000041 enum ViewType {
42 TypeInt8,
43 TypeUint8,
44 TypeUint8Clamped,
45 TypeInt16,
46 TypeUint16,
47 TypeInt32,
48 TypeUint32,
49 TypeFloat32,
50 TypeFloat64,
51 TypeDataView
52 };
53 virtual ViewType getType() const = 0;
cmarrin@apple.com24616122009-09-08 13:01:34 +000054
kbr@google.com3706f9b2010-05-14 22:37:05 +000055 PassRefPtr<ArrayBuffer> buffer() const
kbr@google.comb3ff6442010-05-10 17:53:21 +000056 {
cmarrin@apple.coma4ae3282009-11-16 21:30:15 +000057 return m_buffer;
58 }
cmarrin@apple.com24616122009-09-08 13:01:34 +000059
kbr@google.comb3ff6442010-05-10 17:53:21 +000060 void* baseAddress() const
61 {
cmarrin@apple.coma4ae3282009-11-16 21:30:15 +000062 return m_baseAddress;
63 }
cmarrin@apple.com24616122009-09-08 13:01:34 +000064
kbr@google.comb3ff6442010-05-10 17:53:21 +000065 unsigned byteOffset() const
66 {
cmarrin@apple.coma4ae3282009-11-16 21:30:15 +000067 return m_byteOffset;
68 }
cmarrin@apple.com24616122009-09-08 13:01:34 +000069
cmarrin@apple.coma4ae3282009-11-16 21:30:15 +000070 virtual unsigned byteLength() const = 0;
cmarrin@apple.coma4ae3282009-11-16 21:30:15 +000071
kevino@webkit.orgf19517d2012-03-29 19:22:02 +000072 WTF_EXPORT_PRIVATE_NO_RTTI virtual ~ArrayBufferView();
cmarrin@apple.coma4ae3282009-11-16 21:30:15 +000073
74 protected:
kevino@webkit.orgf19517d2012-03-29 19:22:02 +000075 WTF_EXPORT_PRIVATE_NO_RTTI ArrayBufferView(PassRefPtr<ArrayBuffer>, unsigned byteOffset);
cmarrin@apple.coma4ae3282009-11-16 21:30:15 +000076
oliver@apple.com9ce01c62011-11-28 23:40:08 +000077 inline bool setImpl(ArrayBufferView*, unsigned byteOffset);
cmarrin@apple.coma4ae3282009-11-16 21:30:15 +000078
oliver@apple.com9ce01c62011-11-28 23:40:08 +000079 inline bool setRangeImpl(const char* data, size_t dataByteLength, unsigned byteOffset);
crogers@google.com29d0a382010-09-09 22:22:17 +000080
oliver@apple.com9ce01c62011-11-28 23:40:08 +000081 inline bool zeroRangeImpl(unsigned byteOffset, size_t rangeByteLength);
crogers@google.com29d0a382010-09-09 22:22:17 +000082
oliver@apple.com9ce01c62011-11-28 23:40:08 +000083 static inline void calculateOffsetAndLength(int start, int end, unsigned arraySize,
kbr@google.comb3ff6442010-05-10 17:53:21 +000084 unsigned* offset, unsigned* length);
eric@webkit.orgcb99c712010-03-20 01:58:11 +000085
kbr@google.comcb5a2332010-03-01 21:20:31 +000086 // Helper to verify that a given sub-range of an ArrayBuffer is
87 // within range.
88 template <typename T>
kbr@google.com3706f9b2010-05-14 22:37:05 +000089 static bool verifySubRange(PassRefPtr<ArrayBuffer> buffer,
kbr@google.comcb5a2332010-03-01 21:20:31 +000090 unsigned byteOffset,
91 unsigned numElements)
92 {
93 if (!buffer)
94 return false;
95 if (sizeof(T) > 1 && byteOffset % sizeof(T))
96 return false;
97 if (byteOffset > buffer->byteLength())
98 return false;
99 unsigned remainingElements = (buffer->byteLength() - byteOffset) / sizeof(T);
100 if (numElements > remainingElements)
101 return false;
102 return true;
103 }
104
eric@webkit.orgc6e1e9b2010-04-14 05:29:23 +0000105 // 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.
kbr@google.comcb5a2332010-03-01 21:20:31 +0000107 template <typename T>
kbr@google.com3706f9b2010-05-14 22:37:05 +0000108 static void clampOffsetAndNumElements(PassRefPtr<ArrayBuffer> buffer,
eric@webkit.orgc6e1e9b2010-04-14 05:29:23 +0000109 unsigned arrayByteOffset,
110 unsigned *offset,
kbr@google.comcb5a2332010-03-01 21:20:31 +0000111 unsigned *numElements)
112 {
eric@webkit.orgc6e1e9b2010-04-14 05:29:23 +0000113 unsigned maxOffset = (UINT_MAX - arrayByteOffset) / sizeof(T);
114 if (*offset > maxOffset) {
115 *offset = buffer->byteLength();
116 *numElements = 0;
117 return;
118 }
119 *offset = arrayByteOffset + *offset * sizeof(T);
120 *offset = std::min(buffer->byteLength(), *offset);
121 unsigned remainingElements = (buffer->byteLength() - *offset) / sizeof(T);
kbr@google.comcb5a2332010-03-01 21:20:31 +0000122 *numElements = std::min(remainingElements, *numElements);
123 }
124
kevino@webkit.orgf19517d2012-03-29 19:22:02 +0000125 WTF_EXPORT_PRIVATE_NO_RTTI virtual void neuter();
dslomov@google.com529e5302011-11-04 21:43:56 +0000126
kbr@google.com3706f9b2010-05-14 22:37:05 +0000127 // This is the address of the ArrayBuffer's storage, plus the byte offset.
cmarrin@apple.coma4ae3282009-11-16 21:30:15 +0000128 void* m_baseAddress;
129
130 unsigned m_byteOffset;
131
132 private:
dslomov@google.com529e5302011-11-04 21:43:56 +0000133 friend class ArrayBuffer;
kbr@google.com3706f9b2010-05-14 22:37:05 +0000134 RefPtr<ArrayBuffer> m_buffer;
dslomov@google.com529e5302011-11-04 21:43:56 +0000135 ArrayBufferView* m_prevView;
136 ArrayBufferView* m_nextView;
cmarrin@apple.coma4ae3282009-11-16 21:30:15 +0000137};
138
oliver@apple.com9ce01c62011-11-28 23:40:08 +0000139bool ArrayBufferView::setImpl(ArrayBufferView* array, unsigned byteOffset)
140{
141 if (byteOffset > byteLength()
142 || byteOffset + array->byteLength() > byteLength()
143 || byteOffset + array->byteLength() < byteOffset) {
144 // Out of range offset or overflow
145 return false;
146 }
147
148 char* base = static_cast<char*>(baseAddress());
149 memmove(base + byteOffset, array->baseAddress(), array->byteLength());
150 return true;
151}
152
153bool ArrayBufferView::setRangeImpl(const char* data, size_t dataByteLength, unsigned byteOffset)
154{
155 if (byteOffset > byteLength()
156 || byteOffset + dataByteLength > byteLength()
157 || byteOffset + dataByteLength < byteOffset) {
158 // Out of range offset or overflow
159 return false;
160 }
161
162 char* base = static_cast<char*>(baseAddress());
163 memmove(base + byteOffset, data, dataByteLength);
164 return true;
165}
166
167bool ArrayBufferView::zeroRangeImpl(unsigned byteOffset, size_t rangeByteLength)
168{
169 if (byteOffset > byteLength()
170 || byteOffset + rangeByteLength > byteLength()
171 || byteOffset + rangeByteLength < byteOffset) {
172 // Out of range offset or overflow
173 return false;
174 }
175
176 char* base = static_cast<char*>(baseAddress());
177 memset(base + byteOffset, 0, rangeByteLength);
178 return true;
179}
180
181void ArrayBufferView::calculateOffsetAndLength(int start, int end, unsigned arraySize,
182 unsigned* offset, unsigned* length)
183{
184 if (start < 0)
185 start += arraySize;
186 if (start < 0)
187 start = 0;
188 if (end < 0)
189 end += arraySize;
190 if (end < 0)
191 end = 0;
commit-queue@webkit.orgb1da1892012-03-06 15:30:51 +0000192 if (static_cast<unsigned>(end) > arraySize)
193 end = arraySize;
oliver@apple.com9ce01c62011-11-28 23:40:08 +0000194 if (end < start)
195 end = start;
196 *offset = static_cast<unsigned>(start);
197 *length = static_cast<unsigned>(end - start);
198}
199
oliver@apple.comd05cffe2011-11-15 01:52:20 +0000200} // namespace WTF
201
202using WTF::ArrayBufferView;
cmarrin@apple.com24616122009-09-08 13:01:34 +0000203
kbr@google.com3706f9b2010-05-14 22:37:05 +0000204#endif // ArrayBufferView_h