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