From: Mark Wooding Date: Wed, 17 Jul 2024 12:07:14 +0000 (+0100) Subject: checkpath.c: Allocate path-element nodes from the pool. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/checkpath/commitdiff_plain/8a1639e170b9ea4540da484693548bf644b49846?ds=inline checkpath.c: Allocate path-element nodes from the pool. Now we don't need to worry about freeing them. In particular, `popall' now becomes trivial. --- diff --git a/checkpath.c b/checkpath.c index 95524ae..76f80fa 100644 --- a/checkpath.c +++ b/checkpath.c @@ -78,7 +78,8 @@ static const struct elt rootnode = { 0, 0, 0 }; /* Root of the list */ /* --- @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. @@ -88,7 +89,8 @@ static const struct elt rootnode = { 0, 0, 0 }; /* Root of the list */ * 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; @@ -111,7 +113,7 @@ static struct elt *splitpath(const char *path, struct elt *tail) */ 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; @@ -143,7 +145,7 @@ static void pop(struct state *state) 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) - { while (state->sp->e_link) pop(state); } + { state->sp = (/*unconst*/ struct elt *)&rootnode; state->path.len = 0; } /* --- @push@ --- * * @@ -391,9 +393,9 @@ unsigned checkpath(const char *p, const struct checkpath *cp) /* --- Get the initial list of things to process --- */ - ee = splitpath(p, 0); + ee = splitpath(state, p, 0); if (*p != '/') - ee = splitpath(cwd, ee); + ee = splitpath(state, cwd, ee); /* --- 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) { - xfree(ee); 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); - xfree(ee); ee = e; continue; } @@ -467,7 +467,7 @@ unsigned checkpath(const char *p, const struct checkpath *cp) popall(state); else pop(state); - ee = splitpath(buf.buf, ee); + ee = splitpath(state, buf.buf, ee); 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; - xfree(ee); ee = e; } } - popall(state); dstr_destroy(&state->path); dstr_destroy(&buf); pool_destroy(state->p);