From 9fc83eb66f5f855a9d7b357b2f0a49370ac71780 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sat, 11 Jun 2022 15:27:51 +0300 Subject: [PATCH 37/37] Improve timing[ch] coding style See osdn #44796 Signed-off-by: Marko Lindqvist --- utility/timing.c | 25 ++++++++++++++----------- utility/timing.h | 18 ++++++++++-------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/utility/timing.c b/utility/timing.c index 0e747aa294..fc7792af46 100644 --- a/utility/timing.c +++ b/utility/timing.c @@ -11,7 +11,7 @@ GNU General Public License for more details. ***********************************************************************/ -/********************************************************************** +/*********************************************************************** Measuring times; original author: David Pfitzner We assume we have at least ANSI/ISO C timing functions, so @@ -52,13 +52,13 @@ #endif #ifdef HAVE_FTIME -# include +#include #endif /* utility */ #include "log.h" #include "mem.h" -#include "shared.h" /* TRUE, FALSE */ +#include "shared.h" /* TRUE, FALSE */ #include "support.h" #include "timing.h" @@ -67,11 +67,11 @@ #ifdef CLOCKS_PER_SECOND #define CLOCKS_PER_SEC CLOCKS_PER_SECOND #else -#define CLOCKS_PER_SEC 1000000 /* wild guess!! */ +#define CLOCKS_PER_SEC 1000000 /* wild guess!! */ #endif #endif -#define N_USEC_PER_SEC 1000000L /* not 1000! :-) */ +#define N_USEC_PER_SEC 1000000L /* not 1000! :-) */ enum timer_state { TIMER_STARTED, @@ -149,7 +149,6 @@ static void report_time_failed(struct timer *t) } #endif - /*******************************************************************//** Allocate a new timer with specified "type" and "use". The timer is created as cleared, and stopped. @@ -166,7 +165,7 @@ struct timer *timer_new(enum timer_timetype type, enum timer_use use) just re-initialise t and return t. This is intended to be useful to allocate a static t just once, eg: { - static struct timer *t = NULL; + static struct timer *t = NULL; t = timer_renew(t, TIMER_CPU, TIMER_USE); ... stuff ... log_verbose("That took %g seconds.", timer_read_seconds(t)); @@ -174,14 +173,16 @@ struct timer *timer_new(enum timer_timetype type, enum timer_use use) } ***********************************************************************/ struct timer *timer_renew(struct timer *t, enum timer_timetype type, - enum timer_use use) + enum timer_use use) { - if (!t) { + if (t == NULL) { t = (struct timer *)fc_malloc(sizeof(struct timer)); } + t->type = type; t->use = use; timer_clear(t); + return t; } @@ -201,7 +202,7 @@ void timer_destroy(struct timer *t) ***********************************************************************/ bool timer_in_use(struct timer *t) { - return (t && t->use != TIMER_IGNORE); + return (t != NULL && t->use != TIMER_IGNORE); } /*******************************************************************//** @@ -352,6 +353,7 @@ double timer_read_seconds(struct timer *t) timer_stop(t); t->state = TIMER_STARTED; } + return t->sec + t->usec / (double)N_USEC_PER_SEC; } @@ -386,8 +388,9 @@ void timer_usleep_since_start(struct timer *t, long usec) (tv_now.tv_usec - t->start.tv.tv_usec); wait_usec = usec - elapsed_usec; - if (wait_usec > 0) + if (wait_usec > 0) { fc_usleep(wait_usec); + } #elif HAVE_FTIME struct timeb now; long elapsed_usec, wait_usec; diff --git a/utility/timing.h b/utility/timing.h index a6ada5a22d..60f4b151ff 100644 --- a/utility/timing.h +++ b/utility/timing.h @@ -1,4 +1,4 @@ -/********************************************************************** +/*********************************************************************** Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -20,6 +20,7 @@ extern "C" { #endif /* __cplusplus */ +/* utility */ #include "support.h" /* bool type */ /* Undefine this if you don't want timing measurements to appear in logs. @@ -37,14 +38,15 @@ extern "C" { #endif enum timer_timetype { - TIMER_CPU, /* time spent by the CPU */ - TIMER_USER /* time as seen by the user ("wall clock") */ + TIMER_CPU, /* Time spent by the CPU */ + TIMER_USER /* Time as seen by the user ("wall clock") */ }; enum timer_use { - TIMER_ACTIVE, /* use this timer */ - TIMER_IGNORE /* ignore this timer */ + TIMER_ACTIVE, /* Use this timer */ + TIMER_IGNORE /* Ignore this timer */ }; + /* * TIMER_IGNORE is to leave a timer in the code, but not actually * use it, and not make any time-related system calls for it. @@ -59,7 +61,7 @@ enum timer_use { #define TIMER_DEBUG TIMER_IGNORE #endif -struct timer; /* opaque type; see comments in timing.c */ +struct timer; /* opaque type; see comments in timing.c */ #define SPECLIST_TAG timer #define SPECLIST_TYPE struct timer @@ -71,7 +73,7 @@ struct timer; /* opaque type; see comments in timing.c */ struct timer *timer_new(enum timer_timetype type, enum timer_use use); struct timer *timer_renew(struct timer *t, enum timer_timetype type, - enum timer_use use); + enum timer_use use); void timer_destroy(struct timer *t); bool timer_in_use(struct timer *t); @@ -88,4 +90,4 @@ void timer_usleep_since_start(struct timer *t, long usec); } #endif /* __cplusplus */ -#endif /* FC__TIMER_H */ +#endif /* FC__TIMER_H */ -- 2.35.1