]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
mempools-nozero part 2: wordlist
authorFrancesco Chemolli <kinkie@squid-cache.org>
Mon, 24 Aug 2015 16:51:17 +0000 (18:51 +0200)
committerFrancesco Chemolli <kinkie@squid-cache.org>
Mon, 24 Aug 2015 16:51:17 +0000 (18:51 +0200)
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
src/tests/stub_wordlist.cc
src/wordlist.cc
src/wordlist.h

index 323f5fad12b77f7e66f1f6b2cb861da094c00d04..526223f0217722f97092268b7659c5bf6b9cd334 100644 (file)
@@ -1452,7 +1452,6 @@ ftpReadType(Ftp::Gateway * ftpState)
 static void
 ftpTraverseDirectory(Ftp::Gateway * ftpState)
 {
-    wordlist *w;
     debugs(9, 4, HERE << (ftpState->filepath ? ftpState->filepath : "<NULL>"));
 
     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) {
index d1457e5a2f280f71c48bc5d18434dc0990e0616f..d012d78716df740f9af34e952729f407554dd5e0 100644 (file)
@@ -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
 
index b27b4e84a80adb13020faf90b72c93cc02b52d23..a08e6955d0f171d5a329cd568ba4d91d4828bdb3 100644 (file)
@@ -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;
+}
index f1a62e545acfce4dce2cc028c676e179c4f5eb62..2dc2971b9c859e739c3aa41bfd3fa9c2de38f61b 100644 (file)
 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 *);