summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSage Weil <sage.weil@dreamhost.com>2012-04-09 11:26:34 -0700
committerSage Weil <sage.weil@dreamhost.com>2012-04-09 12:08:19 -0700
commit36d42deab8746245cc9900e5cf1cce9a9aceb43d (patch)
tree2a9333a9437597e2fb590f8504760bb3d1b2213f
parent7951d7e4421b35849a7ca30d11c4c0e200a6ebf5 (diff)
downloadceph-36d42deab8746245cc9900e5cf1cce9a9aceb43d.tar.gz
buffer: allow advance() to move an iterator backward
Signed-off-by: Sage Weil <sage.weil@dreamhost.com>
-rw-r--r--src/common/buffer.cc45
-rw-r--r--src/include/buffer.h2
2 files changed, 33 insertions, 14 deletions
diff --git a/src/common/buffer.cc b/src/common/buffer.cc
index a6ad8c0d736..64c4d54008c 100644
--- a/src/common/buffer.cc
+++ b/src/common/buffer.cc
@@ -428,25 +428,44 @@ bool buffer_track_alloc = get_env_bool("CEPH_BUFFER_TRACK");
return *this;
}*/
- void buffer::list::iterator::advance(unsigned o)
+ void buffer::list::iterator::advance(int o)
{
//cout << this << " advance " << o << " from " << off << " (p_off " << p_off << " in " << p->length() << ")" << std::endl;
- p_off += o;
- while (p_off > 0) {
- if (p == ls->end())
- throw end_of_buffer();
- if (p_off >= p->length()) {
- // skip this buffer
- p_off -= p->length();
- p++;
+ if (o > 0) {
+ p_off += o;
+ while (p_off > 0) {
+ if (p == ls->end())
+ throw end_of_buffer();
+ if (p_off >= p->length()) {
+ // skip this buffer
+ p_off -= p->length();
+ p++;
+ } else {
+ // somewhere in this buffer!
+ break;
+ }
+ }
+ off += o;
+ return;
+ }
+ while (o < 0) {
+ if (p_off) {
+ unsigned d = -o;
+ if (d > p_off)
+ d = p_off;
+ p_off -= d;
+ off -= d;
+ o += d;
+ } else if (off > 0) {
+ assert(p != ls->begin());
+ p--;
+ p_off = p->length();
} else {
- // somewhere in this buffer!
- break;
+ throw end_of_buffer();
}
}
- off += o;
}
-
+
void buffer::list::iterator::seek(unsigned o)
{
//cout << this << " seek " << o << std::endl;
diff --git a/src/include/buffer.h b/src/include/buffer.h
index 65c101f6f96..2ba138f579c 100644
--- a/src/include/buffer.h
+++ b/src/include/buffer.h
@@ -270,7 +270,7 @@ public:
//return off == bl->length();
}
- void advance(unsigned o);
+ void advance(int o);
void seek(unsigned o);
char operator*();
iterator& operator++();