[tor-commits] [tor/master] Switch to offsetof()

nickm at torproject.org nickm at torproject.org
Thu Aug 3 12:58:36 UTC 2017


commit 5ee6ca8da22ad8da94c829e6c02c05920742c364
Author: Neel Chauhan <neel at neelc.org>
Date:   Mon Jul 31 19:30:30 2017 -0400

    Switch to offsetof()
---
 src/common/container.c       |  4 ++--
 src/common/crypto.c          |  2 +-
 src/common/memarea.c         |  3 ++-
 src/common/util.h            | 12 ++----------
 src/or/buffers.c             |  2 +-
 src/or/circuitmux_ewma.c     |  6 +++---
 src/or/config.c              |  4 ++--
 src/or/connection_or.c       |  4 ++--
 src/or/dircollate.c          |  2 +-
 src/or/dns.c                 |  6 +++---
 src/or/ext_orport.c          |  2 +-
 src/or/policies.c            |  2 +-
 src/or/scheduler.c           | 16 ++++++++--------
 src/or/shared_random_state.c |  6 +++---
 src/or/statefile.c           |  6 +++---
 src/test/test_containers.c   |  2 +-
 16 files changed, 36 insertions(+), 43 deletions(-)

diff --git a/src/common/container.c b/src/common/container.c
index 689e7e55e..8645cb482 100644
--- a/src/common/container.c
+++ b/src/common/container.c
@@ -843,13 +843,13 @@ smartlist_sort_pointers(smartlist_t *sl)
  *   }
  *
  *   void timer_heap_insert(smartlist_t *heap, timer_t *timer) {
- *      smartlist_pqueue_add(heap, compare, STRUCT_OFFSET(timer_t, heap_index),
+ *      smartlist_pqueue_add(heap, compare, offsetof(timer_t, heap_index),
  *         timer);
  *   }
  *
  *   void timer_heap_pop(smartlist_t *heap) {
  *      return smartlist_pqueue_pop(heap, compare,
- *         STRUCT_OFFSET(timer_t, heap_index));
+ *         offsetof(timer_t, heap_index));
  *   }
  */
 
diff --git a/src/common/crypto.c b/src/common/crypto.c
index c258f239a..4d6a70bc4 100644
--- a/src/common/crypto.c
+++ b/src/common/crypto.c
@@ -1884,7 +1884,7 @@ crypto_digest_alloc_bytes(digest_algorithm_t alg)
   /* Helper: returns the number of bytes in the 'f' field of 'st' */
 #define STRUCT_FIELD_SIZE(st, f) (sizeof( ((st*)0)->f ))
   /* Gives the length of crypto_digest_t through the end of the field 'd' */
