Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ngtcp2 port #242

Merged
merged 4 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/nghttp3_conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ static int conn_new(nghttp3_conn **pconn, int server, int callbacks_version,
nghttp3_pq_init(&conn->sched[i].spq, cycle_less, mem);
}

nghttp3_idtr_init(&conn->remote.bidi.idtr, server, mem);
nghttp3_idtr_init(&conn->remote.bidi.idtr, !server, mem);

conn->callbacks = *callbacks;
conn->local.settings = *settings;
Expand Down
20 changes: 7 additions & 13 deletions lib/nghttp3_gaptr.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,8 @@ void nghttp3_gaptr_init(nghttp3_gaptr *gaptr, const nghttp3_mem *mem) {

static int gaptr_gap_init(nghttp3_gaptr *gaptr) {
nghttp3_range range = {0, UINT64_MAX};
int rv;

rv = nghttp3_ksl_insert(&gaptr->gap, NULL, &range, NULL);
if (rv != 0) {
return rv;
}

return 0;
return nghttp3_ksl_insert(&gaptr->gap, NULL, &range, NULL);
}

void nghttp3_gaptr_free(nghttp3_gaptr *gaptr) {
Expand Down Expand Up @@ -82,7 +76,9 @@ int nghttp3_gaptr_push(nghttp3_gaptr *gaptr, uint64_t offset,
nghttp3_ksl_remove_hint(&gaptr->gap, &it, &it, &k);
continue;
}

nghttp3_range_cut(&l, &r, &k, &m);

if (nghttp3_range_len(&l)) {
nghttp3_ksl_update_key(&gaptr->gap, &k, &l);

Expand All @@ -95,23 +91,23 @@ int nghttp3_gaptr_push(nghttp3_gaptr *gaptr, uint64_t offset,
} else if (nghttp3_range_len(&r)) {
nghttp3_ksl_update_key(&gaptr->gap, &k, &r);
}

nghttp3_ksl_it_next(&it);
}

return 0;
}

uint64_t nghttp3_gaptr_first_gap_offset(nghttp3_gaptr *gaptr) {
nghttp3_ksl_it it;
nghttp3_range r;

if (nghttp3_ksl_len(&gaptr->gap) == 0) {
return 0;
}

it = nghttp3_ksl_begin(&gaptr->gap);
r = *(nghttp3_range *)nghttp3_ksl_it_key(&it);

return r.begin;
return ((nghttp3_range *)nghttp3_ksl_it_key(&it))->begin;
}

nghttp3_range nghttp3_gaptr_get_first_gap_after(nghttp3_gaptr *gaptr,
Expand All @@ -136,7 +132,6 @@ int nghttp3_gaptr_is_pushed(nghttp3_gaptr *gaptr, uint64_t offset,
uint64_t datalen) {
nghttp3_range q = {offset, offset + datalen};
nghttp3_ksl_it it;
nghttp3_range k;
nghttp3_range m;

if (nghttp3_ksl_len(&gaptr->gap) == 0) {
Expand All @@ -145,8 +140,7 @@ int nghttp3_gaptr_is_pushed(nghttp3_gaptr *gaptr, uint64_t offset,

it = nghttp3_ksl_lower_bound_compar(&gaptr->gap, &q,
nghttp3_ksl_range_exclusive_compar);
k = *(nghttp3_range *)nghttp3_ksl_it_key(&it);
m = nghttp3_range_intersect(&q, &k);
m = nghttp3_range_intersect(&q, (nghttp3_range *)nghttp3_ksl_it_key(&it));

return nghttp3_range_len(&m) == 0;
}
Expand Down
10 changes: 5 additions & 5 deletions lib/nghttp3_gaptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@
* nghttp3_gaptr maintains the gap in the range [0, UINT64_MAX).
*/
typedef struct nghttp3_gaptr {
/* gap maintains the range of offset which is not received
yet. Initially, its range is [0, UINT64_MAX). */
/* gap maintains the range of offset which is not pushed
yet. Initially, its range is [0, UINT64_MAX). "gap" is the range
that is not pushed yet. */
nghttp3_ksl gap;
/* mem is custom memory allocator */
const nghttp3_mem *mem;
Expand All @@ -58,8 +59,7 @@ void nghttp3_gaptr_init(nghttp3_gaptr *gaptr, const nghttp3_mem *mem);
void nghttp3_gaptr_free(nghttp3_gaptr *gaptr);

/*
* nghttp3_gaptr_push adds new data of length |datalen| at the stream
* offset |offset|.
* nghttp3_gaptr_push pushes the range [offset, offset + datalen).
*
* This function returns 0 if it succeeds, or one of the following
* negative error codes:
Expand All @@ -77,7 +77,7 @@ uint64_t nghttp3_gaptr_first_gap_offset(nghttp3_gaptr *gaptr);

/*
* nghttp3_gaptr_get_first_gap_after returns the first gap which
* overlaps or comes after |offset|.
* includes or comes after |offset|.
*/
nghttp3_range nghttp3_gaptr_get_first_gap_after(nghttp3_gaptr *gaptr,
uint64_t offset);
Expand Down
15 changes: 5 additions & 10 deletions lib/nghttp3_idtr.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ void nghttp3_idtr_free(nghttp3_idtr *idtr) {
}

/*
* id_from_stream_id translates |stream_id| to id space used by
* nghttp3_idtr.
* id_from_stream_id translates |stream_id| to an internal ID.
*/
static uint64_t id_from_stream_id(int64_t stream_id) {
return (uint64_t)(stream_id >> 2);
Expand All @@ -52,8 +51,8 @@ static uint64_t id_from_stream_id(int64_t stream_id) {
int nghttp3_idtr_open(nghttp3_idtr *idtr, int64_t stream_id) {
uint64_t q;

assert((idtr->server && (stream_id % 2)) ||
(!idtr->server && (stream_id % 2)) == 0);
assert((idtr->server && (stream_id & 1)) ||
(!idtr->server && !(stream_id & 1)));

q = id_from_stream_id(stream_id);

Expand All @@ -67,14 +66,10 @@ int nghttp3_idtr_open(nghttp3_idtr *idtr, int64_t stream_id) {
int nghttp3_idtr_is_open(nghttp3_idtr *idtr, int64_t stream_id) {
uint64_t q;

assert((idtr->server && (stream_id % 2)) ||
(!idtr->server && (stream_id % 2)) == 0);
assert((idtr->server && (stream_id & 1)) ||
(!idtr->server && !(stream_id & 1)));

q = id_from_stream_id(stream_id);

return nghttp3_gaptr_is_pushed(&idtr->gap, q, 1);
}

uint64_t nghttp3_idtr_first_gap(nghttp3_idtr *idtr) {
return nghttp3_gaptr_first_gap_offset(&idtr->gap);
}
23 changes: 8 additions & 15 deletions lib/nghttp3_idtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@
* nghttp3_idtr tracks the usage of stream ID.
*/
typedef struct nghttp3_idtr {
/* gap maintains the range of ID which is not used yet. Initially,
its range is [0, UINT64_MAX). */
/* gap maintains the range of an internal ID which is not used yet.
Initially, its range is [0, UINT64_MAX). The internal ID and
stream ID are in the different number spaces. See
id_from_stream_id to convert a stream ID to an internal ID. */
nghttp3_gaptr gap;
/* server is nonzero if this object records server initiated stream
ID. */
Expand All @@ -50,7 +52,7 @@ typedef struct nghttp3_idtr {
/*
* nghttp3_idtr_init initializes |idtr|.
*
* If this object records server initiated ID (even number), set
* If this object records server initiated stream ID (odd number), set
* |server| to nonzero.
*/
void nghttp3_idtr_init(nghttp3_idtr *idtr, int server, const nghttp3_mem *mem);
Expand All @@ -61,30 +63,21 @@ void nghttp3_idtr_init(nghttp3_idtr *idtr, int server, const nghttp3_mem *mem);
void nghttp3_idtr_free(nghttp3_idtr *idtr);

/*
* nghttp3_idtr_open claims that |stream_id| is in used.
* nghttp3_idtr_open claims that |stream_id| is in use.
*
* It returns 0 if it succeeds, or one of the following negative error
* codes:
*
* NGHTTP3_ERR_STREAM_IN_USE
* ID has already been used.
* |stream_id| has already been used.
* NGHTTP3_ERR_NOMEM
* Out of memory.
*/
int nghttp3_idtr_open(nghttp3_idtr *idtr, int64_t stream_id);

/*
* nghttp3_idtr_open tells whether ID |stream_id| is in used or not.
*
* It returns nonzero if |stream_id| is used.
* nghttp3_idtr_open returns nonzero if |stream_id| is in use.
*/
int nghttp3_idtr_is_open(nghttp3_idtr *idtr, int64_t stream_id);

/*
* nghttp3_idtr_first_gap returns the first id of first gap. If there
* is no gap, it returns UINT64_MAX. The returned id is an id space
* used in this object internally, and not stream ID.
*/
uint64_t nghttp3_idtr_first_gap(nghttp3_idtr *idtr);

#endif /* NGHTTP3_IDTR_H */
52 changes: 8 additions & 44 deletions lib/nghttp3_ksl.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,9 @@ static void ksl_insert_node(nghttp3_ksl *ksl, nghttp3_ksl_blk *blk, size_t i,
++blk->n;
}

static size_t ksl_bsearch(nghttp3_ksl *ksl, nghttp3_ksl_blk *blk,
const nghttp3_ksl_key *key,
nghttp3_ksl_compar compar) {
static size_t ksl_search(nghttp3_ksl *ksl, nghttp3_ksl_blk *blk,
const nghttp3_ksl_key *key,
nghttp3_ksl_compar compar) {
size_t i;
nghttp3_ksl_node *node;

Expand Down Expand Up @@ -307,7 +307,7 @@ int nghttp3_ksl_insert(nghttp3_ksl *ksl, nghttp3_ksl_it *it,
}

for (;;) {
i = ksl_bsearch(ksl, blk, key, ksl->compar);
i = ksl_search(ksl, blk, key, ksl->compar);

if (blk->leaf) {
if (i < blk->n &&
Expand Down Expand Up @@ -549,7 +549,7 @@ int nghttp3_ksl_remove(nghttp3_ksl *ksl, nghttp3_ksl_it *it,
}

for (;;) {
i = ksl_bsearch(ksl, blk, key, ksl->compar);
i = ksl_search(ksl, blk, key, ksl->compar);

if (i == blk->n) {
if (it) {
Expand Down Expand Up @@ -613,43 +613,7 @@ int nghttp3_ksl_remove(nghttp3_ksl *ksl, nghttp3_ksl_it *it,

nghttp3_ksl_it nghttp3_ksl_lower_bound(nghttp3_ksl *ksl,
const nghttp3_ksl_key *key) {
nghttp3_ksl_blk *blk = ksl->head;
nghttp3_ksl_it it;
size_t i;

if (!blk) {
nghttp3_ksl_it_init(&it, ksl, &null_blk, 0);
return it;
}

for (;;) {
i = ksl_bsearch(ksl, blk, key, ksl->compar);

if (blk->leaf) {
if (i == blk->n && blk->next) {
blk = blk->next;
i = 0;
}
nghttp3_ksl_it_init(&it, ksl, blk, i);
return it;
}

if (i == blk->n) {
/* This happens if descendant has smaller key. Fast forward to
find last node in this subtree. */
for (; !blk->leaf; blk = nghttp3_ksl_nth_node(ksl, blk, blk->n - 1)->blk)
;
if (blk->next) {
blk = blk->next;
i = 0;
} else {
i = blk->n;
}
nghttp3_ksl_it_init(&it, ksl, blk, i);
return it;
}
blk = nghttp3_ksl_nth_node(ksl, blk, i)->blk;
}
return nghttp3_ksl_lower_bound_compar(ksl, key, ksl->compar);
}

nghttp3_ksl_it nghttp3_ksl_lower_bound_compar(nghttp3_ksl *ksl,
Expand All @@ -665,7 +629,7 @@ nghttp3_ksl_it nghttp3_ksl_lower_bound_compar(nghttp3_ksl *ksl,
}

for (;;) {
i = ksl_bsearch(ksl, blk, key, compar);
i = ksl_search(ksl, blk, key, compar);

if (blk->leaf) {
if (i == blk->n && blk->next) {
Expand Down Expand Up @@ -703,7 +667,7 @@ void nghttp3_ksl_update_key(nghttp3_ksl *ksl, const nghttp3_ksl_key *old_key,
assert(ksl->head);

for (;;) {
i = ksl_bsearch(ksl, blk, old_key, ksl->compar);
i = ksl_search(ksl, blk, old_key, ksl->compar);

assert(i < blk->n);
node = nghttp3_ksl_nth_node(ksl, blk, i);
Expand Down
2 changes: 2 additions & 0 deletions lib/nghttp3_range.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ nghttp3_range nghttp3_range_intersect(const nghttp3_range *a,
nghttp3_range r = {0, 0};
uint64_t begin = nghttp3_max_uint64(a->begin, b->begin);
uint64_t end = nghttp3_min_uint64(a->end, b->end);

if (begin < end) {
nghttp3_range_init(&r, begin, end);
}

return r;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/nghttp3_range.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ uint64_t nghttp3_range_len(const nghttp3_range *r);

/*
* nghttp3_range_eq returns nonzero if |a| equals |b|, such that
* a->begin == b->begin, and a->end == b->end hold.
* a->begin == b->begin and a->end == b->end hold.
*/
int nghttp3_range_eq(const nghttp3_range *a, const nghttp3_range *b);

Expand Down
Loading