91dd71e6c18dd67dcb5b24a6a93ab88dfa9d887d
[project/libubox.git] / utils.c
1 /*
2 * utils - misc libubox utility functions
3 *
4 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
5 *
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.
9 *
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.
17 */
18
19 #include "utils.h"
20 #include <stdarg.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23
24 #define foreach_arg(_arg, _addr, _len, _first_addr, _first_len) \
25 for (_addr = (_first_addr), _len = (_first_len); \
26 _addr; \
27 _addr = va_arg(_arg, void **), _len = _addr ? va_arg(_arg, size_t) : 0)
28
29 void *__calloc_a(size_t len, ...)
30 {
31 va_list ap, ap1;
32 void *ret;
33 void **cur_addr;
34 size_t cur_len;
35 int alloc_len = 0;
36 char *ptr;
37
38 va_start(ap, len);
39
40 va_copy(ap1, ap);
41 foreach_arg(ap1, cur_addr, cur_len, &ret, len)
42 alloc_len += cur_len;
43 va_end(ap1);
44
45 ptr = calloc(1, alloc_len);
46 if (!ptr)
47 return NULL;
48 alloc_len = 0;
49 foreach_arg(ap, cur_addr, cur_len, &ret, len) {
50 *cur_addr = &ptr[alloc_len];
51 alloc_len += cur_len;
52 }
53 va_end(ap);
54
55 return ret;
56 }
57
58 #ifdef __APPLE__
59 #include <mach/mach_host.h> /* host_get_clock_service() */
60 #include <mach/mach_port.h> /* mach_port_deallocate() */
61 #include <mach/mach_init.h> /* mach_host_self(), mach_task_self() */
62 #include <mach/clock.h> /* clock_get_time() */
63
64 static clock_serv_t clock_realtime;
65 static clock_serv_t clock_monotonic;
66
67 static void __constructor clock_name_init(void)
68 {
69 mach_port_t host_self = mach_host_self();
70
71 host_get_clock_service(host_self, CLOCK_REALTIME, &clock_realtime);
72 host_get_clock_service(host_self, CLOCK_MONOTONIC, &clock_monotonic);
73 }
74
75 static void __destructor clock_name_dealloc(void)
76 {
77 mach_port_t self = mach_task_self();
78
79 mach_port_deallocate(self, clock_realtime);
80 mach_port_deallocate(self, clock_monotonic);
81 }
82
83 int clock_gettime(int type, struct timespec *tv)
84 {
85 int retval = -1;
86 mach_timespec_t mts;
87
88 switch (type) {
89 case CLOCK_REALTIME:
90 retval = clock_get_time(clock_realtime, &mts);
91 break;
92 case CLOCK_MONOTONIC:
93 retval = clock_get_time(clock_monotonic, &mts);
94 break;
95 default:
96 goto out;
97 }
98
99 tv->tv_sec = mts.tv_sec;
100 tv->tv_nsec = mts.tv_nsec;
101 out:
102 return retval;
103 }
104
105 #endif