2 * Copyright 2014 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef FOLLY_IO_TYPEDIOBUF_H_
18 #define FOLLY_IO_TYPEDIOBUF_H_
22 #include <type_traits>
24 #include "folly/Malloc.h"
25 #include "folly/io/IOBuf.h"
30 * Wrapper class to handle a IOBuf as a typed buffer (to a standard layout
33 * This class punts on alignment, and assumes that you know what you're doing.
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.
41 static_assert(std::is_standard_layout<T>::value, "must be standard layout");
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;
50 explicit TypedIOBuf(IOBuf* buf) : buf_(buf) { }
55 const IOBuf* ioBuf() const {
62 const T* data() const {
63 return cast(buf_->data());
66 return cast(buf_->writableData());
68 const T* tail() const {
69 return cast(buf_->tail());
72 return cast(buf_->writableTail());
74 uint32_t length() const {
75 return sdiv(buf_->length());
77 uint32_t size() const { return length(); }
79 uint32_t headroom() const {
80 return sdiv(buf_->headroom());
82 uint32_t tailroom() const {
83 return sdiv(buf_->tailroom());
85 const T* buffer() const {
86 return cast(buf_->buffer());
89 return cast(buf_->writableBuffer());
91 const T* bufferEnd() const {
92 return cast(buf_->bufferEnd());
94 uint32_t capacity() const {
95 return sdiv(buf_->capacity());
97 void advance(uint32_t n) {
98 buf_->advance(smul(n));
100 void retreat(uint32_t n) {
101 buf_->retreat(smul(n));
103 void prepend(uint32_t n) {
104 buf_->prepend(smul(n));
106 void append(uint32_t n) {
107 buf_->append(smul(n));
109 void trimStart(uint32_t n) {
110 buf_->trimStart(smul(n));
112 void trimEnd(uint32_t n) {
113 buf_->trimEnd(smul(n));
118 void reserve(uint32_t minHeadroom, uint32_t minTailroom) {
119 buf_->reserve(smul(minHeadroom), smul(minTailroom));
121 void reserve(uint32_t minTailroom) { reserve(0, minTailroom); }
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(); }
130 const T& front() const {
138 const T& back() const {
148 * Simple wrapper to make it easier to treat this TypedIOBuf as an array of
151 const T& operator[](ssize_t idx) const {
152 assert(idx >= 0 && idx < length());
156 T& operator[](ssize_t idx) {
157 assert(idx >= 0 && idx < length());
158 return writableData()[idx];
162 * Append one element.
164 void push(const T& data) {
165 push(&data, &data + 1);
167 void push_back(const T& data) { push(data); }
170 * Append multiple elements in a sequence; will call distance().
173 void push(IT begin, IT end) {
174 uint32_t n = std::distance(begin, end);
175 if (usingJEMalloc()) {
176 // Rely on rallocm() 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));
182 std::copy(begin, end, writableTail());
187 TypedIOBuf(TypedIOBuf&&) = default;
188 TypedIOBuf& operator=(TypedIOBuf&&) = default;
192 TypedIOBuf(const TypedIOBuf&) = delete;
193 TypedIOBuf& operator=(const TypedIOBuf&) = delete;
196 static T* cast(uint8_t* p) {
197 return reinterpret_cast<T*>(p);
199 static const T* cast(const uint8_t* p) {
200 return reinterpret_cast<const T*>(p);
203 static uint32_t sdiv(uint32_t n) {
204 return n / sizeof(T);
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);
218 #endif /* FOLLY_IO_TYPEDIOBUF_H_ */