2014-12-27 18:59:06 +01:00
|
|
|
/*
|
|
|
|
* nghttp2 - HTTP/2 C Library
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#ifndef MEMCHUNK_H
|
|
|
|
#define MEMCHUNK_H
|
|
|
|
|
|
|
|
#include "nghttp2_config.h"
|
|
|
|
|
2015-07-26 19:28:36 +02:00
|
|
|
#include <limits.h>
|
2017-10-12 18:15:12 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
/* Structure for scatter/gather I/O. */
|
|
|
|
struct iovec {
|
2017-10-14 04:45:41 +02:00
|
|
|
void *iov_base; /* Pointer to data. */
|
|
|
|
size_t iov_len; /* Length of data. */
|
2017-10-12 18:15:12 +02:00
|
|
|
};
|
2017-10-14 04:50:16 +02:00
|
|
|
#else // !_WIN32
|
2018-06-09 09:21:30 +02:00
|
|
|
# include <sys/uio.h>
|
2017-10-14 04:50:16 +02:00
|
|
|
#endif // !_WIN32
|
2015-01-10 15:01:03 +01:00
|
|
|
|
2015-02-05 15:21:53 +01:00
|
|
|
#include <cassert>
|
2014-12-27 18:59:06 +01:00
|
|
|
#include <cstring>
|
|
|
|
#include <memory>
|
2015-02-05 14:45:17 +01:00
|
|
|
#include <array>
|
2015-02-06 13:35:03 +01:00
|
|
|
#include <algorithm>
|
2015-09-11 16:07:00 +02:00
|
|
|
#include <string>
|
2018-11-02 07:40:53 +01:00
|
|
|
#include <utility>
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-02-05 15:21:53 +01:00
|
|
|
#include "template.h"
|
2014-12-27 18:59:06 +01:00
|
|
|
|
|
|
|
namespace nghttp2 {
|
|
|
|
|
2016-01-26 15:04:53 +01:00
|
|
|
#define DEFAULT_WR_IOVCNT 16
|
|
|
|
|
|
|
|
#if defined(IOV_MAX) && IOV_MAX < DEFAULT_WR_IOVCNT
|
2018-06-09 09:21:30 +02:00
|
|
|
# define MAX_WR_IOVCNT IOV_MAX
|
2016-01-26 15:04:53 +01:00
|
|
|
#else // !defined(IOV_MAX) || IOV_MAX >= DEFAULT_WR_IOVCNT
|
2018-06-09 09:21:30 +02:00
|
|
|
# define MAX_WR_IOVCNT DEFAULT_WR_IOVCNT
|
2016-01-26 15:04:53 +01:00
|
|
|
#endif // !defined(IOV_MAX) || IOV_MAX >= DEFAULT_WR_IOVCNT
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
template <size_t N> struct Memchunk {
|
2017-05-22 16:17:04 +02:00
|
|
|
Memchunk(Memchunk *next_chunk)
|
|
|
|
: pos(std::begin(buf)), last(pos), knext(next_chunk), next(nullptr) {}
|
2014-12-27 18:59:06 +01:00
|
|
|
size_t len() const { return last - pos; }
|
2015-02-05 14:45:17 +01:00
|
|
|
size_t left() const { return std::end(buf) - last; }
|
|
|
|
void reset() { pos = last = std::begin(buf); }
|
|
|
|
std::array<uint8_t, N> buf;
|
|
|
|
uint8_t *pos, *last;
|
2017-05-22 16:17:04 +02:00
|
|
|
Memchunk *knext;
|
2014-12-27 18:59:06 +01:00
|
|
|
Memchunk *next;
|
|
|
|
static const size_t size = N;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T> struct Pool {
|
2020-12-16 15:27:58 +01:00
|
|
|
Pool() : pool(nullptr), freelist(nullptr), poolsize(0), freelistsize(0) {}
|
2017-05-22 16:17:04 +02:00
|
|
|
~Pool() { clear(); }
|
2014-12-27 18:59:06 +01:00
|
|
|
T *get() {
|
|
|
|
if (freelist) {
|
|
|
|
auto m = freelist;
|
|
|
|
freelist = freelist->next;
|
|
|
|
m->next = nullptr;
|
|
|
|
m->reset();
|
2020-12-16 15:27:58 +01:00
|
|
|
freelistsize -= T::size;
|
2014-12-27 18:59:06 +01:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2017-05-22 16:17:04 +02:00
|
|
|
pool = new T{pool};
|
2014-12-27 18:59:06 +01:00
|
|
|
poolsize += T::size;
|
2017-05-22 16:17:04 +02:00
|
|
|
return pool;
|
2014-12-27 18:59:06 +01:00
|
|
|
}
|
|
|
|
void recycle(T *m) {
|
2015-10-03 12:14:41 +02:00
|
|
|
m->next = freelist;
|
2014-12-27 18:59:06 +01:00
|
|
|
freelist = m;
|
2020-12-16 15:27:58 +01:00
|
|
|
freelistsize += T::size;
|
2014-12-27 18:59:06 +01:00
|
|
|
}
|
2015-04-07 15:13:01 +02:00
|
|
|
void clear() {
|
|
|
|
freelist = nullptr;
|
2020-12-16 15:27:58 +01:00
|
|
|
freelistsize = 0;
|
2017-05-22 16:17:04 +02:00
|
|
|
for (auto p = pool; p;) {
|
|
|
|
auto knext = p->knext;
|
|
|
|
delete p;
|
|
|
|
p = knext;
|
|
|
|
}
|
2015-04-07 15:13:01 +02:00
|
|
|
pool = nullptr;
|
|
|
|
poolsize = 0;
|
|
|
|
}
|
2015-01-10 15:01:03 +01:00
|
|
|
using value_type = T;
|
2017-05-22 16:17:04 +02:00
|
|
|
T *pool;
|
2014-12-27 18:59:06 +01:00
|
|
|
T *freelist;
|
|
|
|
size_t poolsize;
|
2020-12-16 15:27:58 +01:00
|
|
|
size_t freelistsize;
|
2014-12-27 18:59:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Memchunk> struct Memchunks {
|
|
|
|
Memchunks(Pool<Memchunk> *pool)
|
|
|
|
: pool(pool), head(nullptr), tail(nullptr), len(0) {}
|
2015-08-12 17:04:41 +02:00
|
|
|
Memchunks(const Memchunks &) = delete;
|
2016-10-15 11:36:04 +02:00
|
|
|
Memchunks(Memchunks &&other) noexcept
|
2018-11-02 07:40:53 +01:00
|
|
|
: pool{other.pool}, // keep other.pool
|
|
|
|
head{std::exchange(other.head, nullptr)},
|
|
|
|
tail{std::exchange(other.tail, nullptr)},
|
|
|
|
len{std::exchange(other.len, 0)} {}
|
2015-08-12 17:04:41 +02:00
|
|
|
Memchunks &operator=(const Memchunks &) = delete;
|
2016-01-17 09:16:20 +01:00
|
|
|
Memchunks &operator=(Memchunks &&other) noexcept {
|
2015-08-12 17:04:41 +02:00
|
|
|
if (this == &other) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
reset();
|
|
|
|
|
|
|
|
pool = other.pool;
|
2018-11-02 07:40:53 +01:00
|
|
|
head = std::exchange(other.head, nullptr);
|
|
|
|
tail = std::exchange(other.tail, nullptr);
|
|
|
|
len = std::exchange(other.len, 0);
|
2015-08-12 17:04:41 +02:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
~Memchunks() {
|
2015-01-02 03:16:37 +01:00
|
|
|
if (!pool) {
|
|
|
|
return;
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
for (auto m = head; m;) {
|
|
|
|
auto next = m->next;
|
|
|
|
pool->recycle(m);
|
|
|
|
m = next;
|
|
|
|
}
|
|
|
|
}
|
2015-09-11 16:07:00 +02:00
|
|
|
size_t append(char c) {
|
|
|
|
if (!tail) {
|
|
|
|
head = tail = pool->get();
|
2015-09-11 19:10:07 +02:00
|
|
|
} else if (tail->left() == 0) {
|
2015-09-11 16:07:00 +02:00
|
|
|
tail->next = pool->get();
|
|
|
|
tail = tail->next;
|
|
|
|
}
|
|
|
|
*tail->last++ = c;
|
2015-09-11 18:22:40 +02:00
|
|
|
++len;
|
2015-09-11 16:07:00 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2015-02-05 14:45:17 +01:00
|
|
|
size_t append(const void *src, size_t count) {
|
2014-12-27 18:59:06 +01:00
|
|
|
if (count == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-05 14:45:17 +01:00
|
|
|
auto first = static_cast<const uint8_t *>(src);
|
|
|
|
auto last = first + count;
|
2014-12-27 18:59:06 +01:00
|
|
|
|
|
|
|
if (!tail) {
|
|
|
|
head = tail = pool->get();
|
|
|
|
}
|
|
|
|
|
2015-02-05 14:45:17 +01:00
|
|
|
for (;;) {
|
|
|
|
auto n = std::min(static_cast<size_t>(last - first), tail->left());
|
2015-02-06 13:35:03 +01:00
|
|
|
tail->last = std::copy_n(first, n, tail->last);
|
2015-02-05 14:45:17 +01:00
|
|
|
first += n;
|
2014-12-27 18:59:06 +01:00
|
|
|
len += n;
|
2015-02-05 14:45:17 +01:00
|
|
|
if (first == last) {
|
2014-12-27 18:59:06 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
tail->next = pool->get();
|
|
|
|
tail = tail->next;
|
|
|
|
}
|
|
|
|
|
2015-02-05 14:45:17 +01:00
|
|
|
return count;
|
2014-12-27 18:59:06 +01:00
|
|
|
}
|
2016-10-15 11:36:04 +02:00
|
|
|
template <size_t N> size_t append(const char (&s)[N]) {
|
2014-12-27 18:59:06 +01:00
|
|
|
return append(s, N - 1);
|
|
|
|
}
|
2015-09-11 16:07:00 +02:00
|
|
|
size_t append(const std::string &s) { return append(s.c_str(), s.size()); }
|
2016-01-17 09:04:16 +01:00
|
|
|
size_t append(const StringRef &s) { return append(s.c_str(), s.size()); }
|
2016-08-26 15:28:09 +02:00
|
|
|
size_t append(const ImmutableString &s) {
|
|
|
|
return append(s.c_str(), s.size());
|
|
|
|
}
|
2019-01-19 03:12:05 +01:00
|
|
|
size_t copy(Memchunks &dest) {
|
|
|
|
auto m = head;
|
|
|
|
while (m) {
|
|
|
|
dest.append(m->pos, m->len());
|
|
|
|
m = m->next;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
2015-02-05 14:45:17 +01:00
|
|
|
size_t remove(void *dest, size_t count) {
|
2014-12-27 18:59:06 +01:00
|
|
|
if (!tail || count == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-02-05 14:45:17 +01:00
|
|
|
|
|
|
|
auto first = static_cast<uint8_t *>(dest);
|
|
|
|
auto last = first + count;
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
auto m = head;
|
|
|
|
|
|
|
|
while (m) {
|
|
|
|
auto next = m->next;
|
2015-02-05 14:45:17 +01:00
|
|
|
auto n = std::min(static_cast<size_t>(last - first), m->len());
|
2014-12-27 18:59:06 +01:00
|
|
|
|
|
|
|
assert(m->len());
|
2015-02-06 13:35:03 +01:00
|
|
|
first = std::copy_n(m->pos, n, first);
|
2014-12-27 18:59:06 +01:00
|
|
|
m->pos += n;
|
|
|
|
len -= n;
|
|
|
|
if (m->len() > 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pool->recycle(m);
|
|
|
|
m = next;
|
|
|
|
}
|
|
|
|
head = m;
|
|
|
|
if (head == nullptr) {
|
|
|
|
tail = nullptr;
|
|
|
|
}
|
|
|
|
|
2015-02-05 14:45:17 +01:00
|
|
|
return first - static_cast<uint8_t *>(dest);
|
2014-12-27 18:59:06 +01:00
|
|
|
}
|
2016-01-26 15:04:53 +01:00
|
|
|
size_t remove(Memchunks &dest, size_t count) {
|
|
|
|
if (!tail || count == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto left = count;
|
|
|
|
auto m = head;
|
|
|
|
|
|
|
|
while (m) {
|
|
|
|
auto next = m->next;
|
|
|
|
auto n = std::min(left, m->len());
|
|
|
|
|
|
|
|
assert(m->len());
|
|
|
|
dest.append(m->pos, n);
|
|
|
|
m->pos += n;
|
|
|
|
len -= n;
|
|
|
|
left -= n;
|
|
|
|
if (m->len() > 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pool->recycle(m);
|
|
|
|
m = next;
|
|
|
|
}
|
|
|
|
head = m;
|
|
|
|
if (head == nullptr) {
|
|
|
|
tail = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count - left;
|
|
|
|
}
|
2018-08-22 15:32:25 +02:00
|
|
|
size_t remove(Memchunks &dest) {
|
|
|
|
assert(pool == dest.pool);
|
|
|
|
|
|
|
|
if (head == nullptr) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto n = len;
|
|
|
|
|
|
|
|
if (dest.tail == nullptr) {
|
|
|
|
dest.head = head;
|
|
|
|
} else {
|
|
|
|
dest.tail->next = head;
|
|
|
|
}
|
|
|
|
|
|
|
|
dest.tail = tail;
|
|
|
|
dest.len += len;
|
|
|
|
|
|
|
|
head = tail = nullptr;
|
|
|
|
len = 0;
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
size_t drain(size_t count) {
|
|
|
|
auto ndata = count;
|
|
|
|
auto m = head;
|
|
|
|
while (m) {
|
|
|
|
auto next = m->next;
|
|
|
|
auto n = std::min(count, m->len());
|
|
|
|
m->pos += n;
|
|
|
|
count -= n;
|
|
|
|
len -= n;
|
|
|
|
if (m->len() > 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
pool->recycle(m);
|
|
|
|
m = next;
|
|
|
|
}
|
|
|
|
head = m;
|
|
|
|
if (head == nullptr) {
|
|
|
|
tail = nullptr;
|
|
|
|
}
|
|
|
|
return ndata - count;
|
|
|
|
}
|
2015-10-03 04:10:07 +02:00
|
|
|
int riovec(struct iovec *iov, int iovcnt) const {
|
2014-12-27 18:59:06 +01:00
|
|
|
if (!head) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
auto m = head;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < iovcnt && m; ++i, m = m->next) {
|
|
|
|
iov[i].iov_base = m->pos;
|
|
|
|
iov[i].iov_len = m->len();
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
size_t rleft() const { return len; }
|
2015-08-12 17:04:41 +02:00
|
|
|
void reset() {
|
|
|
|
for (auto m = head; m;) {
|
|
|
|
auto next = m->next;
|
|
|
|
pool->recycle(m);
|
|
|
|
m = next;
|
|
|
|
}
|
|
|
|
len = 0;
|
|
|
|
head = tail = nullptr;
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
|
|
|
Pool<Memchunk> *pool;
|
|
|
|
Memchunk *head, *tail;
|
|
|
|
size_t len;
|
|
|
|
};
|
|
|
|
|
2015-08-12 17:04:41 +02:00
|
|
|
// Wrapper around Memchunks to offer "peeking" functionality.
|
|
|
|
template <typename Memchunk> struct PeekMemchunks {
|
|
|
|
PeekMemchunks(Pool<Memchunk> *pool)
|
2016-01-27 13:14:07 +01:00
|
|
|
: memchunks(pool),
|
|
|
|
cur(nullptr),
|
|
|
|
cur_pos(nullptr),
|
|
|
|
cur_last(nullptr),
|
|
|
|
len(0),
|
|
|
|
peeking(true) {}
|
2015-08-12 17:04:41 +02:00
|
|
|
PeekMemchunks(const PeekMemchunks &) = delete;
|
2016-01-17 09:16:20 +01:00
|
|
|
PeekMemchunks(PeekMemchunks &&other) noexcept
|
2018-11-02 07:40:53 +01:00
|
|
|
: memchunks{std::move(other.memchunks)},
|
|
|
|
cur{std::exchange(other.cur, nullptr)},
|
|
|
|
cur_pos{std::exchange(other.cur_pos, nullptr)},
|
|
|
|
cur_last{std::exchange(other.cur_last, nullptr)},
|
|
|
|
len{std::exchange(other.len, 0)},
|
|
|
|
peeking{std::exchange(other.peeking, true)} {}
|
2015-08-12 17:04:41 +02:00
|
|
|
PeekMemchunks &operator=(const PeekMemchunks &) = delete;
|
2016-01-17 09:16:20 +01:00
|
|
|
PeekMemchunks &operator=(PeekMemchunks &&other) noexcept {
|
2015-08-12 17:04:41 +02:00
|
|
|
if (this == &other) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
memchunks = std::move(other.memchunks);
|
2018-11-02 07:40:53 +01:00
|
|
|
cur = std::exchange(other.cur, nullptr);
|
|
|
|
cur_pos = std::exchange(other.cur_pos, nullptr);
|
|
|
|
cur_last = std::exchange(other.cur_last, nullptr);
|
|
|
|
len = std::exchange(other.len, 0);
|
|
|
|
peeking = std::exchange(other.peeking, true);
|
2015-08-12 17:04:41 +02:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
size_t append(const void *src, size_t count) {
|
|
|
|
count = memchunks.append(src, count);
|
|
|
|
len += count;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
size_t remove(void *dest, size_t count) {
|
|
|
|
if (!peeking) {
|
|
|
|
count = memchunks.remove(dest, count);
|
|
|
|
len -= count;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count == 0 || len == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cur) {
|
|
|
|
cur = memchunks.head;
|
|
|
|
cur_pos = cur->pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cur_last could be updated in append
|
|
|
|
cur_last = cur->last;
|
|
|
|
|
|
|
|
if (cur_pos == cur_last) {
|
|
|
|
assert(cur->next);
|
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto first = static_cast<uint8_t *>(dest);
|
|
|
|
auto last = first + count;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
auto n = std::min(last - first, cur_last - cur_pos);
|
|
|
|
|
|
|
|
first = std::copy_n(cur_pos, n, first);
|
|
|
|
cur_pos += n;
|
|
|
|
len -= n;
|
|
|
|
|
|
|
|
if (first == last) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
assert(cur_pos == cur_last);
|
|
|
|
if (!cur->next) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cur = cur->next;
|
|
|
|
cur_pos = cur->pos;
|
|
|
|
cur_last = cur->last;
|
|
|
|
}
|
|
|
|
return first - static_cast<uint8_t *>(dest);
|
|
|
|
}
|
|
|
|
size_t rleft() const { return len; }
|
|
|
|
size_t rleft_buffered() const { return memchunks.rleft(); }
|
|
|
|
void disable_peek(bool drain) {
|
|
|
|
if (!peeking) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (drain) {
|
|
|
|
auto n = rleft_buffered() - rleft();
|
|
|
|
memchunks.drain(n);
|
|
|
|
assert(len == memchunks.rleft());
|
|
|
|
} else {
|
|
|
|
len = memchunks.rleft();
|
|
|
|
}
|
|
|
|
cur = nullptr;
|
|
|
|
cur_pos = cur_last = nullptr;
|
|
|
|
peeking = false;
|
|
|
|
}
|
|
|
|
void reset() {
|
|
|
|
memchunks.reset();
|
|
|
|
cur = nullptr;
|
|
|
|
cur_pos = cur_last = nullptr;
|
|
|
|
len = 0;
|
|
|
|
peeking = true;
|
|
|
|
}
|
|
|
|
Memchunks<Memchunk> memchunks;
|
|
|
|
// Pointer to the Memchunk currently we are reading/writing.
|
|
|
|
Memchunk *cur;
|
|
|
|
// Region inside cur, we have processed to cur_pos.
|
|
|
|
uint8_t *cur_pos, *cur_last;
|
|
|
|
// This is the length we have left unprocessed. len <=
|
|
|
|
// memchunk.rleft() must hold.
|
|
|
|
size_t len;
|
|
|
|
// true if peeking is enabled. Initially it is true.
|
|
|
|
bool peeking;
|
|
|
|
};
|
|
|
|
|
2015-06-21 07:32:47 +02:00
|
|
|
using Memchunk16K = Memchunk<16_k>;
|
2015-01-20 17:47:43 +01:00
|
|
|
using MemchunkPool = Pool<Memchunk16K>;
|
|
|
|
using DefaultMemchunks = Memchunks<Memchunk16K>;
|
2015-08-12 17:04:41 +02:00
|
|
|
using DefaultPeekMemchunks = PeekMemchunks<Memchunk16K>;
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-02-04 13:15:58 +01:00
|
|
|
inline int limit_iovec(struct iovec *iov, int iovcnt, size_t max) {
|
|
|
|
if (max == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < iovcnt; ++i) {
|
|
|
|
auto d = std::min(max, iov[i].iov_len);
|
|
|
|
iov[i].iov_len = d;
|
|
|
|
max -= d;
|
|
|
|
if (max == 0) {
|
|
|
|
return i + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return iovcnt;
|
|
|
|
}
|
|
|
|
|
2017-01-08 08:41:02 +01:00
|
|
|
// MemchunkBuffer is similar to Buffer, but it uses pooled Memchunk
|
|
|
|
// for its underlying buffer.
|
|
|
|
template <typename Memchunk> struct MemchunkBuffer {
|
|
|
|
MemchunkBuffer(Pool<Memchunk> *pool) : pool(pool), chunk(nullptr) {}
|
|
|
|
MemchunkBuffer(const MemchunkBuffer &) = delete;
|
|
|
|
MemchunkBuffer(MemchunkBuffer &&other) noexcept
|
|
|
|
: pool(other.pool), chunk(other.chunk) {
|
|
|
|
other.chunk = nullptr;
|
|
|
|
}
|
|
|
|
MemchunkBuffer &operator=(const MemchunkBuffer &) = delete;
|
|
|
|
MemchunkBuffer &operator=(MemchunkBuffer &&other) noexcept {
|
|
|
|
if (this == &other) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
pool = other.pool;
|
|
|
|
chunk = other.chunk;
|
|
|
|
|
|
|
|
other.chunk = nullptr;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
~MemchunkBuffer() {
|
|
|
|
if (!pool || !chunk) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pool->recycle(chunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensures that the underlying buffer is allocated.
|
|
|
|
void ensure_chunk() {
|
|
|
|
if (chunk) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
chunk = pool->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Releases the underlying buffer.
|
|
|
|
void release_chunk() {
|
|
|
|
if (!chunk) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pool->recycle(chunk);
|
|
|
|
chunk = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if the underlying buffer is allocated.
|
|
|
|
bool chunk_avail() const { return chunk != nullptr; }
|
|
|
|
|
|
|
|
// The functions below must be called after the underlying buffer is
|
|
|
|
// allocated (use ensure_chunk).
|
|
|
|
|
|
|
|
// MemchunkBuffer provides the same interface functions with Buffer.
|
|
|
|
// Since we has chunk as a member variable, pos and last are
|
|
|
|
// implemented as wrapper functions.
|
|
|
|
|
|
|
|
uint8_t *pos() const { return chunk->pos; }
|
|
|
|
uint8_t *last() const { return chunk->last; }
|
|
|
|
|
|
|
|
size_t rleft() const { return chunk->len(); }
|
|
|
|
size_t wleft() const { return chunk->left(); }
|
|
|
|
size_t write(const void *src, size_t count) {
|
|
|
|
count = std::min(count, wleft());
|
|
|
|
auto p = static_cast<const uint8_t *>(src);
|
|
|
|
chunk->last = std::copy_n(p, count, chunk->last);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
size_t write(size_t count) {
|
|
|
|
count = std::min(count, wleft());
|
|
|
|
chunk->last += count;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
size_t drain(size_t count) {
|
|
|
|
count = std::min(count, rleft());
|
|
|
|
chunk->pos += count;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
size_t drain_reset(size_t count) {
|
|
|
|
count = std::min(count, rleft());
|
|
|
|
std::copy(chunk->pos + count, chunk->last, std::begin(chunk->buf));
|
|
|
|
chunk->last = std::begin(chunk->buf) + (chunk->last - (chunk->pos + count));
|
|
|
|
chunk->pos = std::begin(chunk->buf);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
void reset() { chunk->reset(); }
|
|
|
|
uint8_t *begin() { return std::begin(chunk->buf); }
|
|
|
|
uint8_t &operator[](size_t n) { return chunk->buf[n]; }
|
|
|
|
const uint8_t &operator[](size_t n) const { return chunk->buf[n]; }
|
|
|
|
|
|
|
|
Pool<Memchunk> *pool;
|
|
|
|
Memchunk *chunk;
|
|
|
|
};
|
|
|
|
|
|
|
|
using DefaultMemchunkBuffer = MemchunkBuffer<Memchunk16K>;
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
} // namespace nghttp2
|
|
|
|
|
|
|
|
#endif // MEMCHUNK_H
|