]>
git.saurik.com Git - apple/javascriptcore.git/blob - runtime/ArrayBufferView.h
2 * Copyright (C) 2009, 2013 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 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 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 "ArrayBuffer.h"
30 #include "TypedArrayType.h"
33 #include <wtf/PassRefPtr.h>
34 #include <wtf/RefCounted.h>
35 #include <wtf/RefPtr.h>
39 class JSArrayBufferView
;
43 class ArrayBufferView
: public RefCounted
<ArrayBufferView
> {
45 virtual TypedArrayType
getType() const = 0;
47 bool isNeutered() const
49 return !m_buffer
|| m_buffer
->isNeutered();
52 PassRefPtr
<ArrayBuffer
> buffer() const
59 void* baseAddress() const
66 unsigned byteOffset() const
73 virtual unsigned byteLength() const = 0;
75 JS_EXPORT_PRIVATE
void setNeuterable(bool flag
);
76 bool isNeuterable() const { return m_isNeuterable
; }
78 JS_EXPORT_PRIVATE
virtual ~ArrayBufferView();
80 // Helper to verify byte offset is size aligned.
81 static bool verifyByteOffsetAlignment(unsigned byteOffset
, size_t size
)
83 return !(byteOffset
& (size
- 1));
86 // Helper to verify that a given sub-range of an ArrayBuffer is
88 static bool verifySubRangeLength(PassRefPtr
<ArrayBuffer
> buffer
, unsigned byteOffset
, unsigned numElements
, size_t size
)
90 unsigned byteLength
= buffer
->byteLength();
91 if (byteOffset
> byteLength
)
93 unsigned remainingElements
= (byteLength
- byteOffset
) / size
;
94 if (numElements
> remainingElements
)
99 virtual JSArrayBufferView
* wrap(ExecState
*, JSGlobalObject
*) = 0;
102 JS_EXPORT_PRIVATE
ArrayBufferView(PassRefPtr
<ArrayBuffer
>, unsigned byteOffset
);
104 inline bool setImpl(ArrayBufferView
*, unsigned byteOffset
);
106 inline bool setRangeImpl(const char* data
, size_t dataByteLength
, unsigned byteOffset
);
108 inline bool zeroRangeImpl(unsigned byteOffset
, size_t rangeByteLength
);
110 static inline void calculateOffsetAndLength(
111 int start
, int end
, unsigned arraySize
,
112 unsigned* offset
, unsigned* length
);
114 // Input offset is in number of elements from this array's view;
115 // output offset is in number of bytes from the underlying buffer's view.
116 template <typename T
>
117 static void clampOffsetAndNumElements(
118 PassRefPtr
<ArrayBuffer
> buffer
,
119 unsigned arrayByteOffset
,
121 unsigned *numElements
)
123 unsigned maxOffset
= (UINT_MAX
- arrayByteOffset
) / sizeof(T
);
124 if (*offset
> maxOffset
) {
125 *offset
= buffer
->byteLength();
129 *offset
= arrayByteOffset
+ *offset
* sizeof(T
);
130 *offset
= std::min(buffer
->byteLength(), *offset
);
131 unsigned remainingElements
= (buffer
->byteLength() - *offset
) / sizeof(T
);
132 *numElements
= std::min(remainingElements
, *numElements
);
135 // This is the address of the ArrayBuffer's storage, plus the byte offset.
138 unsigned m_byteOffset
: 31;
139 bool m_isNeuterable
: 1;
142 friend class ArrayBuffer
;
143 RefPtr
<ArrayBuffer
> m_buffer
;
146 bool ArrayBufferView::setImpl(ArrayBufferView
* array
, unsigned byteOffset
)
148 if (byteOffset
> byteLength()
149 || byteOffset
+ array
->byteLength() > byteLength()
150 || byteOffset
+ array
->byteLength() < byteOffset
) {
151 // Out of range offset or overflow
155 char* base
= static_cast<char*>(baseAddress());
156 memmove(base
+ byteOffset
, array
->baseAddress(), array
->byteLength());
160 bool ArrayBufferView::setRangeImpl(const char* data
, size_t dataByteLength
, unsigned byteOffset
)
162 if (byteOffset
> byteLength()
163 || byteOffset
+ dataByteLength
> byteLength()
164 || byteOffset
+ dataByteLength
< byteOffset
) {
165 // Out of range offset or overflow
169 char* base
= static_cast<char*>(baseAddress());
170 memmove(base
+ byteOffset
, data
, dataByteLength
);
174 bool ArrayBufferView::zeroRangeImpl(unsigned byteOffset
, size_t rangeByteLength
)
176 if (byteOffset
> byteLength()
177 || byteOffset
+ rangeByteLength
> byteLength()
178 || byteOffset
+ rangeByteLength
< byteOffset
) {
179 // Out of range offset or overflow
183 char* base
= static_cast<char*>(baseAddress());
184 memset(base
+ byteOffset
, 0, rangeByteLength
);
188 void ArrayBufferView::calculateOffsetAndLength(
189 int start
, int end
, unsigned arraySize
, unsigned* offset
, unsigned* length
)
199 if (static_cast<unsigned>(end
) > arraySize
)
203 *offset
= static_cast<unsigned>(start
);
204 *length
= static_cast<unsigned>(end
- start
);
209 using JSC::ArrayBufferView
;
211 #endif // ArrayBufferView_h