Commit 5adfa09f authored by Nick Mathewson's avatar Nick Mathewson 🤹
Browse files

r13477@catbus: nickm | 2007-06-17 14:22:03 -0400

 Sun CC likes to give warnings for the do { } while(0) construction for making statement-like macros.  Define STMT_BEGIN/STMT_END macros that do the right thing, and use them everywhere.


svn:r10645
parent 93f32db4
Loading
Loading
Loading
Loading
+18 −3
Original line number Diff line number Diff line
@@ -120,6 +120,21 @@ extern INLINE double U64_TO_DBL(uint64_t x) {
#define PREDICT_UNLIKELY(exp) (exp)
#endif

/* Ways to declare macros. */
#define STMT_NIL (void)0
#ifdef __GNUC__
#define STMT_BEGIN (void) ({
#define STMT_END })
#else
#if defined(sun) || defined(__sun__)
#define STMT_BEGIN if (1) {
#define STMT_END } else STMT_NIL
#else
#define STMT_BEGIN do {
#define STMT_END } while(0)
#endif
#endif

/* ===== String compatibility */
#ifdef MS_WINDOWS
/* Windows names string functions differently from most other platforms. */
@@ -339,9 +354,9 @@ void tor_mutex_free(tor_mutex_t *m);
unsigned long tor_get_thread_id(void);
#else
#define tor_mutex_new() ((tor_mutex_t*)tor_malloc(sizeof(int)))
#define tor_mutex_acquire(m) do { } while (0)
#define tor_mutex_release(m) do { } while (0)
#define tor_mutex_free(m) do { tor_free(m); } while (0)
#define tor_mutex_acquire(m) STMT_NIL
#define tor_mutex_release(m) STMT_NIL
#define tor_mutex_free(m) STMT_BEGIN tor_free(m); STMT_END
#define tor_get_thread_id() (1UL)
#endif

+2 −2
Original line number Diff line number Diff line
@@ -665,8 +665,8 @@ smartlist_uniq_digests(smartlist_t *sl)
    HT_HEAD(prefix ## impl, prefix ## entry_t) head;      \
  }

DEFINE_MAP_STRUCTS(strmap_t, char *key, strmap_)
DEFINE_MAP_STRUCTS(digestmap_t, char key[DIGEST_LEN], digestmap_)
DEFINE_MAP_STRUCTS(strmap_t, char *key, strmap_);
DEFINE_MAP_STRUCTS(digestmap_t, char key[DIGEST_LEN], digestmap_);

/** Helper: compare strmap_entry_t objects by key value. */
static INLINE int
+4 −4
Original line number Diff line number Diff line
@@ -159,24 +159,24 @@ char *smartlist_join_strings2(smartlist_t *sl, const char *join,
 * </pre>
 */
#define SMARTLIST_FOREACH(sl, type, var, cmd)                   \
  do {                                                          \
  STMT_BEGIN                                                    \
    int var ## _sl_idx, var ## _sl_len=(sl)->num_used;          \
    type var;                                                   \
    for (var ## _sl_idx = 0; var ## _sl_idx < var ## _sl_len;   \
         ++var ## _sl_idx) {                                    \
      var = (sl)->list[var ## _sl_idx];                         \
      cmd;                                                      \
    } } while (0)
    } STMT_END

/** Helper: While in a SMARTLIST_FOREACH loop over the list <b>sl</b> indexed
 * with the variable <b>var</b>, remove the current element in a way that
 * won't confuse the loop. */
#define SMARTLIST_DEL_CURRENT(sl, var)          \
  do {                                          \
  STMT_BEGIN                                    \
    smartlist_del(sl, var ## _sl_idx);          \
    --var ## _sl_idx;                           \
    --var ## _sl_len;                           \
  } while (0);
  STMT_END

#define DECLARE_MAP_FNS(maptype, keytype, prefix)                       \
  typedef struct maptype maptype;                                       \
+1 −3
Original line number Diff line number Diff line
@@ -79,9 +79,7 @@ ht_string_hash(const char *s)
}

#define _HT_SET_HASH(elm, field, hashfn)        \
  do {                                          \
    (elm)->field.hte_hash = hashfn(elm);        \
  } while (0)
    (elm)->field.hte_hash = hashfn(elm)

#define HT_FOREACH(x, name, head)                 \
  for ((x) = HT_START(name, head);                \
+2 −2
Original line number Diff line number Diff line
@@ -131,10 +131,10 @@ void _log_fn(int severity, uint32_t domain,
#define log_fn(severity, domain, args...)               \
  _log_fn(severity, domain, __PRETTY_FUNCTION__, args)
#define log_debug(domain, args...)                                      \
  do {                                                                  \
  STMT_BEGIN                                                            \
    if (PREDICT_UNLIKELY(_log_global_min_severity == LOG_DEBUG))        \
      _log_fn(LOG_DEBUG, domain, __PRETTY_FUNCTION__, args);            \
  } while (0)
  STMT_END
#define log_info(domain, args...)                           \
  _log_fn(LOG_INFO, domain, __PRETTY_FUNCTION__, args)
#define log_notice(domain, args...)                         \
Loading