Skip to content

Commit

Permalink
Merge pull request #253 from ngtcp2/qpack-buf-reserve
Browse files Browse the repository at this point in the history
Refactor qpack reserve_buf
  • Loading branch information
tatsuhiro-t authored Sep 4, 2024
2 parents 2c4a381 + b8c6cca commit 99ba689
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions lib/nghttp3_qpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -1094,43 +1094,47 @@ static void qpack_encoder_remove_stream(nghttp3_qpack_encoder *encoder,
}

/*
* reserve_buf_internal ensures that |buf| contains at least
* |extra_size| of free space. In other words, if this function
* succeeds, nghttp3_buf_left(buf) >= extra_size holds. |min_size| is
* the minimum size of buffer. The allocated buffer has at least
* |min_size| bytes.
* reserve_buf ensures that |buf| contains at least |extra_size| of
* free space. In other words, if this function succeeds,
* nghttp3_buf_left(buf) >= extra_size holds.
*
* This function returns 0 if it succeeds, or one of the following
* negative error codes:
*
* NGHTTP3_ERR_NOMEM
* Out of memory.
*/
static int reserve_buf_internal(nghttp3_buf *buf, size_t extra_size,
size_t min_size, const nghttp3_mem *mem) {
static int reserve_buf(nghttp3_buf *buf, size_t extra_size,
const nghttp3_mem *mem) {
size_t left = nghttp3_buf_left(buf);
size_t n = min_size, need;
size_t n = 32;

if (left >= extra_size) {
return 0;
}

need = nghttp3_buf_cap(buf) + extra_size - left;

for (; n < need; n *= 2)
;
n = nghttp3_max_size(n, nghttp3_buf_cap(buf) + extra_size - left);

return nghttp3_buf_reserve(buf, n, mem);
}
/* Check whether we are requesting too much memory */
if (n > UINT32_MAX) {
return NGHTTP3_ERR_NOMEM;
}

static int reserve_buf_small(nghttp3_buf *buf, size_t extra_size,
const nghttp3_mem *mem) {
return reserve_buf_internal(buf, extra_size, 32, mem);
}
#ifndef WIN32
n = 1 << (32 - __builtin_clz((uint32_t)n - 1));
#else /* WIN32 */
/* Round up to the next highest power of 2 from Bit Twiddling
Hacks */
--n;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
++n;
#endif /* WIN32 */

static int reserve_buf(nghttp3_buf *buf, size_t extra_size,
const nghttp3_mem *mem) {
return reserve_buf_internal(buf, extra_size, 32, mem);
return nghttp3_buf_reserve(buf, n, mem);
}

int nghttp3_qpack_encoder_encode(nghttp3_qpack_encoder *encoder,
Expand Down Expand Up @@ -3715,9 +3719,9 @@ int nghttp3_qpack_decoder_write_section_ack(
return NGHTTP3_ERR_QPACK_FATAL;
}

rv = reserve_buf_small(
dbuf, nghttp3_qpack_put_varint_len((uint64_t)sctx->stream_id, 7),
decoder->ctx.mem);
rv = reserve_buf(dbuf,
nghttp3_qpack_put_varint_len((uint64_t)sctx->stream_id, 7),
decoder->ctx.mem);
if (rv != 0) {
return rv;
}
Expand Down

0 comments on commit 99ba689

Please sign in to comment.