-#define END_OF_FIELD(f) (STRUCT_OFFSET(crypto_digest_t, f) + \
+#define END_OF_FIELD(f) (offsetof(crypto_digest_t, f) + \
                          STRUCT_FIELD_SIZE(crypto_digest_t, f))
   switch (alg) {
     case DIGEST_SHA1:
diff --git a/src/common/memarea.c b/src/common/memarea.c
index 659d1edf5..4e2a5e5fc 100644
--- a/src/common/memarea.c
+++ b/src/common/memarea.c
@@ -7,6 +7,7 @@
  */
 
 #include "orconfig.h"
+#include <stddef.h>
 #include <stdlib.h>
 #include "memarea.h"
 #include "util.h"
@@ -101,7 +102,7 @@ typedef struct memarea_chunk_t {
 
 /** How many bytes are needed for overhead before we get to the memory part
  * of a chunk? */
-#define CHUNK_HEADER_SIZE STRUCT_OFFSET(memarea_chunk_t, U_MEM)
+#define CHUNK_HEADER_SIZE offsetof(memarea_chunk_t, U_MEM)
 
 /** What's the smallest that we'll allocate a chunk? */
 #define CHUNK_SIZE 4096
diff --git a/src/common/util.h b/src/common/util.h
index d56abcee2..df581d240 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -109,19 +109,11 @@ extern int dmalloc_free(const char *file, const int line, void *pnt,
 
 void tor_log_mallinfo(int severity);
 
-/** Return the offset of <b>member</b> within the type <b>tp</b>, in bytes */
-#if defined(__GNUC__) && __GNUC__ > 3
-#define STRUCT_OFFSET(tp, member) __builtin_offsetof(tp, member)
-#else
- #define STRUCT_OFFSET(tp, member) \
-   ((off_t) (((char*)&((tp*)0)->member)-(char*)0))
-#endif
-
 /** Macro: yield a pointer to the field at position <b>off</b> within the
  * structure <b>st</b>.  Example:
  * <pre>
  *   struct a { int foo; int bar; } x;
- *   off_t bar_offset = STRUCT_OFFSET(struct a, bar);
+ *   off_t bar_offset = offsetof(struct a, bar);
  *   int *bar_p = STRUCT_VAR_P(&x, bar_offset);
  *   *bar_p = 3;
  * </pre>
@@ -138,7 +130,7 @@ void tor_log_mallinfo(int severity);
  * </pre>
  */
 #define SUBTYPE_P(p, subtype, basemember) \
-  ((void*) ( ((char*)(p)) - STRUCT_OFFSET(subtype, basemember) ))
+  ((void*) ( ((char*)(p)) - offsetof(subtype, basemember) ))
 
 /* Logic */
 /** Macro: true if two values have the same boolean value. */
diff --git a/src/or/buffers.c b/src/or/buffers.c
index d5ecfb848..bd84103c3 100644
--- a/src/or/buffers.c
+++ b/src/or/buffers.c
@@ -80,7 +80,7 @@ static int parse_socks_client(const uint8_t *data, size_t datalen,
 
 /* Chunk manipulation functions */
 
-#define CHUNK_HEADER_LEN STRUCT_OFFSET(chunk_t, mem[0])
+#define CHUNK_HEADER_LEN offsetof(chunk_t, mem[0])
 
 /* We leave this many NUL bytes at the end of the buffer. */
 #ifdef DISABLE_MEMORY_SENTINELS
diff --git a/src/or/circuitmux_ewma.c b/src/or/circuitmux_ewma.c
index c2440b13f..fde2d22a8 100644
--- a/src/or/circuitmux_ewma.c
+++ b/src/or/circuitmux_ewma.c
@@ -731,7 +731,7 @@ add_cell_ewma(ewma_policy_data_t *pol, cell_ewma_t *ewma)
 
   smartlist_pqueue_add(pol->active_circuit_pqueue,
                        compare_cell_ewma_counts,
-                       STRUCT_OFFSET(cell_ewma_t, heap_index),
+                       offsetof(cell_ewma_t, heap_index),
                        ewma);
 }
 
@@ -746,7 +746,7 @@ remove_cell_ewma(ewma_policy_data_t *pol, cell_ewma_t *ewma)
 
   smartlist_pqueue_remove(pol->active_circuit_pqueue,
                           compare_cell_ewma_counts,
-                          STRUCT_OFFSET(cell_ewma_t, heap_index),
+                          offsetof(cell_ewma_t, heap_index),
                           ewma);
 }
 
@@ -760,6 +760,6 @@ pop_first_cell_ewma(ewma_policy_data_t *pol)
 
   return smartlist_pqueue_pop(pol->active_circuit_pqueue,
                               compare_cell_ewma_counts,
-                              STRUCT_OFFSET(cell_ewma_t, heap_index));
+                              offsetof(cell_ewma_t, heap_index));
 }
 
