• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2014 Tatsuhiro Tsujikawa
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 #ifndef MEMCHUNK_H
26 #define MEMCHUNK_H
27 
28 #include "nghttp2_config.h"
29 
30 #include <limits.h>
31 #ifdef _WIN32
32 /* Structure for scatter/gather I/O.  */
33 struct iovec {
34   void *iov_base; /* Pointer to data.  */
35   size_t iov_len; /* Length of data.  */
36 };
37 #else // !_WIN32
38 #  include <sys/uio.h>
39 #endif // !_WIN32
40 
41 #include <cassert>
42 #include <cstring>
43 #include <memory>
44 #include <array>
45 #include <algorithm>
46 #include <string>
47 #include <utility>
48 
49 #include "template.h"
50 
51 namespace nghttp2 {
52 
53 #define DEFAULT_WR_IOVCNT 16
54 
55 #if defined(IOV_MAX) && IOV_MAX < DEFAULT_WR_IOVCNT
56 #  define MAX_WR_IOVCNT IOV_MAX
57 #else // !defined(IOV_MAX) || IOV_MAX >= DEFAULT_WR_IOVCNT
58 #  define MAX_WR_IOVCNT DEFAULT_WR_IOVCNT
59 #endif // !defined(IOV_MAX) || IOV_MAX >= DEFAULT_WR_IOVCNT
60 
61 template <size_t N> struct Memchunk {
MemchunkMemchunk62   Memchunk(Memchunk *next_chunk)
63       : pos(std::begin(buf)), last(pos), knext(next_chunk), next(nullptr) {}
lenMemchunk64   size_t len() const { return last - pos; }
leftMemchunk65   size_t left() const { return std::end(buf) - last; }
resetMemchunk66   void reset() { pos = last = std::begin(buf); }
67   std::array<uint8_t, N> buf;
68   uint8_t *pos, *last;
69   Memchunk *knext;
70   Memchunk *next;
71   static const size_t size = N;
72 };
73 
74 template <typename T> struct Pool {
PoolPool75   Pool() : pool(nullptr), freelist(nullptr), poolsize(0), freelistsize(0) {}
~PoolPool76   ~Pool() { clear(); }
getPool77   T *get() {
78     if (freelist) {
79       auto m = freelist;
80       freelist = freelist->next;
81       m->next = nullptr;
82       m->reset();
83       freelistsize -= T::size;
84       return m;
85     }
86 
87     pool = new T{pool};
88     poolsize += T::size;
89     return pool;
90   }
recyclePool91   void recycle(T *m) {
92     m->next = freelist;
93     freelist = m;
94     freelistsize += T::size;
95   }
clearPool96   void clear() {
97     freelist = nullptr;
98     freelistsize = 0;
99     for (auto p = pool; p;) {
100       auto knext = p->knext;
101       delete p;
102       p = knext;
103     }
104     pool = nullptr;
105     poolsize = 0;
106   }
107   using value_type = T;
108   T *pool;
109   T *freelist;
110   size_t poolsize;
111   size_t freelistsize;
112 };
113 
114 template <typename Memchunk> struct Memchunks {
MemchunksMemchunks115   Memchunks(Pool<Memchunk> *pool)
116       : pool(pool), head(nullptr), tail(nullptr), len(0) {}
117   Memchunks(const Memchunks &) = delete;
MemchunksMemchunks118   Memchunks(Memchunks &&other) noexcept
119       : pool{other.pool}, // keep other.pool
120         head{std::exchange(other.head, nullptr)},
121         tail{std::exchange(other.tail, nullptr)},
122         len{std::exchange(other.len, 0)} {}
123   Memchunks &operator=(const Memchunks &) = delete;
124   Memchunks &operator=(Memchunks &&other) noexcept {
125     if (this == &other) {
126       return *this;
127     }
128 
129     reset();
130 
131     pool = other.pool;
132     head = std::exchange(other.head, nullptr);
133     tail = std::exchange(other.tail, nullptr);
134     len = std::exchange(other.len, 0);
135 
136     return *this;
137   }
~MemchunksMemchunks138   ~Memchunks() {
139     if (!pool) {
140       return;
141     }
142     for (auto m = head; m;) {
143       auto next = m->next;
144       pool->recycle(m);
145       m = next;
146     }
147   }
appendMemchunks148   size_t append(char c) {
149     if (!tail) {
150       head = tail = pool->get();
151     } else if (tail->left() == 0) {
152       tail->next = pool->get();
153       tail = tail->next;
154     }
155     *tail->last++ = c;
156     ++len;
157     return 1;
158   }
appendMemchunks159   size_t append(const void *src, size_t count) {
160     if (count == 0) {
161       return 0;
162     }
163 
164     auto first = static_cast<const uint8_t *>(src);
165     auto last = first + count;
166 
167     if (!tail) {
168       head = tail = pool->get();
169     }
170 
171     for (;;) {
172       auto n = std::min(static_cast<size_t>(last - first), tail->left());
173       tail->last = std::copy_n(first, n, tail->last);
174       first += n;
175       len += n;
176       if (first == last) {
177         break;
178       }
179 
180       tail->next = pool->get();
181       tail = tail->next;
182     }
183 
184     return count;
185   }
appendMemchunks186   template <size_t N> size_t append(const char (&s)[N]) {
187     return append(s, N - 1);
188   }
appendMemchunks189   size_t append(const std::string &s) { return append(s.c_str(), s.size()); }
appendMemchunks190   size_t append(const StringRef &s) { return append(s.c_str(), s.size()); }
appendMemchunks191   size_t append(const ImmutableString &s) {
192     return append(s.c_str(), s.size());
193   }
copyMemchunks194   size_t copy(Memchunks &dest) {
195     auto m = head;
196     while (m) {
197       dest.append(m->pos, m->len());
198       m = m->next;
199     }
200     return len;
201   }
removeMemchunks202   size_t remove(void *dest, size_t count) {
203     if (!tail || count == 0) {
204       return 0;
205     }
206 
207     auto first = static_cast<uint8_t *>(dest);
208     auto last = first + count;
209 
210     auto m = head;
211 
212     while (m) {
213       auto next = m->next;
214       auto n = std::min(static_cast<size_t>(last - first), m->len());
215 
216       assert(m->len());
217       first = std::copy_n(m->pos, n, first);
218       m->pos += n;
219       len -= n;
220       if (m->len() > 0) {
221         break;
222       }
223       pool->recycle(m);
224       m = next;
225     }
226     head = m;
227     if (head == nullptr) {
228       tail = nullptr;
229     }
230 
231     return first - static_cast<uint8_t *>(dest);
232   }
removeMemchunks233   size_t remove(Memchunks &dest, size_t count) {
234     if (!tail || count == 0) {
235       return 0;
236     }
237 
238     auto left = count;
239     auto m = head;
240 
241     while (m) {
242       auto next = m->next;
243       auto n = std::min(left, m->len());
244 
245       assert(m->len());
246       dest.append(m->pos, n);
247       m->pos += n;
248       len -= n;
249       left -= n;
250       if (m->len() > 0) {
251         break;
252       }
253       pool->recycle(m);
254       m = next;
255     }
256     head = m;
257     if (head == nullptr) {
258       tail = nullptr;
259     }
260 
261     return count - left;
262   }
removeMemchunks263   size_t remove(Memchunks &dest) {
264     assert(pool == dest.pool);
265 
266     if (head == nullptr) {
267       return 0;
268     }
269 
270     auto n = len;
271 
272     if (dest.tail == nullptr) {
273       dest.head = head;
274     } else {
275       dest.tail->next = head;
276     }
277 
278     dest.tail = tail;
279     dest.len += len;
280 
281     head = tail = nullptr;
282     len = 0;
283 
284     return n;
285   }
drainMemchunks286   size_t drain(size_t count) {
287     auto ndata = count;
288     auto m = head;
289     while (m) {
290       auto next = m->next;
291       auto n = std::min(count, m->len());
292       m->pos += n;
293       count -= n;
294       len -= n;
295       if (m->len() > 0) {
296         break;
297       }
298 
299       pool->recycle(m);
300       m = next;
301     }
302     head = m;
303     if (head == nullptr) {
304       tail = nullptr;
305     }
306     return ndata - count;
307   }
riovecMemchunks308   int riovec(struct iovec *iov, int iovcnt) const {
309     if (!head) {
310       return 0;
311     }
312     auto m = head;
313     int i;
314     for (i = 0; i < iovcnt && m; ++i, m = m->next) {
315       iov[i].iov_base = m->pos;
316       iov[i].iov_len = m->len();
317     }
318     return i;
319   }
rleftMemchunks320   size_t rleft() const { return len; }
resetMemchunks321   void reset() {
322     for (auto m = head; m;) {
323       auto next = m->next;
324       pool->recycle(m);
325       m = next;
326     }
327     len = 0;
328     head = tail = nullptr;
329   }
330 
331   Pool<Memchunk> *pool;
332   Memchunk *head, *tail;
333   size_t len;
334 };
335 
336 // Wrapper around Memchunks to offer "peeking" functionality.
337 template <typename Memchunk> struct PeekMemchunks {
PeekMemchunksPeekMemchunks338   PeekMemchunks(Pool<Memchunk> *pool)
339       : memchunks(pool),
340         cur(nullptr),
341         cur_pos(nullptr),
342         cur_last(nullptr),
343         len(0),
344         peeking(true) {}
345   PeekMemchunks(const PeekMemchunks &) = delete;
PeekMemchunksPeekMemchunks346   PeekMemchunks(PeekMemchunks &&other) noexcept
347       : memchunks{std::move(other.memchunks)},
348         cur{std::exchange(other.cur, nullptr)},
349         cur_pos{std::exchange(other.cur_pos, nullptr)},
350         cur_last{std::exchange(other.cur_last, nullptr)},
351         len{std::exchange(other.len, 0)},
352         peeking{std::exchange(other.peeking, true)} {}
353   PeekMemchunks &operator=(const PeekMemchunks &) = delete;
354   PeekMemchunks &operator=(PeekMemchunks &&other) noexcept {
355     if (this == &other) {
356       return *this;
357     }
358 
359     memchunks = std::move(other.memchunks);
360     cur = std::exchange(other.cur, nullptr);
361     cur_pos = std::exchange(other.cur_pos, nullptr);
362     cur_last = std::exchange(other.cur_last, nullptr);
363     len = std::exchange(other.len, 0);
364     peeking = std::exchange(other.peeking, true);
365 
366     return *this;
367   }
appendPeekMemchunks368   size_t append(const void *src, size_t count) {
369     count = memchunks.append(src, count);
370     len += count;
371     return count;
372   }
removePeekMemchunks373   size_t remove(void *dest, size_t count) {
374     if (!peeking) {
375       count = memchunks.remove(dest, count);
376       len -= count;
377       return count;
378     }
379 
380     if (count == 0 || len == 0) {
381       return 0;
382     }
383 
384     if (!cur) {
385       cur = memchunks.head;
386       cur_pos = cur->pos;
387     }
388 
389     // cur_last could be updated in append
390     cur_last = cur->last;
391 
392     if (cur_pos == cur_last) {
393       assert(cur->next);
394       cur = cur->next;
395     }
396 
397     auto first = static_cast<uint8_t *>(dest);
398     auto last = first + count;
399 
400     for (;;) {
401       auto n = std::min(last - first, cur_last - cur_pos);
402 
403       first = std::copy_n(cur_pos, n, first);
404       cur_pos += n;
405       len -= n;
406 
407       if (first == last) {
408         break;
409       }
410       assert(cur_pos == cur_last);
411       if (!cur->next) {
412         break;
413       }
414       cur = cur->next;
415       cur_pos = cur->pos;
416       cur_last = cur->last;
417     }
418     return first - static_cast<uint8_t *>(dest);
419   }
rleftPeekMemchunks420   size_t rleft() const { return len; }
rleft_bufferedPeekMemchunks421   size_t rleft_buffered() const { return memchunks.rleft(); }
disable_peekPeekMemchunks422   void disable_peek(bool drain) {
423     if (!peeking) {
424       return;
425     }
426     if (drain) {
427       auto n = rleft_buffered() - rleft();
428       memchunks.drain(n);
429       assert(len == memchunks.rleft());
430     } else {
431       len = memchunks.rleft();
432     }
433     cur = nullptr;
434     cur_pos = cur_last = nullptr;
435     peeking = false;
436   }
resetPeekMemchunks437   void reset() {
438     memchunks.reset();
439     cur = nullptr;
440     cur_pos = cur_last = nullptr;
441     len = 0;
442     peeking = true;
443   }
444   Memchunks<Memchunk> memchunks;
445   // Pointer to the Memchunk currently we are reading/writing.
446   Memchunk *cur;
447   // Region inside cur, we have processed to cur_pos.
448   uint8_t *cur_pos, *cur_last;
449   // This is the length we have left unprocessed.  len <=
450   // memchunk.rleft() must hold.
451   size_t len;
452   // true if peeking is enabled.  Initially it is true.
453   bool peeking;
454 };
455 
456 using Memchunk16K = Memchunk<16_k>;
457 using MemchunkPool = Pool<Memchunk16K>;
458 using DefaultMemchunks = Memchunks<Memchunk16K>;
459 using DefaultPeekMemchunks = PeekMemchunks<Memchunk16K>;
460 
limit_iovec(struct iovec * iov,int iovcnt,size_t max)461 inline int limit_iovec(struct iovec *iov, int iovcnt, size_t max) {
462   if (max == 0) {
463     return 0;
464   }
465   for (int i = 0; i < iovcnt; ++i) {
466     auto d = std::min(max, iov[i].iov_len);
467     iov[i].iov_len = d;
468     max -= d;
469     if (max == 0) {
470       return i + 1;
471     }
472   }
473   return iovcnt;
474 }
475 
476 // MemchunkBuffer is similar to Buffer, but it uses pooled Memchunk
477 // for its underlying buffer.
478 template <typename Memchunk> struct MemchunkBuffer {
MemchunkBufferMemchunkBuffer479   MemchunkBuffer(Pool<Memchunk> *pool) : pool(pool), chunk(nullptr) {}
480   MemchunkBuffer(const MemchunkBuffer &) = delete;
MemchunkBufferMemchunkBuffer481   MemchunkBuffer(MemchunkBuffer &&other) noexcept
482       : pool(other.pool), chunk(other.chunk) {
483     other.chunk = nullptr;
484   }
485   MemchunkBuffer &operator=(const MemchunkBuffer &) = delete;
486   MemchunkBuffer &operator=(MemchunkBuffer &&other) noexcept {
487     if (this == &other) {
488       return *this;
489     }
490 
491     pool = other.pool;
492     chunk = other.chunk;
493 
494     other.chunk = nullptr;
495 
496     return *this;
497   }
498 
~MemchunkBufferMemchunkBuffer499   ~MemchunkBuffer() {
500     if (!pool || !chunk) {
501       return;
502     }
503     pool->recycle(chunk);
504   }
505 
506   // Ensures that the underlying buffer is allocated.
ensure_chunkMemchunkBuffer507   void ensure_chunk() {
508     if (chunk) {
509       return;
510     }
511     chunk = pool->get();
512   }
513 
514   // Releases the underlying buffer.
release_chunkMemchunkBuffer515   void release_chunk() {
516     if (!chunk) {
517       return;
518     }
519     pool->recycle(chunk);
520     chunk = nullptr;
521   }
522 
523   // Returns true if the underlying buffer is allocated.
chunk_availMemchunkBuffer524   bool chunk_avail() const { return chunk != nullptr; }
525 
526   // The functions below must be called after the underlying buffer is
527   // allocated (use ensure_chunk).
528 
529   // MemchunkBuffer provides the same interface functions with Buffer.
530   // Since we has chunk as a member variable, pos and last are
531   // implemented as wrapper functions.
532 
posMemchunkBuffer533   uint8_t *pos() const { return chunk->pos; }
lastMemchunkBuffer534   uint8_t *last() const { return chunk->last; }
535 
rleftMemchunkBuffer536   size_t rleft() const { return chunk->len(); }
wleftMemchunkBuffer537   size_t wleft() const { return chunk->left(); }
writeMemchunkBuffer538   size_t write(const void *src, size_t count) {
539     count = std::min(count, wleft());
540     auto p = static_cast<const uint8_t *>(src);
541     chunk->last = std::copy_n(p, count, chunk->last);
542     return count;
543   }
writeMemchunkBuffer544   size_t write(size_t count) {
545     count = std::min(count, wleft());
546     chunk->last += count;
547     return count;
548   }
drainMemchunkBuffer549   size_t drain(size_t count) {
550     count = std::min(count, rleft());
551     chunk->pos += count;
552     return count;
553   }
drain_resetMemchunkBuffer554   size_t drain_reset(size_t count) {
555     count = std::min(count, rleft());
556     std::copy(chunk->pos + count, chunk->last, std::begin(chunk->buf));
557     chunk->last = std::begin(chunk->buf) + (chunk->last - (chunk->pos + count));
558     chunk->pos = std::begin(chunk->buf);
559     return count;
560   }
resetMemchunkBuffer561   void reset() { chunk->reset(); }
beginMemchunkBuffer562   uint8_t *begin() { return std::begin(chunk->buf); }
563   uint8_t &operator[](size_t n) { return chunk->buf[n]; }
564   const uint8_t &operator[](size_t n) const { return chunk->buf[n]; }
565 
566   Pool<Memchunk> *pool;
567   Memchunk *chunk;
568 };
569 
570 using DefaultMemchunkBuffer = MemchunkBuffer<Memchunk16K>;
571 
572 } // namespace nghttp2
573 
574 #endif // MEMCHUNK_H
575