Commit 6969bd9a authored by Nick Mathewson's avatar Nick Mathewson 🦀
Browse files

Autoconvert most circuit-list iterations to smartlist iterations

Breaks compilation.

Used this coccinelle script:

@@
identifier c;
typedef circuit_t;
iterator name TOR_LIST_FOREACH;
iterator name SMARTLIST_FOREACH_BEGIN;
statement S;
@@
- circuit_t *c;
   ...
- TOR_LIST_FOREACH(c, \(&global_circuitlist\|circuit_get_global_list()\), head)
+ SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, c)
  S
+ SMARTLIST_FOREACH_END(c);
parent db2af2ab
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -1140,11 +1140,10 @@ pathbias_count_circs_in_states(entry_guard_t *guard,
                              path_state_t from,
                              path_state_t to)
{
  circuit_t *circ;
  int open_circuits = 0;

  /* Count currently open circuits. Give them the benefit of the doubt. */
  TOR_LIST_FOREACH(circ, circuit_get_global_list(), head) {
  SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
    origin_circuit_t *ocirc = NULL;
    if (!CIRCUIT_IS_ORIGIN(circ) || /* didn't originate here */
        circ->marked_for_close) /* already counted */
@@ -1167,6 +1166,7 @@ pathbias_count_circs_in_states(entry_guard_t *guard,
      open_circuits++;
    }
  }
  SMARTLIST_FOREACH_END(circ);

  return open_circuits;
}
+16 −16
Original line number Diff line number Diff line
@@ -933,10 +933,9 @@ circuit_dump_conn_details(int severity,
void
circuit_dump_by_conn(connection_t *conn, int severity)
{
  circuit_t *circ;
  edge_connection_t *tmpconn;

  TOR_LIST_FOREACH(circ, &global_circuitlist, head) {
  SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
    circid_t n_circ_id = circ->n_circ_id, p_circ_id = 0;

    if (circ->marked_for_close) {
@@ -967,6 +966,7 @@ circuit_dump_by_conn(connection_t *conn, int severity)
      }
    }
  }
  SMARTLIST_FOREACH_END(circ);
}

