summary refs log tree commit diff
path: root/imap.h
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--imap.h33
1 files changed, 21 insertions, 12 deletions
diff --git a/imap.h b/imap.h
index 0c9f6b9..e4ac7ea 100644
--- a/imap.h
+++ b/imap.h
@@ -96,14 +96,27 @@ struct Data {
 	};
 };
 
+static inline void listPush(struct List *list, struct Data data) {
+	if (list->len == list->cap) {
+		list->cap = (list->cap ? list->cap * 2 : 4);
+		list->ptr = realloc(list->ptr, sizeof(*list->ptr) * list->cap);
+		if (!list->ptr) err(EX_OSERR, "realloc");
+	}
+	list->ptr[list->len++] = data;
+}
+
+static inline void dataFree(struct Data data);
+
+static inline void listFree(struct List list) {
+	for (size_t i = 0; i < list.len; ++i) {
+		dataFree(list.ptr[i]);
+	}
+	free(list.ptr);
+}
+
 static inline void dataFree(struct Data data) {
 	if (data.type == String) free(data.string);
-	if (data.type == List) {
-		for (size_t i = 0; i < data.list.len; ++i) {
-			dataFree(data.list.ptr[i]);
-		}
-		free(data.list.ptr);
-	}
+	if (data.type == List) listFree(data.list);
 }
 
 struct Resp {
@@ -116,12 +129,8 @@ struct Resp {
 };
 
 static inline void respFree(struct Resp resp) {
-	for (size_t i = 0; i < resp.code.len; ++i) {
-		dataFree(resp.code.ptr[i]);
-	}
-	for (size_t i = 0; i < resp.data.len; ++i) {
-		dataFree(resp.data.ptr[i]);
-	}
+	listFree(resp.code);
+	listFree(resp.data);
 }
 
 extern bool imapVerbose;