exponential capacity growth for io::TypedIOBuf
[folly.git] / folly / io / TypedIOBuf.h
1 /*
2  * Copyright 2013 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   /**
157    * Append one element.
158    */
159   void push(const T& data) {
160     push(&data, &data + 1);
161   }
162   void push_back(const T& data) { push(data); }
163
164   /**
165    * Append multiple elements in a sequence; will call distance().
166    */
167   template <class IT>
168   void push(IT begin, IT end) {
169     uint32_t n = std::distance(begin, end);
170     if (usingJEMalloc()) {
171       // Rely on rallocm() and avoid exponential growth to limit
172       // amount of memory wasted.
173       reserve(headroom(), n);
174     } else if (tailroom() < n) {
175       reserve(headroom(), std::max(n, 3 + size() / 2));
176     }
177     std::copy(begin, end, writableTail());
178     append(n);
179   }
180
181   // Movable
182   TypedIOBuf(TypedIOBuf&&) = default;
183   TypedIOBuf& operator=(TypedIOBuf&&) = default;
184
185  private:
186   // Non-copyable
187   TypedIOBuf(const TypedIOBuf&) = delete;
188   TypedIOBuf& operator=(const TypedIOBuf&) = delete;
189
190   // cast to T*
191   static T* cast(uint8_t* p) {
192     return reinterpret_cast<T*>(p);
193   }
194   static const T* cast(const uint8_t* p) {
195     return reinterpret_cast<const T*>(p);
196   }
197   // divide by size
198   static uint32_t sdiv(uint32_t n) {
199     return n / sizeof(T);
200   }
201   // multiply by size
202   static uint32_t smul(uint32_t n) {
203     // In debug mode, check for overflow
204     assert((uint64_t(n) * sizeof(T)) < (uint64_t(1) << 32));
205     return n * sizeof(T);
206   }
207
208   IOBuf* buf_;
209 };
210
211 }  // namespace folly
212
213 #endif /* FOLLY_IO_TYPEDIOBUF_H_ */
214