From 49a8ef4fcbc8e76ca0f484e6b2d2d9eea70596b6 Mon Sep 17 00:00:00 2001 From: Hector Martin Date: Thu, 30 Apr 2009 05:52:10 +0200 Subject: too much stuff and it WORKS --- utils.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'utils.c') diff --git a/utils.c b/utils.c index 41c3bcb..1ffa04a 100644 --- a/utils.c +++ b/utils.c @@ -23,7 +23,9 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #endif #include +#include #include "utils.h" +#include "log.h" void fdlist_create(struct fdlist *list) { @@ -55,3 +57,54 @@ void fdlist_free(struct fdlist *list) free(list->fds); list->fds = NULL; } + +void collection_init(struct collection *col) +{ + col->list = malloc(sizeof(void *)); + memset(col->list, 0, sizeof(void *)); + col->capacity = 1; +} + +void collection_free(struct collection *col) +{ + free(col->list); + col->list = NULL; + col->capacity = 0; +} + +void collection_add(struct collection *col, void *element) +{ + int i; + for(i=0; icapacity; i++) { + if(!col->list[i]) { + col->list[i] = element; + return; + } + } + col->list = realloc(col->list, sizeof(void*) * col->capacity * 2); + memset(&col->list[col->capacity], 0, sizeof(void *) * col->capacity); + col->list[col->capacity] = element; + col->capacity *= 2; +} + +void collection_remove(struct collection *col, void *element) +{ + int i; + for(i=0; icapacity; i++) { + if(col->list[i] == element) { + col->list[i] = NULL; + return; + } + } + usbmuxd_log(LL_ERROR, "collection_remove: element %p not present in collection %p (cap %d)", element, col, col->capacity); +} + +int collection_count(struct collection *col) +{ + int i, cnt = 0; + for(i=0; icapacity; i++) { + if(col->list[i]) + cnt++; + } + return cnt; +} -- cgit v1.1-32-gdbae