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
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"
39 class WTF_EXPORT ArrayBufferView
: public RefCounted
<ArrayBufferView
> {
53 virtual ViewType
type() const = 0;
54 const char* typeName();
56 PassRefPtr
<ArrayBuffer
> buffer() const
61 void* baseAddress() const
66 unsigned byteOffset() const
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();
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
87 static bool verifySubRange(PassRefPtr
<ArrayBuffer
> buffer
,
93 if (sizeof(T
) > 1 && byteOffset
% sizeof(T
))
95 if (byteOffset
> buffer
->byteLength())
97 unsigned remainingElements
= (buffer
->byteLength() - byteOffset
) / sizeof(T
);
98 if (numElements
> remainingElements
)
103 virtual void neuter();
105 // This is the address of the ArrayBuffer's storage, plus the byte offset.
108 unsigned m_byteOffset
: 31;
109 bool m_isNeuterable
: 1;
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
127 char* base
= static_cast<char*>(baseAddress());
128 memmove(base
+ byteOffset
, array
->baseAddress(), array
->byteLength());
134 using WTF::ArrayBufferView
;
136 #endif // ArrayBufferView_h