| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2009 Apple Inc. All rights reserved. | 2 * Copyright (C) 2009 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 | 25 |
| 26 #ifndef ArrayBufferView_h | 26 #ifndef ArrayBufferView_h |
| 27 #define ArrayBufferView_h | 27 #define ArrayBufferView_h |
| 28 | 28 |
| 29 #include "wtf/ArrayBuffer.h" | 29 #include "wtf/ArrayBuffer.h" |
| 30 | 30 |
| 31 #include "wtf/PassRefPtr.h" | 31 #include "wtf/PassRefPtr.h" |
| 32 #include "wtf/RefCounted.h" | 32 #include "wtf/RefCounted.h" |
| 33 #include "wtf/RefPtr.h" | 33 #include "wtf/RefPtr.h" |
| 34 #include "wtf/WTFExport.h" | 34 #include "wtf/WTFExport.h" |
| 35 #include <algorithm> | |
| 36 #include <limits.h> | 35 #include <limits.h> |
| 37 | 36 |
| 38 namespace WTF { | 37 namespace WTF { |
| 39 | 38 |
| 40 class WTF_EXPORT ArrayBufferView : public RefCounted<ArrayBufferView> { | 39 class WTF_EXPORT ArrayBufferView : public RefCounted<ArrayBufferView> { |
| 41 public: | 40 public: |
| 42 enum ViewType { | 41 enum ViewType { |
| 43 TypeInt8, | 42 TypeInt8, |
| 44 TypeUint8, | 43 TypeUint8, |
| 45 TypeUint8Clamped, | 44 TypeUint8Clamped, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 | 77 |
| 79 protected: | 78 protected: |
| 80 ArrayBufferView(PassRefPtr<ArrayBuffer>, unsigned byteOffset); | 79 ArrayBufferView(PassRefPtr<ArrayBuffer>, unsigned byteOffset); |
| 81 | 80 |
| 82 inline bool setImpl(ArrayBufferView*, unsigned byteOffset); | 81 inline bool setImpl(ArrayBufferView*, unsigned byteOffset); |
| 83 | 82 |
| 84 inline bool setRangeImpl(const char* data, size_t dataByteLength, unsigned b
yteOffset); | 83 inline bool setRangeImpl(const char* data, size_t dataByteLength, unsigned b
yteOffset); |
| 85 | 84 |
| 86 inline bool zeroRangeImpl(unsigned byteOffset, size_t rangeByteLength); | 85 inline bool zeroRangeImpl(unsigned byteOffset, size_t rangeByteLength); |
| 87 | 86 |
| 88 static inline void calculateOffsetAndLength(int start, int end, unsigned arr
aySize, | |
| 89 unsigned* offset, unsigned* length); | |
| 90 | |
| 91 // Helper to verify that a given sub-range of an ArrayBuffer is | 87 // Helper to verify that a given sub-range of an ArrayBuffer is |
| 92 // within range. | 88 // within range. |
| 93 template <typename T> | 89 template <typename T> |
| 94 static bool verifySubRange(PassRefPtr<ArrayBuffer> buffer, | 90 static bool verifySubRange(PassRefPtr<ArrayBuffer> buffer, |
| 95 unsigned byteOffset, | 91 unsigned byteOffset, |
| 96 unsigned numElements) | 92 unsigned numElements) |
| 97 { | 93 { |
| 98 if (!buffer) | 94 if (!buffer) |
| 99 return false; | 95 return false; |
| 100 if (sizeof(T) > 1 && byteOffset % sizeof(T)) | 96 if (sizeof(T) > 1 && byteOffset % sizeof(T)) |
| 101 return false; | 97 return false; |
| 102 if (byteOffset > buffer->byteLength()) | 98 if (byteOffset > buffer->byteLength()) |
| 103 return false; | 99 return false; |
| 104 unsigned remainingElements = (buffer->byteLength() - byteOffset) / sizeo
f(T); | 100 unsigned remainingElements = (buffer->byteLength() - byteOffset) / sizeo
f(T); |
| 105 if (numElements > remainingElements) | 101 if (numElements > remainingElements) |
| 106 return false; | 102 return false; |
| 107 return true; | 103 return true; |
| 108 } | 104 } |
| 109 | 105 |
| 110 // Input offset is in number of elements from this array's view; | |
| 111 // output offset is in number of bytes from the underlying buffer's view. | |
| 112 template <typename T> | |
| 113 static void clampOffsetAndNumElements(PassRefPtr<ArrayBuffer> buffer, | |
| 114 unsigned arrayByteOffset, | |
| 115 unsigned *offset, | |
| 116 unsigned *numElements) | |
| 117 { | |
| 118 unsigned maxOffset = (UINT_MAX - arrayByteOffset) / sizeof(T); | |
| 119 if (*offset > maxOffset) { | |
| 120 *offset = buffer->byteLength(); | |
| 121 *numElements = 0; | |
| 122 return; | |
| 123 } | |
| 124 *offset = arrayByteOffset + *offset * sizeof(T); | |
| 125 *offset = std::min(buffer->byteLength(), *offset); | |
| 126 unsigned remainingElements = (buffer->byteLength() - *offset) / sizeof(T
); | |
| 127 *numElements = std::min(remainingElements, *numElements); | |
| 128 } | |
| 129 | |
| 130 virtual void neuter(); | 106 virtual void neuter(); |
| 131 | 107 |
| 132 // This is the address of the ArrayBuffer's storage, plus the byte offset. | 108 // This is the address of the ArrayBuffer's storage, plus the byte offset. |
| 133 void* m_baseAddress; | 109 void* m_baseAddress; |
| 134 | 110 |
| 135 unsigned m_byteOffset : 31; | 111 unsigned m_byteOffset : 31; |
| 136 bool m_isNeuterable : 1; | 112 bool m_isNeuterable : 1; |
| 137 | 113 |
| 138 private: | 114 private: |
| 139 friend class ArrayBuffer; | 115 friend class ArrayBuffer; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 || byteOffset + rangeByteLength < byteOffset) { | 153 || byteOffset + rangeByteLength < byteOffset) { |
| 178 // Out of range offset or overflow | 154 // Out of range offset or overflow |
| 179 return false; | 155 return false; |
| 180 } | 156 } |
| 181 | 157 |
| 182 char* base = static_cast<char*>(baseAddress()); | 158 char* base = static_cast<char*>(baseAddress()); |
| 183 memset(base + byteOffset, 0, rangeByteLength); | 159 memset(base + byteOffset, 0, rangeByteLength); |
| 184 return true; | 160 return true; |
| 185 } | 161 } |
| 186 | 162 |
| 187 void ArrayBufferView::calculateOffsetAndLength(int start, int end, unsigned arra
ySize, | |
| 188 unsigned* offset, unsigned* lengt
h) | |
| 189 { | |
| 190 if (start < 0) | |
| 191 start += arraySize; | |
| 192 if (start < 0) | |
| 193 start = 0; | |
| 194 if (end < 0) | |
| 195 end += arraySize; | |
| 196 if (end < 0) | |
| 197 end = 0; | |
| 198 if (static_cast<unsigned>(end) > arraySize) | |
| 199 end = arraySize; | |
| 200 if (end < start) | |
| 201 end = start; | |
| 202 *offset = static_cast<unsigned>(start); | |
| 203 *length = static_cast<unsigned>(end - start); | |
| 204 } | |
| 205 | |
| 206 } // namespace WTF | 163 } // namespace WTF |
| 207 | 164 |
| 208 using WTF::ArrayBufferView; | 165 using WTF::ArrayBufferView; |
| 209 | 166 |
| 210 #endif // ArrayBufferView_h | 167 #endif // ArrayBufferView_h |
| OLD | NEW |