BoundsCheckedPointer.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Copyright (C) 2011 Apple Inc. All rights reserved.
  3. *
  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. *
  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.
  16. *
  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.
  27. */
  28. #ifndef WTF_BoundsCheckedPointer_h
  29. #define WTF_BoundsCheckedPointer_h
  30. #include <wtf/Assertions.h>
  31. namespace WTF {
  32. // Useful for when you'd like to do pointer arithmetic on a buffer, but
  33. // you'd also like to get some ASSERT()'s that prevent you from overflowing.
  34. // This should be performance-neutral in release builds, while providing
  35. // you with strong assertions in debug builds. Note that all of the
  36. // asserting happens when you actually access the pointer. You are allowed
  37. // to overflow or underflow with arithmetic so long as no accesses are
  38. // performed.
  39. template<typename T>
  40. class BoundsCheckedPointer {
  41. public:
  42. BoundsCheckedPointer()
  43. : m_pointer(0)
  44. #if !ASSERT_DISABLED
  45. , m_begin(0)
  46. , m_end(0)
  47. #endif
  48. {
  49. }
  50. BoundsCheckedPointer(T* pointer, size_t numElements)
  51. : m_pointer(pointer)
  52. #if !ASSERT_DISABLED
  53. , m_begin(pointer)
  54. , m_end(pointer + numElements)
  55. #endif
  56. {
  57. UNUSED_PARAM(numElements);
  58. }
  59. BoundsCheckedPointer(T* pointer, T* end)
  60. : m_pointer(pointer)
  61. #if !ASSERT_DISABLED
  62. , m_begin(pointer)
  63. , m_end(end)
  64. #endif
  65. {
  66. UNUSED_PARAM(end);
  67. }
  68. BoundsCheckedPointer(T* pointer, T* begin, size_t numElements)
  69. : m_pointer(pointer)
  70. #if !ASSERT_DISABLED
  71. , m_begin(begin)
  72. , m_end(begin + numElements)
  73. #endif
  74. {
  75. UNUSED_PARAM(begin);
  76. UNUSED_PARAM(numElements);
  77. }
  78. BoundsCheckedPointer(T* pointer, T* begin, T* end)
  79. : m_pointer(pointer)
  80. #if !ASSERT_DISABLED
  81. , m_begin(begin)
  82. , m_end(end)
  83. #endif
  84. {
  85. UNUSED_PARAM(begin);
  86. UNUSED_PARAM(end);
  87. }
  88. BoundsCheckedPointer& operator=(T* value)
  89. {
  90. m_pointer = value;
  91. return *this;
  92. }
  93. BoundsCheckedPointer& operator+=(ptrdiff_t amount)
  94. {
  95. m_pointer += amount;
  96. return *this;
  97. }
  98. BoundsCheckedPointer& operator-=(ptrdiff_t amount)
  99. {
  100. m_pointer -= amount;
  101. return *this;
  102. }
  103. BoundsCheckedPointer operator+(ptrdiff_t amount) const
  104. {
  105. BoundsCheckedPointer result = *this;
  106. result.m_pointer += amount;
  107. return result;
  108. }
  109. BoundsCheckedPointer operator-(ptrdiff_t amount) const
  110. {
  111. BoundsCheckedPointer result = *this;
  112. result.m_pointer -= amount;
  113. return result;
  114. }
  115. BoundsCheckedPointer operator++() // prefix
  116. {
  117. m_pointer++;
  118. return *this;
  119. }
  120. BoundsCheckedPointer operator--() // prefix
  121. {
  122. m_pointer--;
  123. return *this;
  124. }
  125. BoundsCheckedPointer operator++(int) // postfix
  126. {
  127. BoundsCheckedPointer result = *this;
  128. m_pointer++;
  129. return result;
  130. }
  131. BoundsCheckedPointer operator--(int) // postfix
  132. {
  133. BoundsCheckedPointer result = *this;
  134. m_pointer--;
  135. return result;
  136. }
  137. bool operator<(T* other) const
  138. {
  139. return m_pointer < other;
  140. }
  141. bool operator<=(T* other) const
  142. {
  143. return m_pointer <= other;
  144. }
  145. bool operator>(T* other) const
  146. {
  147. return m_pointer > other;
  148. }
  149. bool operator>=(T* other) const
  150. {
  151. return m_pointer >= other;
  152. }
  153. bool operator==(T* other) const
  154. {
  155. return m_pointer == other;
  156. }
  157. bool operator!=(T* other) const
  158. {
  159. return m_pointer != other;
  160. }
  161. bool operator<(BoundsCheckedPointer other) const
  162. {
  163. return m_pointer < other.m_pointer;
  164. }
  165. bool operator<=(BoundsCheckedPointer other) const
  166. {
  167. return m_pointer <= other.m_pointer;
  168. }
  169. bool operator>(BoundsCheckedPointer other) const
  170. {
  171. return m_pointer > other.m_pointer;
  172. }
  173. bool operator>=(BoundsCheckedPointer other) const
  174. {
  175. return m_pointer >= other.m_pointer;
  176. }
  177. bool operator==(BoundsCheckedPointer other) const
  178. {
  179. return m_pointer == other.m_pointer;
  180. }
  181. bool operator!=(BoundsCheckedPointer other) const
  182. {
  183. return m_pointer != other.m_pointer;
  184. }
  185. BoundsCheckedPointer operator!()
  186. {
  187. return !m_pointer;
  188. }
  189. T* get()
  190. {
  191. return m_pointer;
  192. }
  193. T& operator*()
  194. {
  195. validate();
  196. return *m_pointer;
  197. }
  198. const T& operator*() const
  199. {
  200. validate();
  201. return *m_pointer;
  202. }
  203. T& operator[](ptrdiff_t index)
  204. {
  205. validate(m_pointer + index);
  206. return m_pointer[index];
  207. }
  208. const T& operator[](ptrdiff_t index) const
  209. {
  210. validate(m_pointer + index);
  211. return m_pointer[index];
  212. }
  213. // The only thing this has in common with strcat() is that it
  214. // keeps appending from the given pointer until reaching 0.
  215. BoundsCheckedPointer& strcat(const T* source)
  216. {
  217. while (*source)
  218. *(*this)++ = *source++;
  219. return *this;
  220. }
  221. private:
  222. void validate(T* pointer) const
  223. {
  224. ASSERT_UNUSED(pointer, pointer >= m_begin);
  225. // This guard is designed to protect against the misaligned case.
  226. // A simple pointer < m_end would miss the case if, for example,
  227. // T = int16_t and pointer is 1 byte less than m_end.
  228. ASSERT_UNUSED(pointer, pointer + 1 <= m_end);
  229. }
  230. void validate() const
  231. {
  232. validate(m_pointer);
  233. }
  234. T* m_pointer;
  235. #if !ASSERT_DISABLED
  236. T* m_begin;
  237. T* m_end;
  238. #endif
  239. };
  240. } // namespace WTF
  241. using WTF::BoundsCheckedPointer;
  242. #endif // WTF_BoundsCheckedPointer_h