Sync am-vector.h with AMTL (fix bug 6090).
authorDavid Anderson <dvander@alliedmods.net>
Sat Apr 19 19:21:40 2014 -0700 (2014-04-19)
changeset 4324f93e67044fa7
parent 4323 142b0c03e6bb
child 4325 c12f9b9042f9
Sync am-vector.h with AMTL (fix bug 6090).
public/amtl/am-vector.h
     1.1 --- a/public/amtl/am-vector.h	Sun Apr 20 21:38:40 2014 -0400
     1.2 +++ b/public/amtl/am-vector.h	Sat Apr 19 19:21:40 2014 -0700
     1.3 @@ -167,6 +167,14 @@
     1.4      return growIfNeeded(desired - length());
     1.5    }
     1.6  
     1.7 +  Vector &operator =(Moveable<Vector<T, AllocPolicy> > other) {
     1.8 +    data_ = other->data_;
     1.9 +    nitems_ = other->nitems_;
    1.10 +    maxsize_ = other->maxsize_;
    1.11 +    other->reset();
    1.12 +    return *this;
    1.13 +  }
    1.14 +
    1.15   private:
    1.16    // These are disallowed because they basically violate the failure handling
    1.17    // model for AllocPolicies and are also likely to have abysmal performance.
    1.18 @@ -186,10 +194,15 @@
    1.19    }
    1.20  
    1.21    bool moveUp(size_t at) {
    1.22 -    assert(at < nitems_);
    1.23 -    if (!append(Moveable<T>(data_[nitems_ - 1])))
    1.24 +    // Note: we don't use append() here. Passing an element as a Moveable into
    1.25 +    // insert() or append() can break, since the underlying storage could be
    1.26 +    // reallocated, invalidating the Moveable reference. Instead, we inline
    1.27 +    // the logic to append() to ensure growIfNeeded occurs before any
    1.28 +    // references are taken.
    1.29 +    if (!growIfNeeded(1))
    1.30        return false;
    1.31 -
    1.32 +    new (&data_[nitems_]) T(Moveable<T>(data_[nitems_ - 1]));
    1.33 +    nitems_++;
    1.34      for (size_t i = nitems_ - 2; i > at; i--)
    1.35        data_[i] = Moveable<T>(data_[i - 1]);
    1.36      return true;