/** Return the circuit whose global ID is <b>id</b>, or NULL if no
@@ -974,8 +974,7 @@ circuit_dump_by_conn(connection_t *conn, int severity)
origin_circuit_t *
circuit_get_by_global_id(uint32_t id)
{
  circuit_t *circ;
  TOR_LIST_FOREACH(circ, &global_circuitlist, head) {
  SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
    if (CIRCUIT_IS_ORIGIN(circ) &&
        TO_ORIGIN_CIRCUIT(circ)->global_identifier == id) {
      if (circ->marked_for_close)
@@ -984,6 +983,7 @@ circuit_get_by_global_id(uint32_t id)
        return TO_ORIGIN_CIRCUIT(circ);
    }
  }
  SMARTLIST_FOREACH_END(circ);
  return NULL;
}

@@ -1152,17 +1152,17 @@ circuit_unlink_all_from_channel(channel_t *chan, int reason)

#ifdef DEBUG_CIRCUIT_UNLINK_ALL
  {
    circuit_t *circ;
    smartlist_t *detached_2 = smartlist_new();
    int mismatch = 0, badlen = 0;

    TOR_LIST_FOREACH(circ, &global_circuitlist, head) {
    SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
      if (circ->n_chan == chan ||
          (!CIRCUIT_IS_ORIGIN(circ) &&
           TO_OR_CIRCUIT(circ)->p_chan == chan)) {
        smartlist_add(detached_2, circ);
      }
    }
    SMARTLIST_FOREACH_END(circ);

    if (smartlist_len(detached) != smartlist_len(detached_2)) {
       log_warn(LD_BUG, "List of detached circuits had the wrong length! "
@@ -1236,8 +1236,7 @@ circuit_unlink_all_from_channel(channel_t *chan, int reason)
origin_circuit_t *
circuit_get_ready_rend_circ_by_rend_data(const rend_data_t *rend_data)
{
  circuit_t *circ;
  TOR_LIST_FOREACH(circ, &global_circuitlist, head) {
  SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
    if (!circ->marked_for_close &&
        circ->purpose == CIRCUIT_PURPOSE_C_REND_READY) {
      origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
@@ -1250,6 +1249,7 @@ circuit_get_ready_rend_circ_by_rend_data(const rend_data_t *rend_data)
        return ocirc;
    }
  }
  SMARTLIST_FOREACH_END(circ);
  return NULL;
}

@@ -1470,7 +1470,6 @@ origin_circuit_t *
circuit_find_to_cannibalize(uint8_t purpose, extend_info_t *info,
                            int flags)
{
  circuit_t *circ_;
  origin_circuit_t *best=NULL;
  int need_uptime = (flags & CIRCLAUNCH_NEED_UPTIME) != 0;
  int need_capacity = (flags & CIRCLAUNCH_NEED_CAPACITY) != 0;
@@ -1486,7 +1485,7 @@ circuit_find_to_cannibalize(uint8_t purpose, extend_info_t *info,
            "capacity %d, internal %d",
            purpose, need_uptime, need_capacity, internal);

  TOR_LIST_FOREACH(circ_, &global_circuitlist, head) {
  SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ_) {
    if (CIRCUIT_IS_ORIGIN(circ_) &&
        circ_->state == CIRCUIT_STATE_OPEN &&
        !circ_->marked_for_close &&
@@ -1536,6 +1535,7 @@ circuit_find_to_cannibalize(uint8_t purpose, extend_info_t *info,
      }
    }
  }
  SMARTLIST_FOREACH_END(circ_);
  return best;
}

@@ -1575,13 +1575,13 @@ circuit_get_cpath_hop(origin_circuit_t *circ, int hopnum)
void
circuit_mark_all_unused_circs(void)
{
  circuit_t *circ;
  TOR_LIST_FOREACH(circ, &global_circuitlist, head) {
  SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
    if (CIRCUIT_IS_ORIGIN(circ) &&
        !circ->marked_for_close &&
        !circ->timestamp_dirty)
      circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
  }
  SMARTLIST_FOREACH_END(circ);
}

/** Go through the circuitlist; for each circuit that starts at us
@@ -1594,14 +1594,14 @@ circuit_mark_all_unused_circs(void)
void
circuit_mark_all_dirty_circs_as_unusable(void)
{
  circuit_t *circ;
  TOR_LIST_FOREACH(circ, &global_circuitlist, head) {
  SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
    if (CIRCUIT_IS_ORIGIN(circ) &&
        !circ->marked_for_close &&
        circ->timestamp_dirty) {
      mark_circuit_unusable_for_new_conns(TO_ORIGIN_CIRCUIT(circ));
    }
  }
  SMARTLIST_FOREACH_END(circ);
}

/** Mark <b>circ</b> to be closed next time we call
@@ -1953,7 +1953,6 @@ circuits_handle_oom(size_t current_allocation)
{
  /* Let's hope there's enough slack space for this allocation here... */
  smartlist_t *circlist = smartlist_new();
  circuit_t *circ;
  size_t mem_to_recover;
  size_t mem_recovered=0;
  int n_circuits_killed=0;
@@ -1987,10 +1986,11 @@ circuits_handle_oom(size_t current_allocation)

  /* This algorithm itself assumes that you've got enough memory slack
   * to actually run it. */
  TOR_LIST_FOREACH(circ, &global_circuitlist, head) {
  SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
    circ->age_tmp = circuit_max_queued_item_age(circ, now_ms);
    smartlist_add(circlist, circ);
  }
  SMARTLIST_FOREACH_END(circ);

  /* This is O(n log n); there are faster algorithms we could use instead.
   * Let's hope this doesn't happen enough to be in the critical path. */
