clover/util: Have compat::vector track separate size and capacity.
authorFrancisco Jerez <currojerez@riseup.net>
Mon, 18 Aug 2014 05:30:46 +0000 (08:30 +0300)
committerFrancisco Jerez <currojerez@riseup.net>
Fri, 5 Sep 2014 06:27:20 +0000 (09:27 +0300)
In order to make the behaviour of resize() and reserve() closer to the
standard.

Reported-by: EdB <edb+mesa@sigluy.net>
src/gallium/state_trackers/clover/core/module.cpp
src/gallium/state_trackers/clover/util/compat.hpp

index 55ed91af7998d298b6374ce6304f0fdf486e105e..9ef584b298ec0d53e23498bdc46ec778f3f489e0 100644 (file)
@@ -94,7 +94,7 @@ namespace {
 
       static void
       proc(compat::istream &is, compat::vector<T> &v) {
-         v.reserve(_proc<uint32_t>(is));
+         v.resize(_proc<uint32_t>(is));
 
          for (size_t i = 0; i < v.size(); i++)
             new(&v[i]) T(_proc<T>(is));
@@ -122,7 +122,7 @@ namespace {
 
       static void
       proc(compat::istream &is, compat::vector<T> &v) {
-         v.reserve(_proc<uint32_t>(is));
+         v.resize(_proc<uint32_t>(is));
          is.read(reinterpret_cast<char *>(v.begin()),
                  v.size() * sizeof(T));
       }
index 50e1c7d47d5788a9e9ad314a8d71a0e692c634bc..a4e3938a9f444b5bce9cf8670088f8a173f370ca 100644 (file)
@@ -66,65 +66,81 @@ namespace clover {
          typedef std::ptrdiff_t difference_type;
          typedef std::size_t size_type;
 
-         vector() : p(NULL), n(0) {
+         vector() : p(NULL), _size(0), _capacity(0) {
          }
 
-         vector(const vector &v) : p(alloc(v.n, v.p, v.n)), n(v.n) {
+         vector(const vector &v) :
+            p(alloc(v._size, v.p, v._size)),
+            _size(v._size), _capacity(v._size) {
          }
 
-         vector(const_iterator p, size_type n) : p(alloc(n, p, n)), n(n) {
+         vector(const_iterator p, size_type n) :
+            p(alloc(n, p, n)), _size(n), _capacity(n) {
          }
 
          template<typename C>
          vector(const C &v) :
-            p(alloc(v.size(), &*v.begin(), v.size())), n(v.size()) {
+            p(alloc(v.size(), &*v.begin(), v.size())),
+            _size(v.size()) , _capacity(v.size()) {
          }
 
          ~vector() {
-            free(n, p);
+            free(_size, p);
          }
 
          vector &
          operator=(const vector &v) {
-            free(n, p);
+            free(_size, p);
 
-            p = alloc(v.n, v.p, v.n);
-            n = v.n;
+            p = alloc(v._size, v.p, v._size);
+            _size = v._size;
+            _capacity = v._size;
 
             return *this;
          }
 
          void
-         reserve(size_type m) {
-            if (n < m) {
-               T *q = alloc(m, p, n);
-               free(n, p);
+         reserve(size_type n) {
+            if (_capacity < n) {
+               T *q = alloc(n, p, _size);
+               free(_size, p);
 
                p = q;
-               n = m;
+               _capacity = n;
             }
          }
 
          void
-         resize(size_type m, T x = T()) {
-            size_type n = size();
+         resize(size_type n, T x = T()) {
+            if (n <= _size) {
+               for (size_type i = n; i < _size; ++i)
+                  p[i].~T();
 
-            reserve(m);
+            } else {
+               reserve(n);
 
-            for (size_type i = n; i < m; ++i)
-               new(&p[i]) T(x);
+               for (size_type i = _size; i < n; ++i)
+                  new(&p[i]) T(x);
+            }
+
+            _size = n;
          }
 
          void
          push_back(const T &x) {
-            size_type n = size();
-            reserve(n + 1);
-            new(&p[n]) T(x);
+            reserve(_size + 1);
+            new(&p[_size]) T(x);
+            ++_size;
          }
 
          size_type
          size() const {
-            return n;
+            return _size;
+         }
+
+         size_type
+         capacity() const {
+            return _capacity;
          }
 
          iterator
@@ -139,12 +155,12 @@ namespace clover {
 
          iterator
          end() {
-            return p + n;
+            return p + _size;
          }
 
          const_iterator
          end() const {
-            return p + n;
+            return p + _size;
          }
 
          reference
@@ -159,7 +175,8 @@ namespace clover {
 
       private:
          iterator p;
-         size_type n;
+         size_type _size;
+         size_type _capacity;
       };
 
       template<typename T>