smartlist_add(new_primary_guards, guard);
} SMARTLIST_FOREACH_END(guard);
- /* Can we keep any older primary guards? First remove all the ones
- * that we already kept. */
SMARTLIST_FOREACH_BEGIN(old_primary_guards, entry_guard_t *, guard) {
+ /* Can we keep any older primary guards? First remove all the ones
+ * that we already kept. */
if (smartlist_contains(new_primary_guards, guard)) {
SMARTLIST_DEL_CURRENT_KEEPORDER(old_primary_guards, guard);
- }
- } SMARTLIST_FOREACH_END(guard);
-
- /* Now add any that are still good. */
- SMARTLIST_FOREACH_BEGIN(old_primary_guards, entry_guard_t *, guard) {
- if (smartlist_len(new_primary_guards) >= N_PRIMARY_GUARDS)
- break;
- if (! guard->is_filtered_guard)
continue;
- guard->is_primary = 1;
- smartlist_add(new_primary_guards, guard);
- SMARTLIST_DEL_CURRENT_KEEPORDER(old_primary_guards, guard);
- } SMARTLIST_FOREACH_END(guard);
+ }
- /* Mark the remaining previous primary guards as non-primary */
- SMARTLIST_FOREACH_BEGIN(old_primary_guards, entry_guard_t *, guard) {
- guard->is_primary = 0;
+ /* Now add any that are still good. */
+ if (smartlist_len(new_primary_guards) < N_PRIMARY_GUARDS &&
+ guard->is_filtered_guard) {
+ guard->is_primary = 1;
+ smartlist_add(new_primary_guards, guard);
+ SMARTLIST_DEL_CURRENT_KEEPORDER(old_primary_guards, guard);
+ } else {
+ /* Mark the remaining previous primary guards as non-primary */
+ guard->is_primary = 0;
+ }
} SMARTLIST_FOREACH_END(guard);
/* Finally, fill out the list with sampled guards. */
});
#endif /* 1 */
- int any_change = 0;
- if (smartlist_len(gs->primary_entry_guards) !=
- smartlist_len(new_primary_guards)) {
- any_change = 1;
- } else {
- SMARTLIST_FOREACH_BEGIN(gs->primary_entry_guards, entry_guard_t *, g) {
- if (g != smartlist_get(new_primary_guards, g_sl_idx)) {
- any_change = 1;
- }
- } SMARTLIST_FOREACH_END(g);
- }
-
+ const int any_change = !smartlist_ptrs_eq(gs->primary_entry_guards,
+ new_primary_guards);
if (any_change) {
log_info(LD_GUARD, "Primary entry guards have changed. "
"New primary guard list is: ");
return 1;
}
+/**
+ * Return true if there is shallow equality between smartlists -
+ * i.e. all indices correspond to exactly same object (pointer
+ * values are matching). Otherwise, return false.
+ */
+int
+smartlist_ptrs_eq(const smartlist_t *s1, const smartlist_t *s2)
+{
+ if (s1 == s2)
+ return 1;
+
+ // Note: pointers cannot both be NULL at this point, because
+ // above check.
+ if (s1 == NULL || s2 == NULL)
+ return 0;
+
+ if (smartlist_len(s1) != smartlist_len(s2))
+ return 0;
+
+ for (int i = 0; i < smartlist_len(s1); i++) {
+ if (smartlist_get(s1, i) != smartlist_get(s2, i))
+ return 0;
+ }
+
+ return 1;
+}
+
/** Return true iff <b>sl</b> has some element E such that
* tor_memeq(E,<b>element</b>,DIGEST_LEN)
*/
void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2);
void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2);
+int smartlist_ptrs_eq(const smartlist_t *s1,
+ const smartlist_t *s2);
+
void smartlist_sort(smartlist_t *sl,
int (*compare)(const void **a, const void **b));
void *smartlist_get_most_frequent_(const smartlist_t *sl,