diff options
author | Sergey Sharybin <sergey.vfx@gmail.com> | 2015-08-23 16:50:31 +0300 |
---|---|---|
committer | Sergey Sharybin <sergey.vfx@gmail.com> | 2015-08-24 10:46:40 +0300 |
commit | 7be6dba091d600c0a4fd327cebe9651721e1307b (patch) | |
tree | 77fcd83ae384bc05fab865652beb1d0d1156de98 /intern/cycles/util/util_vector.h | |
parent | 2230130099dd6921df9681c4e4908ff8e1beaed7 (diff) |
Cycles: Implement reseve() for aligned array class
The title says it all actually, just support reserving memory in the array class.
Diffstat (limited to 'intern/cycles/util/util_vector.h')
-rw-r--r-- | intern/cycles/util/util_vector.h | 34 |
1 files changed, 28 insertions, 6 deletions
diff --git a/intern/cycles/util/util_vector.h b/intern/cycles/util/util_vector.h index 15a65be0ef0..ee1f997721d 100644 --- a/intern/cycles/util/util_vector.h +++ b/intern/cycles/util/util_vector.h @@ -105,6 +105,7 @@ public: { data = NULL; datasize = 0; + capacity = 0; } array(size_t newsize) @@ -112,10 +113,12 @@ public: if(newsize == 0) { data = NULL; datasize = 0; + capacity = 0; } else { data = (T*)util_aligned_malloc(sizeof(T)*newsize, alignment); datasize = newsize; + capacity = datasize; } } @@ -129,11 +132,13 @@ public: if(from.datasize == 0) { data = NULL; datasize = 0; + capacity = 0; } else { data = (T*)util_aligned_malloc(sizeof(T)*from.datasize, alignment); memcpy(data, from.data, from.datasize*sizeof(T)); datasize = from.datasize; + capacity = datasize; } return *this; @@ -142,6 +147,7 @@ public: array& operator=(const vector<T>& from) { datasize = from.size(); + capacity = datasize; data = NULL; if(datasize > 0) { @@ -163,13 +169,15 @@ public: clear(); } else if(newsize != datasize) { - T *newdata = (T*)util_aligned_malloc(sizeof(T)*newsize, alignment); - if(data) { - memcpy(newdata, data, ((datasize < newsize)? datasize: newsize)*sizeof(T)); - util_aligned_free(data); + if(newsize > capacity) { + T *newdata = (T*)util_aligned_malloc(sizeof(T)*newsize, alignment); + if(data) { + memcpy(newdata, data, ((datasize < newsize)? datasize: newsize)*sizeof(T)); + util_aligned_free(data); + } + data = newdata; + capacity = newsize; } - - data = newdata; datasize = newsize; } } @@ -179,6 +187,7 @@ public: util_aligned_free(data); data = NULL; datasize = 0; + capacity = 0; } size_t size() const @@ -192,9 +201,22 @@ public: return data[i]; } + void reserve(size_t newcapacity) { + if(newcapacity > capacity) { + T *newdata = (T*)util_aligned_malloc(sizeof(T)*newcapacity, alignment); + if(data) { + memcpy(newdata, data, ((datasize < newcapacity)? datasize: newcapacity)*sizeof(T)); + util_aligned_free(data); + } + data = newdata; + capacity = newcapacity; + } + } + protected: T *data; size_t datasize; + size_t capacity; }; CCL_NAMESPACE_END |