Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / wtf / ArrayBufferView.h
blobe9bbe4908eb6e8e1ee5147ac3bb12835e5e4c1d0
1 /*
2 * Copyright (C) 2009 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
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.
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.
26 #ifndef ArrayBufferView_h
27 #define ArrayBufferView_h
29 #include "wtf/ArrayBuffer.h"
31 #include "wtf/PassRefPtr.h"
32 #include "wtf/RefCounted.h"
33 #include "wtf/RefPtr.h"
34 #include "wtf/WTFExport.h"
35 #include <limits.h>
37 namespace WTF {
39 class WTF_EXPORT ArrayBufferView : public RefCounted<ArrayBufferView> {
40 public:
41 enum ViewType {
42 TypeInt8,
43 TypeUint8,
44 TypeUint8Clamped,
45 TypeInt16,
46 TypeUint16,
47 TypeInt32,
48 TypeUint32,
49 TypeFloat32,
50 TypeFloat64,
51 TypeDataView
53 virtual ViewType type() const = 0;
54 const char* typeName();
56 PassRefPtr<ArrayBuffer> buffer() const
58 return m_buffer;
61 void* baseAddress() const
63 return m_baseAddress;
66 unsigned byteOffset() const
68 return m_byteOffset;
71 virtual unsigned byteLength() const = 0;
73 void setNeuterable(bool flag) { m_isNeuterable = flag; }
74 bool isNeuterable() const { return m_isNeuterable; }
75 bool isShared() const { return m_buffer ? m_buffer->isShared() : false; }
77 virtual ~ArrayBufferView();
79 protected:
80 ArrayBufferView(PassRefPtr<ArrayBuffer>, unsigned byteOffset);
82 inline bool setImpl(ArrayBufferView*, unsigned byteOffset);
84 // Helper to verify that a given sub-range of an ArrayBuffer is
85 // within range.
86 template <typename T>
87 static bool verifySubRange(PassRefPtr<ArrayBuffer> buffer,
88 unsigned byteOffset,
89 unsigned numElements)
91 if (!buffer)
92 return false;
93 if (sizeof(T) > 1 && byteOffset % sizeof(T))
94 return false;
95 if (byteOffset > buffer->byteLength())
96 return false;
97 unsigned remainingElements = (buffer->byteLength() - byteOffset) / sizeof(T);
98 if (numElements > remainingElements)
99 return false;
100 return true;
103 virtual void neuter();
105 // This is the address of the ArrayBuffer's storage, plus the byte offset.
106 void* m_baseAddress;
108 unsigned m_byteOffset : 31;
109 bool m_isNeuterable : 1;
111 private:
112 friend class ArrayBuffer;
113 RefPtr<ArrayBuffer> m_buffer;
114 ArrayBufferView* m_prevView;
115 ArrayBufferView* m_nextView;
118 bool ArrayBufferView::setImpl(ArrayBufferView* array, unsigned byteOffset)
120 if (byteOffset > byteLength()
121 || byteOffset + array->byteLength() > byteLength()
122 || byteOffset + array->byteLength() < byteOffset) {
123 // Out of range offset or overflow
124 return false;
127 char* base = static_cast<char*>(baseAddress());
128 memmove(base + byteOffset, array->baseAddress(), array->byteLength());
129 return true;
132 } // namespace WTF
134 using WTF::ArrayBufferView;
136 #endif // ArrayBufferView_h