| 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 19 matching lines...) Expand all Loading... |
| 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 <limits.h> | 35 #include <limits.h> |
| 36 | 36 |
| 37 namespace WTF { | 37 namespace WTF { |
| 38 | 38 |
| 39 class WTF_EXPORT ArrayBufferView : public RefCounted<ArrayBufferView> { | 39 class WTF_EXPORT ArrayBufferView : public RefCounted<ArrayBufferView> { |
| 40 public: | 40 public: |
| 41 enum ViewType { | 41 enum ViewType { |
| 42 TypeInt8, | 42 TypeInt8, |
| 43 TypeUint8, | 43 TypeUint8, |
| 44 TypeUint8Clamped, | 44 TypeUint8Clamped, |
| 45 TypeInt16, | 45 TypeInt16, |
| 46 TypeUint16, | 46 TypeUint16, |
| 47 TypeInt32, | 47 TypeInt32, |
| 48 TypeUint32, | 48 TypeUint32, |
| 49 TypeFloat32, | 49 TypeFloat32, |
| 50 TypeFloat64, | 50 TypeFloat64, |
| 51 TypeDataView | 51 TypeDataView |
| 52 }; | 52 }; |
| 53 virtual ViewType type() const = 0; | 53 virtual ViewType type() const = 0; |
| 54 const char* typeName(); | 54 const char* typeName(); |
| 55 | 55 |
| 56 PassRefPtr<ArrayBuffer> buffer() const | 56 PassRefPtr<ArrayBuffer> buffer() const { |
| 57 { | 57 return m_buffer; |
| 58 return m_buffer; | 58 } |
| 59 } | |
| 60 | 59 |
| 61 void* baseAddress() const | 60 void* baseAddress() const { |
| 62 { | 61 return m_baseAddress; |
| 63 return m_baseAddress; | 62 } |
| 64 } | |
| 65 | 63 |
| 66 unsigned byteOffset() const | 64 unsigned byteOffset() const { |
| 67 { | 65 return m_byteOffset; |
| 68 return m_byteOffset; | 66 } |
| 69 } | |
| 70 | 67 |
| 71 virtual unsigned byteLength() const = 0; | 68 virtual unsigned byteLength() const = 0; |
| 72 | 69 |
| 73 void setNeuterable(bool flag) { m_isNeuterable = flag; } | 70 void setNeuterable(bool flag) { m_isNeuterable = flag; } |
| 74 bool isNeuterable() const { return m_isNeuterable; } | 71 bool isNeuterable() const { return m_isNeuterable; } |
| 75 bool isShared() const { return m_buffer ? m_buffer->isShared() : false; } | 72 bool isShared() const { return m_buffer ? m_buffer->isShared() : false; } |
| 76 | 73 |
| 77 virtual ~ArrayBufferView(); | 74 virtual ~ArrayBufferView(); |
| 78 | 75 |
| 79 protected: | 76 protected: |
| 80 ArrayBufferView(PassRefPtr<ArrayBuffer>, unsigned byteOffset); | 77 ArrayBufferView(PassRefPtr<ArrayBuffer>, unsigned byteOffset); |
| 81 | 78 |
| 82 inline bool setImpl(ArrayBufferView*, unsigned byteOffset); | 79 inline bool setImpl(ArrayBufferView*, unsigned byteOffset); |
| 83 | 80 |
| 84 // Helper to verify that a given sub-range of an ArrayBuffer is | 81 // Helper to verify that a given sub-range of an ArrayBuffer is |
| 85 // within range. | 82 // within range. |
| 86 template <typename T> | 83 template <typename T> |
| 87 static bool verifySubRange(PassRefPtr<ArrayBuffer> buffer, unsigned byteOffs
et, unsigned numElements) | 84 static bool verifySubRange(PassRefPtr<ArrayBuffer> buffer, unsigned byteOffset
, unsigned numElements) { |
| 88 { | 85 if (!buffer) |
| 89 if (!buffer) | 86 return false; |
| 90 return false; | 87 if (sizeof(T) > 1 && byteOffset % sizeof(T)) |
| 91 if (sizeof(T) > 1 && byteOffset % sizeof(T)) | 88 return false; |
| 92 return false; | 89 if (byteOffset > buffer->byteLength()) |
| 93 if (byteOffset > buffer->byteLength()) | 90 return false; |
| 94 return false; | 91 unsigned remainingElements = (buffer->byteLength() - byteOffset) / sizeof(T)
; |
| 95 unsigned remainingElements = (buffer->byteLength() - byteOffset) / sizeo
f(T); | 92 if (numElements > remainingElements) |
| 96 if (numElements > remainingElements) | 93 return false; |
| 97 return false; | 94 return true; |
| 98 return true; | 95 } |
| 99 } | |
| 100 | 96 |
| 101 virtual void neuter(); | 97 virtual void neuter(); |
| 102 | 98 |
| 103 // This is the address of the ArrayBuffer's storage, plus the byte offset. | 99 // This is the address of the ArrayBuffer's storage, plus the byte offset. |
| 104 void* m_baseAddress; | 100 void* m_baseAddress; |
| 105 | 101 |
| 106 unsigned m_byteOffset : 31; | 102 unsigned m_byteOffset : 31; |
| 107 bool m_isNeuterable : 1; | 103 bool m_isNeuterable : 1; |
| 108 | 104 |
| 109 private: | 105 private: |
| 110 friend class ArrayBuffer; | 106 friend class ArrayBuffer; |
| 111 RefPtr<ArrayBuffer> m_buffer; | 107 RefPtr<ArrayBuffer> m_buffer; |
| 112 ArrayBufferView* m_prevView; | 108 ArrayBufferView* m_prevView; |
| 113 ArrayBufferView* m_nextView; | 109 ArrayBufferView* m_nextView; |
| 114 }; | 110 }; |
| 115 | 111 |
| 116 bool ArrayBufferView::setImpl(ArrayBufferView* array, unsigned byteOffset) | 112 bool ArrayBufferView::setImpl(ArrayBufferView* array, unsigned byteOffset) { |
| 117 { | 113 if (byteOffset > byteLength() || byteOffset + array->byteLength() > byteLength
() || byteOffset + array->byteLength() < byteOffset) { |
| 118 if (byteOffset > byteLength() | 114 // Out of range offset or overflow |
| 119 || byteOffset + array->byteLength() > byteLength() | 115 return false; |
| 120 || byteOffset + array->byteLength() < byteOffset) { | 116 } |
| 121 // Out of range offset or overflow | |
| 122 return false; | |
| 123 } | |
| 124 | 117 |
| 125 char* base = static_cast<char*>(baseAddress()); | 118 char* base = static_cast<char*>(baseAddress()); |
| 126 memmove(base + byteOffset, array->baseAddress(), array->byteLength()); | 119 memmove(base + byteOffset, array->baseAddress(), array->byteLength()); |
| 127 return true; | 120 return true; |
| 128 } | 121 } |
| 129 | 122 |
| 130 } // namespace WTF | 123 } // namespace WTF |
| 131 | 124 |
| 132 using WTF::ArrayBufferView; | 125 using WTF::ArrayBufferView; |
| 133 | 126 |
| 134 #endif // ArrayBufferView_h | 127 #endif // ArrayBufferView_h |
| OLD | NEW |