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>
|
2015-01-10 15:01:03 +01:00
|
|
|
#include <sys/uio.h>
|
|
|
|
|
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>
|
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 {
|
|
|
|
|
|
|
|
template <size_t N> struct Memchunk {
|
2015-01-29 13:18:30 +01:00
|
|
|
Memchunk(std::unique_ptr<Memchunk> next_chunk)
|
2015-02-05 14:45:17 +01:00
|
|
|
: pos(std::begin(buf)), last(pos), knext(std::move(next_chunk)),
|
|
|
|
kprev(nullptr), next(nullptr) {
|
2015-01-29 13:18:30 +01:00
|
|
|
if (knext) {
|
|
|
|
knext->kprev = this;
|
|
|
|
}
|
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;
|
2014-12-27 18:59:06 +01:00
|
|
|
std::unique_ptr<Memchunk> knext;
|
|
|
|
Memchunk *kprev;
|
|
|
|
Memchunk *next;
|
|
|
|
static const size_t size = N;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T> struct Pool {
|
|
|
|
Pool() : pool(nullptr), freelist(nullptr), poolsize(0) {}
|
|
|
|
T *get() {
|
|
|
|
if (freelist) {
|
|
|
|
auto m = freelist;
|
|
|
|
freelist = freelist->next;
|
|
|
|
m->next = nullptr;
|
|
|
|
m->reset();
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2015-02-05 15:21:53 +01:00
|
|
|
pool = make_unique<T>(std::move(pool));
|
2014-12-27 18:59:06 +01:00
|
|
|
poolsize += T::size;
|
2015-01-29 13:18:30 +01:00
|
|
|
return pool.get();
|
2014-12-27 18:59:06 +01:00
|
|
|
}
|
|
|
|
void recycle(T *m) {
|
|
|
|
if (freelist) {
|
|
|
|
m->next = freelist;
|
|
|
|
} else {
|
|
|
|
m->next = nullptr;
|
|
|
|
}
|
|
|
|
freelist = m;
|
|
|
|
}
|
|
|
|
void shrink(size_t max) {
|
|
|
|
auto m = freelist;
|
|
|
|
for (; m && poolsize > max;) {
|
|
|
|
auto next = m->next;
|
|
|
|
poolsize -= T::size;
|
|
|
|
auto p = m->kprev;
|
|
|
|
if (p) {
|
|
|
|
p->knext = std::move(m->knext);
|
|
|
|
if (p->knext) {
|
|
|
|
p->knext->kprev = p;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pool = std::move(m->knext);
|
|
|
|
if (pool) {
|
|
|
|
pool->kprev = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m = next;
|
|
|
|
}
|
|
|
|
freelist = m;
|
|
|
|
}
|
2015-04-07 15:13:01 +02:00
|
|
|
void clear() {
|
|
|
|
freelist = nullptr;
|
|
|
|
pool = nullptr;
|
|
|
|
poolsize = 0;
|
|
|
|
}
|
2015-01-10 15:01:03 +01:00
|
|
|
using value_type = T;
|
2014-12-27 18:59:06 +01:00
|
|
|
std::unique_ptr<T> pool;
|
|
|
|
T *freelist;
|
|
|
|
size_t poolsize;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Memchunk> struct Memchunks {
|
|
|
|
Memchunks(Pool<Memchunk> *pool)
|
|
|
|
: pool(pool), head(nullptr), tail(nullptr), len(0) {}
|
|
|
|
~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-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
|
|
|
}
|
2015-01-22 15:54:30 +01: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-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
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
int riovec(struct iovec *iov, int iovcnt) {
|
|
|
|
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; }
|
|
|
|
|
|
|
|
Pool<Memchunk> *pool;
|
|
|
|
Memchunk *head, *tail;
|
|
|
|
size_t len;
|
|
|
|
};
|
|
|
|
|
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>;
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-02-04 13:15:58 +01:00
|
|
|
#define DEFAULT_WR_IOVCNT 16
|
|
|
|
|
|
|
|
#if defined(IOV_MAX) && IOV_MAX < DEFAULT_WR_IOVCNT
|
|
|
|
#define MAX_WR_IOVCNT IOV_MAX
|
|
|
|
#else // !defined(IOV_MAX) || IOV_MAX >= DEFAULT_WR_IOVCNT
|
|
|
|
#define MAX_WR_IOVCNT DEFAULT_WR_IOVCNT
|
|
|
|
#endif // !defined(IOV_MAX) || IOV_MAX >= DEFAULT_WR_IOVCNT
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
} // namespace nghttp2
|
|
|
|
|
|
|
|
#endif // MEMCHUNK_H
|