replace a linked list in the process_parts function with a low level array

Sun, 31 Aug 2025 14:32:55 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 31 Aug 2025 14:32:55 +0200
changeset 875
3b9ac2dd757d
parent 874
8f0add189b2f
child 876
9c2e229b05f9

replace a linked list in the process_parts function with a low level array

.hgignore file | annotate | diff | comparison | revisions
dav/db.c file | annotate | diff | comparison | revisions
--- a/.hgignore	Sun Jun 01 22:08:35 2025 +0200
+++ b/.hgignore	Sun Aug 31 14:32:55 2025 +0200
@@ -11,3 +11,4 @@
 ^make/vs/vcpkg_installed$
 ^make/vs/.vs$
 ^make/.*vcxproj.user$
+gmon.out
--- a/dav/db.c	Sun Jun 01 22:08:35 2025 +0200
+++ b/dav/db.c	Sun Aug 31 14:32:55 2025 +0200
@@ -34,6 +34,7 @@
 #include "db.h"
 
 #include <cx/utils.h>
+#include <cx/array_list.h>
 
 #include <libidav/utils.h>
 
@@ -112,13 +113,12 @@
 }
 
 void process_parts(xmlTextReaderPtr reader, LocalResource *res) {
-    // TODO: rewrite using low level array
-    
-    CxList *parts = cxLinkedListCreateSimple(CX_STORE_POINTERS);
+    size_t parts_alloc = 32;
+    size_t parts_size = 0;
+    FilePart *parts = calloc(parts_alloc, sizeof(FilePart));
     
     FilePart *current_part = NULL;
     
-    size_t count = 0;
     int field = -1;
     int err = 0;
     while(xmlTextReaderRead(reader)) {
@@ -128,10 +128,11 @@
         
         if(type == XML_READER_TYPE_ELEMENT) {
             if(depth == 3 && xstreq(name, "part")) {
-                current_part = calloc(1, sizeof(FilePart));
-                current_part->block = count;
-                cxListAdd(parts, current_part);
-                count++;
+                FilePart newpart = { 0 };
+                cx_array_add(&parts, &parts_size, &parts_alloc, sizeof(FilePart), &newpart, cx_array_default_reallocator);
+                
+                current_part = &parts[parts_size-1];
+                current_part->block = parts_size;
             } else if(depth == 4) {
                 if(xstreq(name, "hash")) {
                     field = 0;
@@ -164,18 +165,15 @@
     }
     
     if(!err) {
-        FilePart *file_parts = calloc(count, sizeof(FilePart));
-        CxIterator iter = cxListIterator(parts);
-        cx_foreach(FilePart*, p, iter) {
-            file_parts[iter.index] = *p;
-            free(p);
+        res->parts = parts;
+        res->numparts = parts_size;
+    } else {
+        for(int i=0;i<parts_size;i++) {
+            free(parts[i].etag);
+            free(parts[i].hash);
         }
-        
-        res->parts = file_parts;
-        res->numparts = count;
-    } 
-    
-    cxListFree(parts);
+        free(parts);
+    }
 }
 
 LocalResource* process_resource(xmlTextReaderPtr reader) {

mercurial