2 * utils - misc libubox utility functions
4 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 #define foreach_arg(_arg, _addr, _len, _first_addr, _first_len) \
25 for (_addr = (_first_addr), _len = (_first_len); \
27 _addr = va_arg(_arg, void **), _len = _addr ? va_arg(_arg, size_t) : 0)
29 void *__calloc_a(size_t len
, ...)
41 foreach_arg(ap1
, cur_addr
, cur_len
, &ret
, len
)
45 ptr
= calloc(1, alloc_len
);
47 foreach_arg(ap
, cur_addr
, cur_len
, &ret
, len
) {
48 *cur_addr
= &ptr
[alloc_len
];
57 #include <mach/mach_time.h>
59 static void clock_gettime_realtime(struct timespec
*tv
)
63 gettimeofday(&_tv
, NULL
);
64 tv
->tv_sec
= _tv
.tv_sec
;
65 tv
->tv_nsec
= _tv
.tv_usec
* 1000;
68 static void clock_gettime_monotonic(struct timespec
*tv
)
70 mach_timebase_info_data_t info
;
74 mach_timebase_info(&info
);
76 val
= mach_absolute_time();
77 tv
->tv_nsec
= (val
* info
.numer
/ info
.denom
) % 1000000000;
86 void clock_gettime(int type
, struct timespec
*tv
)
88 if (type
== CLOCK_REALTIME
)
89 return clock_gettime_realtime(tv
);
91 return clock_gettime_monotonic(tv
);