@@ -29,7 +29,9 @@ typedef std::function<void(int status)> done_cb;
2929// Default non-op done handler.
3030inline void default_quic_buffer_chunk_done (int status) {}
3131
32- #define EMPTY_BUF (buf ) (buf.len == 0 || buf.base == nullptr )
32+ inline bool IsEmptyBuffer (const uv_buf_t & buf) {
33+ return buf.len == 0 || buf.base == nullptr ;
34+ }
3335
3436// A quic_buffer_chunk contains the actual buffered data
3537// along with a callback, and optional V8 object that
@@ -44,40 +46,36 @@ struct quic_buffer_chunk : public MemoryRetainer {
4446 v8::Global<v8::Object> keep_alive;
4547 std::unique_ptr<quic_buffer_chunk> next;
4648
47- inline quic_buffer_chunk (
48- MallocedBuffer<uint8_t >&& buf_,
49- done_cb done_,
50- v8::Local<v8::Object> keep_alive_)
51- : data_buf(std::move(buf_)),
52- buf(uv_buf_init(
53- reinterpret_cast <char *>(data_buf.data),
54- data_buf.size)),
55- done(done_) {
56- if (!keep_alive.IsEmpty ())
57- keep_alive.Reset (keep_alive_->GetIsolate (), keep_alive_);
49+ quic_buffer_chunk (
50+ MallocedBuffer<uint8_t >&& buf_,
51+ done_cb done_,
52+ v8::Local<v8::Object> keep_alive_)
53+ : quic_buffer_chunk(uv_buf_init(reinterpret_cast <char *>(buf_.data),
54+ buf_.size),
55+ done_,
56+ keep_alive_) {
57+ data_buf = std::move (buf_);
5858 }
5959
60- inline explicit quic_buffer_chunk (
61- uv_buf_t buf_) :
62- buf(buf_) {}
60+ explicit quic_buffer_chunk (uv_buf_t buf_) : buf(buf_) {}
6361
64- inline quic_buffer_chunk (
65- uv_buf_t buf_,
66- done_cb done_,
67- v8::Local<v8::Object> keep_alive_)
68- : buf (buf_),
69- done (done_) {
62+ quic_buffer_chunk (
63+ uv_buf_t buf_,
64+ done_cb done_,
65+ v8::Local<v8::Object> keep_alive_)
66+ : quic_buffer_chunk (buf_) {
67+ done = std::move (done_);
7068 if (!keep_alive.IsEmpty ())
7169 keep_alive.Reset (keep_alive_->GetIsolate (), keep_alive_);
7270 }
7371
74- inline ~quic_buffer_chunk () override {
72+ ~quic_buffer_chunk () override {
7573 CHECK (done_called);
7674 }
7775
7876 void Done (int status) {
7977 done_called = true ;
80- done (status);
78+ std::move ( done) (status);
8179 }
8280
8381 void MemoryInfo (MemoryTracker* tracker) const override {
@@ -125,21 +123,21 @@ struct quic_buffer_chunk : public MemoryRetainer {
125123// Will append the contents of buf1 to buf2, then reset buf1
126124class QuicBuffer : public MemoryRetainer {
127125 public:
128- inline QuicBuffer () :
129- head_(nullptr ),
130- tail_(nullptr ),
131- size_(0 ),
132- count_(0 ),
133- length_(0 ),
134- rlength_(0 ) {}
135-
136- inline QuicBuffer (QuicBuffer&& src) noexcept :
137- head_(src.head_),
138- tail_(src.tail_),
139- size_(src.size_),
140- count_(src.count_),
141- length_(src.length_),
142- rlength_(src.rlength_) {
126+ QuicBuffer ()
127+ : head_(nullptr ),
128+ tail_ (nullptr ),
129+ size_(0 ),
130+ count_(0 ),
131+ length_(0 ),
132+ rlength_(0 ) {}
133+
134+ QuicBuffer (QuicBuffer&& src) noexcept
135+ : head_(src.head_),
136+ tail_(src.tail_),
137+ size_(src.size_),
138+ count_(src.count_),
139+ length_(src.length_),
140+ rlength_(src.rlength_) {
143141 root_ = std::move (src.root_ );
144142 src.head_ = nullptr ;
145143 src.tail_ = nullptr ;
@@ -149,44 +147,42 @@ class QuicBuffer : public MemoryRetainer {
149147 }
150148
151149 QuicBuffer& operator =(QuicBuffer&& src) noexcept {
150+ if (this == &src) return *this ;
152151 this ->~QuicBuffer ();
153152 return *new (this ) QuicBuffer (std::move (src));
154153 }
155154
156155 QuicBuffer& operator +=(QuicBuffer&& src) noexcept {
157- if (! tail_) {
156+ if (tail_ == nullptr ) {
158157 // If this thing is empty, just do a move...
159- this ->~QuicBuffer ();
160- return *new (this ) QuicBuffer (std::move (src));
161- } else {
162- tail_->next = std::move (src.root_ );
163- // If head_ is null, then it had been read to the
164- // end, set the new head_ equal to the appended
165- // root.
166- if (head_ == nullptr )
167- head_ = tail_->next .get ();
168- tail_ = src.tail_ ;
169- length_ += src.length_ ;
170- rlength_ += src.length_ ;
171- size_ += src.size_ ;
172- count_ += src.size_ ;
173- src.head_ = nullptr ;
174- src.tail_ = nullptr ;
175- src.size_ = 0 ;
176- src.length_ = 0 ;
177- src.rlength_ = 0 ;
178- return *this ;
158+ return *this = std::move (src);
179159 }
160+
161+ tail_->next = std::move (src.root_ );
162+ // If head_ is null, then it had been read to the
163+ // end, set the new head_ equal to the appended
164+ // root.
165+ if (head_ == nullptr )
166+ head_ = tail_->next .get ();
167+ tail_ = src.tail_ ;
168+ length_ += src.length_ ;
169+ rlength_ += src.length_ ;
170+ size_ += src.size_ ;
171+ count_ += src.size_ ;
172+ src.head_ = nullptr ;
173+ src.tail_ = nullptr ;
174+ src.size_ = 0 ;
175+ src.length_ = 0 ;
176+ src.rlength_ = 0 ;
177+ return *this ;
180178 }
181179
182- inline ~QuicBuffer () override {
180+ ~QuicBuffer () override {
183181 Cancel (); // Cancel the remaining data
184182 CHECK_EQ (length_, 0 );
185183 }
186184
187- inline size_t Copy (
188- uv_buf_t * bufs,
189- size_t nbufs) {
185+ size_t Copy (uv_buf_t * bufs, size_t nbufs) {
190186 size_t total = 0 ;
191187 for (size_t n = 0 ; n < nbufs; n++) {
192188 MallocedBuffer<uint8_t > data (bufs[n].len );
@@ -203,21 +199,19 @@ class QuicBuffer : public MemoryRetainer {
203199 // of the internal linked list. The keep_alive allows a reference to a
204200 // JS object to be kept around until the final uv_buf_t
205201 // is consumed.
206- inline size_t Push (
202+ size_t Push (
207203 uv_buf_t * bufs,
208204 size_t nbufs,
209205 done_cb done = default_quic_buffer_chunk_done,
210206 v8::Local<v8::Object> keep_alive = v8::Local<v8::Object>()) {
211207 size_t len = 0 ;
212- if (nbufs == 0 ||
213- bufs == nullptr ||
214- EMPTY_BUF (bufs[0 ])) {
208+ if (nbufs == 0 || bufs == nullptr || IsEmptyBuffer (bufs[0 ])) {
215209 done (0 );
216210 return 0 ;
217211 }
218212 size_t n = 0 ;
219213 while (nbufs > 1 ) {
220- if (!EMPTY_BUF (bufs[n])) {
214+ if (!IsEmptyBuffer (bufs[n])) {
221215 Push (bufs[n]);
222216 length_ += bufs[n].len ;
223217 rlength_ += bufs[n].len ;
@@ -238,7 +232,7 @@ class QuicBuffer : public MemoryRetainer {
238232 // and popped out of the internal linked list. The keep_alive allows a
239233 // reference to a JS object to be kept around until the
240234 // final uv_buf_t is consumed.
241- inline size_t Push (
235+ size_t Push (
242236 MallocedBuffer<uint8_t >&& buffer,
243237 done_cb done = default_quic_buffer_chunk_done,
244238 v8::Local<v8::Object> keep_alive = v8::Local<v8::Object>()) {
@@ -255,84 +249,52 @@ class QuicBuffer : public MemoryRetainer {
255249 // Consume the given number of bytes within the buffer. If amount is
256250 // negative, all buffered bytes that are available to be consumed are
257251 // consumed.
258- inline void Consume (ssize_t amount = -1 ) { Consume (0 , amount); }
252+ void Consume (ssize_t amount = -1 ) { Consume (0 , amount); }
259253
260254 // Cancels the remaining bytes within the buffer
261- inline size_t Cancel (int status = UV_ECANCELED) {
255+ size_t Cancel (int status = UV_ECANCELED) {
262256 size_t remaining = Length ();
263257 Consume (status, -1 );
264258 return remaining;
265259 }
266260
267261 // The total buffered bytes
268- inline size_t Length () {
262+ size_t Length () {
269263 return length_;
270264 }
271265
272- inline size_t RemainingLength () {
266+ size_t RemainingLength () {
273267 return rlength_;
274268 }
275269
276270 // The total number of buffers
277- inline size_t Size () {
271+ size_t Size () {
278272 return size_;
279273 }
280274
281275 // The number of buffers remaining to be read
282- inline size_t ReadRemaining () {
276+ size_t ReadRemaining () {
283277 return count_;
284278 }
285279
286280 // Drain the remaining buffers into the given vector.
287281 // The function will return the number of positions the
288282 // read head_ can be advanced.
289- inline size_t DrainInto (
290- std::vector<uv_buf_t >* list,
291- size_t * length = nullptr ) {
292- size_t len = 0 ;
293- bool seen_head = false ;
294- quic_buffer_chunk* pos = head_;
295- if (pos == nullptr )
296- return 0 ;
297- if (length != nullptr ) *length = 0 ;
298- while (pos != nullptr ) {
299- size_t datalen = pos->buf .len - pos->roffset ;
300- if (length != nullptr ) *length += datalen;
301- list->push_back (
302- uv_buf_init (pos->buf .base + pos->roffset , datalen));
303- if (pos == head_) seen_head = true ;
304- if (seen_head) len++;
305- pos = pos->next .get ();
306- }
307- return len;
283+ size_t DrainInto (std::vector<uv_buf_t >* list, size_t * length = nullptr ) {
284+ return DrainInto ([&](uv_buf_t buf) { list->push_back (buf); }, length);
308285 }
309286
310- inline size_t DrainInto (
311- std::vector<ngtcp2_vec>* list,
312- size_t * length = nullptr ) {
313- size_t len = 0 ;
314- bool seen_head = false ;
315- quic_buffer_chunk* pos = head_;
316- if (pos == nullptr )
317- return 0 ;
318- if (length != nullptr ) *length = 0 ;
319- while (pos != nullptr ) {
320- size_t datalen = pos->buf .len - pos->roffset ;
321- if (length != nullptr ) *length += datalen;
322- list->push_back (ngtcp2_vec{
323- reinterpret_cast <uint8_t *>(pos->buf .base ) + pos->roffset ,
324- datalen});
325- if (pos == head_) seen_head = true ;
326- if (seen_head) len++;
327- pos = pos->next .get ();
328- }
329- return len;
287+ size_t DrainInto (std::vector<ngtcp2_vec>* list, size_t * length = nullptr ) {
288+ return DrainInto ([&](uv_buf_t buf) {
289+ list->push_back (ngtcp2_vec {
290+ reinterpret_cast <uint8_t *>(buf.base ), buf.len });
291+ }, length);
330292 }
331293
332294 // Returns the current read head or an empty buffer if
333295 // we're empty
334- inline uv_buf_t Head () {
335- if (! head_)
296+ uv_buf_t Head () {
297+ if (head_ == nullptr )
336298 return uv_buf_init (nullptr , 0 );
337299 return uv_buf_init (
338300 head_->buf .base + head_->roffset ,
@@ -343,20 +305,20 @@ class QuicBuffer : public MemoryRetainer {
343305 // number of buffers. If amount is greater than
344306 // the number of buffers remaining, move to the
345307 // end, and return the actual number advanced.
346- inline size_t SeekHead (size_t amount = 1 ) {
308+ size_t SeekHead (size_t amount = 1 ) {
347309 size_t n = 0 ;
348310 size_t amt = amount;
349- while (head_ && amt > 0 ) {
311+ while (head_ != nullptr && amt > 0 ) {
350312 head_ = head_->next .get ();
351313 n++;
352314 amt--;
353315 count_--;
354- rlength_ -= ! head_ ? 0 : head_->buf .len ;
316+ rlength_ -= head_ == nullptr ? 0 : head_->buf .len ;
355317 }
356318 return n;
357319 }
358320
359- inline void SeekHeadOffset (ssize_t amount) {
321+ void SeekHeadOffset (ssize_t amount) {
360322 if (amount < 0 )
361323 return ;
362324 size_t amt = std::min (amount < 0 ? length_ : amount, length_);
@@ -384,7 +346,27 @@ class QuicBuffer : public MemoryRetainer {
384346 SET_SELF_SIZE (QuicBuffer);
385347
386348 private:
387- inline void Push (quic_buffer_chunk* chunk) {
349+ template <typename Fn>
350+ size_t DrainInto (Fn&& add_to_list, size_t * length) {
351+ size_t len = 0 ;
352+ bool seen_head = false ;
353+ quic_buffer_chunk* pos = head_;
354+ if (pos == nullptr )
355+ return 0 ;
356+ if (length != nullptr ) *length = 0 ;
357+ while (pos != nullptr ) {
358+ size_t datalen = pos->buf .len - pos->roffset ;
359+ if (length != nullptr ) *length += datalen;
360+ add_to_list (uv_buf_init (pos->buf .base + pos->roffset , datalen));
361+ if (pos == head_) seen_head = true ;
362+ if (seen_head) len++;
363+ pos = pos->next .get ();
364+ }
365+ return len;
366+ }
367+
368+
369+ void Push (quic_buffer_chunk* chunk) {
388370 size_++;
389371 count_++;
390372 if (!tail_) {
@@ -398,18 +380,15 @@ class QuicBuffer : public MemoryRetainer {
398380 }
399381 }
400382
401- inline void Push (uv_buf_t buf) {
383+ void Push (uv_buf_t buf) {
402384 Push (new quic_buffer_chunk (buf));
403385 }
404386
405- inline void Push (
406- uv_buf_t buf,
407- done_cb done,
408- v8::Local<v8::Object> keep_alive) {
387+ void Push (uv_buf_t buf, done_cb done, v8::Local<v8::Object> keep_alive) {
409388 Push (new quic_buffer_chunk (buf, done, keep_alive));
410389 }
411390
412- inline bool Pop (int status = 0 ) {
391+ bool Pop (int status = 0 ) {
413392 if (!root_)
414393 return false ;
415394 std::unique_ptr<quic_buffer_chunk> root (std::move (root_));
@@ -425,7 +404,7 @@ class QuicBuffer : public MemoryRetainer {
425404 return true ;
426405 }
427406
428- inline void Consume (int status, ssize_t amount) {
407+ void Consume (int status, ssize_t amount) {
429408 size_t amt = std::min (amount < 0 ? length_ : amount, length_);
430409 while (root_ && amt > 0 ) {
431410 auto root = root_.get ();
0 commit comments