buffer_view.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_ZUCCHINI_BUFFER_VIEW_H_
  5. #define COMPONENTS_ZUCCHINI_BUFFER_VIEW_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <algorithm>
  9. #include <type_traits>
  10. #include "base/check_op.h"
  11. #include "components/zucchini/algorithm.h"
  12. namespace zucchini {
  13. // Describes a region within a buffer, with starting offset and size.
  14. struct BufferRegion {
  15. // The region data are stored as |offset| and |size|, but often it is useful
  16. // to represent it as an interval [lo(), hi()) = [offset, offset + size).
  17. size_t lo() const { return offset; }
  18. size_t hi() const { return offset + size; }
  19. // Returns whether the Region fits in |[0, container_size)|. Special case:
  20. // a size-0 region starting at |container_size| fits.
  21. bool FitsIn(size_t container_size) const {
  22. return offset <= container_size && container_size - offset >= size;
  23. }
  24. // Returns |v| clipped to the inclusive range |[lo(), hi()]|.
  25. size_t InclusiveClamp(size_t v) const {
  26. return zucchini::InclusiveClamp(v, lo(), hi());
  27. }
  28. // Region data use size_t to match BufferViewBase::size_type, to make it
  29. // convenient to index into buffer view.
  30. size_t offset;
  31. size_t size;
  32. };
  33. namespace internal {
  34. // TODO(huangs): Rename to BasicBufferView.
  35. // BufferViewBase should not be used directly; it is an implementation used for
  36. // both BufferView and MutableBufferView.
  37. template <class T>
  38. class BufferViewBase {
  39. public:
  40. using value_type = T;
  41. using reference = T&;
  42. using pointer = T*;
  43. using iterator = T*;
  44. using const_iterator = typename std::add_const<T>::type*;
  45. using size_type = std::size_t;
  46. using difference_type = std::ptrdiff_t;
  47. static BufferViewBase FromRange(iterator first, iterator last) {
  48. DCHECK_GE(last, first);
  49. BufferViewBase ret;
  50. ret.first_ = first;
  51. ret.last_ = last;
  52. return ret;
  53. }
  54. BufferViewBase() = default;
  55. BufferViewBase(iterator first, size_type size)
  56. : first_(first), last_(first_ + size) {
  57. DCHECK_GE(last_, first_);
  58. }
  59. template <class U>
  60. BufferViewBase(const BufferViewBase<U>& that)
  61. : first_(that.begin()), last_(that.end()) {}
  62. template <class U>
  63. BufferViewBase(BufferViewBase<U>&& that)
  64. : first_(that.begin()), last_(that.end()) {}
  65. BufferViewBase(const BufferViewBase&) = default;
  66. BufferViewBase& operator=(const BufferViewBase&) = default;
  67. // Iterators
  68. iterator begin() const { return first_; }
  69. iterator end() const { return last_; }
  70. const_iterator cbegin() const { return begin(); }
  71. const_iterator cend() const { return end(); }
  72. // Capacity
  73. bool empty() const { return first_ == last_; }
  74. size_type size() const { return last_ - first_; }
  75. // Returns whether the buffer is large enough to cover |region|.
  76. bool covers(const BufferRegion& region) const {
  77. return region.FitsIn(size());
  78. }
  79. // Returns whether the buffer is large enough to cover an array starting at
  80. // |offset| with |num| elements, each taking |elt_size| bytes.
  81. bool covers_array(size_t offset, size_t num, size_t elt_size) {
  82. DCHECK_GT(elt_size, 0U);
  83. // Use subtraction and division to avoid overflow.
  84. return offset <= size() && (size() - offset) / elt_size >= num;
  85. }
  86. // Element access
  87. // Returns the raw value at specified location |pos|.
  88. // If |pos| is not within the range of the buffer, the process is terminated.
  89. reference operator[](size_type pos) const {
  90. CHECK_LT(pos, size());
  91. return first_[pos];
  92. }
  93. // Returns a sub-buffer described by |region|.
  94. BufferViewBase operator[](BufferRegion region) const {
  95. DCHECK_LE(region.offset, size());
  96. DCHECK_LE(region.size, size() - region.offset);
  97. return {begin() + region.offset, region.size};
  98. }
  99. template <class U>
  100. const U& read(size_type pos) const {
  101. // TODO(huangs): Use can_access<U>(pos) after fixing can_access().
  102. CHECK_LE(sizeof(U), size());
  103. CHECK_LE(pos, size() - sizeof(U));
  104. return *reinterpret_cast<const U*>(begin() + pos);
  105. }
  106. template <class U>
  107. void write(size_type pos, const U& value) {
  108. // TODO(huangs): Use can_access<U>(pos) after fixing can_access().
  109. CHECK_LE(sizeof(U), size());
  110. CHECK_LE(pos, size() - sizeof(U));
  111. *reinterpret_cast<U*>(begin() + pos) = value;
  112. }
  113. // Returns a mutable reference to an object type U whose raw storage starts
  114. // at location |pos|.
  115. template <class U>
  116. U& modify(size_type pos) {
  117. // TODO(huangs): Use can_access<U>(pos) after fixing can_access().
  118. CHECK_LE(sizeof(U), size());
  119. CHECK_LE(pos, size() - sizeof(U));
  120. return *reinterpret_cast<U*>(begin() + pos);
  121. }
  122. template <class U>
  123. bool can_access(size_type pos) const {
  124. return pos < size() && size() - pos >= sizeof(U);
  125. }
  126. // Returns a BufferRegion describing the full view, with offset = 0. If the
  127. // BufferViewBase is derived from another, this does *not* return the
  128. // original region used for its definition (hence "local").
  129. BufferRegion local_region() const { return BufferRegion{0, size()}; }
  130. bool equals(BufferViewBase other) const {
  131. return size() == other.size() && std::equal(begin(), end(), other.begin());
  132. }
  133. // Modifiers
  134. void shrink(size_type new_size) {
  135. DCHECK_LE(first_ + new_size, last_);
  136. last_ = first_ + new_size;
  137. }
  138. // Moves the start of the view forward by n bytes.
  139. void remove_prefix(size_type n) {
  140. DCHECK_LE(n, size());
  141. first_ += n;
  142. }
  143. // Moves the start of the view to |it|, which is in range [begin(), end()).
  144. void seek(iterator it) {
  145. DCHECK_GE(it, begin());
  146. DCHECK_LE(it, end());
  147. first_ = it;
  148. }
  149. // Given |origin| that contains |*this|, minimally increase |first_| (possibly
  150. // by 0) so that |first_ <= last_|, and |first_ - origin.first_| is a multiple
  151. // of |alignment|. On success, updates |first_| and returns true. Otherwise
  152. // returns false.
  153. bool AlignOn(BufferViewBase origin, size_type alignment) {
  154. DCHECK_GT(alignment, 0U);
  155. DCHECK_LE(origin.first_, first_);
  156. DCHECK_GE(origin.last_, last_);
  157. size_type aligned_size =
  158. AlignCeil(static_cast<size_type>(first_ - origin.first_), alignment);
  159. if (aligned_size > static_cast<size_type>(last_ - origin.first_))
  160. return false;
  161. first_ = origin.first_ + aligned_size;
  162. return true;
  163. }
  164. private:
  165. iterator first_ = nullptr;
  166. iterator last_ = nullptr;
  167. };
  168. } // namespace internal
  169. // Classes to encapsulate a contiguous sequence of raw data, without owning the
  170. // encapsulated memory regions. These are intended to be used as value types.
  171. using ConstBufferView = internal::BufferViewBase<const uint8_t>;
  172. using MutableBufferView = internal::BufferViewBase<uint8_t>;
  173. } // namespace zucchini
  174. #endif // COMPONENTS_ZUCCHINI_BUFFER_VIEW_H_