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_host.h> /* host_get_clock_service() */
58 #include <mach/mach_port.h> /* mach_port_deallocate() */
59 #include <mach/mach_init.h> /* mach_host_self(), mach_task_self() */
60 #include <mach/clock.h> /* clock_get_time() */
62 static clock_serv_t clock_realtime
;
63 static clock_serv_t clock_monotonic
;
65 static void __constructor
clock_name_init(void)
67 mach_port_t host_self
= mach_host_self();
69 host_get_clock_service(host_self
, CLOCK_REALTIME
, &clock_realtime
);
70 host_get_clock_service(host_self
, CLOCK_MONOTONIC
, &clock_monotonic
);
73 static void __destructor
clock_name_dealloc(void)
75 mach_port_t self
= mach_task_self();
77 mach_port_deallocate(self
, clock_realtime
);
78 mach_port_deallocate(self
, clock_monotonic
);
81 int clock_gettime(int type
, struct timespec
*tv
)
88 retval
= clock_get_time(clock_realtime
, &mts
);
91 retval
= clock_get_time(clock_monotonic
, &mts
);
97 tv
->tv_sec
= mts
.tv_sec
;
98 tv
->tv_nsec
= mts
.tv_nsec
;