2 * Copyright (C) 2011 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
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #ifndef WTF_BoundsCheckedPointer_h
30 #define WTF_BoundsCheckedPointer_h
32 #include <wtf/Assertions.h>
33 #include <wtf/UnusedParam.h>
37 // Useful for when you'd like to do pointer arithmetic on a buffer, but
38 // you'd also like to get some ASSERT()'s that prevent you from overflowing.
39 // This should be performance-neutral in release builds, while providing
40 // you with strong assertions in debug builds. Note that all of the
41 // asserting happens when you actually access the pointer. You are allowed
42 // to overflow or underflow with arithmetic so long as no accesses are
46 class BoundsCheckedPointer {
48 BoundsCheckedPointer()
57 BoundsCheckedPointer(T* pointer, size_t numElements)
61 , m_end(pointer + numElements)
64 UNUSED_PARAM(numElements);
67 BoundsCheckedPointer(T* pointer, T* end)
77 BoundsCheckedPointer(T* pointer, T* begin, size_t numElements)
81 , m_end(begin + numElements)
85 UNUSED_PARAM(numElements);
88 BoundsCheckedPointer(T* pointer, T* begin, T* end)
99 BoundsCheckedPointer& operator=(T* value)
105 BoundsCheckedPointer& operator+=(ptrdiff_t amount)
111 BoundsCheckedPointer& operator-=(ptrdiff_t amount)
117 BoundsCheckedPointer operator+(ptrdiff_t amount) const
119 BoundsCheckedPointer result = *this;
120 result.m_pointer += amount;
124 BoundsCheckedPointer operator-(ptrdiff_t amount) const
126 BoundsCheckedPointer result = *this;
127 result.m_pointer -= amount;
131 BoundsCheckedPointer operator++() // prefix
137 BoundsCheckedPointer operator--() // prefix
143 BoundsCheckedPointer operator++(int) // postfix
145 BoundsCheckedPointer result = *this;
150 BoundsCheckedPointer operator--(int) // postfix
152 BoundsCheckedPointer result = *this;
157 bool operator<(T* other) const
159 return m_pointer < other;
162 bool operator<=(T* other) const
164 return m_pointer <= other;
167 bool operator>(T* other) const
169 return m_pointer > other;
172 bool operator>=(T* other) const
174 return m_pointer >= other;
177 bool operator==(T* other) const
179 return m_pointer == other;
182 bool operator!=(T* other) const
184 return m_pointer != other;
187 bool operator<(BoundsCheckedPointer other) const
189 return m_pointer < other.m_pointer;
192 bool operator<=(BoundsCheckedPointer other) const
194 return m_pointer <= other.m_pointer;
197 bool operator>(BoundsCheckedPointer other) const
199 return m_pointer > other.m_pointer;
202 bool operator>=(BoundsCheckedPointer other) const
204 return m_pointer >= other.m_pointer;
207 bool operator==(BoundsCheckedPointer other) const
209 return m_pointer == other.m_pointer;
212 bool operator!=(BoundsCheckedPointer other) const
214 return m_pointer != other.m_pointer;
217 BoundsCheckedPointer operator!()
233 const T& operator*() const
239 T& operator[](ptrdiff_t index)
241 validate(m_pointer + index);
242 return m_pointer[index];
245 const T& operator[](ptrdiff_t index) const
247 validate(m_pointer + index);
248 return m_pointer[index];
251 // The only thing this has in common with strcat() is that it
252 // keeps appending from the given pointer until reaching 0.
253 BoundsCheckedPointer& strcat(const T* source)
256 *(*this)++ = *source++;
261 void validate(T* pointer) const
263 ASSERT_UNUSED(pointer, pointer >= m_begin);
265 // This guard is designed to protect against the misaligned case.
266 // A simple pointer < m_end would miss the case if, for example,
267 // T = int16_t and pointer is 1 byte less than m_end.
268 ASSERT_UNUSED(pointer, pointer + 1 <= m_end);
271 void validate() const
285 using WTF::BoundsCheckedPointer;
287 #endif // WTF_BoundsCheckedPointer_h