Use field initializers for readability

This commit is contained in:
Mike Gelfand
2017-06-24 13:17:04 +03:00
parent 88ef7c1344
commit 13bbaeec76
17 changed files with 92 additions and 24 deletions
+1 -1
View File
@@ -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)
+9 -1
View File
@@ -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)
{
+9 -1
View File
@@ -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
};
/****
*****
+8 -1
View File
@@ -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;
+6 -1
View File
@@ -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;
+11 -2
View File
@@ -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)
{
+6 -1
View File
@@ -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)
{
+17 -2
View File
@@ -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 }
};
/**
***
+3 -2
View File
@@ -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)
+11 -3
View File
@@ -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)
{
+2
View File
@@ -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);
+1 -1
View File
@@ -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))
+1 -1
View File
@@ -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;
+1 -1
View File
@@ -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 };
/***
****
+1 -1
View File
@@ -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)
{
+3 -3
View File
@@ -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)
{
+2 -2
View File
@@ -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)