jshn: use an avl tree for env variables to speed up processing of bigger json files
[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         alloc_len = 0;
47         foreach_arg(ap, cur_addr, cur_len, &ret, len) {
48                 *cur_addr = &ptr[alloc_len];
49                 alloc_len += cur_len;
50         }
51         va_end(ap);
52
53         return ret;
54 }
55
56 #ifdef __APPLE__
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() */
61
62 static clock_serv_t clock_realtime;
63 static clock_serv_t clock_monotonic;
64
65 static void __constructor clock_name_init(void)
66 {
67         mach_port_t host_self = mach_host_self();
68
69         host_get_clock_service(host_self, CLOCK_REALTIME, &clock_realtime);
70         host_get_clock_service(host_self, CLOCK_MONOTONIC, &clock_monotonic);
71 }
72
73 static void __destructor clock_name_dealloc(void)
74 {
75         mach_port_t self = mach_task_self();
76
77         mach_port_deallocate(self, clock_realtime);
78         mach_port_deallocate(self, clock_monotonic);
79 }
80
81 int clock_gettime(int type, struct timespec *tv)
82 {
83         int retval = -1;
84         mach_timespec_t mts;
85
86         switch (type) {
87                 case CLOCK_REALTIME:
88                         retval = clock_get_time(clock_realtime, &mts);
89                         break;
90                 case CLOCK_MONOTONIC:
91                         retval = clock_get_time(clock_monotonic, &mts);
92                         break;
93                 default:
94                         goto out;
95         }
96
97         tv->tv_sec = mts.tv_sec;
98         tv->tv_nsec = mts.tv_nsec;
99 out:
100         return retval;
101 }
102
103 #endif