diff options
Diffstat (limited to 'utils.c')
| -rw-r--r-- | utils.c | 53 |
1 files changed, 53 insertions, 0 deletions
| @@ -23,7 +23,9 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |||
| 23 | #endif | 23 | #endif |
| 24 | 24 | ||
| 25 | #include <stdlib.h> | 25 | #include <stdlib.h> |
| 26 | #include <string.h> | ||
| 26 | #include "utils.h" | 27 | #include "utils.h" |
| 28 | #include "log.h" | ||
| 27 | 29 | ||
| 28 | void fdlist_create(struct fdlist *list) | 30 | void fdlist_create(struct fdlist *list) |
| 29 | { | 31 | { |
| @@ -55,3 +57,54 @@ void fdlist_free(struct fdlist *list) | |||
| 55 | free(list->fds); | 57 | free(list->fds); |
| 56 | list->fds = NULL; | 58 | list->fds = NULL; |
| 57 | } | 59 | } |
| 60 | |||
| 61 | void collection_init(struct collection *col) | ||
| 62 | { | ||
| 63 | col->list = malloc(sizeof(void *)); | ||
| 64 | memset(col->list, 0, sizeof(void *)); | ||
| 65 | col->capacity = 1; | ||
| 66 | } | ||
| 67 | |||
| 68 | void collection_free(struct collection *col) | ||
| 69 | { | ||
| 70 | free(col->list); | ||
| 71 | col->list = NULL; | ||
| 72 | col->capacity = 0; | ||
| 73 | } | ||
| 74 | |||
| 75 | void collection_add(struct collection *col, void *element) | ||
| 76 | { | ||
| 77 | int i; | ||
| 78 | for(i=0; i<col->capacity; i++) { | ||
| 79 | if(!col->list[i]) { | ||
| 80 | col->list[i] = element; | ||
| 81 | return; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | col->list = realloc(col->list, sizeof(void*) * col->capacity * 2); | ||
| 85 | memset(&col->list[col->capacity], 0, sizeof(void *) * col->capacity); | ||
| 86 | col->list[col->capacity] = element; | ||
| 87 | col->capacity *= 2; | ||
| 88 | } | ||
| 89 | |||
| 90 | void collection_remove(struct collection *col, void *element) | ||
| 91 | { | ||
| 92 | int i; | ||
| 93 | for(i=0; i<col->capacity; i++) { | ||
| 94 | if(col->list[i] == element) { | ||
| 95 | col->list[i] = NULL; | ||
| 96 | return; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | usbmuxd_log(LL_ERROR, "collection_remove: element %p not present in collection %p (cap %d)", element, col, col->capacity); | ||
| 100 | } | ||
| 101 | |||
| 102 | int collection_count(struct collection *col) | ||
| 103 | { | ||
| 104 | int i, cnt = 0; | ||
| 105 | for(i=0; i<col->capacity; i++) { | ||
| 106 | if(col->list[i]) | ||
| 107 | cnt++; | ||
| 108 | } | ||
| 109 | return cnt; | ||
| 110 | } | ||
