blobmsg: add support for double
[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 <sys/mman.h>
20 #include <stdarg.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include "utils.h"
24
25 #define foreach_arg(_arg, _addr, _len, _first_addr, _first_len) \
26         for (_addr = (_first_addr), _len = (_first_len); \
27                 _addr; \
28                 _addr = va_arg(_arg, void **), _len = _addr ? va_arg(_arg, size_t) : 0)
29
30 void *__calloc_a(size_t len, ...)
31 {
32         va_list ap, ap1;
33         void *ret;
34         void **cur_addr;
35         size_t cur_len;
36         int alloc_len = 0;
37         char *ptr;
38
39         va_start(ap, len);
40
41         va_copy(ap1, ap);
42         foreach_arg(ap1, cur_addr, cur_len, &ret, len)
43                 alloc_len += cur_len;
44         va_end(ap1);
45
46         ptr = calloc(1, alloc_len);
47         if (!ptr)
48                 return NULL;
49         alloc_len = 0;
50         foreach_arg(ap, cur_addr, cur_len, &ret, len) {
51                 *cur_addr = &ptr[alloc_len];
52                 alloc_len += cur_len;
53         }
54         va_end(ap);
55
56         return ret;
57 }
58
59 #ifdef __APPLE__
60 #include <mach/mach_host.h>             /* host_get_clock_service() */
61 #include <mach/mach_port.h>             /* mach_port_deallocate() */
62 #include <mach/mach_init.h>             /* mach_host_self(), mach_task_self() */
63 #include <mach/clock.h>                 /* clock_get_time() */
64
65 static clock_serv_t clock_realtime;
66 static clock_serv_t clock_monotonic;
67
68 static void __constructor clock_name_init(void)
69 {
70         mach_port_t host_self = mach_host_self();
71
72         host_get_clock_service(host_self, CLOCK_REALTIME, &clock_realtime);
73         host_get_clock_service(host_self, CLOCK_MONOTONIC, &clock_monotonic);
74 }
75
76 static void __destructor clock_name_dealloc(void)
77 {
78         mach_port_t self = mach_task_self();
79
80         mach_port_deallocate(self, clock_realtime);
81         mach_port_deallocate(self, clock_monotonic);
82 }
83
84 int clock_gettime(int type, struct timespec *tv)
85 {
86         int retval = -1;
87         mach_timespec_t mts;
88
89         switch (type) {
90                 case CLOCK_REALTIME:
91                         retval = clock_get_time(clock_realtime, &mts);
92                         break;
93                 case CLOCK_MONOTONIC:
94                         retval = clock_get_time(clock_monotonic, &mts);
95                         break;
96                 default:
97                         goto out;
98         }
99
100         tv->tv_sec = mts.tv_sec;
101         tv->tv_nsec = mts.tv_nsec;
102 out:
103         return retval;
104 }
105
106 #endif
107
108 void *cbuf_alloc(unsigned int order)
109 {
110         char path[] = "/tmp/cbuf-XXXXXX";
111         unsigned long size = cbuf_size(order);
112         void *ret = NULL;
113         int fd;
114
115         fd = mkstemp(path);
116         if (fd < 0)
117                 return NULL;
118
119         if (unlink(path))
120                 goto close;
121
122         if (ftruncate(fd, cbuf_size(order)))
123                 goto close;
124
125 #ifndef MAP_ANONYMOUS
126 #define MAP_ANONYMOUS MAP_ANON
127 #endif
128
129         ret = mmap(NULL, size * 2, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0);
130         if (ret == MAP_FAILED) {
131                 ret = NULL;
132                 goto close;
133         }
134
135         if (mmap(ret, size, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED,
136                  fd, 0) != ret ||
137             mmap(ret + size, size, PROT_READ | PROT_WRITE,
138                  MAP_FIXED | MAP_SHARED, fd, 0) != ret + size) {
139                 munmap(ret, size * 2);
140                 ret = NULL;
141         }
142
143 close:
144         close(fd);
145         return ret;
146 }
147
148 void cbuf_free(void *ptr, unsigned int order)
149 {
150         munmap(ptr, cbuf_size(order) * 2);
151 }