From: Francisco Jerez Date: Mon, 18 Aug 2014 05:30:46 +0000 (+0300) Subject: clover/util: Have compat::vector track separate size and capacity. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=7c1e6d582c772753cfdbfd3d09509af608b54d91;p=mesa.git clover/util: Have compat::vector track separate size and capacity. In order to make the behaviour of resize() and reserve() closer to the standard. Reported-by: EdB --- diff --git a/src/gallium/state_trackers/clover/core/module.cpp b/src/gallium/state_trackers/clover/core/module.cpp index 55ed91af799..9ef584b298e 100644 --- a/src/gallium/state_trackers/clover/core/module.cpp +++ b/src/gallium/state_trackers/clover/core/module.cpp @@ -94,7 +94,7 @@ namespace { static void proc(compat::istream &is, compat::vector &v) { - v.reserve(_proc(is)); + v.resize(_proc(is)); for (size_t i = 0; i < v.size(); i++) new(&v[i]) T(_proc(is)); @@ -122,7 +122,7 @@ namespace { static void proc(compat::istream &is, compat::vector &v) { - v.reserve(_proc(is)); + v.resize(_proc(is)); is.read(reinterpret_cast(v.begin()), v.size() * sizeof(T)); } diff --git a/src/gallium/state_trackers/clover/util/compat.hpp b/src/gallium/state_trackers/clover/util/compat.hpp index 50e1c7d47d5..a4e3938a9f4 100644 --- a/src/gallium/state_trackers/clover/util/compat.hpp +++ b/src/gallium/state_trackers/clover/util/compat.hpp @@ -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 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