From 0d225c059e9d6c571606387aea6aacfe5ecf2a6c Mon Sep 17 00:00:00 2001 From: Francesco Chemolli Date: Mon, 24 Aug 2015 18:51:17 +0200 Subject: [PATCH] mempools-nozero part 2: wordlist Implement constructors for wordlist allowing them to support non-zeroing pools Make destructor private in order to force clients to use wordlistDestroy Implement wordlistChopHead to support the only user of deleting the head of a wordlist --- src/clients/FtpGateway.cc | 9 +-------- src/tests/stub_wordlist.cc | 1 - src/wordlist.cc | 36 ++++++++++++++---------------------- src/wordlist.h | 24 +++++++++++++++++++++--- 4 files changed, 36 insertions(+), 34 deletions(-) diff --git a/src/clients/FtpGateway.cc b/src/clients/FtpGateway.cc index 323f5fad12..526223f021 100644 --- a/src/clients/FtpGateway.cc +++ b/src/clients/FtpGateway.cc @@ -1452,7 +1452,6 @@ ftpReadType(Ftp::Gateway * ftpState) static void ftpTraverseDirectory(Ftp::Gateway * ftpState) { - wordlist *w; debugs(9, 4, HERE << (ftpState->filepath ? ftpState->filepath : "")); safe_free(ftpState->dirpath); @@ -1468,13 +1467,7 @@ ftpTraverseDirectory(Ftp::Gateway * ftpState) } /* Go to next path component */ - w = ftpState->pathcomps; - - ftpState->filepath = w->key; - - ftpState->pathcomps = w->next; - - delete w; + ftpState->filepath = wordlistChopHead(& ftpState->pathcomps); /* Check if we are to CWD or RETR */ if (ftpState->pathcomps != NULL || ftpState->flags.isdir) { diff --git a/src/tests/stub_wordlist.cc b/src/tests/stub_wordlist.cc index d1457e5a2f..d012d78716 100644 --- a/src/tests/stub_wordlist.cc +++ b/src/tests/stub_wordlist.cc @@ -15,6 +15,5 @@ const char *wordlistAdd(wordlist **, const char *) STUB_RETVAL(NULL) void wordlistAddWl(wordlist **, wordlist *) STUB void wordlistJoin(wordlist **, wordlist **) STUB -wordlist *wordlistDup(const wordlist *) STUB_RETVAL(NULL) void wordlistDestroy(wordlist **) STUB diff --git a/src/wordlist.cc b/src/wordlist.cc index b27b4e84a8..a08e6955d0 100644 --- a/src/wordlist.cc +++ b/src/wordlist.cc @@ -32,12 +32,7 @@ wordlistAdd(wordlist ** list, const char *key) while (*list) list = &(*list)->next; - *list = new wordlist; - - (*list)->key = xstrdup(key); - - (*list)->next = NULL; - + *list = new wordlist(key); return (*list)->key; } @@ -59,9 +54,7 @@ wordlistAddWl(wordlist ** list, wordlist * wl) list = &(*list)->next; for (; wl; wl = wl->next, list = &(*list)->next) { - *list = new wordlist(); - (*list)->key = xstrdup(wl->key); - (*list)->next = NULL; + *list = new wordlist(wl->key); } } @@ -74,19 +67,6 @@ wordlistCat(const wordlist * w, MemBuf * mb) } } -wordlist * -wordlistDup(const wordlist * w) -{ - wordlist *D = NULL; - - while (NULL != w) { - wordlistAdd(&D, w->key); - w = w->next; - } - - return D; -} - SBufList ToSBufList(wordlist *wl) { @@ -98,3 +78,15 @@ ToSBufList(wordlist *wl) return rv; } +char * +wordlistChopHead(wordlist **wl) +{ + if (*wl == nullptr) + return nullptr; + + wordlist *w = *wl; + char *rv = w->key; + *wl = w->next; + delete w; + return rv; +} diff --git a/src/wordlist.h b/src/wordlist.h index f1a62e545a..2dc2971b9c 100644 --- a/src/wordlist.h +++ b/src/wordlist.h @@ -20,10 +20,23 @@ class wordlist { MEMPROXY_CLASS(wordlist); + friend void wordlistDestroy(wordlist ** list); + friend char *wordlistChopHead(wordlist **); public: + wordlist() : key(nullptr), next(nullptr) {} + // create a new wordlist node, with a copy of k as key + explicit wordlist(const char *k) : key(xstrdup(k)), next(nullptr) {} + + wordlist(const wordlist &) = delete; + wordlist &operator=(const wordlist &) = delete; + char *key; wordlist *next; + +private: + // use wordlistDestroy instead + ~wordlist() = default; }; class MemBuf; @@ -52,12 +65,17 @@ void wordlistAddWl(wordlist **, wordlist *); */ void wordlistJoin(wordlist **, wordlist **); -/// duplicate a wordlist -wordlist *wordlistDup(const wordlist *); - /// destroy a wordlist void wordlistDestroy(wordlist **); +/** remove the first element in a wordlist, and return its key + * + * \note the returned key must be freed by the caller using safe_free + * \note wl is altered so that it points to the second element + * \return nullptr if pointed-to wordlist is nullptr. + */ +char *wordlistChopHead(wordlist **); + /// convert a wordlist to a SBufList SBufList ToSBufList(wordlist *); -- 2.47.2