Skip to content

Commit

Permalink
Merge pull request #223 from ngtcp2/typed-nghttp3-min-max
Browse files Browse the repository at this point in the history
Introduce typed nghttp3_min and nghttp3_max
  • Loading branch information
tatsuhiro-t authored May 9, 2024
2 parents a6f1af4 + 11471e6 commit 3918cdd
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 34 deletions.
24 changes: 12 additions & 12 deletions lib/nghttp3_conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ nghttp3_ssize nghttp3_conn_read_control(nghttp3_conn *conn,
}

/* Read Identifier */
len = (size_t)nghttp3_min(rstate->left, (int64_t)(end - p));
len = (size_t)nghttp3_min_int64(rstate->left, (int64_t)(end - p));
assert(len > 0);
nread = nghttp3_read_varint(rvint, p, len, frame_fin(rstate, len));
if (nread < 0) {
Expand Down Expand Up @@ -818,7 +818,7 @@ nghttp3_ssize nghttp3_conn_read_control(nghttp3_conn *conn,
}
break;
case NGHTTP3_CTRL_STREAM_STATE_SETTINGS_ID:
len = (size_t)nghttp3_min(rstate->left, (int64_t)(end - p));
len = (size_t)nghttp3_min_int64(rstate->left, (int64_t)(end - p));
assert(len > 0);
nread = nghttp3_read_varint(rvint, p, len, frame_fin(rstate, len));
if (nread < 0) {
Expand All @@ -845,7 +845,7 @@ nghttp3_ssize nghttp3_conn_read_control(nghttp3_conn *conn,
}
/* Fall through */
case NGHTTP3_CTRL_STREAM_STATE_SETTINGS_VALUE:
len = (size_t)nghttp3_min(rstate->left, (int64_t)(end - p));
len = (size_t)nghttp3_min_int64(rstate->left, (int64_t)(end - p));
assert(len > 0);
nread = nghttp3_read_varint(rvint, p, len, frame_fin(rstate, len));
if (nread < 0) {
Expand Down Expand Up @@ -879,7 +879,7 @@ nghttp3_ssize nghttp3_conn_read_control(nghttp3_conn *conn,
nghttp3_stream_read_state_reset(rstate);
break;
case NGHTTP3_CTRL_STREAM_STATE_GOAWAY:
len = (size_t)nghttp3_min(rstate->left, (int64_t)(end - p));
len = (size_t)nghttp3_min_int64(rstate->left, (int64_t)(end - p));
assert(len > 0);
nread = nghttp3_read_varint(rvint, p, len, frame_fin(rstate, len));
if (nread < 0) {
Expand Down Expand Up @@ -916,7 +916,7 @@ nghttp3_ssize nghttp3_conn_read_control(nghttp3_conn *conn,
break;
case NGHTTP3_CTRL_STREAM_STATE_MAX_PUSH_ID:
/* server side only */
len = (size_t)nghttp3_min(rstate->left, (int64_t)(end - p));
len = (size_t)nghttp3_min_int64(rstate->left, (int64_t)(end - p));
assert(len > 0);
nread = nghttp3_read_varint(rvint, p, len, frame_fin(rstate, len));
if (nread < 0) {
Expand All @@ -941,7 +941,7 @@ nghttp3_ssize nghttp3_conn_read_control(nghttp3_conn *conn,
break;
case NGHTTP3_CTRL_STREAM_STATE_PRIORITY_UPDATE_PRI_ELEM_ID:
/* server side only */
len = (size_t)nghttp3_min(rstate->left, (int64_t)(end - p));
len = (size_t)nghttp3_min_int64(rstate->left, (int64_t)(end - p));
assert(len > 0);
nread = nghttp3_read_varint(rvint, p, len, frame_fin(rstate, len));
if (nread < 0) {
Expand Down Expand Up @@ -977,7 +977,7 @@ nghttp3_ssize nghttp3_conn_read_control(nghttp3_conn *conn,
case NGHTTP3_CTRL_STREAM_STATE_PRIORITY_UPDATE:
/* We need to buffer Priority Field Value because it might be
fragmented. */
len = (size_t)nghttp3_min(rstate->left, (int64_t)(end - p));
len = (size_t)nghttp3_min_int64(rstate->left, (int64_t)(end - p));
assert(len > 0);
if (conn->rx.pri_fieldbuflen == 0 && rstate->left == (int64_t)len) {
/* Everything is in the input buffer. Apply same length
Expand Down Expand Up @@ -1032,7 +1032,7 @@ nghttp3_ssize nghttp3_conn_read_control(nghttp3_conn *conn,
nghttp3_stream_read_state_reset(rstate);
break;
case NGHTTP3_CTRL_STREAM_STATE_IGN_FRAME:
len = (size_t)nghttp3_min(rstate->left, (int64_t)(end - p));
len = (size_t)nghttp3_min_int64(rstate->left, (int64_t)(end - p));
p += len;
nconsumed += len;
rstate->left -= (int64_t)len;
Expand Down Expand Up @@ -1351,7 +1351,7 @@ nghttp3_ssize nghttp3_conn_read_bidi(nghttp3_conn *conn, size_t *pnproc,
}
break;
case NGHTTP3_REQ_STREAM_STATE_DATA:
len = (size_t)nghttp3_min(rstate->left, (int64_t)(end - p));
len = (size_t)nghttp3_min_int64(rstate->left, (int64_t)(end - p));
rv = nghttp3_conn_on_data(conn, stream, p, len);
if (rv != 0) {
return rv;
Expand All @@ -1370,7 +1370,7 @@ nghttp3_ssize nghttp3_conn_read_bidi(nghttp3_conn *conn, size_t *pnproc,
nghttp3_stream_read_state_reset(rstate);
break;
case NGHTTP3_REQ_STREAM_STATE_HEADERS:
len = (size_t)nghttp3_min(rstate->left, (int64_t)(end - p));
len = (size_t)nghttp3_min_int64(rstate->left, (int64_t)(end - p));
nread = nghttp3_conn_on_headers(conn, stream, p, len,
(int64_t)len == rstate->left);
if (nread < 0) {
Expand Down Expand Up @@ -1478,7 +1478,7 @@ nghttp3_ssize nghttp3_conn_read_bidi(nghttp3_conn *conn, size_t *pnproc,

break;
case NGHTTP3_REQ_STREAM_STATE_IGN_FRAME:
len = (size_t)nghttp3_min(rstate->left, (int64_t)(end - p));
len = (size_t)nghttp3_min_int64(rstate->left, (int64_t)(end - p));
p += len;
nconsumed += len;
rstate->left -= (int64_t)len;
Expand Down Expand Up @@ -2319,7 +2319,7 @@ int nghttp3_conn_shutdown(nghttp3_conn *conn) {
frent.fr.hd.type = NGHTTP3_FRAME_GOAWAY;
if (conn->server) {
frent.fr.goaway.id =
nghttp3_min((1ll << 62) - 4, conn->rx.max_stream_id_bidi + 4);
nghttp3_min_int64((1ll << 62) - 4, conn->rx.max_stream_id_bidi + 4);
} else {
frent.fr.goaway.id = 0;
}
Expand Down
36 changes: 36 additions & 0 deletions lib/nghttp3_macro.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,40 @@
variable-length integer encoding. */
#define NGHTTP3_MAX_VARINT ((1ULL << 62) - 1)

/*
* nghttp3_max variants but they are inline functions. The
* intermediate values are stored in parameters so that they are
* evaluated just once.
*/
#define nghttp3_max_def(SUFFIX, T) \
static inline T nghttp3_max_##SUFFIX(T a, T b) { return a < b ? b : a; }

nghttp3_max_def(int8, int8_t);
nghttp3_max_def(int16, int16_t);
nghttp3_max_def(int32, int32_t);
nghttp3_max_def(int64, int64_t);
nghttp3_max_def(uint8, uint8_t);
nghttp3_max_def(uint16, uint16_t);
nghttp3_max_def(uint32, uint32_t);
nghttp3_max_def(uint64, uint64_t);
nghttp3_max_def(size, size_t);

/*
* nghttp3_min variants but they are inline functions. The
* intermediate values are stored in parameters so that they are
* evaluated just once.
*/
#define nghttp3_min_def(SUFFIX, T) \
static inline T nghttp3_min_##SUFFIX(T a, T b) { return a < b ? a : b; }

nghttp3_min_def(int8, int8_t);
nghttp3_min_def(int16, int16_t);
nghttp3_min_def(int32, int32_t);
nghttp3_min_def(int64, int64_t);
nghttp3_min_def(uint8, uint8_t);
nghttp3_min_def(uint16, uint16_t);
nghttp3_min_def(uint32, uint32_t);
nghttp3_min_def(uint64, uint64_t);
nghttp3_min_def(size, size_t);

#endif /* NGHTTP3_MACRO_H */
2 changes: 1 addition & 1 deletion lib/nghttp3_pq.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ int nghttp3_pq_push(nghttp3_pq *pq, nghttp3_pq_entry *item) {
void *nq;
size_t ncapacity;

ncapacity = nghttp3_max(4, (pq->capacity * 2));
ncapacity = nghttp3_max_size(4, (pq->capacity * 2));

nq = nghttp3_mem_realloc(pq->mem, pq->q,
ncapacity * sizeof(nghttp3_pq_entry *));
Expand Down
29 changes: 15 additions & 14 deletions lib/nghttp3_qpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -1431,7 +1431,7 @@ static int qpack_encoder_can_index_duplicate(nghttp3_qpack_encoder *encoder,
static int qpack_context_check_draining(nghttp3_qpack_context *ctx,
uint64_t absidx) {
const size_t safe = ctx->max_dtable_capacity -
nghttp3_min(512, ctx->max_dtable_capacity * 1 / 8);
nghttp3_min_size(512, ctx->max_dtable_capacity * 1 / 8);
nghttp3_qpack_entry *ent = nghttp3_qpack_context_dtable_get(ctx, absidx);

return ctx->dtable_sum - ent->sum > safe;
Expand Down Expand Up @@ -1512,8 +1512,8 @@ int nghttp3_qpack_encoder_encode_nv(nghttp3_qpack_encoder *encoder,
new_ent = nghttp3_qpack_context_dtable_top(&encoder->ctx);
dres.index = (nghttp3_ssize)new_ent->absidx;
}
*pmax_cnt = nghttp3_max(*pmax_cnt, (size_t)(dres.index + 1));
*pmin_cnt = nghttp3_min(*pmin_cnt, (size_t)(dres.index + 1));
*pmax_cnt = nghttp3_max_uint64(*pmax_cnt, (uint64_t)(dres.index + 1));
*pmin_cnt = nghttp3_min_uint64(*pmin_cnt, (uint64_t)(dres.index + 1));

return nghttp3_qpack_encoder_write_dynamic_indexed(
encoder, rbuf, (size_t)dres.index, base);
Expand All @@ -1533,8 +1533,8 @@ int nghttp3_qpack_encoder_encode_nv(nghttp3_qpack_encoder *encoder,
}
if (allow_blocking) {
new_ent = nghttp3_qpack_context_dtable_top(&encoder->ctx);
*pmax_cnt = nghttp3_max(*pmax_cnt, new_ent->absidx + 1);
*pmin_cnt = nghttp3_min(*pmin_cnt, new_ent->absidx + 1);
*pmax_cnt = nghttp3_max_uint64(*pmax_cnt, new_ent->absidx + 1);
*pmin_cnt = nghttp3_min_uint64(*pmin_cnt, new_ent->absidx + 1);

return nghttp3_qpack_encoder_write_dynamic_indexed(
encoder, rbuf, new_ent->absidx, base);
Expand All @@ -1549,16 +1549,17 @@ int nghttp3_qpack_encoder_encode_nv(nghttp3_qpack_encoder *encoder,
if (just_index &&
qpack_encoder_can_index_nv(
encoder, nv,
allow_blocking ? *pmin_cnt
: nghttp3_min((size_t)dres.index + 1, *pmin_cnt))) {
allow_blocking
? *pmin_cnt
: nghttp3_min_uint64((uint64_t)dres.index + 1, *pmin_cnt))) {
rv = nghttp3_qpack_encoder_write_dynamic_insert(encoder, ebuf,
(size_t)dres.index, nv);
if (rv != 0) {
return rv;
}

if (!allow_blocking) {
*pmin_cnt = nghttp3_min(*pmin_cnt, (size_t)dres.index + 1);
*pmin_cnt = nghttp3_min_uint64(*pmin_cnt, (uint64_t)dres.index + 1);
}

rv = nghttp3_qpack_encoder_dtable_dynamic_add(encoder, (size_t)dres.index,
Expand All @@ -1569,16 +1570,16 @@ int nghttp3_qpack_encoder_encode_nv(nghttp3_qpack_encoder *encoder,

if (allow_blocking) {
new_ent = nghttp3_qpack_context_dtable_top(&encoder->ctx);
*pmax_cnt = nghttp3_max(*pmax_cnt, new_ent->absidx + 1);
*pmin_cnt = nghttp3_min(*pmin_cnt, new_ent->absidx + 1);
*pmax_cnt = nghttp3_max_uint64(*pmax_cnt, new_ent->absidx + 1);
*pmin_cnt = nghttp3_min_uint64(*pmin_cnt, new_ent->absidx + 1);

return nghttp3_qpack_encoder_write_dynamic_indexed(
encoder, rbuf, new_ent->absidx, base);
}
}

*pmax_cnt = nghttp3_max(*pmax_cnt, (size_t)(dres.index + 1));
*pmin_cnt = nghttp3_min(*pmin_cnt, (size_t)(dres.index + 1));
*pmax_cnt = nghttp3_max_uint64(*pmax_cnt, (uint64_t)(dres.index + 1));
*pmin_cnt = nghttp3_min_uint64(*pmin_cnt, (uint64_t)(dres.index + 1));

return nghttp3_qpack_encoder_write_dynamic_indexed_name(
encoder, rbuf, (size_t)dres.index, base, nv);
Expand All @@ -1595,8 +1596,8 @@ int nghttp3_qpack_encoder_encode_nv(nghttp3_qpack_encoder *encoder,
}
if (allow_blocking) {
new_ent = nghttp3_qpack_context_dtable_top(&encoder->ctx);
*pmax_cnt = nghttp3_max(*pmax_cnt, new_ent->absidx + 1);
*pmin_cnt = nghttp3_min(*pmin_cnt, new_ent->absidx + 1);
*pmax_cnt = nghttp3_max_uint64(*pmax_cnt, new_ent->absidx + 1);
*pmin_cnt = nghttp3_min_uint64(*pmin_cnt, new_ent->absidx + 1);

return nghttp3_qpack_encoder_write_dynamic_indexed(encoder, rbuf,
new_ent->absidx, base);
Expand Down
2 changes: 1 addition & 1 deletion lib/nghttp3_ringbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void nghttp3_ringbuf_free(nghttp3_ringbuf *rb) {

void *nghttp3_ringbuf_push_front(nghttp3_ringbuf *rb) {
rb->first = (rb->first - 1) & (rb->nmemb - 1);
rb->len = nghttp3_min(rb->nmemb, rb->len + 1);
rb->len = nghttp3_min_size(rb->nmemb, rb->len + 1);

return (void *)&rb->buf[rb->first * rb->size];
}
Expand Down
13 changes: 7 additions & 6 deletions lib/nghttp3_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ int nghttp3_stream_frq_add(nghttp3_stream *stream,
int rv;

if (nghttp3_ringbuf_full(frq)) {
size_t nlen = nghttp3_max(NGHTTP3_MIN_RBLEN, nghttp3_ringbuf_len(frq) * 2);
size_t nlen =
nghttp3_max_size(NGHTTP3_MIN_RBLEN, nghttp3_ringbuf_len(frq) * 2);
rv = nghttp3_ringbuf_reserve(frq, nlen);
if (rv != 0) {
return rv;
Expand Down Expand Up @@ -737,7 +738,7 @@ int nghttp3_stream_outq_add(nghttp3_stream *stream,
}

if (nghttp3_ringbuf_full(outq)) {
size_t nlen = nghttp3_max(NGHTTP3_MIN_RBLEN, len * 2);
size_t nlen = nghttp3_max_size(NGHTTP3_MIN_RBLEN, len * 2);
rv = nghttp3_ringbuf_reserve(outq, nlen);
if (rv != 0) {
return rv;
Expand Down Expand Up @@ -779,7 +780,7 @@ int nghttp3_stream_ensure_chunk(nghttp3_stream *stream, size_t need) {
}

if (nghttp3_ringbuf_full(chunks)) {
size_t nlen = nghttp3_max(NGHTTP3_MIN_RBLEN, len * 2);
size_t nlen = nghttp3_max_size(NGHTTP3_MIN_RBLEN, len * 2);
rv = nghttp3_ringbuf_reserve(chunks, nlen);
if (rv != 0) {
return rv;
Expand Down Expand Up @@ -942,8 +943,8 @@ int nghttp3_stream_update_ack_offset(nghttp3_stream *stream, uint64_t offset) {
/* For NGHTTP3_BUF_TYPE_ALIEN, we never add 0 length buffer. */
if (tbuf->type == NGHTTP3_BUF_TYPE_ALIEN && stream->ack_offset < offset &&
stream->callbacks.acked_data) {
nack =
nghttp3_min(offset, stream->ack_base + buflen) - stream->ack_offset;
nack = nghttp3_min_uint64(offset, stream->ack_base + buflen) -
stream->ack_offset;

rv = stream->callbacks.acked_data(stream, stream->node.id, nack,
stream->user_data);
Expand Down Expand Up @@ -1004,7 +1005,7 @@ int nghttp3_stream_buffer_data(nghttp3_stream *stream, const uint8_t *data,
for (; datalen;) {
if (nghttp3_ringbuf_full(inq)) {
size_t nlen =
nghttp3_max(NGHTTP3_MIN_RBLEN, nghttp3_ringbuf_len(inq) * 2);
nghttp3_max_size(NGHTTP3_MIN_RBLEN, nghttp3_ringbuf_len(inq) * 2);
rv = nghttp3_ringbuf_reserve(inq, nlen);
if (rv != 0) {
return rv;
Expand Down

0 comments on commit 3918cdd

Please sign in to comment.