Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tag: v0.3
Fetching contributors…

Cannot retrieve contributors at this time

file 999 lines (874 sloc) 24.173 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/

#ifndef __BUFFER_H
#define __BUFFER_H

#define _XOPEN_SOURCE 600
#include <stdlib.h>
#include <malloc.h>
#include <stdint.h>
#include <assert.h>
#include <string.h>

#ifndef __CYGWIN__
# include <sys/mman.h>
#endif

#include <iostream>
#include <iomanip>
#include <list>

#include "atomic.h"
#include "page.h"

// <hack>
// these are in config.o
extern atomic_t buffer_total_alloc;
// </hack>


class buffer {
private:
  
  /* hack for memory utilization debugging. */
  static void inc_total_alloc(unsigned len) {
    buffer_total_alloc.add(len);
  }
  static void dec_total_alloc(unsigned len) {
    buffer_total_alloc.sub(len);
  }

  /*
* an abstract raw buffer. with a reference count.
*/
  class raw {
  public:
    char *data;
    unsigned len;
    atomic_t nref;

    raw(unsigned l) : len(l), nref(0)
    { }
    raw(char *c, unsigned l) : data(c), len(l), nref(0)
    { }
    virtual ~raw() {};

    // no copying.
    raw(const raw &other);
    const raw& operator=(const raw &other);

    virtual raw* clone_empty() = 0;
    raw *clone() {
      raw *c = clone_empty();
      memcpy(c->data, data, len);
      return c;
    }

    bool is_page_aligned() {
      return ((long)data & ~PAGE_MASK) == 0;
    }
    bool is_n_page_sized() {
      return (len & ~PAGE_MASK) == 0;
    }
  };

  friend std::ostream& operator<<(std::ostream& out, const raw &r);

  /*
* primitive buffer types
*/
  class raw_char : public raw {
  public:
    raw_char(unsigned l) : raw(l) {
      if (len)
data = new char[len];
      else
data = 0;
      inc_total_alloc(len);
    }
    ~raw_char() {
      delete[] data;
      dec_total_alloc(len);
    }
    raw* clone_empty() {
      return new raw_char(len);
    }
  };

  class raw_static : public raw {
  public:
    raw_static(const char *d, unsigned l) : raw((char*)d, l) { }
    ~raw_static() {}
    raw* clone_empty() {
      return new raw_char(len);
    }
  };

#ifndef __CYGWIN__
  class raw_mmap_pages : public raw {
  public:
    raw_mmap_pages(unsigned l) : raw(l) {
      data = (char*)::mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
      assert(data);
      inc_total_alloc(len);
    }
    ~raw_mmap_pages() {
      ::munmap(data, len);
      dec_total_alloc(len);
    }
    raw* clone_empty() {
      return new raw_mmap_pages(len);
    }
  };

  class raw_posix_aligned : public raw {
  public:
    raw_posix_aligned(unsigned l) : raw(l) {
#ifdef DARWIN
      data = (char *) valloc (len);
#else
      data = 0;
      ::posix_memalign((void**)(void*)&data, PAGE_SIZE, len);
#endif /* DARWIN */
      assert(data);
      inc_total_alloc(len);
    }
    ~raw_posix_aligned() {
      ::free((void*)data);
      dec_total_alloc(len);
    }
    raw* clone_empty() {
      return new raw_posix_aligned(len);
    }
  };
#endif

#ifdef __CYGWIN__
  class raw_hack_aligned : public raw {
    char *realdata;
  public:
    raw_hack_aligned(unsigned l) : raw(l) {
      realdata = new char[len+PAGE_SIZE-1];
      unsigned off = ((unsigned)realdata) & ~PAGE_MASK;
      if (off)
data = realdata + PAGE_SIZE - off;
      else
data = realdata;
      inc_total_alloc(len+PAGE_SIZE-1);
      //cout << "hack aligned " << (unsigned)data
      //<< " in raw " << (unsigned)realdata
      //<< " off " << off << std::endl;
      assert(((unsigned)data & (PAGE_SIZE-1)) == 0);
    }
    ~raw_hack_aligned() {
      delete[] realdata;
      dec_total_alloc(len+PAGE_SIZE-1);
    }
    raw* clone_empty() {
      return new raw_hack_aligned(len);
    }
  };
#endif

public:

  /*
* named constructors
*/

