9ccb7bc54e30b472b61f3bd5e1f648c933048ae5
[folly.git] / folly / io / TypedIOBuf.h
1 /*
2  * Copyright 2014 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef FOLLY_IO_TYPEDIOBUF_H_
18 #define FOLLY_IO_TYPEDIOBUF_H_
19
20 #include <algorithm>
21 #include <iterator>
22 #include <type_traits>
23
24 #include <folly/Malloc.h>
25 #include <folly/io/IOBuf.h>
26
27 namespace folly {
28
29 /**
30  * Wrapper class to handle a IOBuf as a typed buffer (to a standard layout
31  * class).
32  *
33  * This class punts on alignment, and assumes that you know what you're doing.
34  *
35  * All methods are wrappers around the corresponding IOBuf methods.  The
36  * TypedIOBuf object is stateless, so it's perfectly okay to access the
37  * underlying IOBuf in between TypedIOBuf method calls.
38  */
39 template <class T>
40 class TypedIOBuf {
41   static_assert(std::is_standard_layout<T>::value, "must be standard layout");
42  public:
43   typedef T value_type;
44   typedef value_type& reference;
45   typedef const value_type& const_reference;
46   typedef uint32_t size_type;
47   typedef value_type* iterator;
48   typedef const value_type* const_iterator;
49
50   explicit TypedIOBuf(IOBuf* buf) : buf_(buf) { }
51
52   IOBuf* ioBuf() {
53     return buf_;
54   }
55   const IOBuf* ioBuf() const {
56     return buf_;
57   }
58
59   bool empty() const {
60     return buf_->empty();
61   }
62   const T* data() const {
63     return cast(buf_->data());
64   }
65   T* writableData() {
66     return cast(buf_->writableData());
67   }
68   const T* tail() const {
69     return cast(buf_->tail());
70   }
71   T* writableTail() {
72     return cast(buf_->writableTail());
73   }
74   uint32_t length() const {
75     return sdiv(buf_->length());
76   }
77   uint32_t size() const { return length(); }
78
79   uint32_t headroom() const {
80     return sdiv(buf_->headroom());
81   }
82   uint32_t tailroom() const {
83     return sdiv(buf_->tailroom());
84   }
85   const T* buffer() const {
86     return cast(buf_->buffer());
87   }
88   T* writableBuffer() {
89     return cast(buf_->writableBuffer());
90   }
91   const T* bufferEnd() const {
92     return cast(buf_->bufferEnd());
93   }
94   uint32_t capacity() const {
95     return sdiv(buf_->capacity());
96   }
97   void advance(uint32_t n) {
98     buf_->advance(smul(n));
99   }
100   void retreat(uint32_t n) {
101     buf_->retreat(smul(n));
102   }
103   void prepend(uint32_t n) {
104     buf_->prepend(smul(n));
105   }
106   void append(uint32_t n) {
107     buf_->append(smul(n));
108   }
109   void trimStart(uint32_t n) {
110     buf_->trimStart(smul(n));
111   }
112   void trimEnd(uint32_t n) {
113     buf_->trimEnd(smul(n));
114   }
115   void clear() {
116     buf_->clear();
117   }
118   void reserve(uint32_t minHeadroom, uint32_t minTailroom) {
119     buf_->reserve(smul(minHeadroom), smul(minTailroom));
120   }
121   void reserve(uint32_t minTailroom) { reserve(0, minTailroom); }
122
123   const T* cbegin() const { return data(); }
124   const T* cend() const { return tail(); }
125   const T* begin() const { return cbegin(); }
126   const T* end() const { return cend(); }
127   T* begin() { return writableData(); }
128   T* end() { return writableTail(); }
129
130   const T& front() const {
131     assert(!empty());
132     return *begin();
133   }
134   T& front() {
135     assert(!empty());
136     return *begin();
137   }
138   const T& back() const {
139     assert(!empty());
140     return end()[-1];
141   }
142   T& back() {
143     assert(!empty());
144     return end()[-1];
145   }
146
147   /**
148    * Simple wrapper to make it easier to treat this TypedIOBuf as an array of
149    * T.
150    */
151   const T& operator[](ssize_t idx) const {
152     assert(idx >= 0 && idx < length());
153     return data()[idx];
154   }
155
156   T& operator[](ssize_t idx) {
157     assert(idx >= 0 && idx < length());
158     return writableData()[idx];
159   }
160
161   /**
162    * Append one element.
163    */
164   void push(const T& data) {
165     push(&data, &data + 1);
166   }
167   void push_back(const T& data) { push(data); }
168
169   /**
170    * Append multiple elements in a sequence; will call distance().
171    */
172   template <class IT>
173   void push(IT begin, IT end) {
174     uint32_t n = std::distance(begin, end);
175     if (usingJEMalloc()) {
176       // Rely on xallocx() and avoid exponential growth to limit
177       // amount of memory wasted.
178       reserve(headroom(), n);
179     } else if (tailroom() < n) {
180       reserve(headroom(), std::max(n, 3 + size() / 2));
181     }
182     std::copy(begin, end, writableTail());
183     append(n);
184   }
185
186   // Movable
187   TypedIOBuf(TypedIOBuf&&) = default;
188   TypedIOBuf& operator=(TypedIOBuf&&) = default;
189
190  private:
191   // Non-copyable
192   TypedIOBuf(const TypedIOBuf&) = delete;
193   TypedIOBuf& operator=(const TypedIOBuf&) = delete;
194
195   // cast to T*
196   static T* cast(uint8_t* p) {
197     return reinterpret_cast<T*>(p);
198   }
199   static const T* cast(const uint8_t* p) {
200     return reinterpret_cast<const T*>(p);
201   }
202   // divide by size
203   static uint32_t sdiv(uint32_t n) {
204     return n / sizeof(T);
205   }
206   // multiply by size
207   static uint32_t smul(uint32_t n) {
208     // In debug mode, check for overflow
209     assert((uint64_t(n) * sizeof(T)) < (uint64_t(1) << 32));
210     return n * sizeof(T);
211   }
212
213   IOBuf* buf_;
214 };
215
216 }  // namespace folly
217
218 #endif /* FOLLY_IO_TYPEDIOBUF_H_ */
219