diff options
Diffstat (limited to 'src/utils.c')
| -rw-r--r-- | src/utils.c | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..ceef535 --- /dev/null +++ b/src/utils.c | |||
| @@ -0,0 +1,120 @@ | |||
| 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 library is free software; you can redistribute it and/or modify | ||
| 8 | it under the terms of the GNU Lesser General Public License as | ||
| 9 | published by the Free Software Foundation, either version 2.1 of the | ||
| 10 | License, or (at your option) any later version. | ||
| 11 | |||
| 12 | This library is distributed in the hope that it will be useful, | ||
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 15 | GNU General Public License for more details. | ||
| 16 | |||
| 17 | You should have received a copy of the GNU Lesser General Public | ||
| 18 | License along with this program; if not, write to the Free Software | ||
| 19 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | |||
| 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 "utils.h" | ||
| 31 | |||
| 32 | #include "log.h" | ||
| 33 | #define util_error(...) usbmuxd_log(LL_ERROR, __VA_ARGS__) | ||
| 34 | |||
| 35 | void fdlist_create(struct fdlist *list) | ||
| 36 | { | ||
| 37 | list->count = 0; | ||
| 38 | list->capacity = 4; | ||
| 39 | list->owners = malloc(sizeof(*list->owners) * list->capacity); | ||
| 40 | list->fds = malloc(sizeof(*list->fds) * list->capacity); | ||
| 41 | } | ||
| 42 | void fdlist_add(struct fdlist *list, enum fdowner owner, int fd, short events) | ||
| 43 | { | ||
| 44 | if(list->count == list->capacity) { | ||
| 45 | list->capacity *= 2; | ||
| 46 | list->owners = realloc(list->owners, sizeof(*list->owners) * list->capacity); | ||
| 47 | list->fds = realloc(list->fds, sizeof(*list->fds) * list->capacity); | ||
| 48 | } | ||
| 49 | list->owners[list->count] = owner; | ||
| 50 | list->fds[list->count].fd = fd; | ||
| 51 | list->fds[list->count].events = events; | ||
| 52 | list->fds[list->count].revents = 0; | ||
| 53 | list->count++; | ||
| 54 | } | ||
| 55 | |||
| 56 | void fdlist_free(struct fdlist *list) | ||
| 57 | { | ||
| 58 | list->count = 0; | ||
| 59 | list->capacity = 0; | ||
| 60 | free(list->owners); | ||
| 61 | list->owners = NULL; | ||
| 62 | free(list->fds); | ||
| 63 | list->fds = NULL; | ||
| 64 | } | ||
| 65 | |||
| 66 | void fdlist_reset(struct fdlist *list) | ||
| 67 | { | ||
| 68 | list->count = 0; | ||
| 69 | } | ||
| 70 | |||
| 71 | void collection_init(struct collection *col) | ||
| 72 | { | ||
| 73 | col->list = malloc(sizeof(void *)); | ||
| 74 | memset(col->list, 0, sizeof(void *)); | ||
| 75 | col->capacity = 1; | ||
| 76 | } | ||
| 77 | |||
| 78 | void collection_free(struct collection *col) | ||
| 79 | { | ||
| 80 | free(col->list); | ||
| 81 | col->list = NULL; | ||
| 82 | col->capacity = 0; | ||
| 83 | } | ||
| 84 | |||
| 85 | void collection_add(struct collection *col, void *element) | ||
| 86 | { | ||
| 87 | int i; | ||
| 88 | for(i=0; i<col->capacity; i++) { | ||
| 89 | if(!col->list[i]) { | ||
| 90 | col->list[i] = element; | ||
| 91 | return; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | col->list = realloc(col->list, sizeof(void*) * col->capacity * 2); | ||
| 95 | memset(&col->list[col->capacity], 0, sizeof(void *) * col->capacity); | ||
| 96 | col->list[col->capacity] = element; | ||
| 97 | col->capacity *= 2; | ||
| 98 | } | ||
| 99 | |||
| 100 | void collection_remove(struct collection *col, void *element) | ||
| 101 | { | ||
| 102 | int i; | ||
| 103 | for(i=0; i<col->capacity; i++) { | ||
| 104 | if(col->list[i] == element) { | ||
| 105 | col->list[i] = NULL; | ||
| 106 | return; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | util_error("collection_remove: element %p not present in collection %p (cap %d)", element, col, col->capacity); | ||
| 110 | } | ||
| 111 | |||
| 112 | int collection_count(struct collection *col) | ||
| 113 | { | ||
| 114 | int i, cnt = 0; | ||
| 115 | for(i=0; i<col->capacity; i++) { | ||
| 116 | if(col->list[i]) | ||
| 117 | cnt++; | ||
| 118 | } | ||
| 119 | return cnt; | ||
| 120 | } | ||
