summaryrefslogtreecommitdiffstats
path: root/common/utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'common/utils.c')
-rw-r--r--common/utils.c125
1 files changed, 0 insertions, 125 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
4Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com>
5Copyright (C) 2009 Nikias Bassen <nikias@gmx.li>
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation, either version 2 or version 3.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, 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
39void 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}
46void 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
60void 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
70void fdlist_reset(struct fdlist *list)
71{
72 list->count = 0;
73}
74#endif
75
76void 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
83void collection_free(struct collection *col)
84{
85 free(col->list);
86 col->list = NULL;
87 col->capacity = 0;
88}
89
90void 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
105void 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
117int 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}