  static raw* copy(const char *c, unsigned len) {
    raw* r = new raw_char(len);
    memcpy(r->data, c, len);
    return r;
  }
  static raw* create(unsigned len) {
    return new raw_char(len);
  }

  static raw* create_page_aligned(unsigned len) {
#ifndef __CYGWIN__
    //return new raw_mmap_pages(len);
    return new raw_posix_aligned(len);
#else
    return new raw_hack_aligned(len);
#endif
  }
  
  
  /*
* a buffer pointer. references (a subsequence of) a raw buffer.
*/
  class ptr {
    raw *_raw;
    unsigned _off, _len;

  public:
    ptr() : _raw(0), _off(0), _len(0) {}
    ptr(raw *r) : _raw(r), _off(0), _len(r->len) { // no lock needed; this is an unref raw.
      r->nref.inc();
    }
    ptr(unsigned l) : _off(0), _len(l) {
      _raw = create(l);
      _raw->nref.inc();
    }
    ptr(char *d, unsigned l) : _off(0), _len(l) { // ditto.
      _raw = copy(d, l);
      _raw->nref.inc();
    }
    ptr(const ptr& p) : _raw(p._raw), _off(p._off), _len(p._len) {
      if (_raw) {
_raw->nref.inc();
      }
    }
    ptr(const ptr& p, unsigned o, unsigned l) : _raw(p._raw), _off(p._off + o), _len(l) {
      assert(o+l <= p._len);
      assert(_raw);
      _raw->nref.inc();
    }
    ptr& operator= (const ptr& p) {
      // be careful -- we need to properly handle self-assignment.
      if (p._raw)
p._raw->nref.inc(); // inc new
      release(); // dec (+ dealloc) old (if any)
      if (p._raw) {
_raw = p._raw;
_off = p._off;
_len = p._len;
      } else {
_off = _len = 0;
      }
      return *this;
    }
    ~ptr() {
      release();
    }
    
    raw *clone() {
      return _raw->clone();
    }
    
    void clone_in_place() {
      raw *newraw = _raw->clone();
      release();
      newraw->nref.inc();
      _raw = newraw;
    }
    bool do_cow() {
      if (_raw->nref.test() > 1) {
//std::cout << "doing cow on " << _raw << " len " << _len << std::endl;
clone_in_place();
return true;
      } else
return false;
    }

    void swap(ptr& other) {
      raw *r = _raw;
      unsigned o = _off;
      unsigned l = _len;
      _raw = other._raw;
      _off = other._off;
      _len = other._len;
      other._raw = r;
      other._off = o;
      other._len = l;
    }

    void release() {
      if (_raw) {
        if (_raw->nref.dec() == 0) {
          //cout << "hosing raw " << (void*)_raw << " len " << _raw->len << std::endl;
          delete _raw; // dealloc old (if any)
}
_raw = 0;
      }
    }

    // misc
    bool at_buffer_head() const { return _off == 0; }
    bool at_buffer_tail() const { return _off + _len == _raw->len; }

    bool is_page_aligned() const { return ((long)c_str() & ~PAGE_MASK) == 0; }
    bool is_n_page_sized() const { return (length() & ~PAGE_MASK) == 0; }

    // accessors
    raw *get_raw() const { return _raw; }
    const char *c_str() const { assert(_raw); return _raw->data + _off; }
    char *c_str() { assert(_raw); return _raw->data + _off; }
    unsigned length() const { return _len; }
    unsigned offset() const { return _off; }
    unsigned start() const { return _off; }
    unsigned end() const { return _off + _len; }
    unsigned unused_tail_length() const {
      if (_raw)
return _raw->len - (_off+_len);
      else
return 0;
    }
    const char& operator[](unsigned n) const {
      assert(_raw);
      assert(n < _len);
      return _raw->data[_off + n];
    }
    char& operator[](unsigned n) {
      assert(_raw);
      assert(n < _len);
      return _raw->data[_off + n];
    }

    const char *raw_c_str() const { assert(_raw); return _raw->data; }
    unsigned raw_length() const { assert(_raw); return _raw->len; }
    int raw_nref() const { assert(_raw); return _raw->nref.test(); }

    void copy_out(unsigned o, unsigned l, char *dest) const {
      assert(_raw);
      assert(o >= 0 && o <= _len);
      assert(l >= 0 && o+l <= _len);
      memcpy(dest, c_str()+o, l);
    }

