OLD | NEW |
1 /* | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 * Copyright (C) 2009 Apple Inc. All rights reserved. | 2 // Use of this source code is governed by a BSD-style license that can be |
3 * | 3 // found in the LICENSE file. |
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 | 4 |
26 #ifndef ArrayBufferView_h | 5 #include "platform/wtf/typed_arrays/ArrayBufferView.h" |
27 #define ArrayBufferView_h | |
28 | 6 |
29 #include "wtf/PassRefPtr.h" | 7 // The contents of this header was moved to platform/wtf as part of |
30 #include "wtf/RefCounted.h" | 8 // WTF migration project. See the following post for details: |
31 #include "wtf/RefPtr.h" | 9 // https://groups.google.com/a/chromium.org/d/msg/blink-dev/tLdAZCTlcAA/bYXVT8gY
CAAJ |
32 #include "wtf/WTFExport.h" | |
33 #include "wtf/typed_arrays/ArrayBuffer.h" | |
34 #include <limits.h> | |
35 | |
36 namespace WTF { | |
37 | |
38 class WTF_EXPORT ArrayBufferView : public RefCounted<ArrayBufferView> { | |
39 public: | |
40 enum ViewType { | |
41 TypeInt8, | |
42 TypeUint8, | |
43 TypeUint8Clamped, | |
44 TypeInt16, | |
45 TypeUint16, | |
46 TypeInt32, | |
47 TypeUint32, | |
48 TypeFloat32, | |
49 TypeFloat64, | |
50 TypeDataView | |
51 }; | |
52 virtual ViewType type() const = 0; | |
53 const char* typeName(); | |
54 | |
55 ArrayBuffer* buffer() const { return m_buffer.get(); } | |
56 | |
57 void* baseAddress() const { | |
58 DCHECK(!isShared()); | |
59 return m_baseAddress; | |
60 } | |
61 | |
62 unsigned byteOffset() const { return m_byteOffset; } | |
63 | |
64 virtual unsigned byteLength() const = 0; | |
65 virtual unsigned typeSize() const = 0; | |
66 | |
67 void setNeuterable(bool flag) { m_isNeuterable = flag; } | |
68 bool isNeuterable() const { return m_isNeuterable; } | |
69 bool isShared() const { return m_buffer ? m_buffer->isShared() : false; } | |
70 | |
71 virtual ~ArrayBufferView(); | |
72 | |
73 protected: | |
74 ArrayBufferView(PassRefPtr<ArrayBuffer>, unsigned byteOffset); | |
75 | |
76 inline bool setImpl(ArrayBufferView*, unsigned byteOffset); | |
77 | |
78 // Helper to verify that a given sub-range of an ArrayBuffer is | |
79 // within range. | |
80 template <typename T> | |
81 static bool verifySubRange(PassRefPtr<ArrayBuffer> buffer, | |
82 unsigned byteOffset, | |
83 unsigned numElements) { | |
84 if (!buffer) | |
85 return false; | |
86 if (sizeof(T) > 1 && byteOffset % sizeof(T)) | |
87 return false; | |
88 if (byteOffset > buffer->byteLength()) | |
89 return false; | |
90 unsigned remainingElements = | |
91 (buffer->byteLength() - byteOffset) / sizeof(T); | |
92 if (numElements > remainingElements) | |
93 return false; | |
94 return true; | |
95 } | |
96 | |
97 virtual void neuter(); | |
98 | |
99 // This is the address of the ArrayBuffer's storage, plus the byte offset. | |
100 void* m_baseAddress; | |
101 | |
102 unsigned m_byteOffset : 31; | |
103 unsigned m_isNeuterable : 1; | |
104 | |
105 private: | |
106 friend class ArrayBuffer; | |
107 RefPtr<ArrayBuffer> m_buffer; | |
108 ArrayBufferView* m_prevView; | |
109 ArrayBufferView* m_nextView; | |
110 }; | |
111 | |
112 bool ArrayBufferView::setImpl(ArrayBufferView* array, unsigned byteOffset) { | |
113 if (byteOffset > byteLength() || | |
114 byteOffset + array->byteLength() > byteLength() || | |
115 byteOffset + array->byteLength() < byteOffset) { | |
116 // Out of range offset or overflow | |
117 return false; | |
118 } | |
119 | |
120 char* base = static_cast<char*>(baseAddress()); | |
121 memmove(base + byteOffset, array->baseAddress(), array->byteLength()); | |
122 return true; | |
123 } | |
124 | |
125 } // namespace WTF | |
126 | |
127 using WTF::ArrayBufferView; | |
128 | |
129 #endif // ArrayBufferView_h | |
OLD | NEW |