Skip to content

Commit d35c01c

Browse files
committed
Return instead of pointer-out
1 parent d2a61b5 commit d35c01c

2 files changed

Lines changed: 50 additions & 31 deletions

File tree

Zend/zend_time.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,38 @@
1616

1717
#include "zend_time.h"
1818

19-
ZEND_API void zend_time_real_spec(struct timespec *ts) {
19+
ZEND_API struct timespec zend_time_real_spec() {
20+
struct timespec ts;
21+
2022
#if defined(HAVE_CLOCK_GETTIME)
2123

22-
(void) clock_gettime(CLOCK_REALTIME, ts);
24+
(void) clock_gettime(CLOCK_REALTIME, &ts);
2325

2426
#elif defined(HAVE_TIMESPEC_GET)
2527

26-
(void) timespec_get(ts, TIME_UTC);
28+
(void) timespec_get(&ts, TIME_UTC);
2729

2830
#elif defined(HAVE_GETTIMEOFDAY)
2931

3032
struct timeval tv;
3133
(void) gettimeofday(&tv, NULL);
32-
zend_time_val2spec(tv, ts);
34+
ts = zend_time_val2spec(tv);
3335

3436
#else
3537

36-
ts->tv_sec = zend_time_real_get();
37-
ts->tv_nsec = 0;
38+
ts.tv_sec = zend_time_real_get();
39+
ts.tv_nsec = 0;
3840

3941
#endif
42+
43+
return ts;
4044
}
4145

4246
ZEND_API uint64_t zend_time_mono_fallback_nsec(void) {
4347
#if ZEND_HRTIME_AVAILABLE
4448
return (uint64_t)zend_hrtime();
4549
#else
46-
struct timespec ts;
47-
zend_time_real_spec(&ts);
50+
struct timespec ts = zend_time_real_spec();
4851
return ((uint64_t) ts.tv_sec * ZEND_NANO_IN_SEC) + ts.tv_nsec;
4952
#endif
5053
}

Zend/zend_time.h

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,41 +43,57 @@
4343
BEGIN_EXTERN_C()
4444

4545
/* Assign seconds to timeval */
46-
static zend_always_inline void zend_time_sec2val(time_t s, struct timeval *tv) {
47-
tv->tv_sec = (tv_sec_t) s;
48-
tv->tv_usec = 0;
46+
static zend_always_inline struct timeval zend_time_sec2val(time_t s) {
47+
struct timeval tv;
48+
49+
tv.tv_sec = (tv_sec_t) s;
50+
tv.tv_usec = 0;
51+
52+
return tv;
4953
}
5054

5155
/* Assign microseconds to timeval */
52-
static zend_always_inline void zend_time_usec2val(int64_t usec, struct timeval *tv) {
53-
tv->tv_sec = (tv_sec_t) (usec / ZEND_TIME_MICRO_IN_SEC);
54-
tv->tv_usec = (tv_usec_t) (usec % ZEND_TIME_MICRO_IN_SEC);
56+
static zend_always_inline struct timeval zend_time_usec2val(int64_t usec) {
57+
struct timeval tv;
58+
59+
tv.tv_sec = (tv_sec_t) (usec / ZEND_TIME_MICRO_IN_SEC);
60+
tv.tv_usec = (tv_usec_t) (usec % ZEND_TIME_MICRO_IN_SEC);
5561

56-
if (UNEXPECTED(tv->tv_usec < 0)) {
57-
tv->tv_usec += ZEND_TIME_MICRO_IN_SEC;
58-
tv->tv_sec -= 1;
62+
if (UNEXPECTED(tv.tv_usec < 0)) {
63+
tv.tv_usec += ZEND_TIME_MICRO_IN_SEC;
64+
tv.tv_sec -= 1;
5965
}
66+
67+
return tv;
6068
}
6169

6270
/* Assign double (seconds) to timeval */
63-
static zend_always_inline void zend_time_dbl2val(double s, struct timeval *tv) {
64-
tv->tv_sec = (tv_sec_t) s;
65-
tv->tv_usec = (tv_usec_t) ((s - tv->tv_sec) * ZEND_TIME_MICRO_IN_SEC);
66-
67-
if (UNEXPECTED(tv->tv_usec < 0)) {
68-
tv->tv_usec += ZEND_TIME_MICRO_IN_SEC;
69-
tv->tv_sec -= 1;
70-
} else if (UNEXPECTED(tv->tv_usec >= ZEND_TIME_MICRO_IN_SEC)) {
71+
static zend_always_inline struct timeval zend_time_dbl2val(double s) {
72+
struct timeval tv;
73+
74+
tv.tv_sec = (tv_sec_t) s;
75+
tv.tv_usec = (tv_usec_t) ((s - tv.tv_sec) * ZEND_TIME_MICRO_IN_SEC);
76+
77+
if (UNEXPECTED(tv.tv_usec < 0)) {
78+
tv.tv_usec += ZEND_TIME_MICRO_IN_SEC;
79+
tv.tv_sec -= 1;
80+
} else if (UNEXPECTED(tv.tv_usec >= ZEND_TIME_MICRO_IN_SEC)) {
7181
// rare, but protects against rounding up to exactly 1 second
72-
tv->tv_usec -= ZEND_TIME_MICRO_IN_SEC;
73-
tv->tv_sec += 1;
82+
tv.tv_usec -= ZEND_TIME_MICRO_IN_SEC;
83+
tv.tv_sec += 1;
7484
}
85+
86+
return tv;
7587
}
7688

7789
/* Assign timeval to timespec */
78-
static zend_always_inline void zend_time_val2spec(struct timeval tv, struct timespec *ts) {
79-
ts->tv_sec = (time_t) tv.tv_sec;
80-
ts->tv_nsec = (long) (tv.tv_usec * 1000);
90+
static zend_always_inline struct timespec zend_time_val2spec(struct timeval tv) {
91+
struct timespec ts;
92+
93+
ts.tv_sec = (time_t) tv.tv_sec;
94+
ts.tv_nsec = (long) (tv.tv_usec * 1000);
95+
96+
return ts;
8197
}
8298

8399
/* Current real/wall-time in seconds */
@@ -86,7 +102,7 @@ static zend_always_inline time_t zend_time_real_sec(void) {
86102
}
87103

88104
/* Current real/wall-time in up-to nano seconds */
89-
ZEND_API void zend_time_real_spec(struct timespec *ts);
105+
ZEND_API struct timespec zend_time_real_spec(void);
90106

91107
/* Monotonic time in nanoseconds with a fallback to real/wall-time
92108
if no monotonic timer is available */

0 commit comments

Comments
 (0)