ArrayBufferView.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  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. #ifndef ArrayBufferView_h
  26. #define ArrayBufferView_h
  27. #include <wtf/ArrayBuffer.h>
  28. #include <algorithm>
  29. #include <limits.h>
  30. #include <wtf/PassRefPtr.h>
  31. #include <wtf/RefCounted.h>
  32. #include <wtf/RefPtr.h>
  33. namespace WTF {
  34. class ArrayBufferView : public RefCounted<ArrayBufferView> {
  35. public:
  36. enum ViewType {
  37. TypeInt8,
  38. TypeUint8,
  39. TypeUint8Clamped,
  40. TypeInt16,
  41. TypeUint16,
  42. TypeInt32,
  43. TypeUint32,
  44. TypeFloat32,
  45. TypeFloat64,
  46. TypeDataView
  47. };
  48. virtual ViewType getType() const = 0;
  49. PassRefPtr<ArrayBuffer> buffer() const
  50. {
  51. return m_buffer;
  52. }
  53. void* baseAddress() const
  54. {
  55. return m_baseAddress;
  56. }
  57. unsigned byteOffset() const
  58. {
  59. return m_byteOffset;
  60. }
  61. virtual unsigned byteLength() const = 0;
  62. void setNeuterable(bool flag) { m_isNeuterable = flag; }
  63. bool isNeuterable() const { return m_isNeuterable; }
  64. WTF_EXPORT_PRIVATE virtual ~ArrayBufferView();
  65. protected:
  66. WTF_EXPORT_PRIVATE ArrayBufferView(PassRefPtr<ArrayBuffer>, unsigned byteOffset);
  67. inline bool setImpl(ArrayBufferView*, unsigned byteOffset);
  68. inline bool setRangeImpl(const char* data, size_t dataByteLength, unsigned byteOffset);
  69. inline bool zeroRangeImpl(unsigned byteOffset, size_t rangeByteLength);
  70. static inline void calculateOffsetAndLength(int start, int end, unsigned arraySize,
  71. unsigned* offset, unsigned* length);
  72. // Helper to verify that a given sub-range of an ArrayBuffer is
  73. // within range.
  74. template <typename T>
  75. static bool verifySubRange(PassRefPtr<ArrayBuffer> buffer,
  76. unsigned byteOffset,
  77. unsigned numElements)
  78. {
  79. if (!buffer)
  80. return false;
  81. if (sizeof(T) > 1 && byteOffset % sizeof(T))
  82. return false;
  83. if (byteOffset > buffer->byteLength())
  84. return false;
  85. unsigned remainingElements = (buffer->byteLength() - byteOffset) / sizeof(T);
  86. if (numElements > remainingElements)
  87. return false;
  88. return true;
  89. }
  90. // Input offset is in number of elements from this array's view;
  91. // output offset is in number of bytes from the underlying buffer's view.
  92. template <typename T>
  93. static void clampOffsetAndNumElements(PassRefPtr<ArrayBuffer> buffer,
  94. unsigned arrayByteOffset,
  95. unsigned *offset,
  96. unsigned *numElements)
  97. {
  98. unsigned maxOffset = (UINT_MAX - arrayByteOffset) / sizeof(T);
  99. if (*offset > maxOffset) {
  100. *offset = buffer->byteLength();
  101. *numElements = 0;
  102. return;
  103. }
  104. *offset = arrayByteOffset + *offset * sizeof(T);
  105. *offset = std::min(buffer->byteLength(), *offset);
  106. unsigned remainingElements = (buffer->byteLength() - *offset) / sizeof(T);
  107. *numElements = std::min(remainingElements, *numElements);
  108. }
  109. WTF_EXPORT_PRIVATE virtual void neuter();
  110. // This is the address of the ArrayBuffer's storage, plus the byte offset.
  111. void* m_baseAddress;
  112. unsigned m_byteOffset : 31;
  113. bool m_isNeuterable : 1;
  114. private:
  115. friend class ArrayBuffer;
  116. RefPtr<ArrayBuffer> m_buffer;
  117. ArrayBufferView* m_prevView;
  118. ArrayBufferView* m_nextView;
  119. };
  120. bool ArrayBufferView::setImpl(ArrayBufferView* array, unsigned byteOffset)
  121. {
  122. if (byteOffset > byteLength()
  123. || byteOffset + array->byteLength() > byteLength()
  124. || byteOffset + array->byteLength() < byteOffset) {
  125. // Out of range offset or overflow
  126. return false;
  127. }
  128. char* base = static_cast<char*>(baseAddress());
  129. memmove(base + byteOffset, array->baseAddress(), array->byteLength());
  130. return true;
  131. }
  132. bool ArrayBufferView::setRangeImpl(const char* data, size_t dataByteLength, unsigned byteOffset)
  133. {
  134. if (byteOffset > byteLength()
  135. || byteOffset + dataByteLength > byteLength()
  136. || byteOffset + dataByteLength < byteOffset) {
  137. // Out of range offset or overflow
  138. return false;
  139. }
  140. char* base = static_cast<char*>(baseAddress());
  141. memmove(base + byteOffset, data, dataByteLength);
  142. return true;
  143. }
  144. bool ArrayBufferView::zeroRangeImpl(unsigned byteOffset, size_t rangeByteLength)
  145. {
  146. if (byteOffset > byteLength()
  147. || byteOffset + rangeByteLength > byteLength()
  148. || byteOffset + rangeByteLength < byteOffset) {
  149. // Out of range offset or overflow
  150. return false;
  151. }
  152. char* base = static_cast<char*>(baseAddress());
  153. memset(base + byteOffset, 0, rangeByteLength);
  154. return true;
  155. }
  156. void ArrayBufferView::calculateOffsetAndLength(int start, int end, unsigned arraySize,
  157. unsigned* offset, unsigned* length)
  158. {
  159. if (start < 0)
  160. start += arraySize;
  161. if (start < 0)
  162. start = 0;
  163. if (end < 0)
  164. end += arraySize;
  165. if (end < 0)
  166. end = 0;
  167. if (static_cast<unsigned>(end) > arraySize)
  168. end = arraySize;
  169. if (end < start)
  170. end = start;
  171. *offset = static_cast<unsigned>(start);
  172. *length = static_cast<unsigned>(end - start);
  173. }
  174. } // namespace WTF
  175. using WTF::ArrayBufferView;
  176. #endif // ArrayBufferView_h