util: constify input parameters

This commit is contained in:
Miroslav Lichvar 2020-04-08 15:16:18 +02:00
parent 4dde7198c8
commit d3f4292968
2 changed files with 71 additions and 61 deletions

66
util.c
View file

@ -56,7 +56,7 @@ UTI_IsZeroTimespec(struct timespec *ts)
/* ================================================== */ /* ================================================== */
void void
UTI_TimevalToTimespec(struct timeval *tv, struct timespec *ts) UTI_TimevalToTimespec(const struct timeval *tv, struct timespec *ts)
{ {
ts->tv_sec = tv->tv_sec; ts->tv_sec = tv->tv_sec;
ts->tv_nsec = 1000 * tv->tv_usec; ts->tv_nsec = 1000 * tv->tv_usec;
@ -65,7 +65,7 @@ UTI_TimevalToTimespec(struct timeval *tv, struct timespec *ts)
/* ================================================== */ /* ================================================== */
void void
UTI_TimespecToTimeval(struct timespec *ts, struct timeval *tv) UTI_TimespecToTimeval(const struct timespec *ts, struct timeval *tv)
{ {
tv->tv_sec = ts->tv_sec; tv->tv_sec = ts->tv_sec;
tv->tv_usec = ts->tv_nsec / 1000; tv->tv_usec = ts->tv_nsec / 1000;
@ -74,7 +74,7 @@ UTI_TimespecToTimeval(struct timespec *ts, struct timeval *tv)
/* ================================================== */ /* ================================================== */
double double
UTI_TimespecToDouble(struct timespec *ts) UTI_TimespecToDouble(const struct timespec *ts)
{ {
return ts->tv_sec + 1.0e-9 * ts->tv_nsec; return ts->tv_sec + 1.0e-9 * ts->tv_nsec;
} }
@ -109,7 +109,7 @@ UTI_NormaliseTimespec(struct timespec *ts)
/* ================================================== */ /* ================================================== */
double double
UTI_TimevalToDouble(struct timeval *tv) UTI_TimevalToDouble(const struct timeval *tv)
{ {
return tv->tv_sec + 1.0e-6 * tv->tv_usec; return tv->tv_sec + 1.0e-6 * tv->tv_usec;
} }
@ -149,7 +149,7 @@ UTI_NormaliseTimeval(struct timeval *x)
/* ================================================== */ /* ================================================== */
int int
UTI_CompareTimespecs(struct timespec *a, struct timespec *b) UTI_CompareTimespecs(const struct timespec *a, const struct timespec *b)
{ {
if (a->tv_sec < b->tv_sec) if (a->tv_sec < b->tv_sec)
return -1; return -1;
@ -165,7 +165,7 @@ UTI_CompareTimespecs(struct timespec *a, struct timespec *b)
/* ================================================== */ /* ================================================== */
void void
UTI_DiffTimespecs(struct timespec *result, struct timespec *a, struct timespec *b) UTI_DiffTimespecs(struct timespec *result, const struct timespec *a, const struct timespec *b)
{ {
result->tv_sec = a->tv_sec - b->tv_sec; result->tv_sec = a->tv_sec - b->tv_sec;
result->tv_nsec = a->tv_nsec - b->tv_nsec; result->tv_nsec = a->tv_nsec - b->tv_nsec;
@ -176,7 +176,7 @@ UTI_DiffTimespecs(struct timespec *result, struct timespec *a, struct timespec *
/* Calculate result = a - b and return as a double */ /* Calculate result = a - b and return as a double */
double double
UTI_DiffTimespecsToDouble(struct timespec *a, struct timespec *b) UTI_DiffTimespecsToDouble(const struct timespec *a, const struct timespec *b)
{ {
return ((double)a->tv_sec - (double)b->tv_sec) + 1.0e-9 * (a->tv_nsec - b->tv_nsec); return ((double)a->tv_sec - (double)b->tv_sec) + 1.0e-9 * (a->tv_nsec - b->tv_nsec);
} }
@ -184,7 +184,7 @@ UTI_DiffTimespecsToDouble(struct timespec *a, struct timespec *b)
/* ================================================== */ /* ================================================== */
void void
UTI_AddDoubleToTimespec(struct timespec *start, double increment, struct timespec *end) UTI_AddDoubleToTimespec(const struct timespec *start, double increment, struct timespec *end)
{ {
time_t int_part; time_t int_part;
@ -198,7 +198,7 @@ UTI_AddDoubleToTimespec(struct timespec *start, double increment, struct timespe
/* Calculate the average and difference (as a double) of two timespecs */ /* Calculate the average and difference (as a double) of two timespecs */
void void
UTI_AverageDiffTimespecs(struct timespec *earlier, struct timespec *later, UTI_AverageDiffTimespecs(const struct timespec *earlier, const struct timespec *later,
struct timespec *average, double *diff) struct timespec *average, double *diff)
{ {
*diff = UTI_DiffTimespecsToDouble(later, earlier); *diff = UTI_DiffTimespecsToDouble(later, earlier);
@ -208,8 +208,8 @@ UTI_AverageDiffTimespecs(struct timespec *earlier, struct timespec *later,
/* ================================================== */ /* ================================================== */
void void
UTI_AddDiffToTimespec(struct timespec *a, struct timespec *b, UTI_AddDiffToTimespec(const struct timespec *a, const struct timespec *b,
struct timespec *c, struct timespec *result) const struct timespec *c, struct timespec *result)
{ {
double diff; double diff;
@ -230,7 +230,7 @@ static int pool_ptr = 0;
/* Convert a timespec into a temporary string, largely for diagnostic display */ /* Convert a timespec into a temporary string, largely for diagnostic display */
char * char *
UTI_TimespecToString(struct timespec *ts) UTI_TimespecToString(const struct timespec *ts)
{ {
char *result; char *result;
@ -250,7 +250,7 @@ UTI_TimespecToString(struct timespec *ts)
for diagnostic display */ for diagnostic display */
char * char *
UTI_Ntp64ToString(NTP_int64 *ntp_ts) UTI_Ntp64ToString(const NTP_int64 *ntp_ts)
{ {
struct timespec ts; struct timespec ts;
UTI_Ntp64ToTimespec(ntp_ts, &ts); UTI_Ntp64ToTimespec(ntp_ts, &ts);
@ -281,10 +281,10 @@ UTI_RefidToString(uint32_t ref_id)
/* ================================================== */ /* ================================================== */
char * char *
UTI_IPToString(IPAddr *addr) UTI_IPToString(const IPAddr *addr)
{ {
unsigned long a, b, c, d, ip; unsigned long a, b, c, d, ip;
uint8_t *ip6; const uint8_t *ip6;
char *result; char *result;
result = NEXT_BUFFER; result = NEXT_BUFFER;
@ -375,7 +375,7 @@ UTI_StringToIdIP(const char *addr, IPAddr *ip)
/* ================================================== */ /* ================================================== */
int int
UTI_IsIPReal(IPAddr *ip) UTI_IsIPReal(const IPAddr *ip)
{ {
switch (ip->family) { switch (ip->family) {
case IPADDR_INET4: case IPADDR_INET4:
@ -389,7 +389,7 @@ UTI_IsIPReal(IPAddr *ip)
/* ================================================== */ /* ================================================== */
uint32_t uint32_t
UTI_IPToRefid(IPAddr *ip) UTI_IPToRefid(const IPAddr *ip)
{ {
static int MD5_hash = -1; static int MD5_hash = -1;
unsigned char buf[16]; unsigned char buf[16];
@ -414,10 +414,10 @@ UTI_IPToRefid(IPAddr *ip)
/* ================================================== */ /* ================================================== */
uint32_t uint32_t
UTI_IPToHash(IPAddr *ip) UTI_IPToHash(const IPAddr *ip)
{ {
static uint32_t seed = 0; static uint32_t seed = 0;
unsigned char *addr; const unsigned char *addr;
unsigned int i, len; unsigned int i, len;
uint32_t hash; uint32_t hash;
@ -452,7 +452,7 @@ UTI_IPToHash(IPAddr *ip)
/* ================================================== */ /* ================================================== */
void void
UTI_IPHostToNetwork(IPAddr *src, IPAddr *dest) UTI_IPHostToNetwork(const IPAddr *src, IPAddr *dest)
{ {
/* Don't send uninitialized bytes over network */ /* Don't send uninitialized bytes over network */
memset(dest, 0, sizeof (IPAddr)); memset(dest, 0, sizeof (IPAddr));
@ -477,7 +477,7 @@ UTI_IPHostToNetwork(IPAddr *src, IPAddr *dest)
/* ================================================== */ /* ================================================== */
void void
UTI_IPNetworkToHost(IPAddr *src, IPAddr *dest) UTI_IPNetworkToHost(const IPAddr *src, IPAddr *dest)
{ {
dest->family = ntohs(src->family); dest->family = ntohs(src->family);
dest->_pad = 0; dest->_pad = 0;
@ -500,7 +500,7 @@ UTI_IPNetworkToHost(IPAddr *src, IPAddr *dest)
/* ================================================== */ /* ================================================== */
int int
UTI_CompareIPs(IPAddr *a, IPAddr *b, IPAddr *mask) UTI_CompareIPs(const IPAddr *a, const IPAddr *b, const IPAddr *mask)
{ {
int i, d; int i, d;
@ -536,7 +536,7 @@ UTI_CompareIPs(IPAddr *a, IPAddr *b, IPAddr *mask)
/* ================================================== */ /* ================================================== */
char * char *
UTI_IPSockAddrToString(IPSockAddr *sa) UTI_IPSockAddrToString(const IPSockAddr *sa)
{ {
char *result; char *result;
@ -571,7 +571,8 @@ UTI_TimeToLogForm(time_t t)
/* ================================================== */ /* ================================================== */
void void
UTI_AdjustTimespec(struct timespec *old_ts, struct timespec *when, struct timespec *new_ts, double *delta_time, double dfreq, double doffset) UTI_AdjustTimespec(const struct timespec *old_ts, const struct timespec *when,
struct timespec *new_ts, double *delta_time, double dfreq, double doffset)
{ {
double elapsed; double elapsed;
@ -644,7 +645,7 @@ UTI_ZeroNtp64(NTP_int64 *ts)
/* ================================================== */ /* ================================================== */
int int
UTI_IsZeroNtp64(NTP_int64 *ts) UTI_IsZeroNtp64(const NTP_int64 *ts)
{ {
return !ts->hi && !ts->lo; return !ts->hi && !ts->lo;
} }
@ -652,7 +653,7 @@ UTI_IsZeroNtp64(NTP_int64 *ts)
/* ================================================== */ /* ================================================== */
int int
UTI_CompareNtp64(NTP_int64 *a, NTP_int64 *b) UTI_CompareNtp64(const NTP_int64 *a, const NTP_int64 *b)
{ {
int32_t diff; int32_t diff;
@ -672,7 +673,8 @@ UTI_CompareNtp64(NTP_int64 *a, NTP_int64 *b)
/* ================================================== */ /* ================================================== */
int int
UTI_IsEqualAnyNtp64(NTP_int64 *a, NTP_int64 *b1, NTP_int64 *b2, NTP_int64 *b3) UTI_IsEqualAnyNtp64(const NTP_int64 *a, const NTP_int64 *b1, const NTP_int64 *b2,
const NTP_int64 *b3)
{ {
if (b1 && a->lo == b1->lo && a->hi == b1->hi) if (b1 && a->lo == b1->lo && a->hi == b1->hi)
return 1; return 1;
@ -694,7 +696,7 @@ UTI_IsEqualAnyNtp64(NTP_int64 *a, NTP_int64 *b1, NTP_int64 *b2, NTP_int64 *b3)
#define NSEC_PER_NTP64 4.294967296 #define NSEC_PER_NTP64 4.294967296
void void
UTI_TimespecToNtp64(struct timespec *src, NTP_int64 *dest, NTP_int64 *fuzz) UTI_TimespecToNtp64(const struct timespec *src, NTP_int64 *dest, const NTP_int64 *fuzz)
{ {
uint32_t hi, lo, sec, nsec; uint32_t hi, lo, sec, nsec;
@ -723,7 +725,7 @@ UTI_TimespecToNtp64(struct timespec *src, NTP_int64 *dest, NTP_int64 *fuzz)
/* ================================================== */ /* ================================================== */
void void
UTI_Ntp64ToTimespec(NTP_int64 *src, struct timespec *dest) UTI_Ntp64ToTimespec(const NTP_int64 *src, struct timespec *dest)
{ {
uint32_t ntp_sec, ntp_frac; uint32_t ntp_sec, ntp_frac;
@ -755,7 +757,7 @@ UTI_Ntp64ToTimespec(NTP_int64 *src, struct timespec *dest)
#define MIN_ENDOFTIME_DISTANCE (365 * 24 * 3600) #define MIN_ENDOFTIME_DISTANCE (365 * 24 * 3600)
int int
UTI_IsTimeOffsetSane(struct timespec *ts, double offset) UTI_IsTimeOffsetSane(const struct timespec *ts, double offset)
{ {
double t; double t;
@ -801,7 +803,7 @@ UTI_Log2ToDouble(int l)
/* ================================================== */ /* ================================================== */
void void
UTI_TimespecNetworkToHost(Timespec *src, struct timespec *dest) UTI_TimespecNetworkToHost(const Timespec *src, struct timespec *dest)
{ {
uint32_t sec_low, nsec; uint32_t sec_low, nsec;
#ifdef HAVE_LONG_TIME_T #ifdef HAVE_LONG_TIME_T
@ -826,7 +828,7 @@ UTI_TimespecNetworkToHost(Timespec *src, struct timespec *dest)
/* ================================================== */ /* ================================================== */
void void
UTI_TimespecHostToNetwork(struct timespec *src, Timespec *dest) UTI_TimespecHostToNetwork(const struct timespec *src, Timespec *dest)
{ {
dest->tv_nsec = htonl(src->tv_nsec); dest->tv_nsec = htonl(src->tv_nsec);
#ifdef HAVE_LONG_TIME_T #ifdef HAVE_LONG_TIME_T

66
util.h
View file

@ -41,13 +41,13 @@ extern void UTI_ZeroTimespec(struct timespec *ts);
extern int UTI_IsZeroTimespec(struct timespec *ts); extern int UTI_IsZeroTimespec(struct timespec *ts);
/* Convert a timeval into a timespec */ /* Convert a timeval into a timespec */
extern void UTI_TimevalToTimespec(struct timeval *tv, struct timespec *ts); extern void UTI_TimevalToTimespec(const struct timeval *tv, struct timespec *ts);
/* Convert a timespec into a timeval */ /* Convert a timespec into a timeval */
extern void UTI_TimespecToTimeval(struct timespec *ts, struct timeval *tv); extern void UTI_TimespecToTimeval(const struct timespec *ts, struct timeval *tv);
/* Convert a timespec into a floating point number of seconds */ /* Convert a timespec into a floating point number of seconds */
extern double UTI_TimespecToDouble(struct timespec *ts); extern double UTI_TimespecToDouble(const struct timespec *ts);
/* Convert a number of seconds expressed in floating point into a /* Convert a number of seconds expressed in floating point into a
timespec */ timespec */
@ -58,7 +58,7 @@ extern void UTI_DoubleToTimespec(double d, struct timespec *ts);
extern void UTI_NormaliseTimespec(struct timespec *ts); extern void UTI_NormaliseTimespec(struct timespec *ts);
/* Convert a timeval into a floating point number of seconds */ /* Convert a timeval into a floating point number of seconds */
extern double UTI_TimevalToDouble(struct timeval *tv); extern double UTI_TimevalToDouble(const struct timeval *tv);
/* Convert a number of seconds expressed in floating point into a /* Convert a number of seconds expressed in floating point into a
timeval */ timeval */
@ -70,54 +70,60 @@ extern void UTI_NormaliseTimeval(struct timeval *x);
/* Returns -1 if a comes earlier than b, 0 if a is the same time as b, /* Returns -1 if a comes earlier than b, 0 if a is the same time as b,
and +1 if a comes after b */ and +1 if a comes after b */
extern int UTI_CompareTimespecs(struct timespec *a, struct timespec *b); extern int UTI_CompareTimespecs(const struct timespec *a, const struct timespec *b);
/* Calculate result = a - b */ /* Calculate result = a - b */
extern void UTI_DiffTimespecs(struct timespec *result, struct timespec *a, struct timespec *b); extern void UTI_DiffTimespecs(struct timespec *result,
const struct timespec *a, const struct timespec *b);
/* Calculate result = a - b and return as a double */ /* Calculate result = a - b and return as a double */
extern double UTI_DiffTimespecsToDouble(struct timespec *a, struct timespec *b); extern double UTI_DiffTimespecsToDouble(const struct timespec *a, const struct timespec *b);
/* Add a double increment to a timespec to get a new one. 'start' is /* Add a double increment to a timespec to get a new one. 'start' is
the starting time, 'end' is the result that we return. This is the starting time, 'end' is the result that we return. This is
safe to use if start and end are the same */ safe to use if start and end are the same */
extern void UTI_AddDoubleToTimespec(struct timespec *start, double increment, struct timespec *end); extern void UTI_AddDoubleToTimespec(const struct timespec *start, double increment,
struct timespec *end);
/* Calculate the average and difference (as a double) of two timespecs */ /* Calculate the average and difference (as a double) of two timespecs */
extern void UTI_AverageDiffTimespecs(struct timespec *earlier, struct timespec *later, struct timespec *average, double *diff); extern void UTI_AverageDiffTimespecs(const struct timespec *earlier, const struct timespec *later,
struct timespec *average, double *diff);
/* Calculate result = a - b + c */ /* Calculate result = a - b + c */
extern void UTI_AddDiffToTimespec(struct timespec *a, struct timespec *b, struct timespec *c, struct timespec *result); extern void UTI_AddDiffToTimespec(const struct timespec *a, const struct timespec *b,
const struct timespec *c, struct timespec *result);
/* Convert a timespec into a temporary string, largely for diagnostic /* Convert a timespec into a temporary string, largely for diagnostic
display */ display */
extern char *UTI_TimespecToString(struct timespec *ts); extern char *UTI_TimespecToString(const struct timespec *ts);
/* Convert an NTP timestamp into a temporary string, largely for /* Convert an NTP timestamp into a temporary string, largely for
diagnostic display */ diagnostic display */
extern char *UTI_Ntp64ToString(NTP_int64 *ts); extern char *UTI_Ntp64ToString(const NTP_int64 *ts);
/* Convert ref_id into a temporary string, for diagnostics */ /* Convert ref_id into a temporary string, for diagnostics */
extern char *UTI_RefidToString(uint32_t ref_id); extern char *UTI_RefidToString(uint32_t ref_id);
/* Convert an IP address to string, for diagnostics */ /* Convert an IP address to string, for diagnostics */
extern char *UTI_IPToString(IPAddr *ip); extern char *UTI_IPToString(const IPAddr *ip);
extern int UTI_StringToIP(const char *addr, IPAddr *ip); extern int UTI_StringToIP(const char *addr, IPAddr *ip);
extern int UTI_StringToIdIP(const char *addr, IPAddr *ip); extern int UTI_StringToIdIP(const char *addr, IPAddr *ip);
extern int UTI_IsIPReal(IPAddr *ip); extern int UTI_IsIPReal(const IPAddr *ip);
extern uint32_t UTI_IPToRefid(IPAddr *ip); extern uint32_t UTI_IPToRefid(const IPAddr *ip);
extern uint32_t UTI_IPToHash(IPAddr *ip); extern uint32_t UTI_IPToHash(const IPAddr *ip);
extern void UTI_IPHostToNetwork(IPAddr *src, IPAddr *dest); extern void UTI_IPHostToNetwork(const IPAddr *src, IPAddr *dest);
extern void UTI_IPNetworkToHost(IPAddr *src, IPAddr *dest); extern void UTI_IPNetworkToHost(const IPAddr *src, IPAddr *dest);
extern int UTI_CompareIPs(IPAddr *a, IPAddr *b, IPAddr *mask); extern int UTI_CompareIPs(const IPAddr *a, const IPAddr *b, const IPAddr *mask);
extern char *UTI_IPSockAddrToString(IPSockAddr *sa); extern char *UTI_IPSockAddrToString(const IPSockAddr *sa);
extern char *UTI_TimeToLogForm(time_t t); extern char *UTI_TimeToLogForm(time_t t);
/* Adjust time following a frequency/offset change */ /* Adjust time following a frequency/offset change */
extern void UTI_AdjustTimespec(struct timespec *old_ts, struct timespec *when, struct timespec *new_ts, double *delta_time, double dfreq, double doffset); extern void UTI_AdjustTimespec(const struct timespec *old_ts, const struct timespec *when,
struct timespec *new_ts, double *delta_time,
double dfreq, double doffset);
/* Get zero NTP timestamp with random bits below precision */ /* Get zero NTP timestamp with random bits below precision */
extern void UTI_GetNtp64Fuzz(NTP_int64 *ts, int precision); extern void UTI_GetNtp64Fuzz(NTP_int64 *ts, int precision);
@ -129,30 +135,32 @@ extern NTP_int32 UTI_DoubleToNtp32(double x);
extern void UTI_ZeroNtp64(NTP_int64 *ts); extern void UTI_ZeroNtp64(NTP_int64 *ts);
/* Check if an NTP timestamp is zero */ /* Check if an NTP timestamp is zero */
extern int UTI_IsZeroNtp64(NTP_int64 *ts); extern int UTI_IsZeroNtp64(const NTP_int64 *ts);
/* Compare two NTP timestamps. Returns -1 if a is before b, 0 if a is equal to /* Compare two NTP timestamps. Returns -1 if a is before b, 0 if a is equal to
b, and 1 if a is after b. */ b, and 1 if a is after b. */
extern int UTI_CompareNtp64(NTP_int64 *a, NTP_int64 *b); extern int UTI_CompareNtp64(const NTP_int64 *a, const NTP_int64 *b);
/* Compare an NTP timestamp with up to three other timestamps. Returns 0 /* Compare an NTP timestamp with up to three other timestamps. Returns 0
if a is not equal to any of b1, b2, and b3, 1 otherwise. */ if a is not equal to any of b1, b2, and b3, 1 otherwise. */
extern int UTI_IsEqualAnyNtp64(NTP_int64 *a, NTP_int64 *b1, NTP_int64 *b2, NTP_int64 *b3); extern int UTI_IsEqualAnyNtp64(const NTP_int64 *a, const NTP_int64 *b1,
const NTP_int64 *b2, const NTP_int64 *b3);
/* Convert a timespec into an NTP timestamp */ /* Convert a timespec into an NTP timestamp */
extern void UTI_TimespecToNtp64(struct timespec *src, NTP_int64 *dest, NTP_int64 *fuzz); extern void UTI_TimespecToNtp64(const struct timespec *src, NTP_int64 *dest,
const NTP_int64 *fuzz);
/* Convert an NTP timestamp into a timespec */ /* Convert an NTP timestamp into a timespec */
extern void UTI_Ntp64ToTimespec(NTP_int64 *src, struct timespec *dest); extern void UTI_Ntp64ToTimespec(const NTP_int64 *src, struct timespec *dest);
/* Check if time + offset is sane */ /* Check if time + offset is sane */
extern int UTI_IsTimeOffsetSane(struct timespec *ts, double offset); extern int UTI_IsTimeOffsetSane(const struct timespec *ts, double offset);
/* Get 2 raised to power of a signed integer */ /* Get 2 raised to power of a signed integer */
extern double UTI_Log2ToDouble(int l); extern double UTI_Log2ToDouble(int l);
extern void UTI_TimespecNetworkToHost(Timespec *src, struct timespec *dest); extern void UTI_TimespecNetworkToHost(const Timespec *src, struct timespec *dest);
extern void UTI_TimespecHostToNetwork(struct timespec *src, Timespec *dest); extern void UTI_TimespecHostToNetwork(const struct timespec *src, Timespec *dest);
extern double UTI_FloatNetworkToHost(Float x); extern double UTI_FloatNetworkToHost(Float x);
extern Float UTI_FloatHostToNetwork(double x); extern Float UTI_FloatHostToNetwork(double x);