]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Fix issues with move SBuf::compare, implement SBuf move constructors and move assignm...
authorFrancesco Chemolli <kinkie@squid-cache.org>
Wed, 18 Feb 2015 16:13:33 +0000 (17:13 +0100)
committerFrancesco Chemolli <kinkie@squid-cache.org>
Wed, 18 Feb 2015 16:13:33 +0000 (17:13 +0100)
src/SBuf.cc
src/SBuf.h

index e1f98837e589d57c0ad9d97fa738d83838cbd9b6..28f440acb68f31a5837e70e8e55b390a472bb18e 100644 (file)
@@ -37,7 +37,7 @@ const SBuf::size_type SBuf::maxSize;
 
 SBufStats::SBufStats()
     : alloc(0), allocCopy(0), allocFromString(0), allocFromCString(0),
-      assignFast(0), clear(0), append(0), toStream(0), setChar(0),
+      assignFast(0), clear(0), append(0), moves(0), toStream(0), setChar(0),
       getChar(0), compareSlow(0), compareFast(0), copyOut(0),
       rawAccess(0), nulTerminate(0), chop(0), trim(0), find(0), scanf(0),
       caseChange(0), cowFast(0), cowSlow(0), live(0)
@@ -53,6 +53,7 @@ SBufStats::operator +=(const SBufStats& ss)
     assignFast += ss.assignFast;
     clear += ss.clear;
     append += ss.append;
+    moves += ss.moves;
     toStream += ss.toStream;
     setChar += ss.setChar;
     getChar += ss.getChar;
