2011-04-27 11:58:34 +00:00
|
|
|
/*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Copyright 2011-2013 Blender Foundation
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
2014-12-25 01:50:24 +00:00
|
|
|
* limitations under the License.
|
2011-04-27 11:58:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __UTIL_VECTOR_H__
|
|
|
|
#define __UTIL_VECTOR_H__
|
|
|
|
|
|
|
|
/* Vector */
|
|
|
|
|
2015-06-28 09:18:48 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstring>
|
2011-04-27 11:58:34 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2015-02-14 12:29:47 +00:00
|
|
|
#include "util_aligned_malloc.h"
|
2013-06-22 14:35:09 +00:00
|
|
|
#include "util_types.h"
|
|
|
|
|
2015-02-14 13:03:39 +00:00
|
|
|
#ifdef WITH_CYCLES_DEBUG
|
|
|
|
# include "util_guarded_allocator.h"
|
|
|
|
#endif
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
2015-02-14 13:03:39 +00:00
|
|
|
/* Vector
|
|
|
|
*
|
|
|
|
* Own subclass-ed vestion of std::vector. Subclass is needed because:
|
|
|
|
*
|
|
|
|
* - When building with WITH_CYCLES_DEBUG we need to use own allocator which
|
|
|
|
* keeps track of used/peak memory.
|
|
|
|
*
|
|
|
|
* - Have method to ensure capacity is re-set to 0.
|
|
|
|
*/
|
2015-06-26 20:36:31 +00:00
|
|
|
template<typename value_type,
|
2015-02-14 13:03:39 +00:00
|
|
|
#ifdef WITH_CYCLES_DEBUG
|
2015-06-26 20:36:31 +00:00
|
|
|
typename allocator_type = GuardedAllocator<value_type>
|
|
|
|
#else
|
|
|
|
typename allocator_type = std::allocator<value_type>
|
2015-02-14 13:03:39 +00:00
|
|
|
#endif
|
2015-06-26 20:36:31 +00:00
|
|
|
>
|
|
|
|
class vector : public std::vector<value_type, allocator_type>
|
2015-02-14 13:03:39 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/* Default constructor. */
|
|
|
|
explicit vector() : std::vector<value_type, allocator_type>() { }
|
|
|
|
|
|
|
|
/* Fill constructor. */
|
|
|
|
explicit vector(size_t n, const value_type& val = value_type())
|
|
|
|
: std::vector<value_type, allocator_type>(n, val) { }
|
|
|
|
|
|
|
|
/* Range constructor. */
|
|
|
|
template <class InputIterator>
|
2015-06-28 08:51:49 +00:00
|
|
|
vector(InputIterator first, InputIterator last)
|
2015-02-14 13:03:39 +00:00
|
|
|
: std::vector<value_type, allocator_type>(first, last) { }
|
|
|
|
|
|
|
|
/* Copy constructor. */
|
|
|
|
vector(const vector &x) : std::vector<value_type, allocator_type>(x) { }
|
|
|
|
|
|
|
|
void shrink_to_fit(void)
|
|
|
|
{
|
2015-02-16 10:38:13 +00:00
|
|
|
#if __cplusplus < 201103L
|
2015-02-14 13:03:39 +00:00
|
|
|
vector<value_type>().swap(*this);
|
2015-02-16 10:38:13 +00:00
|
|
|
#else
|
|
|
|
std::vector<value_type, allocator_type>::shrink_to_fit();
|
2015-02-14 13:03:39 +00:00
|
|
|
#endif
|
2015-02-16 10:38:13 +00:00
|
|
|
}
|
2015-02-14 13:03:39 +00:00
|
|
|
|
2015-06-26 20:36:31 +00:00
|
|
|
void free_memory(void)
|
|
|
|
{
|
2015-02-14 15:49:20 +00:00
|
|
|
std::vector<value_type, allocator_type>::resize(0);
|
|
|
|
shrink_to_fit();
|
|
|
|
}
|
|
|
|
|
2015-02-14 13:03:39 +00:00
|
|
|
/* Some external API might demand working with std::vector. */
|
|
|
|
operator std::vector<value_type>()
|
|
|
|
{
|
|
|
|
return std::vector<value_type>(*this);
|
|
|
|
}
|
|
|
|
};
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
/* Array
|
|
|
|
*
|
2013-06-22 14:35:09 +00:00
|
|
|
* Simplified version of vector, serving multiple purposes:
|
2011-04-27 11:58:34 +00:00
|
|
|
* - somewhat faster in that it does not clear memory on resize/alloc,
|
2013-06-22 14:35:09 +00:00
|
|
|
* this was actually showing up in profiles quite significantly. it
|
|
|
|
* also does not run any constructors/destructors
|
|
|
|
* - if this is used, we are not tempted to use inefficient operations
|
|
|
|
* - aligned allocation for SSE data types */
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2013-06-22 14:35:09 +00:00
|
|
|
template<typename T, size_t alignment = 16>
|
2011-04-27 11:58:34 +00:00
|
|
|
class array
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
array()
|
|
|
|
{
|
|
|
|
data = NULL;
|
|
|
|
datasize = 0;
|
2015-08-23 13:50:31 +00:00
|
|
|
capacity = 0;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
array(size_t newsize)
|
|
|
|
{
|
|
|
|
if(newsize == 0) {
|
|
|
|
data = NULL;
|
|
|
|
datasize = 0;
|
2015-08-23 13:50:31 +00:00
|
|
|
capacity = 0;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-02-14 12:29:47 +00:00
|
|
|
data = (T*)util_aligned_malloc(sizeof(T)*newsize, alignment);
|
2011-04-27 11:58:34 +00:00
|
|
|
datasize = newsize;
|
2015-08-23 13:50:31 +00:00
|
|
|
capacity = datasize;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
array(const array& from)
|
|
|
|
{
|
|
|
|
*this = from;
|
|
|
|
}
|
|
|
|
|
|
|
|
array& operator=(const array& from)
|
|
|
|
{
|
|
|
|
if(from.datasize == 0) {
|
|
|
|
data = NULL;
|
|
|
|
datasize = 0;
|
2015-08-23 13:50:31 +00:00
|
|
|
capacity = 0;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-02-14 12:29:47 +00:00
|
|
|
data = (T*)util_aligned_malloc(sizeof(T)*from.datasize, alignment);
|
2011-04-27 11:58:34 +00:00
|
|
|
memcpy(data, from.data, from.datasize*sizeof(T));
|
|
|
|
datasize = from.datasize;
|
2015-08-23 13:50:31 +00:00
|
|
|
capacity = datasize;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
array& operator=(const vector<T>& from)
|
|
|
|
{
|
|
|
|
datasize = from.size();
|
2015-08-23 13:50:31 +00:00
|
|
|
capacity = datasize;
|
2011-04-27 11:58:34 +00:00
|
|
|
data = NULL;
|
|
|
|
|
|
|
|
if(datasize > 0) {
|
2015-02-14 12:29:47 +00:00
|
|
|
data = (T*)util_aligned_malloc(sizeof(T)*datasize, alignment);
|
2013-06-22 14:35:09 +00:00
|
|
|
memcpy(data, &from[0], datasize*sizeof(T));
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
~array()
|
|
|
|
{
|
2015-02-14 12:29:47 +00:00
|
|
|
util_aligned_free(data);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2015-09-04 07:38:10 +00:00
|
|
|
T* resize(size_t newsize)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
if(newsize == 0) {
|
|
|
|
clear();
|
|
|
|
}
|
2013-06-22 14:35:09 +00:00
|
|
|
else if(newsize != datasize) {
|
2015-08-23 13:50:31 +00:00
|
|
|
if(newsize > capacity) {
|
|
|
|
T *newdata = (T*)util_aligned_malloc(sizeof(T)*newsize, alignment);
|
2015-09-04 07:38:10 +00:00
|
|
|
if(newdata == NULL) {
|
|
|
|
/* Allocation failed, likely out of memory. */
|
|
|
|
clear();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if(data) {
|
2015-08-23 13:50:31 +00:00
|
|
|
memcpy(newdata, data, ((datasize < newsize)? datasize: newsize)*sizeof(T));
|
|
|
|
util_aligned_free(data);
|
|
|
|
}
|
|
|
|
data = newdata;
|
|
|
|
capacity = newsize;
|
2014-06-06 14:08:40 +00:00
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
datasize = newsize;
|
|
|
|
}
|
2015-09-04 07:38:10 +00:00
|
|
|
return data;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
2015-09-04 07:38:10 +00:00
|
|
|
if(data != NULL) {
|
|
|
|
util_aligned_free(data);
|
|
|
|
data = NULL;
|
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
datasize = 0;
|
2015-08-23 13:50:31 +00:00
|
|
|
capacity = 0;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t size() const
|
|
|
|
{
|
|
|
|
return datasize;
|
|
|
|
}
|
|
|
|
|
|
|
|
T& operator[](size_t i) const
|
|
|
|
{
|
2015-06-28 09:18:48 +00:00
|
|
|
assert(i < datasize);
|
2011-04-27 11:58:34 +00:00
|
|
|
return data[i];
|
|
|
|
}
|
|
|
|
|
2015-08-23 13:50:31 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
protected:
|
|
|
|
T *data;
|
|
|
|
size_t datasize;
|
2015-08-23 13:50:31 +00:00
|
|
|
size_t capacity;
|
2011-04-27 11:58:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif /* __UTIL_VECTOR_H__ */
|
|
|
|
|