diff --git a/src/or/config.c b/src/or/config.c
index 58d22d9f1..53fc2795c 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -177,7 +177,7 @@ static config_abbrev_t option_abbrevs_[] = {
  * or_options_t.<b>member</b>"
  */
 #define VAR(name,conftype,member,initvalue)                             \
-  { name, CONFIG_TYPE_ ## conftype, STRUCT_OFFSET(or_options_t, member), \
+  { name, CONFIG_TYPE_ ## conftype, offsetof(or_options_t, member),     \
       initvalue }
 /** As VAR, but the option name and member name are the same. */
 #define V(member,conftype,initvalue)                                    \
@@ -733,7 +733,7 @@ static uint64_t compute_real_max_mem_in_queues(const uint64_t val,
 STATIC config_format_t options_format = {
   sizeof(or_options_t),
   OR_OPTIONS_MAGIC,
-  STRUCT_OFFSET(or_options_t, magic_),
+  offsetof(or_options_t, magic_),
   option_abbrevs_,
   option_deprecation_notes_,
   option_vars_,
diff --git a/src/or/connection_or.c b/src/or/connection_or.c
index 051bf9a17..7c929e527 100644
--- a/src/or/connection_or.c
+++ b/src/or/connection_or.c
@@ -472,7 +472,7 @@ var_cell_pack_header(const var_cell_t *cell, char *hdr_out, int wide_circ_ids)
 var_cell_t *
 var_cell_new(uint16_t payload_len)
 {
-  size_t size = STRUCT_OFFSET(var_cell_t, payload) + payload_len;
+  size_t size = offsetof(var_cell_t, payload) + payload_len;
   var_cell_t *cell = tor_malloc_zero(size);
   cell->payload_len = payload_len;
   cell->command = 0;
@@ -491,7 +491,7 @@ var_cell_copy(const var_cell_t *src)
   size_t size = 0;
 
   if (src != NULL) {
-    size = STRUCT_OFFSET(var_cell_t, payload) + src->payload_len;
+    size = offsetof(var_cell_t, payload) + src->payload_len;
     copy = tor_malloc_zero(size);
     copy->payload_len = src->payload_len;
     copy->command = src->command;
diff --git a/src/or/dircollate.c b/src/or/dircollate.c
index 172364c5f..d34ebe8af 100644
--- a/src/or/dircollate.c
+++ b/src/or/dircollate.c
@@ -53,7 +53,7 @@ ddmap_entry_free(ddmap_entry_t *e)
 static ddmap_entry_t *
 ddmap_entry_new(int n_votes)
 {
-  return tor_malloc_zero(STRUCT_OFFSET(ddmap_entry_t, vrs_lst) +
+  return tor_malloc_zero(offsetof(ddmap_entry_t, vrs_lst) +
                          sizeof(vote_routerstatus_t *) * n_votes);
 }
 
diff --git a/src/or/dns.c b/src/or/dns.c
index cc062e30e..2d642773f 100644
--- a/src/or/dns.c
+++ b/src/or/dns.c
@@ -378,7 +378,7 @@ set_expiry(cached_resolve_t *resolve, time_t expires)
   resolve->expire = expires;
   smartlist_pqueue_add(cached_resolve_pqueue,
                        compare_cached_resolves_by_expiry_,
-                       STRUCT_OFFSET(cached_resolve_t, minheap_idx),
+                       offsetof(cached_resolve_t, minheap_idx),
                        resolve);
 }
 
@@ -425,7 +425,7 @@ purge_expired_resolves(time_t now)
       break;
     smartlist_pqueue_pop(cached_resolve_pqueue,
                          compare_cached_resolves_by_expiry_,
-                         STRUCT_OFFSET(cached_resolve_t, minheap_idx));
+                         offsetof(cached_resolve_t, minheap_idx));
 
     if (resolve->state == CACHE_STATE_PENDING) {
       log_debug(LD_EXIT,
@@ -2083,7 +2083,7 @@ assert_cache_ok_(void)
 
   smartlist_pqueue_assert_ok(cached_resolve_pqueue,
                              compare_cached_resolves_by_expiry_,
-                             STRUCT_OFFSET(cached_resolve_t, minheap_idx));
+                             offsetof(cached_resolve_t, minheap_idx));
 
   SMARTLIST_FOREACH(cached_resolve_pqueue, cached_resolve_t *, res,
     {
diff --git a/src/or/ext_orport.c b/src/or/ext_orport.c
index b60d2e55c..01dc06ce1 100644
--- a/src/or/ext_orport.c
+++ b/src/or/ext_orport.c
@@ -31,7 +31,7 @@
 ext_or_cmd_t *
 ext_or_cmd_new(uint16_t len)
 {
-  size_t size = STRUCT_OFFSET(ext_or_cmd_t, body) + len;
+  size_t size = offsetof(ext_or_cmd_t, body) + len;
   ext_or_cmd_t *cmd = tor_malloc(size);
   cmd->len = len;
   return cmd;
diff --git a/src/or/policies.c b/src/or/policies.c
index 3d49a6110..4c24bfbc3 100644
--- a/src/or/policies.c
+++ b/src/or/policies.c
@@ -2731,7 +2731,7 @@ parse_short_policy(const char *summary)
   }
 
   {
-    size_t size = STRUCT_OFFSET(short_policy_t, entries) +
+    size_t size = offsetof(short_policy_t, entries) +
       sizeof(short_policy_entry_t)*(n_entries);
     result = tor_malloc_zero(size);
 
diff --git a/src/or/scheduler.c b/src/or/scheduler.c
index fac545fba..0d31c7d58 100644
--- a/src/or/scheduler.c
+++ b/src/or/scheduler.c
@@ -282,7 +282,7 @@ scheduler_channel_doesnt_want_writes,(channel_t *chan))
      */
     smartlist_pqueue_remove(channels_pending,
                             scheduler_compare_channels,
-                            STRUCT_OFFSET(channel_t, sched_heap_idx),
+                            offsetof(channel_t, sched_heap_idx),
                             chan);
     chan->scheduler_state = SCHED_CHAN_WAITING_TO_WRITE;
     log_debug(LD_SCHED,
@@ -324,7 +324,7 @@ scheduler_channel_has_waiting_cells,(channel_t *chan))
     chan->scheduler_state = SCHED_CHAN_PENDING;
     smartlist_pqueue_add(channels_pending,
                          scheduler_compare_channels,
-                         STRUCT_OFFSET(channel_t, sched_heap_idx),
+                         offsetof(channel_t, sched_heap_idx),
                          chan);
     log_debug(LD_SCHED,
               "Channel " U64_FORMAT " at %p went from waiting_for_cells "
@@ -400,7 +400,7 @@ scheduler_release_channel,(channel_t *chan))
   if (chan->scheduler_state == SCHED_CHAN_PENDING) {
     smartlist_pqueue_remove(channels_pending,
                             scheduler_compare_channels,
-                            STRUCT_OFFSET(channel_t, sched_heap_idx),
+                            offsetof(channel_t, sched_heap_idx),
                             chan);
   }
 
@@ -430,7 +430,7 @@ scheduler_run, (void))
       /* Pop off a channel */
       chan = smartlist_pqueue_pop(channels_pending,
                                   scheduler_compare_channels,
-                                  STRUCT_OFFSET(channel_t, sched_heap_idx));
+                                  offsetof(channel_t, sched_heap_idx));
       tor_assert(chan);
 
       /* Figure out how many cells we can write */
@@ -531,7 +531,7 @@ scheduler_run, (void))
         readd_chan->scheduler_state = SCHED_CHAN_PENDING;
         smartlist_pqueue_add(channels_pending,
                              scheduler_compare_channels,
-                             STRUCT_OFFSET(channel_t, sched_heap_idx),
+                             offsetof(channel_t, sched_heap_idx),
                              readd_chan);
       } SMARTLIST_FOREACH_END(readd_chan);
       smartlist_free(to_readd);
@@ -581,7 +581,7 @@ scheduler_channel_wants_writes(channel_t *chan)
      */
     smartlist_pqueue_add(channels_pending,
                          scheduler_compare_channels,
-                         STRUCT_OFFSET(channel_t, sched_heap_idx),
+                         offsetof(channel_t, sched_heap_idx),
                          chan);
     chan->scheduler_state = SCHED_CHAN_PENDING;
     log_debug(LD_SCHED,
@@ -624,11 +624,11 @@ scheduler_touch_channel(channel_t *chan)
     /* Remove and re-add it */
     smartlist_pqueue_remove(channels_pending,
                             scheduler_compare_channels,
-                            STRUCT_OFFSET(channel_t, sched_heap_idx),
+                            offsetof(channel_t, sched_heap_idx),
                             chan);
     smartlist_pqueue_add(channels_pending,
                          scheduler_compare_channels,
-                         STRUCT_OFFSET(channel_t, sched_heap_idx),
+                         offsetof(channel_t, sched_heap_idx),
                          chan);
   }
   /* else no-op, since it isn't in the queue */
diff --git a/src/or/shared_random_state.c b/src/or/shared_random_state.c
index 89d2e8d7f..ef026e7f2 100644
--- a/src/or/shared_random_state.c
+++ b/src/or/shared_random_state.c
@@ -42,7 +42,7 @@ static const char dstate_cur_srv_key[] = "SharedRandCurrentValue";
 
 /* These next two are duplicates or near-duplicates from config.c */
 #define VAR(name, conftype, member, initvalue)                              \
-  { name, CONFIG_TYPE_ ## conftype, STRUCT_OFFSET(sr_disk_state_t, member), \
+  { name, CONFIG_TYPE_ ## conftype, offsetof(sr_disk_state_t, member),      \
     initvalue }
 /* As VAR, but the option name and member name are the same. */
 #define V(member, conftype, initvalue) \
@@ -77,14 +77,14 @@ static config_var_t state_vars[] = {
  * lets us preserve options from versions of Tor newer than us. */
 static config_var_t state_extra_var = {
   "__extra", CONFIG_TYPE_LINELIST,
-  STRUCT_OFFSET(sr_disk_state_t, ExtraLines), NULL
+  offsetof(sr_disk_state_t, ExtraLines), NULL
 };
 
 /* Configuration format of sr_disk_state_t. */
 static const config_format_t state_format = {
   sizeof(sr_disk_state_t),
   SR_DISK_STATE_MAGIC,
-  STRUCT_OFFSET(sr_disk_state_t, magic_),
+  offsetof(sr_disk_state_t, magic_),
   NULL,
   NULL,
   state_vars,
diff --git a/src/or/statefile.c b/src/or/statefile.c
index d0606b301..aaed10409 100644
--- a/src/or/statefile.c
+++ b/src/or/statefile.c
@@ -55,7 +55,7 @@ static config_abbrev_t state_abbrevs_[] = {
 
 /*XXXX these next two are duplicates or near-duplicates from config.c */
 #define VAR(name,conftype,member,initvalue)                             \
-  { name, CONFIG_TYPE_ ## conftype, STRUCT_OFFSET(or_state_t, member),  \
+  { name, CONFIG_TYPE_ ## conftype, offsetof(or_state_t, member),       \
       initvalue }
 /** As VAR, but the option name and member name are the same. */
 #define V(member,conftype,initvalue)                                    \
@@ -131,14 +131,14 @@ static int or_state_validate_cb(void *old_options, void *options,
 /** "Extra" variable in the state that receives lines we can't parse. This
  * lets us preserve options from versions of Tor newer than us. */
 static config_var_t state_extra_var = {
-  "__extra", CONFIG_TYPE_LINELIST, STRUCT_OFFSET(or_state_t, ExtraLines), NULL
+  "__extra", CONFIG_TYPE_LINELIST, offsetof(or_state_t, ExtraLines), NULL
 };
 
 /** Configuration format for or_state_t. */
 static const config_format_t state_format = {
   sizeof(or_state_t),
   OR_STATE_MAGIC,
-  STRUCT_OFFSET(or_state_t, magic_),
+  offsetof(or_state_t, magic_),
   state_abbrevs_,
   NULL,
   state_vars_,
diff --git a/src/test/test_containers.c b/src/test/test_containers.c
index 54484a2a9..0005dcb02 100644
--- a/src/test/test_containers.c
+++ b/src/test/test_containers.c
@@ -681,7 +681,7 @@ test_container_pqueue(void *arg)
 {
   smartlist_t *sl = smartlist_new();
   int (*cmp)(const void *, const void*);
-  const int offset = STRUCT_OFFSET(pq_entry_t, idx);
+  const int offset = offsetof(pq_entry_t, idx);
 #define ENTRY(s) pq_entry_t s = { #s, -1 }
   ENTRY(cows);
   ENTRY(zebras);





More information about the tor-commits mailing list