22 #if defined(GRPC_TEST_PICK_PORT)
39 static int* chosen_ports =
nullptr;
40 static size_t num_chosen_ports = 0;
44 static void init_default_port_picker() {
48 static int free_chosen_port_locked(
int port) {
54 for (
i = 0;
i < num_chosen_ports;
i++) {
55 if (chosen_ports[
i] ==
port) {
62 chosen_ports[found_at] = chosen_ports[num_chosen_ports - 1];
69 static void free_chosen_ports(
void) {
73 for (
i = 0;
i < num_chosen_ports;
i++) {
80 static void chose_port_locked(
int port) {
81 if (chosen_ports ==
nullptr) {
82 atexit(free_chosen_ports);
85 chosen_ports =
static_cast<int*
>(
86 gpr_realloc(chosen_ports,
sizeof(
int) * num_chosen_ports));
87 chosen_ports[num_chosen_ports - 1] =
port;
90 static int grpc_pick_unused_port_impl(
void) {
91 gpr_once_init(&g_default_port_picker_init, init_default_port_picker);
95 chose_port_locked(
port);
101 static int grpc_pick_unused_port_or_die_impl(
void) {
105 "gRPC tests require a helper port server to allocate ports used \n"
106 "during the test.\n\n"
107 "This server is not currently running.\n\n"
108 "To start it, run tools/run_tests/start_port_server.py\n\n");
114 static void grpc_recycle_unused_port_impl(
int port) {
115 gpr_once_init(&g_default_port_picker_init, init_default_port_picker);
121 grpc_pick_unused_port_impl, grpc_pick_unused_port_or_die_impl,
122 grpc_recycle_unused_port_impl};
140 g_pick_port_functions = functions;