From 13bbaeec76ac67d250a6fa7b6d3d678620478f97 Mon Sep 17 00:00:00 2001 From: Mike Gelfand Date: Sat, 24 Jun 2017 13:17:04 +0300 Subject: [PATCH] Use field initializers for readability --- daemon/daemon.c | 2 +- libtransmission/announcer.c | 10 +++++++++- libtransmission/bitfield.c | 10 +++++++++- libtransmission/fdlimit.c | 9 ++++++++- libtransmission/list.c | 7 ++++++- libtransmission/net.c | 13 +++++++++++-- libtransmission/peer-io.c | 7 ++++++- libtransmission/peer-mgr.c | 19 +++++++++++++++++-- libtransmission/rpcimpl.c | 5 +++-- libtransmission/stats.c | 14 +++++++++++--- libtransmission/stats.h | 2 ++ libtransmission/tr-dht.c | 2 +- libtransmission/tr-lpd.c | 2 +- libtransmission/watchdir-generic.c | 2 +- libtransmission/watchdir-kqueue.c | 2 +- libtransmission/watchdir-test.c | 6 +++--- libtransmission/watchdir.c | 4 ++-- 17 files changed, 92 insertions(+), 24 deletions(-) diff --git a/daemon/daemon.c b/daemon/daemon.c index e156dd302..7b2a3b425 100644 --- a/daemon/daemon.c +++ b/daemon/daemon.c @@ -725,7 +725,7 @@ static int daemon_start(void* raw_arg, bool foreground) /* Create new timer event to report daemon status */ { - struct timeval one_sec = { 1, 0 }; + struct timeval one_sec = { .tv_sec = 1, .tv_usec = 0 }; status_ev = event_new(ev_base, -1, EV_PERSIST, &periodicUpdate, NULL); if (status_ev == NULL) diff --git a/libtransmission/announcer.c b/libtransmission/announcer.c index 599a07a10..61c4971ea 100644 --- a/libtransmission/announcer.c +++ b/libtransmission/announcer.c @@ -459,7 +459,15 @@ static tr_tier* getTier(tr_announcer* announcer, uint8_t const* info_hash, int t **** PUBLISH ***/ -static tr_tracker_event const TRACKER_EVENT_INIT = { 0, 0, 0, 0, 0, 0 }; +static tr_tracker_event const TRACKER_EVENT_INIT = +{ + .messageType = TR_TRACKER_WARNING, + .text = NULL, + .tracker = NULL, + .pex = NULL, + .pexCount = 0, + .seedProbability = 0 +}; static void publishMessage(tr_tier* tier, char const* msg, int type) { diff --git a/libtransmission/bitfield.c b/libtransmission/bitfield.c index da9dae3dc..6e5d67a7e 100644 --- a/libtransmission/bitfield.c +++ b/libtransmission/bitfield.c @@ -13,7 +13,15 @@ #include "tr-assert.h" #include "utils.h" /* tr_new0() */ -tr_bitfield const TR_BITFIELD_INIT = { NULL, 0, 0, 0, false, false }; +tr_bitfield const TR_BITFIELD_INIT = +{ + .bits = NULL, + .alloc_count = 0, + .bit_count = 0, + .true_count = 0, + .have_all_hint = false, + .have_none_hint = false +}; /**** ***** diff --git a/libtransmission/fdlimit.c b/libtransmission/fdlimit.c index f3005cea1..49c6a0c41 100644 --- a/libtransmission/fdlimit.c +++ b/libtransmission/fdlimit.c @@ -265,7 +265,14 @@ struct tr_fileset static void fileset_construct(struct tr_fileset* set, int n) { - struct tr_cached_file const TR_CACHED_FILE_INIT = { false, TR_BAD_SYS_FILE, 0, 0, 0 }; + struct tr_cached_file const TR_CACHED_FILE_INIT = + { + .is_writable = false, + .fd = TR_BAD_SYS_FILE, + .torrent_id = 0, + .file_index = 0, + .used_at = 0 + }; set->begin = tr_new(struct tr_cached_file, n); set->end = set->begin + n; diff --git a/libtransmission/list.c b/libtransmission/list.c index 421d18d72..5961bf77f 100644 --- a/libtransmission/list.c +++ b/libtransmission/list.c @@ -11,7 +11,12 @@ #include "platform.h" #include "utils.h" -static tr_list const TR_LIST_CLEAR = { NULL, NULL, NULL }; +static tr_list const TR_LIST_CLEAR = +{ + .data = NULL, + .next = NULL, + .prev = NULL +}; static tr_list* recycled_nodes = NULL; diff --git a/libtransmission/net.c b/libtransmission/net.c index 0708127b0..eae47c222 100644 --- a/libtransmission/net.c +++ b/libtransmission/net.c @@ -49,8 +49,17 @@ #define IN_MULTICAST(a) (((a) & 0xf0000000) == 0xe0000000) #endif -tr_address const tr_in6addr_any = { TR_AF_INET6, { IN6ADDR_ANY_INIT } }; -tr_address const tr_inaddr_any = { TR_AF_INET, { { { { INADDR_ANY, 0x00, 0x00, 0x00 } } } } }; +tr_address const tr_in6addr_any = +{ + .type = TR_AF_INET6, + .addr.addr6 = IN6ADDR_ANY_INIT +}; + +tr_address const tr_inaddr_any = +{ + .type = TR_AF_INET, + .addr.addr4.s_addr = INADDR_ANY +}; char* tr_net_strerror(char* buf, size_t buflen, int err) { diff --git a/libtransmission/peer-io.c b/libtransmission/peer-io.c index 5e165384e..a96b7d749 100644 --- a/libtransmission/peer-io.c +++ b/libtransmission/peer-io.c @@ -84,7 +84,12 @@ struct tr_datatype static struct tr_datatype* datatype_pool = NULL; -static struct tr_datatype const TR_DATATYPE_INIT = { NULL, 0, false }; +static struct tr_datatype const TR_DATATYPE_INIT = +{ + .next = NULL, + .length = 0, + .isPieceData = false +}; static struct tr_datatype* datatype_new(void) { diff --git a/libtransmission/peer-mgr.c b/libtransmission/peer-mgr.c index ef350dfa3..d7283bb43 100644 --- a/libtransmission/peer-mgr.c +++ b/libtransmission/peer-mgr.c @@ -83,9 +83,24 @@ enum CANCEL_HISTORY_SEC = 60 }; -tr_peer_event const TR_PEER_EVENT_INIT = { 0, 0, NULL, 0, 0, 0, 0 }; +tr_peer_event const TR_PEER_EVENT_INIT = +{ + .eventType = TR_PEER_CLIENT_GOT_BLOCK, + .pieceIndex = 0, + .bitfield = NULL, + .offset = 0, + .length = 0, + .err = 0, + .port = 0 +}; -tr_swarm_stats const TR_SWARM_STATS_INIT = { { 0, 0 }, 0, 0, { 0, 0, 0, 0, 0, 0, 0 } }; +tr_swarm_stats const TR_SWARM_STATS_INIT = +{ + .activePeerCount = { 0, 0 }, + .activeWebseedCount = 0, + .peerCount = 0, + .peerFromCount = { 0, 0, 0, 0, 0, 0, 0 } +}; /** *** diff --git a/libtransmission/rpcimpl.c b/libtransmission/rpcimpl.c index 480d138c3..e3896f197 100644 --- a/libtransmission/rpcimpl.c +++ b/libtransmission/rpcimpl.c @@ -26,6 +26,7 @@ #include "rpcimpl.h" #include "session.h" #include "session-id.h" +#include "stats.h" #include "torrent.h" #include "tr-assert.h" #include "utils.h" @@ -2097,8 +2098,8 @@ static char const* sessionStats(tr_session* session, tr_variant* args_in UNUSED, int running = 0; int total = 0; tr_variant* d; - tr_session_stats currentStats = { 0.0f, 0, 0, 0, 0, 0 }; - tr_session_stats cumulativeStats = { 0.0f, 0, 0, 0, 0, 0 }; + tr_session_stats currentStats = TR_SESSION_STATS_INIT; + tr_session_stats cumulativeStats = TR_SESSION_STATS_INIT; tr_torrent* tor = NULL; while ((tor = tr_torrentNext(session, tor)) != NULL) diff --git a/libtransmission/stats.c b/libtransmission/stats.c index c3ebae903..bd656d08f 100644 --- a/libtransmission/stats.c +++ b/libtransmission/stats.c @@ -18,7 +18,15 @@ **** ***/ -static struct tr_session_stats const STATS_INIT = { 0.0f, 0, 0, 0, 0, 0 }; +struct tr_session_stats const TR_SESSION_STATS_INIT = +{ + .ratio = 0.0f, + .uploadedBytes = 0, + .downloadedBytes = 0, + .filesAdded = 0, + .sessionCount = 0, + .secondsActive = 0 +}; /** @brief Opaque, per-session data structure for bandwidth use statistics */ struct tr_stats_handle @@ -134,7 +142,7 @@ void tr_statsSaveDirty(tr_session* session) if (h != NULL && h->isDirty) { - tr_session_stats cumulative = STATS_INIT; + tr_session_stats cumulative = TR_SESSION_STATS_INIT; tr_sessionGetCumulativeStats(session, &cumulative); saveCumulativeStats(session, &cumulative); h->isDirty = false; @@ -183,7 +191,7 @@ void tr_sessionGetStats(tr_session const* session, tr_session_stats* setme) void tr_sessionGetCumulativeStats(tr_session const* session, tr_session_stats* setme) { struct tr_stats_handle const* stats = getStats(session); - tr_session_stats current = STATS_INIT; + tr_session_stats current = TR_SESSION_STATS_INIT; if (stats != NULL) { diff --git a/libtransmission/stats.h b/libtransmission/stats.h index 5ac737aa2..1c2c05451 100644 --- a/libtransmission/stats.h +++ b/libtransmission/stats.h @@ -12,6 +12,8 @@ #pragma once +extern struct tr_session_stats const TR_SESSION_STATS_INIT; + void tr_statsInit(tr_session* session); void tr_statsClose(tr_session* session); void tr_statsSaveDirty(tr_session* session); diff --git a/libtransmission/tr-dht.c b/libtransmission/tr-dht.c index a19e1b3f8..59f828a80 100644 --- a/libtransmission/tr-dht.c +++ b/libtransmission/tr-dht.c @@ -535,7 +535,7 @@ static void getstatus(void* cl) int tr_dhtStatus(tr_session* session, int af, int* nodes_return) { - struct getstatus_closure closure = { af, -1, -1 }; + struct getstatus_closure closure = { .af = af, .status = -1, .count = -1 }; if (!tr_dhtEnabled(session) || (af == AF_INET && session->udp_socket == TR_BAD_SOCKET) || (af == AF_INET6 && session->udp6_socket == TR_BAD_SOCKET)) diff --git a/libtransmission/tr-lpd.c b/libtransmission/tr-lpd.c index 44a87f2ac..b469d19b5 100644 --- a/libtransmission/tr-lpd.c +++ b/libtransmission/tr-lpd.c @@ -552,7 +552,7 @@ static int tr_lpdConsiderAnnounce(tr_pex* peer, char const* const msg) maxHashLen = lengthof(lpd_torStaticType->info.hashString) }; - struct lpd_protocolVersion ver = { -1, -1 }; + struct lpd_protocolVersion ver = { .major = -1, .minor = -1 }; char value[maxValueLen] = { 0 }; char hashString[maxHashLen] = { 0 }; int res = 0; diff --git a/libtransmission/watchdir-generic.c b/libtransmission/watchdir-generic.c index 4b36d9f2a..8e2589c43 100644 --- a/libtransmission/watchdir-generic.c +++ b/libtransmission/watchdir-generic.c @@ -43,7 +43,7 @@ tr_watchdir_generic; #define BACKEND_UPCAST(b) ((tr_watchdir_generic*)(b)) /* Non-static and mutable for unit tests */ -struct timeval tr_watchdir_generic_interval = { 10, 0 }; +struct timeval tr_watchdir_generic_interval = { .tv_sec = 10, .tv_usec = 0 }; /*** **** diff --git a/libtransmission/watchdir-kqueue.c b/libtransmission/watchdir-kqueue.c index d966b4e55..c13b540e3 100644 --- a/libtransmission/watchdir-kqueue.c +++ b/libtransmission/watchdir-kqueue.c @@ -66,7 +66,7 @@ static void tr_watchdir_kqueue_on_event(evutil_socket_t fd UNUSED, short type UN tr_watchdir_t const handle = context; tr_watchdir_kqueue* const backend = BACKEND_UPCAST(tr_watchdir_get_backend(handle)); struct kevent ke; - struct timespec const ts = { 0, 0 }; + struct timespec const ts = { .tv_sec = 0, .tv_nsec = 0 }; if (kevent(backend->kq, NULL, 0, &ke, 1, &ts) == -1) { diff --git a/libtransmission/watchdir-test.c b/libtransmission/watchdir-test.c index a9fe5f8ae..79c444618 100644 --- a/libtransmission/watchdir-test.c +++ b/libtransmission/watchdir-test.c @@ -37,9 +37,9 @@ extern unsigned int tr_watchdir_retry_limit; extern struct timeval tr_watchdir_retry_start_interval; extern struct timeval tr_watchdir_retry_max_interval; -static struct timeval const FIFTY_MSEC = { 0, 50000 }; -static struct timeval const ONE_HUNDRED_MSEC = { 0, 100000 }; -static struct timeval const TWO_HUNDRED_MSEC = { 0, 200000 }; +static struct timeval const FIFTY_MSEC = { .tv_sec = 0, .tv_usec = 50000 }; +static struct timeval const ONE_HUNDRED_MSEC = { .tv_sec = 0, .tv_usec = 100000 }; +static struct timeval const TWO_HUNDRED_MSEC = { .tv_sec = 0, .tv_usec = 200000 }; static void process_events(void) { diff --git a/libtransmission/watchdir.c b/libtransmission/watchdir.c index 866ede35d..d415b0cc2 100644 --- a/libtransmission/watchdir.c +++ b/libtransmission/watchdir.c @@ -128,8 +128,8 @@ tr_watchdir_retry; /* Non-static and mutable for unit tests */ unsigned int tr_watchdir_retry_limit = 3; -struct timeval tr_watchdir_retry_start_interval = { 1, 0 }; -struct timeval tr_watchdir_retry_max_interval = { 10, 0 }; +struct timeval tr_watchdir_retry_start_interval = { .tv_sec = 1, .tv_usec = 0 }; +struct timeval tr_watchdir_retry_max_interval = { .tv_sec = 10, .tv_usec = 0 }; #define tr_watchdir_retries_init(r) (void)0 #define tr_watchdir_retries_destroy(r) tr_ptrArrayDestruct((r), (PtrArrayForeachFunc) & tr_watchdir_retry_free) -- 2.40.0