Files
nginx-unit/src/nxt_time.h
Alejandro Colomar 952bcc50bf Fixed #define style.
We had a mix of styles for declaring function-like macros:

Style A:
 #define                    \
 foo()                      \
     do {                   \
         ...                \
     } while (0)

Style B:
 #define foo()              \
     do {                   \
         ...                \
     } while (0)

We had a similar number of occurences of each style:

 $ grep -rnI '^\w*(.*\\' | wc -l
 244
 $ grep -rn 'define.*(.*)' | wc -l
 239

(Those regexes aren't perfect, but a very decent approximation.)

Real examples:

 $ find src -type f | xargs sed -n '/^nxt_double_is_zero/,/^$/p'
 nxt_double_is_zero(f)                                                         \
     (fabs(f) <= FLT_EPSILON)

 $ find src -type f | xargs sed -n '/define nxt_http_field_set/,/^$/p'
 #define nxt_http_field_set(_field, _name, _value)                             \
     do {                                                                      \
         (_field)->name_length = nxt_length(_name);                            \
         (_field)->value_length = nxt_length(_value);                          \
         (_field)->name = (u_char *) _name;                                    \
         (_field)->value = (u_char *) _value;                                  \
     } while (0)

I'd like to standardize on a single style for them, and IMO,
having the identifier in the same line as #define is a better
option for the following reasons:

- Programmers are used to `#define foo() ...` (readability).
- One less line of code.
- The program for finding them is really simple (see below).

 function grep_ngx_func()
 {
     if (($# != 1)); then
         >&2 echo "Usage: ${FUNCNAME[0]} <func>";
         return 1;
     fi;

     find src -type f \
     | grep '\.[ch]$' \
     | xargs grep -l "$1" \
     | sort \
     | xargs pcregrep -Mn "(?s)^\$[\w\s*]+?^$1\(.*?^}";

     find src -type f \
     | grep '\.[ch]$' \
     | xargs grep -l "$1" \
     | sort \
     | xargs pcregrep -Mn "(?s)define $1\(.*?^$" \
     | sed -E '1s/^[^:]+:[0-9]+:/&\n\n/';
 }

 $ grep_ngx_func
 Usage: grep_ngx_func <func>

 $ grep_ngx_func nxt_http_field_set
 src/nxt_http.h:98:

 #define nxt_http_field_set(_field, _name, _value)                             \
     do {                                                                      \
         (_field)->name_length = nxt_length(_name);                            \
         (_field)->value_length = nxt_length(_value);                          \
         (_field)->name = (u_char *) _name;                                    \
         (_field)->value = (u_char *) _value;                                  \
     } while (0)

 $ grep_ngx_func nxt_sprintf
 src/nxt_sprintf.c:56:

 u_char * nxt_cdecl
 nxt_sprintf(u_char *buf, u_char *end, const char *fmt, ...)
 {
     u_char   *p;
     va_list  args;

     va_start(args, fmt);
     p = nxt_vsprintf(buf, end, fmt, args);
     va_end(args);

     return p;
 }

................
Scripted change:
................

$ find src -type f \
  | grep '\.[ch]$' \
  | xargs sed -i '/define *\\$/{N;s/ *\\\n/ /;s/        //}'
2022-05-03 12:11:14 +02:00

108 lines
3.4 KiB
C

/*
* Copyright (C) Igor Sysoev
* Copyright (C) NGINX, Inc.
*/
#ifndef _NXT_UNIX_TIME_H_INCLUDED_
#define _NXT_UNIX_TIME_H_INCLUDED_
typedef uint64_t nxt_nsec_t;
typedef int64_t nxt_nsec_int_t;
#define NXT_INFINITE_NSEC ((nxt_nsec_t) -1)
typedef struct {
nxt_time_t sec;
nxt_uint_t nsec;
} nxt_realtime_t;
/*
* nxt_monotonic_time_t includes nxt_realtime_t to eliminate
* surplus gettimeofday() call on platform without monotonic time.
*/
typedef struct {
nxt_realtime_t realtime;
nxt_nsec_t monotonic;
nxt_nsec_t update;
#if !(NXT_HAVE_CLOCK_MONOTONIC || NXT_SOLARIS || NXT_HPUX || NXT_MACOSX)
nxt_nsec_t previous;
#endif
} nxt_monotonic_time_t;
NXT_EXPORT void nxt_realtime(nxt_realtime_t *now);
NXT_EXPORT void nxt_monotonic_time(nxt_monotonic_time_t *now);
NXT_EXPORT void nxt_localtime(nxt_time_t s, struct tm *tm);
NXT_EXPORT void nxt_timezone_update(void);
/*
* Both localtime() and localtime_r() are not Async-Signal-Safe, therefore,
* they can not be used in signal handlers. Since Daylight Saving Time (DST)
* state changes no more than twice a year, a simple workaround is to use
* a previously cached GMT offset value and nxt_gmtime():
*
* nxt_gmtime(GMT seconds + GMT offset, tm);
*
* GMT offset with account of current DST state can be obtained only
* using localtime()'s struct tm because:
*
* 1) gettimeofday() does not return GMT offset at almost all platforms.
* MacOSX returns a value cached after the first localtime() call.
* AIX returns GMT offset without account of DST state and indicates
* only that timezone has DST, but does not indicate current DST state.
*
* 2) There are the "timezone" and "daylight" variables on Linux, Solaris,
* HP-UX, IRIX, and other systems. The "daylight" variable indicates
* only that timezone has DST, but does not indicate current DST state.
*
* 3) Solaris and IRIX have the "altzone" variable which contains GMT offset
* for timezone with DST applied, but without account of DST state.
*
* 4) There is the "struct tm.tm_gmtoff" field on BSD systems and modern Linux.
* This field contains GMT offset with account of DST state.
*
* 5) The "struct tm.tm_isdst" field returned by localtime() indicates
* current DST state on all platforms. This field may have three values:
* positive means DST in effect, zero means DST is not in effect, and
* negative means DST state is unknown.
*/
#if (NXT_HAVE_TM_GMTOFF)
#define nxt_timezone(tm) \
((tm)->tm_gmtoff)
#elif (NXT_HAVE_ALTZONE)
#define nxt_timezone(tm) \
(-(((tm)->tm_isdst > 0) ? altzone : timezone))
#else
#define nxt_timezone(tm) \
(-(((tm)->tm_isdst > 0) ? timezone + 3600 : timezone))
#endif
typedef uint32_t nxt_msec_t;
typedef int32_t nxt_msec_int_t;
#define NXT_INFINITE_MSEC ((nxt_msec_t) -1)
/*
* Since nxt_msec_t values are stored just in 32 bits, they overflow
* every 49 days. This signed subtraction takes into account that overflow.
* "nxt_msec_diff(m1, m2) < 0" means that m1 is lesser than m2.
*/
#define nxt_msec_diff(m1, m2) \
((int32_t) ((m1) - (m2)))
#endif /* _NXT_UNIX_TIME_H_INCLUDED_ */