    unsigned wasted() {
      assert(_raw);
      return _raw->len - _len;
    }

    // modifiers
    void set_offset(unsigned o) { _off = o; }
    void set_length(unsigned l) { _len = l; }

    void append(const char *p, unsigned l) {
      assert(_raw);
      assert(l <= unused_tail_length());
      memcpy(c_str() + _len, p, l);
      _len += l;
    }

    void copy_in(unsigned o, unsigned l, const char *src) {
      assert(_raw);
      assert(o >= 0 && o <= _len);
      assert(l >= 0 && o+l <= _len);
      memcpy(c_str()+o, src, l);
    }

    void zero() {
      memset(c_str(), 0, _len);
    }
    void zero(unsigned o, unsigned l) {
      assert(o+l <= _len);
      memset(c_str()+o, 0, l);
    }

    void clean() {
      //raw *newraw = _raw->makesib(_len);
    }
  };

  friend std::ostream& operator<<(std::ostream& out, const buffer::ptr& bp);

  /*
* list - the useful bit!
*/

  class list {
    // my private bits
    list *bl;
    std::list<ptr> _buffers;
    unsigned _len;

    ptr append_buffer; // where i put small appends.

  public:
    class iterator {
      list *bl;
      std::list<ptr> &ls;
      unsigned off; // in bl
      std::list<ptr>::iterator p;
      unsigned p_off; // in *p
    public:
      // constructor. position.
      iterator(list *l, unsigned o=0) :
bl(l), ls(bl->_buffers), off(0), p(ls.begin()), p_off(0) {
advance(o);
      }
      iterator(list *l, unsigned o, std::list<ptr>::iterator ip, unsigned po) :
bl(l), ls(bl->_buffers), off(o), p(ip), p_off(po) { }

      iterator operator=(const iterator& other) {
if (this != &other) {
bl = other.bl;
ls = bl->_buffers;
off = other.off;
p = other.p;
p_off = other.p_off;
}
return *this;
      }

      unsigned get_off() { return off; }

      bool end() {
return p == ls.end();
      }

      void advance(unsigned o) {
//cout << this << " advance " << o << " from " << off << " (p_off " << p_off << " in " << p->length() << ")" << std::endl;
p_off += o;
while (p_off > 0) {
assert(p != ls.end());
if (p_off >= p->length()) {
// skip this buffer
p_off -= p->length();
p++;
} else {
// somewhere in this buffer!
break;
}
}
off += o;
      }

      void seek(unsigned o) {
//cout << this << " seek " << o << std::endl;
p = ls.begin();
off = p_off = 0;
advance(o);
      }

      char operator*() {
assert(p != ls.end());
return (*p)[p_off];
      }
      iterator& operator++() {
assert(p != ls.end());
advance(1);
return *this;
      }

      // copy data out.
      // note that these all _append_ to dest!

      void copy(unsigned len, char *dest) {
if (p == ls.end()) seek(off);
while (len > 0) {
assert(p != ls.end());

unsigned howmuch = p->length() - p_off;
if (len < howmuch) howmuch = len;
p->copy_out(p_off, howmuch, dest);
dest += howmuch;

len -= howmuch;
advance(howmuch);
}
      }

      void copy(unsigned len, list &dest) {
if (p == ls.end()) seek(off);
while (len > 0) {
assert(p != ls.end());

unsigned howmuch = p->length() - p_off;
if (len < howmuch) howmuch = len;
dest.append(*p, p_off, howmuch);

len -= howmuch;
advance(howmuch);
}
      }

      void copy(unsigned len, std::string &dest) {
if (p == ls.end()) seek(off);
while (len > 0) {
assert(p != ls.end());

unsigned howmuch = p->length() - p_off;
if (len < howmuch) howmuch = len;
dest.append(p->c_str() + p_off, howmuch);

len -= howmuch;
advance(howmuch);
}
      }

      // copy data in

      void copy_in(unsigned len, const char *src) {
// copy
if (p == ls.end()) seek(off);
while (len > 0) {
assert(p != ls.end());

unsigned howmuch = p->length() - p_off;
if (len < howmuch) howmuch = len;
p->copy_in(p_off, howmuch, src);

src += howmuch;
len -= howmuch;
advance(howmuch);
}
      }