+14 −15
Original line number Diff line number Diff line
@@ -268,7 +268,6 @@ circuit_get_best(const entry_connection_t *conn,
                 int must_be_open, uint8_t purpose,
                 int need_uptime, int need_internal)
{
  circuit_t *circ;
  origin_circuit_t *best=NULL;
  struct timeval now;
  int intro_going_on_but_too_old = 0;
@@ -281,7 +280,7 @@ circuit_get_best(const entry_connection_t *conn,

  tor_gettimeofday(&now);

  TOR_LIST_FOREACH(circ, circuit_get_global_list(), head) {
  SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
    origin_circuit_t *origin_circ;
    if (!CIRCUIT_IS_ORIGIN(circ))
      continue;
@@ -305,6 +304,7 @@ circuit_get_best(const entry_connection_t *conn,
    if (!best || circuit_is_better(origin_circ,best,conn))
      best = origin_circ;
  }
  SMARTLIST_FOREACH_END(circ);

  if (!best && intro_going_on_but_too_old)
    log_info(LD_REND|LD_CIRC, "There is an intro circuit being created "
@@ -318,11 +318,9 @@ circuit_get_best(const entry_connection_t *conn,
static int
count_pending_general_client_circuits(void)
{
  const circuit_t *circ;

  int count = 0;

  TOR_LIST_FOREACH(circ, circuit_get_global_list(), head) {
  SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
    if (circ->marked_for_close ||
        circ->state == CIRCUIT_STATE_OPEN ||
        circ->purpose != CIRCUIT_PURPOSE_C_GENERAL ||
@@ -331,6 +329,7 @@ count_pending_general_client_circuits(void)

    ++count;
  }
  SMARTLIST_FOREACH_END(circ);

  return count;
}
@@ -800,9 +799,8 @@ circuit_log_ancient_one_hop_circuits(int age)
  time_t cutoff = now - age;
  int n_found = 0;
  smartlist_t *log_these = smartlist_new();
  const circuit_t *circ;

  TOR_LIST_FOREACH(circ, circuit_get_global_list(), head) {
  SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
    const origin_circuit_t *ocirc;
    if (! CIRCUIT_IS_ORIGIN(circ))
      continue;
@@ -817,6 +815,7 @@ circuit_log_ancient_one_hop_circuits(int age)
        smartlist_add(log_these, (origin_circuit_t*) ocirc);
    }
  }
  SMARTLIST_FOREACH_END(circ);

  if (n_found == 0)
    goto done;
@@ -938,7 +937,6 @@ int
circuit_stream_is_being_handled(entry_connection_t *conn,
                                uint16_t port, int min)
{
  circuit_t *circ;
  const node_t *exitnode;
  int num=0;
  time_t now = time(NULL);
@@ -946,7 +944,7 @@ circuit_stream_is_being_handled(entry_connection_t *conn,
                                   get_options()->LongLivedPorts,
                                   conn ? conn->socks_request->port : port);

  TOR_LIST_FOREACH(circ, circuit_get_global_list(), head) {
  SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
    if (CIRCUIT_IS_ORIGIN(circ) &&
        !circ->marked_for_close &&
        circ->purpose == CIRCUIT_PURPOSE_C_GENERAL &&
@@ -976,6 +974,7 @@ circuit_stream_is_being_handled(entry_connection_t *conn,
      }
    }
  }
  SMARTLIST_FOREACH_END(circ);
  return 0;
}

@@ -989,7 +988,6 @@ circuit_stream_is_being_handled(entry_connection_t *conn,
static void
circuit_predict_and_launch_new(void)
{
  circuit_t *circ;
  int num=0, num_internal=0, num_uptime_internal=0;
  int hidserv_needs_uptime=0, hidserv_needs_capacity=1;
  int port_needs_uptime=0, port_needs_capacity=1;
@@ -997,7 +995,7 @@ circuit_predict_and_launch_new(void)
  int flags = 0;

  /* First, count how many of each type of circuit we have already. */
  TOR_LIST_FOREACH(circ, circuit_get_global_list(), head) {
  SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
    cpath_build_state_t *build_state;
    origin_circuit_t *origin_circ;
    if (!CIRCUIT_IS_ORIGIN(circ))
@@ -1020,6 +1018,7 @@ circuit_predict_and_launch_new(void)
    if (build_state->need_uptime && build_state->is_internal)
      num_uptime_internal++;
  }
  SMARTLIST_FOREACH_END(circ);

  /* If that's enough, then stop now. */
  if (num >= MAX_UNUSED_OPEN_CIRCUITS)
@@ -1318,11 +1317,10 @@ circuit_expire_old_circuits_clientside(void)
void
circuit_expire_old_circuits_serverside(time_t now)
{
  circuit_t *circ;
  or_circuit_t *or_circ;
  time_t cutoff = now - IDLE_ONE_HOP_CIRC_TIMEOUT;

  TOR_LIST_FOREACH(circ, circuit_get_global_list(), head) {
  SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
    if (circ->marked_for_close || CIRCUIT_IS_ORIGIN(circ))
      continue;
    or_circ = TO_OR_CIRCUIT(circ);
@@ -1339,6 +1337,7 @@ circuit_expire_old_circuits_serverside(time_t now)
      circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
    }
  }
  SMARTLIST_FOREACH_END(circ);
}

/** Number of testing circuits we want open before testing our bandwidth. */
@@ -1363,18 +1362,18 @@ reset_bandwidth_test(void)
int
circuit_enough_testing_circs(void)
{
  circuit_t *circ;
  int num = 0;

  if (have_performed_bandwidth_test)
    return 1;

  TOR_LIST_FOREACH(circ, circuit_get_global_list(), head) {
  SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
    if (!circ->marked_for_close && CIRCUIT_IS_ORIGIN(circ) &&
        circ->purpose == CIRCUIT_PURPOSE_TESTING &&
        circ->state == CIRCUIT_STATE_OPEN)
      num++;
  }
  SMARTLIST_FOREACH_END(circ);
  return num >= NUM_PARALLEL_TESTING_CIRCS;
}

+8 −8
Original line number Diff line number Diff line
@@ -194,14 +194,14 @@ log_severity_to_event(int severity)
static void
clear_circ_bw_fields(void)
{
  circuit_t *circ;
  origin_circuit_t *ocirc;
  TOR_LIST_FOREACH(circ, circuit_get_global_list(), head) {
  SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
    if (!CIRCUIT_IS_ORIGIN(circ))
      continue;
    ocirc = TO_ORIGIN_CIRCUIT(circ);
    ocirc->n_written_circ_bw = ocirc->n_read_circ_bw = 0;
  }
  SMARTLIST_FOREACH_END(circ);
}

/** Set <b>global_event_mask*</b> to the bitwise OR of each live control
@@ -1879,9 +1879,8 @@ getinfo_helper_events(control_connection_t *control_conn,
{
  (void) control_conn;
  if (!strcmp(question, "circuit-status")) {
    circuit_t *circ_;
    smartlist_t *status = smartlist_new();
    TOR_LIST_FOREACH(circ_, circuit_get_global_list(), head) {
    SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ_) {
      origin_circuit_t *circ;
      char *circdesc;
      const char *state;
@@ -1903,6 +1902,7 @@ getinfo_helper_events(control_connection_t *control_conn,
                   state, *circdesc ? " " : "", circdesc);
      tor_free(circdesc);
    }
    SMARTLIST_FOREACH_END(circ_);
    *answer = smartlist_join_strings(status, "\r\n", 0, NULL);
    SMARTLIST_FOREACH(status, char *, cp, tor_free(cp));
    smartlist_free(status);
@@ -3908,12 +3908,11 @@ control_event_stream_bandwidth_used(void)
int
control_event_circ_bandwidth_used(void)
{
  circuit_t *circ;
  origin_circuit_t *ocirc;
  if (!EVENT_IS_INTERESTING(EVENT_CIRC_BANDWIDTH_USED))
    return 0;

  TOR_LIST_FOREACH(circ, circuit_get_global_list(), head) {
  SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
    if (!CIRCUIT_IS_ORIGIN(circ))
      continue;
    ocirc = TO_ORIGIN_CIRCUIT(circ);
@@ -3926,6 +3925,7 @@ control_event_circ_bandwidth_used(void)
                       (unsigned long)ocirc->n_written_circ_bw);
    ocirc->n_written_circ_bw = ocirc->n_read_circ_bw = 0;
  }
  SMARTLIST_FOREACH_END(circ);

  return 0;
}
@@ -4090,14 +4090,13 @@ format_cell_stats(char **event_string, circuit_t *circ,
int
control_event_circuit_cell_stats(void)
{
  circuit_t *circ;
  cell_stats_t *cell_stats;
  char *event_string;
  if (!get_options()->TestingEnableCellStatsEvent ||
      !EVENT_IS_INTERESTING(EVENT_CELL_STATS))
    return 0;
  cell_stats = tor_malloc(sizeof(cell_stats_t));;
  TOR_LIST_FOREACH(circ, circuit_get_global_list(), head) {
  SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
    if (!circ->testing_cell_stats)
      continue;
    sum_up_cell_stats_by_command(circ, cell_stats);
@@ -4106,6 +4105,7 @@ control_event_circuit_cell_stats(void)
                       "650 CELL_STATS %s\r\n", event_string);
    tor_free(event_string);
  }
  SMARTLIST_FOREACH_END(circ);
  tor_free(cell_stats);
  return 0;
}
+2 −2
Original line number Diff line number Diff line
@@ -2322,15 +2322,15 @@ packed_cell_free(packed_cell_t *cell)
void
dump_cell_pool_usage(int severity)
{
  circuit_t *c;
  int n_circs = 0;
  int n_cells = 0;
  TOR_LIST_FOREACH(c, circuit_get_global_list(), head) {
  SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, c) {
    n_cells += c->n_chan_cells.n;
    if (!CIRCUIT_IS_ORIGIN(c))
      n_cells += TO_OR_CIRCUIT(c)->p_chan_cells.n;
    ++n_circs;
  }
  SMARTLIST_FOREACH_END(c);
  tor_log(severity, LD_MM,
          "%d cells allocated on %d circuits. %d cells leaked.",
          n_cells, n_circs, (int)total_cells_allocated - n_cells);
Loading