int i;
unsigned select, count, space;
rbnode_type* node;
- for(i = 0; i<try_random; i++) {
- id = ((unsigned)ub_random(outnet->rnd)>>8) & 0xffff;
- if(!reuse_tcp_by_id_find(reuse, id)) {
- return id;
- }
- }
/* make really sure the tree is not empty */
if(reuse->tree_by_id.count == 0) {
return id;
}
+ /* try to find random empty spots by picking them */
+ for(i = 0; i<try_random; i++) {
+ id = ((unsigned)ub_random(outnet->rnd)>>8) & 0xffff;
+ if(!reuse_tcp_by_id_find(reuse, id)) {
+ return id;
+ }
+ }
+
/* equally pick a random unused element from the tree that is
* not in use. Pick a the n-th index of an ununused number,
* then loop over the empty spaces in the tree and find it */
space = nextid - curid - 1;
if(select < count + space) {
/* here it is */
- return curid + 1 + ub_random_max(
- outnet->rnd, space);
+ return curid + 1 + (select - count);
}
count += space;
}
node = rbtree_last(&reuse->tree_by_id);
log_assert(node && node != RBTREE_NULL); /* tree not empty */
curid = tree_by_id_get_id(node);
- space = 0xffff - curid;
- log_assert(select < count + space);
- return curid + 1 + ub_random_max(outnet->rnd, space);
+ return curid + 1 + (select - count);
}
struct waiting_tcp*