      void copy_in(unsigned len, const list& otherl) {
if (p == ls.end()) seek(off);
unsigned left = len;
for (std::list<ptr>::const_iterator i = otherl._buffers.begin();
i != otherl._buffers.end();
i++) {
unsigned l = (*i).length();
if (left < l) l = left;
copy_in(l, i->c_str());
left -= l;
if (left == 0) break;
}
      }

    };

  private:
    mutable iterator last_p;

  public:
    // cons/des
    list() : _len(0), last_p(this) {}
    list(const list& other) : _buffers(other._buffers), _len(other._len), last_p(this) { }
    list(unsigned l) : _len(0), last_p(this) {
      ptr bp(l);
      push_back(bp);
    }
    ~list() {}
    
    list& operator= (const list& other) {
      _buffers = other._buffers;
      _len = other._len;
      return *this;
    }

    const std::list<ptr>& buffers() const { return _buffers; }
    
    void swap(list& other) {
      unsigned t = _len;
      _len = other._len;
      other._len = t;
      _buffers.swap(other._buffers);
      append_buffer.swap(other.append_buffer);
      //last_p.swap(other.last_p);
      last_p = begin();
      other.last_p = other.begin();
    }

    unsigned length() const {
#if 0
// DEBUG: verify _len
unsigned len = 0;
for (std::list<ptr>::const_iterator it = _buffers.begin();
it != _buffers.end();
it++) {
len += (*it).length();
}
assert(len == _len);
#endif
      return _len;
    }

    bool is_page_aligned() const {
      for (std::list<ptr>::const_iterator it = _buffers.begin();
it != _buffers.end();
it++)
if (!it->is_page_aligned()) return false;
      return true;
    }
    bool is_n_page_sized() const {
      for (std::list<ptr>::const_iterator it = _buffers.begin();
it != _buffers.end();
it++)
if (!it->is_n_page_sized()) return false;
      return true;
    }

    // modifiers
    void clear() {
      _buffers.clear();
      _len = 0;
      last_p = begin();
    }
    void push_front(ptr& bp) {
      _buffers.push_front(bp);
      _len += bp.length();
    }
    void push_front(raw *r) {
      ptr bp(r);
      _buffers.push_front(bp);
      _len += bp.length();
    }
    void push_back(const ptr& bp) {
      _buffers.push_back(bp);
      _len += bp.length();
    }
    void push_back(raw *r) {
      ptr bp(r);
      _buffers.push_back(bp);
      _len += bp.length();
    }
    void zero() {
      for (std::list<ptr>::iterator it = _buffers.begin();
it != _buffers.end();
it++)
        it->zero();
    }
    void do_cow() {
      for (std::list<ptr>::iterator it = _buffers.begin();
it != _buffers.end();
it++)
it->do_cow();
    }
    void clone_in_place() {
      for (std::list<ptr>::iterator it = _buffers.begin();
it != _buffers.end();
it++)
it->clone_in_place();
    }
    void zero(unsigned o, unsigned l) {
      assert(o+l <= _len);
      unsigned p = 0;
      for (std::list<ptr>::iterator it = _buffers.begin();
it != _buffers.end();
it++) {
if (p + it->length() > o) {
if (p >= o && p+it->length() >= o+l)
it->zero(); // all
else if (p >= o)
it->zero(0, o+l-p); // head
else
it->zero(o-p, it->length()-(o-p)); // tail
}
p += it->length();
if (o+l >= p) break; // done
      }
    }

    void rebuild() {
      ptr nb(_len);
      unsigned pos = 0;
      for (std::list<ptr>::iterator it = _buffers.begin();
it != _buffers.end();
it++) {
nb.copy_in(pos, it->length(), it->c_str());
pos += it->length();
      }
      _buffers.clear();
      _buffers.push_back(nb);
    }


    // sort-of-like-assignment-op
    void claim(list& bl) {
      // free my buffers
      clear();
      claim_append(bl);
    }
    void claim_append(list& bl) {
      // steal the other guy's buffers
      _len += bl._len;
      _buffers.splice( _buffers.end(), bl._buffers );
      bl._len = 0;
      bl.last_p = bl.begin();
    }


    

    iterator begin() {
      return iterator(this, 0);
    }
    iterator end() {
      return iterator(this, _len, _buffers.end(), 0);
    }


    // crope lookalikes.
    // **** WARNING: this are horribly inefficient for large bufferlists. ****

    // data OUT

