summaryrefslogtreecommitdiffstats
path: root/utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'utils.c')
-rw-r--r--utils.c53
1 files changed, 53 insertions, 0 deletions
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 <stdlib.h>
+#include <string.h>
#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; i<col->capacity; 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; i<col->capacity; 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; i<col->capacity; i++) {
+ if(col->list[i])
+ cnt++;
+ }
+ return cnt;
+}