@@ -365,7 +366,8 @@ memcasecmp(const char *b1, const char *b2, SBuf::size_type len)
 int
 SBuf::compare(const SBuf &S, const SBufCaseSensitive isCaseSensitive, const size_type n) const
 {
-    if (n != npos && (n > length() || n > S.length())) {
+//    if (n != npos && (n > length() || n > S.length())) {
+    if (n != npos) {
         debugs(24, 8, "length specified. substr and recurse");
         return substr(0,n).compare(S.substr(0,n),isCaseSensitive);
     }
@@ -383,8 +385,12 @@ SBuf::compare(const SBuf &S, const SBufCaseSensitive isCaseSensitive, const size
         debugs(24, 8, "result: " << rv);
         return rv;
     }
+    if (n <= length() || n <= S.length()) {
+        debugs(24, 8, "same contents and bounded length. Equal");
+        return 0;
+    }
     if (length() == S.length()) {
-        debugs(24, 8, "same contents and length. Equal");
+        debugs(24, 8, "same contents and same length. Equal");
         return 0;
     }
     if (length() > S.length()) {
@@ -809,6 +815,7 @@ SBufStats::dump(std::ostream& os) const
        "\nno-copy assignments: " << assignFast <<
        "\nclearing operations: " << clear <<
        "\nappend operations: " << append <<
+       "\nmove operations: " << moves <<
        "\ndump-to-ostream: " << toStream <<
        "\nset-char: " << setChar <<
        "\nget-char: " << getChar <<
index 41b18a6041f6c323f6c5afbffb454515bc8919df..f80bdfb1361323347316031e57352839c89ac4f3 100644 (file)
@@ -51,6 +51,7 @@ public:
     uint64_t assignFast; ///<number of no-copy assignment operations
     uint64_t clear; ///<number of clear operations
     uint64_t append; ///<number of append operations
+    uint64_t moves; ///<number of move constructions/assignments
     uint64_t toStream;  ///<number of write operations to ostreams
     uint64_t setChar; ///<number of calls to setAt
     uint64_t getChar; ///<number of calls to at() and operator[]
@@ -136,7 +137,7 @@ public:
     SBuf();
     SBuf(const SBuf &S);
     SBuf(SBuf&& S) : store_(std::move(S.store_)), off_(S.off_), len_(S.len_) {
-        S.store_=GetStorePrototype(); S.off_=0; S.len_=0;
+        S.store_=NULL; S.off_=0; S.len_=0; //RefCount supports NULL
     }
 
     /** Constructor: import c-style string
@@ -150,7 +151,7 @@ public:
      * \note bounds is 0 <= pos < length(); caller must pay attention to signedness
      */
     explicit SBuf(const char *S, size_type n);
-    explicit SBuf(const char *S) : SBuf(S,strlen(S)) {}
+    explicit SBuf(const char *S) : SBuf(S, npos) {}
 
     /** Constructor: import SquidString, copying contents.
      *
@@ -174,6 +175,18 @@ public:
      * Current SBuf will share backing store with the assigned one.
      */
     SBuf& operator =(const SBuf & S) {return assign(S);}
+    SBuf& operator =(SBuf &&S) {
+        ++stats.moves;
+        if (this != &S) {
+            store_ = std::move(S.store_);
+            off_ = S.off_;
+            len_ = S.len_;
+            S.store_ = NULL; //RefCount supports NULL, and S is about to be destructed
+            S.off_ = 0;
+            S.len_ = 0;
+        }
+        return *this;
+    }
 
     /** Import a c-string into a SBuf, copying the data.
      *
@@ -186,7 +199,7 @@ public:
      *    assign(stdstr.data(), stdstd.length())
      */
     SBuf& assign(const char *S, size_type n);
-    SBuf& assign(const char *S) {return assign(S,strlen(S));}
+    SBuf& assign(const char *S) {return assign(S,npos);}
 
     /** Assignment operator. Copy a NULL-terminated c-style string into a SBuf.
      *
@@ -224,7 +237,7 @@ public:
      *     cstr_append(stdstr.data(), stdstd.length())
      */
     SBuf& append(const char * S, size_type Ssize);
-    SBuf& append(const char * S) { return append(S,strlen(S)); }
+    SBuf& append(const char * S) { return append(S,npos); }
 
     /** Assignment operation with printf(3)-style definition
      * \note arguments may be evaluated more than once, be careful
@@ -287,7 +300,7 @@ public:
      */
     int compare(const SBuf &S, const SBufCaseSensitive isCaseSensitive, const size_type n) const;
     int compare(const SBuf &S, const SBufCaseSensitive isCaseSensitive) const {
-        return compare(S, isCaseSensitive, S.length());
+        return compare(S, isCaseSensitive, npos);
     }
 
     /// shorthand version for compare()
@@ -295,7 +308,7 @@ public:
         return compare(S,caseSensitive,n);
     }
     inline int cmp(const SBuf &S) const {
-        return compare(S,caseSensitive,S.length());
+        return compare(S,caseSensitive,npos);
     }
 
     /// shorthand version for case-insensitive compare()
@@ -303,13 +316,13 @@ public:
         return compare(S,caseInsensitive,n);
     }
     inline int caseCmp(const SBuf &S) const {
-        return compare(S,caseInsensitive,S.length());
+        return compare(S,caseInsensitive,npos);
     }
 
     /// Comparison with a C-string.
     int compare(const char *s, const SBufCaseSensitive isCaseSensitive, const size_type n) const;
     int compare(const char *s, const SBufCaseSensitive isCaseSensitive) const {
-        return compare(s,isCaseSensitive,strlen(s));
+        return compare(s,isCaseSensitive,npos);
     }
 
     /// Shorthand version for C-string compare().
@@ -317,7 +330,7 @@ public:
         return compare(S,caseSensitive,n);
     }
     inline int cmp(const char *S) const {
-        return compare(S,caseSensitive,strlen(S));
+        return compare(S,caseSensitive,npos);
     }
 
     /// Shorthand version for case-insensitive C-string compare().
@@ -325,7 +338,7 @@ public:
         return compare(S,caseInsensitive,n);
     }
     inline int caseCmp(const char *S) const {
-        return compare(S,caseInsensitive,strlen(S));
+        return compare(S,caseInsensitive,npos);
     }
 
     /** check whether the entire supplied argument is a prefix of the SBuf.