    void copy(unsigned off, unsigned len, char *dest) const {
      assert(off >= 0);
      assert(off + len <= length());
      if (last_p.get_off() != off)
last_p.seek(off);
      last_p.copy(len, dest);
    }

    void copy(unsigned off, unsigned len, list &dest) const {
      assert(off >= 0);
      assert(off + len <= length());
      if (last_p.get_off() != off)
last_p.seek(off);
      last_p.copy(len, dest);
    }

    void copy(unsigned off, unsigned len, std::string& dest) const {
      if (last_p.get_off() != off)
last_p.seek(off);
      return last_p.copy(len, dest);
    }
    
    void copy_in(unsigned off, unsigned len, const char *src) {
      assert(off >= 0);
      assert(off + len <= length());

      if (last_p.get_off() != off)
last_p.seek(off);
      last_p.copy_in(len, src);
    }

    void copy_in(unsigned off, unsigned len, const list& src) {
      if (last_p.get_off() != off)
last_p.seek(off);
      last_p.copy_in(len, src);
    }


    void append(const char *data, unsigned len) {
      while (len > 0) {
// put what we can into the existing append_buffer.
unsigned gap = append_buffer.unused_tail_length();
if (gap > 0) {
if (gap > len) gap = len;
//cout << "append first char is " << data[0] << ", last char is " << data[len-1] << std::endl;
append_buffer.append(data, gap);
append(append_buffer, append_buffer.end() - gap, gap); // add segment to the list
len -= gap;
data += gap;
}
if (len == 0) break; // done!

// make a new append_buffer!
unsigned alen = PAGE_SIZE * (((len-1) / PAGE_SIZE) + 1);
append_buffer = create_page_aligned(alen);
append_buffer.set_length(0); // unused, so far.
      }
    }
    void append(const ptr& bp) {
      if (bp.length())
push_back(bp);
    }
    void append(const ptr& bp, unsigned off, unsigned len) {
      assert(len+off <= bp.length());
      if (!_buffers.empty() &&
_buffers.back().get_raw() == bp.get_raw() &&
_buffers.back().end() == bp.start() + off) {
// yay contiguous with tail bp!
_buffers.back().set_length(_buffers.back().length()+len);
_len += len;
      } else {
// add new item to list
ptr tempbp(bp, off, len);
push_back(tempbp);
      }
    }
    void append(const list& bl) {
      _len += bl._len;
      for (std::list<ptr>::const_iterator p = bl._buffers.begin();
p != bl._buffers.end();
++p)
_buffers.push_back(*p);
    }
    
    void append_zero(unsigned len) {
      ptr bp(len);
      bp.zero();
      append(bp);
    }

    
    /*
* get a char
*/
    const char& operator[](unsigned n) {
      assert(n < _len);
      for (std::list<ptr>::iterator p = _buffers.begin();
p != _buffers.end();
p++) {
if (n >= p->length()) {
n -= p->length();
continue;
}
return (*p)[n];
      }
      assert(0);
    }

    /*
* return a contiguous ptr to whole bufferlist contents.
*/
    char *c_str() {
      if (_buffers.size() == 0)
return 0; // no buffers
      if (_buffers.size() > 1)
rebuild();
      assert(_buffers.size() == 1);
      return _buffers.front().c_str(); // good, we're already contiguous.
    }

    void substr_of(const list& other, unsigned off, unsigned len) {
      assert(off + len <= other.length());
      clear();
      
      // skip off
      std::list<ptr>::const_iterator curbuf = other._buffers.begin();
      while (off > 0 &&
off >= curbuf->length()) {
// skip this buffer
//cout << "skipping over " << *curbuf << std::endl;
off -= (*curbuf).length();
curbuf++;
      }
      assert(len == 0 || curbuf != other._buffers.end());
      
      while (len > 0) {
// partial?
if (off + len < curbuf->length()) {
//cout << "copying partial of " << *curbuf << std::endl;
_buffers.push_back( ptr( *curbuf, off, len ) );
_len += len;
break;
}

// through end
//cout << "copying end (all?) of " << *curbuf << std::endl;
unsigned howmuch = curbuf->length() - off;
_buffers.push_back( ptr( *curbuf, off, howmuch ) );
_len += howmuch;
len -= howmuch;
off = 0;
curbuf++;
      }
    }

    

