diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/utils.c | 125 | ||||
-rw-r--r-- | common/utils.h | 69 |
2 files changed, 0 insertions, 194 deletions
diff --git a/common/utils.c b/common/utils.c deleted file mode 100644 index db628ce..0000000 --- a/common/utils.c +++ /dev/null | |||
@@ -1,125 +0,0 @@ | |||
1 | /* | ||
2 | usbmuxd - iPhone/iPod Touch USB multiplex server daemon | ||
3 | |||
4 | Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com> | ||
5 | Copyright (C) 2009 Nikias Bassen <nikias@gmx.li> | ||
6 | |||
7 | This program is free software; you can redistribute it and/or modify | ||
8 | it under the terms of the GNU General Public License as published by | ||
9 | the Free Software Foundation, either version 2 or version 3. | ||
10 | |||
11 | This program is distributed in the hope that it will be useful, | ||
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | GNU General Public License for more details. | ||
15 | |||
16 | You should have received a copy of the GNU General Public License | ||
17 | along with this program; if not, write to the Free Software | ||
18 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
19 | |||
20 | */ | ||
21 | |||
22 | #ifdef HAVE_CONFIG_H | ||
23 | #include <config.h> | ||
24 | #endif | ||
25 | |||
26 | #include <stdlib.h> | ||
27 | #include <string.h> | ||
28 | #include <stdio.h> | ||
29 | #include "utils.h" | ||
30 | |||
31 | #ifdef USBMUXD_DAEMON | ||
32 | # include "log.h" | ||
33 | # define util_error(...) usbmuxd_log(LL_ERROR, __VA_ARGS__) | ||
34 | #else | ||
35 | # define util_error(...) fprintf(stderr, __VA_ARGS__) | ||
36 | #endif | ||
37 | |||
38 | #ifdef USBMUXD_DAEMON | ||
39 | void fdlist_create(struct fdlist *list) | ||
40 | { | ||
41 | list->count = 0; | ||
42 | list->capacity = 4; | ||
43 | list->owners = malloc(sizeof(*list->owners) * list->capacity); | ||
44 | list->fds = malloc(sizeof(*list->fds) * list->capacity); | ||
45 | } | ||
46 | void fdlist_add(struct fdlist *list, enum fdowner owner, int fd, short events) | ||
47 | { | ||
48 | if(list->count == list->capacity) { | ||
49 | list->capacity *= 2; | ||
50 | list->owners = realloc(list->owners, sizeof(*list->owners) * list->capacity); | ||
51 | list->fds = realloc(list->fds, sizeof(*list->fds) * list->capacity); | ||
52 | } | ||
53 | list->owners[list->count] = owner; | ||
54 | list->fds[list->count].fd = fd; | ||
55 | list->fds[list->count].events = events; | ||
56 | list->fds[list->count].revents = 0; | ||
57 | list->count++; | ||
58 | } | ||
59 | |||
60 | void fdlist_free(struct fdlist *list) | ||
61 | { | ||
62 | list->count = 0; | ||
63 | list->capacity = 0; | ||
64 | free(list->owners); | ||
65 | list->owners = NULL; | ||
66 | free(list->fds); | ||
67 | list->fds = NULL; | ||
68 | } | ||
69 | |||
70 | void fdlist_reset(struct fdlist *list) | ||
71 | { | ||
72 | list->count = 0; | ||
73 | } | ||
74 | #endif | ||
75 | |||
76 | void collection_init(struct collection *col) | ||
77 | { | ||
78 | col->list = malloc(sizeof(void *)); | ||
79 | memset(col->list, 0, sizeof(void *)); | ||
80 | col->capacity = 1; | ||
81 | } | ||
82 | |||
83 | void collection_free(struct collection *col) | ||
84 | { | ||
85 | free(col->list); | ||
86 | col->list = NULL; | ||
87 | col->capacity = 0; | ||
88 | } | ||
89 | |||
90 | void collection_add(struct collection *col, void *element) | ||
91 | { | ||
92 | int i; | ||
93 | for(i=0; i<col->capacity; i++) { | ||
94 | if(!col->list[i]) { | ||
95 | col->list[i] = element; | ||
96 | return; | ||
97 | } | ||
98 | } | ||
99 | col->list = realloc(col->list, sizeof(void*) * col->capacity * 2); | ||
100 | memset(&col->list[col->capacity], 0, sizeof(void *) * col->capacity); | ||
101 | col->list[col->capacity] = element; | ||
102 | col->capacity *= 2; | ||
103 | } | ||
104 | |||
105 | void collection_remove(struct collection *col, void *element) | ||
106 | { | ||
107 | int i; | ||
108 | for(i=0; i<col->capacity; i++) { | ||
109 | if(col->list[i] == element) { | ||
110 | col->list[i] = NULL; | ||
111 | return; | ||
112 | } | ||
113 | } | ||
114 | util_error("collection_remove: element %p not present in collection %p (cap %d)", element, col, col->capacity); | ||
115 | } | ||
116 | |||
117 | int collection_count(struct collection *col) | ||
118 | { | ||
119 | int i, cnt = 0; | ||
120 | for(i=0; i<col->capacity; i++) { | ||
121 | if(col->list[i]) | ||
122 | cnt++; | ||
123 | } | ||
124 | return cnt; | ||
125 | } | ||
diff --git a/common/utils.h b/common/utils.h deleted file mode 100644 index ee79e48..0000000 --- a/common/utils.h +++ /dev/null | |||
@@ -1,69 +0,0 @@ | |||
1 | /* | ||
2 | usbmuxd - iPhone/iPod Touch USB multiplex server daemon | ||
3 | |||
4 | Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com> | ||
5 | Copyright (C) 2009 Nikias Bassen <nikias@gmx.li> | ||
6 | |||
7 | This program is free software; you can redistribute it and/or modify | ||
8 | it under the terms of the GNU General Public License as published by | ||
9 | the Free Software Foundation, either version 2 or version 3. | ||
10 | |||
11 | This program is distributed in the hope that it will be useful, | ||
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | GNU General Public License for more details. | ||
15 | |||
16 | You should have received a copy of the GNU General Public License | ||
17 | along with this program; if not, write to the Free Software | ||
18 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
19 | |||
20 | */ | ||
21 | |||
22 | #ifndef __UTILS_H__ | ||
23 | #define __UTILS_H__ | ||
24 | |||
25 | #ifdef USBMUXD_DAEMON | ||
26 | #include <poll.h> | ||
27 | |||
28 | enum fdowner { | ||
29 | FD_LISTEN, | ||
30 | FD_CLIENT, | ||
31 | FD_USB | ||
32 | }; | ||
33 | |||
34 | struct fdlist { | ||
35 | int count; | ||
36 | int capacity; | ||
37 | enum fdowner *owners; | ||
38 | struct pollfd *fds; | ||
39 | }; | ||
40 | |||
41 | void fdlist_create(struct fdlist *list); | ||
42 | void fdlist_add(struct fdlist *list, enum fdowner owner, int fd, short events); | ||
43 | void fdlist_free(struct fdlist *list); | ||
44 | void fdlist_reset(struct fdlist *list); | ||
45 | #endif | ||
46 | |||
47 | struct collection { | ||
48 | void **list; | ||
49 | int capacity; | ||
50 | }; | ||
51 | |||
52 | void collection_init(struct collection *col); | ||
53 | void collection_add(struct collection *col, void *element); | ||
54 | void collection_remove(struct collection *col, void *element); | ||
55 | int collection_count(struct collection *col); | ||
56 | void collection_free(struct collection *col); | ||
57 | |||
58 | #define FOREACH(var, col) \ | ||
59 | do { \ | ||
60 | int _iter; \ | ||
61 | for(_iter=0; _iter<(col)->capacity; _iter++) { \ | ||
62 | if(!(col)->list[_iter]) continue; \ | ||
63 | var = (col)->list[_iter]; | ||
64 | |||
65 | #define ENDFOREACH \ | ||
66 | } \ | ||
67 | } while(0); | ||
68 | |||
69 | #endif | ||