chiark / gitweb /
checkpath.c: Allocate path-element nodes from the pool.
authorMark Wooding <mdw@distorted.org.uk>
Wed, 17 Jul 2024 12:07:14 +0000 (13:07 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Wed, 17 Jul 2024 12:23:10 +0000 (13:23 +0100)
Now we don't need to worry about freeing them.  In particular, `popall'
now becomes trivial.

checkpath.c

index 95524ae24f34c3b9fc01d670c9c1585cd029b32a..76f80fac97f3888b52dbb4e22e71f19c720986e7 100644 (file)
@@ -78,7 +78,8 @@ static const struct elt rootnode = { 0, 0, 0 }; /* Root of the list */
 
 /* --- @splitpath@ --- *
  *
 
 /* --- @splitpath@ --- *
  *
- * Arguments:  @const char *path@ = path string to break apart
+ * Arguments:  @struct state *state@ = pointer to state
+ *             @const char *path@ = path string to break apart
  *             @struct elt *tail@ = tail block to attach to end of list
  *
  * Returns:    Pointer to the new list head.
  *             @struct elt *tail@ = tail block to attach to end of list
  *
  * Returns:    Pointer to the new list head.
@@ -88,7 +89,8 @@ static const struct elt rootnode = { 0, 0, 0 }; /* Root of the list */
  *             be pushed onto the directory stack as required.
  */
 
  *             be pushed onto the directory stack as required.
  */
 
-static struct elt *splitpath(const char *path, struct elt *tail)
+static struct elt *splitpath(struct state *state,
+                            const char *path, struct elt *tail)
 {
   struct elt *head, **ee = &head, *e;
   size_t n;
 {
   struct elt *head, **ee = &head, *e;
   size_t n;
@@ -111,7 +113,7 @@ static struct elt *splitpath(const char *path, struct elt *tail)
      */
 
     n = strcspn(path, "/");
      */
 
     n = strcspn(path, "/");
-    e = xmalloc(sizeof(struct elt) + n + 1);
+    e = pool_alloc(state->p, offsetof(struct elt, e_name) + n + 1);
     memcpy(e->e_name, path, n);
     e->e_name[n] = 0;
     e->e_flags = 0;
     memcpy(e->e_name, path, n);
     e->e_name[n] = 0;
     e->e_flags = 0;
@@ -143,7 +145,7 @@ static void pop(struct state *state)
     e = sp->e_link;
     state->path.len = sp->e_offset;
     DPUTZ(&state->path);
     e = sp->e_link;
     state->path.len = sp->e_offset;
     DPUTZ(&state->path);
-    xfree(state->sp); state->sp = e;
+    state->sp = e;
   }
 }
 
   }
 }
 
@@ -157,7 +159,7 @@ static void pop(struct state *state)
  */
 
 static void popall(struct state *state)
  */
 
 static void popall(struct state *state)
-  { while (state->sp->e_link) pop(state); }
+  { state->sp = (/*unconst*/ struct elt *)&rootnode; state->path.len = 0; }
 
 /* --- @push@ --- *
  *
 
 /* --- @push@ --- *
  *
@@ -391,9 +393,9 @@ unsigned checkpath(const char *p, const struct checkpath *cp)
 
   /* --- Get the initial list of things to process --- */
 
 
   /* --- Get the initial list of things to process --- */
 
-  ee = splitpath(p, 0);
+  ee = splitpath(state, p, 0);
   if (*p != '/')
   if (*p != '/')
-    ee = splitpath(cwd, ee);
+    ee = splitpath(state, cwd, ee);
 
   /* --- While there are list items which still need doing --- */
 
 
   /* --- While there are list items which still need doing --- */
 
@@ -403,7 +405,6 @@ unsigned checkpath(const char *p, const struct checkpath *cp)
     /* --- Strip off simple `.' elements --- */
 
     if (strcmp(ee->e_name, ".") == 0) {
     /* --- Strip off simple `.' elements --- */
 
     if (strcmp(ee->e_name, ".") == 0) {
-      xfree(ee);
       ee = e;
       continue;
     }
       ee = e;
       continue;
     }
@@ -412,7 +413,6 @@ unsigned checkpath(const char *p, const struct checkpath *cp)
 
     else if (strcmp(ee->e_name, "..") == 0) {
       pop(state);
 
     else if (strcmp(ee->e_name, "..") == 0) {
       pop(state);
-      xfree(ee);
       ee = e;
       continue;
     }
       ee = e;
       continue;
     }
@@ -467,7 +467,7 @@ unsigned checkpath(const char *p, const struct checkpath *cp)
        popall(state);
       else
        pop(state);
        popall(state);
       else
        pop(state);
-      ee = splitpath(buf.buf, ee);
+      ee = splitpath(state, buf.buf, ee);
       continue;
     }
 
       continue;
     }
 
@@ -494,12 +494,10 @@ unsigned checkpath(const char *p, const struct checkpath *cp)
       report(cp, CP_ERROR, 0, 0, "junk left over after reaching leaf");
     while (ee) {
       e = ee->e_link;
       report(cp, CP_ERROR, 0, 0, "junk left over after reaching leaf");
     while (ee) {
       e = ee->e_link;
-      xfree(ee);
       ee = e;
     }
   }
 
       ee = e;
     }
   }
 
-  popall(state);
   dstr_destroy(&state->path);
   dstr_destroy(&buf);
   pool_destroy(state->p);
   dstr_destroy(&state->path);
   dstr_destroy(&buf);
   pool_destroy(state->p);