summaryrefslogtreecommitdiffstats
path: root/src/utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils.c')
-rw-r--r--src/utils.c131
1 files changed, 131 insertions, 0 deletions
diff --git a/src/utils.c b/src/utils.c
new file mode 100644
index 0000000..2cc5675
--- /dev/null
+++ b/src/utils.c
@@ -0,0 +1,131 @@
1/*
2 * utils.c
3 *
4 * Copyright (C) 2009 Hector Martin <hector@marcansoft.com>
5 * Copyright (C) 2009 Nikias Bassen <nikias@gmx.li>
6 * Copyright (c) 2013 Federico Mena Quintero
7 *
8 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation, either version 2.1 of the
11 * License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#ifdef HAVE_CONFIG_H
24#include <config.h>
25#endif
26
27#include <stdlib.h>
28#include <string.h>
29#include <stdio.h>
30#include <stdarg.h>
31#include <time.h>
32#include <sys/time.h>
33#include <errno.h>
34#ifdef __APPLE__
35#include <mach/mach_time.h>
36#endif
37
38#include "utils.h"
39
40#include "log.h"
41#define util_error(...) usbmuxd_log(LL_ERROR, __VA_ARGS__)
42
43void fdlist_create(struct fdlist *list)
44{
45 list->count = 0;
46 list->capacity = 4;
47 list->owners = malloc(sizeof(*list->owners) * list->capacity);
48 list->fds = malloc(sizeof(*list->fds) * list->capacity);
49}
50void fdlist_add(struct fdlist *list, enum fdowner owner, int fd, short events)
51{
52 if(list->count == list->capacity) {
53 list->capacity *= 2;
54 list->owners = realloc(list->owners, sizeof(*list->owners) * list->capacity);
55 list->fds = realloc(list->fds, sizeof(*list->fds) * list->capacity);
56 }
57 list->owners[list->count] = owner;
58 list->fds[list->count].fd = fd;
59 list->fds[list->count].events = events;
60 list->fds[list->count].revents = 0;
61 list->count++;
62}
63
64void fdlist_free(struct fdlist *list)
65{
66 list->count = 0;
67 list->capacity = 0;
68 free(list->owners);
69 list->owners = NULL;
70 free(list->fds);
71 list->fds = NULL;
72}
73
74void fdlist_reset(struct fdlist *list)
75{
76 list->count = 0;
77}
78
79#ifndef HAVE_CLOCK_GETTIME
80typedef int clockid_t;
81#define CLOCK_MONOTONIC 1
82
83static int clock_gettime(clockid_t clk_id, struct timespec *ts)
84{
85 // See http://developer.apple.com/library/mac/qa/qa1398
86
87 uint64_t mach_time, nano_sec;
88
89 static mach_timebase_info_data_t base_info;
90
91 mach_time = mach_absolute_time();
92
93 if (base_info.denom == 0) {
94 (void) mach_timebase_info(&base_info);
95 }
96
97 if (base_info.numer == 1 && base_info.denom == 1)
98 nano_sec = mach_time;
99 else
100 nano_sec = mach_time * base_info.numer / base_info.denom;
101
102 ts->tv_sec = nano_sec / 1000000000;
103 ts->tv_nsec = nano_sec % 1000000000;
104
105 return 0;
106}
107#endif
108
109void get_tick_count(struct timeval * tv)
110{
111 struct timespec ts;
112 if(0 == clock_gettime(CLOCK_MONOTONIC, &ts)) {
113 tv->tv_sec = ts.tv_sec;
114 tv->tv_usec = ts.tv_nsec / 1000;
115 } else {
116 gettimeofday(tv, NULL);
117 }
118}
119
120/**
121 * Get number of milliseconds since the epoch.
122 */
123uint64_t mstime64(void)
124{
125 struct timeval tv;
126 get_tick_count(&tv);
127
128 // Careful, avoid overflow on 32 bit systems
129 // time_t could be 4 bytes
130 return ((long long)tv.tv_sec) * 1000LL + ((long long)tv.tv_usec) / 1000LL;
131}