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.
25 #define foreach_arg(_arg, _addr, _len, _first_addr, _first_len) \
26 for (_addr = (_first_addr), _len = (_first_len); \
28 _addr = va_arg(_arg, void **), _len = _addr ? va_arg(_arg, size_t) : 0)
30 #define C_PTR_ALIGN (sizeof(size_t))
31 #define C_PTR_MASK (-C_PTR_ALIGN)
33 void *__calloc_a(size_t len
, ...)
45 foreach_arg(ap1
, cur_addr
, cur_len
, &ret
, len
)
46 alloc_len
+= (cur_len
+ C_PTR_ALIGN
- 1 ) & C_PTR_MASK
;
49 ptr
= calloc(1, alloc_len
);
56 foreach_arg(ap
, cur_addr
, cur_len
, &ret
, len
) {
57 *cur_addr
= &ptr
[alloc_len
];
58 alloc_len
+= (cur_len
+ C_PTR_ALIGN
- 1) & C_PTR_MASK
;
65 #ifdef LIBUBOX_COMPAT_CLOCK_GETTIME
66 #include <mach/mach_host.h> /* host_get_clock_service() */
67 #include <mach/mach_port.h> /* mach_port_deallocate() */
68 #include <mach/mach_init.h> /* mach_host_self(), mach_task_self() */
69 #include <mach/clock.h> /* clock_get_time() */
71 static clock_serv_t clock_realtime
;
72 static clock_serv_t clock_monotonic
;
74 static void __constructor
clock_name_init(void)
76 mach_port_t host_self
= mach_host_self();
78 host_get_clock_service(host_self
, CLOCK_REALTIME
, &clock_realtime
);
79 host_get_clock_service(host_self
, CLOCK_MONOTONIC
, &clock_monotonic
);
82 static void __destructor
clock_name_dealloc(void)
84 mach_port_t self
= mach_task_self();
86 mach_port_deallocate(self
, clock_realtime
);
87 mach_port_deallocate(self
, clock_monotonic
);
90 int clock_gettime(int type
, struct timespec
*tv
)
97 retval
= clock_get_time(clock_realtime
, &mts
);
100 retval
= clock_get_time(clock_monotonic
, &mts
);
106 tv
->tv_sec
= mts
.tv_sec
;
107 tv
->tv_nsec
= mts
.tv_nsec
;
114 void *cbuf_alloc(unsigned int order
)
116 char path
[] = "/tmp/cbuf-XXXXXX";
117 unsigned long size
= cbuf_size(order
);
128 if (ftruncate(fd
, cbuf_size(order
)))
131 #ifndef MAP_ANONYMOUS
132 #define MAP_ANONYMOUS MAP_ANON
135 ret
= mmap(NULL
, size
* 2, PROT_NONE
, MAP_ANON
| MAP_PRIVATE
, -1, 0);
136 if (ret
== MAP_FAILED
) {
141 if (mmap(ret
, size
, PROT_READ
| PROT_WRITE
, MAP_FIXED
| MAP_SHARED
,
143 mmap(ret
+ size
, size
, PROT_READ
| PROT_WRITE
,
144 MAP_FIXED
| MAP_SHARED
, fd
, 0) != ret
+ size
) {
145 munmap(ret
, size
* 2);
154 void cbuf_free(void *ptr
, unsigned int order
)
156 munmap(ptr
, cbuf_size(order
) * 2);