]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
include/interval_set: tolerate maps that invalidate iterator on change
authorSage Weil <sage@redhat.com>
Sat, 28 Oct 2017 20:37:03 +0000 (15:37 -0500)
committerSage Weil <sage@redhat.com>
Sat, 28 Oct 2017 21:25:05 +0000 (16:25 -0500)
These changes picked out of the diff between the original
btree_interval_set.h and interval_set.h (sadly I had it rolled into the
initial commit so it was tedious to identify these).

Signed-off-by: Sage Weil <sage@redhat.com>
src/include/interval_set.h

index d46121302bfe534e183713f3387cca4ca6d83c9c..ff3a23db75edfb265698f069e95702b850e8da42 100644 (file)
 
 #include "encoding.h"
 
+/*
+ * *** NOTE ***
+ *
+ * This class is written to work with a variety of map-like containers,
+ * *include* ones that invalidate iterators when they are modified (e.g.,
+ * flat_map and btree_map).
+ */
+
 #ifndef MIN
 # define MIN(a,b)  ((a)<=(b) ? (a):(b))
 #endif
@@ -458,30 +466,34 @@ class interval_set {
         
         typename Map::iterator n = p;
         n++;
+       if (pstart)
+         *pstart = p->first;
         if (n != m.end() && 
             start+len == n->first) {   // combine with next, too!
           p->second += n->second;
+         if (plen)
+           *plen = p->second;
           m.erase(n);
-        }
-       if (pstart)
-         *pstart = p->first;
-       if (plen)
-         *plen = p->second;
+        } else {
+         if (plen)
+           *plen = p->second;
+       }
       } else {
         if (start+len == p->first) {
-          m[start] = len + p->second;  // append to front 
          if (pstart)
            *pstart = start;
          if (plen)
            *plen = len + p->second;
+         T psecond = p->second;
           m.erase(p);
+          m[start] = len + psecond;  // append to front
         } else {
           assert(p->first > start+len);
-          m[start] = len;              // new interval
          if (pstart)
            *pstart = start;
          if (plen)
            *plen = len;
+          m[start] = len;              // new interval
         }
       }
     }