31 #if defined(GRPC_PORT_ISOLATED_RUNTIME)
36 #define MAX_PORT 32766
38 static int get_random_port_offset() {
40 double rnd =
static_cast<double>(rand()) /
41 (
static_cast<double>(RAND_MAX) + 1.0);
42 return static_cast<int>(rnd * (MAX_PORT - MIN_PORT + 1));
45 static int s_initial_offset = get_random_port_offset();
46 static gpr_atm s_pick_counter = 0;
48 static int grpc_pick_unused_port_or_die_impl(
void) {
49 int orig_counter_val =
51 GPR_ASSERT(orig_counter_val < (MAX_PORT - MIN_PORT + 1));
53 (s_initial_offset + orig_counter_val) % (MAX_PORT - MIN_PORT + 1);
58 int port = grpc_pick_unused_port_or_die_impl();