    // funky modifer
    void splice(unsigned off, unsigned len, list *claim_by=0 /*, bufferlist& replace_with */) { // fixme?
      assert(off < length());
      assert(len > 0);
      //cout << "splice off " << off << " len " << len << " ... mylen = " << length() << std::endl;
      
      // skip off
      std::list<ptr>::iterator curbuf = _buffers.begin();
      while (off > 0) {
assert(curbuf != _buffers.end());
if (off >= (*curbuf).length()) {
// skip this buffer
//cout << "off = " << off << " skipping over " << *curbuf << std::endl;
off -= (*curbuf).length();
curbuf++;
} else {
// somewhere in this buffer!
//cout << "off = " << off << " somewhere in " << *curbuf << std::endl;
break;
}
      }
      assert(off >= 0);
      
      if (off) {
// add a reference to the front bit
// insert it before curbuf (which we'll hose)
//cout << "keeping front " << off << " of " << *curbuf << std::endl;
_buffers.insert( curbuf, ptr( *curbuf, 0, off ) );
_len += off;
      }
      
      while (len > 0) {
// partial?
if (off + len < (*curbuf).length()) {
//cout << "keeping end of " << *curbuf << ", losing first " << off+len << std::endl;
if (claim_by)
claim_by->append( *curbuf, off, len );
(*curbuf).set_offset( off+len + (*curbuf).offset() ); // ignore beginning big
(*curbuf).set_length( (*curbuf).length() - (len+off) );
_len -= off+len;
//cout << " now " << *curbuf << std::endl;
break;
}

// hose though the end
unsigned howmuch = (*curbuf).length() - off;
//cout << "discarding " << howmuch << " of " << *curbuf << std::endl;
if (claim_by)
claim_by->append( *curbuf, off, howmuch );
_len -= (*curbuf).length();
_buffers.erase( curbuf++ );
len -= howmuch;
off = 0;
      }
      
      // splice in *replace (implement me later?)

      last_p = begin(); // just in case we were in the removed region.
    };

    void hexdump(std::ostream &out) {
      out.setf(std::ios::right);
      out.fill('0');
      out << std::hex;
      for (unsigned i=0; i<length(); i++) {
if (i % 16 == 0) {
if (i) out << std::endl;
out << std::setw(4) << i << " :";
}
out << " " << std::setw(2) << ((unsigned)(*this)[i] & 0xff);
      }
      out << std::dec << std::endl;
      out.unsetf(std::ios::right);
    }

    int read_file(const char *fn);
    int write_file(const char *fn);

  };
};

typedef buffer::ptr bufferptr;
typedef buffer::list bufferlist;


inline bool operator>(bufferlist& l, bufferlist& r) {
  for (unsigned p = 0; ; p++) {
    if (l.length() > p && r.length() == p) return true;
    if (l.length() == p) return false;
    if (l[p] > r[p]) return true;
    if (l[p] < r[p]) return false;
    p++;
  }
}
inline bool operator>=(bufferlist& l, bufferlist& r) {
  for (unsigned p = 0; ; p++) {
    if (l.length() > p && r.length() == p) return true;
    if (r.length() == p && l.length() == p) return true;
    if (l[p] > r[p]) return true;
    if (l[p] < r[p]) return false;
    p++;
  }
}
inline bool operator<(bufferlist& l, bufferlist& r) {
  return r > l;
}
inline bool operator<=(bufferlist& l, bufferlist& r) {
  return r >= l;
}


inline std::ostream& operator<<(std::ostream& out, const buffer::raw &r) {
  return out << "buffer::raw(" << (void*)r.data << " len " << r.len << " nref " << r.nref.test() << ")";
}

inline std::ostream& operator<<(std::ostream& out, const buffer::ptr& bp) {
  out << "buffer::ptr(" << bp.offset() << "~" << bp.length()
      << " " << (void*)bp.c_str()
      << " in raw " << (void*)bp.raw_c_str()
      << " len " << bp.raw_length()
      << " nref " << bp.raw_nref() << ")";
  return out;
}

inline std::ostream& operator<<(std::ostream& out, const buffer::list& bl) {
  out << "buffer::list(len=" << bl.length() << "," << std::endl;

  std::list<buffer::ptr>::const_iterator it = bl.buffers().begin();
  while (it != bl.buffers().end()) {
    out << "\t" << *it;
    if (++it == bl.buffers().end()) break;
    out << "," << std::endl;
  }
  out << std::endl << ")";
  return out;
}

#endif
Something went wrong with that request. Please try again.