From c86d4b1d804eb9284eee1c796080196e7a48b3f8 Mon Sep 17 00:00:00 2001 From: Sergey Sharybin Date: Fri, 9 Nov 2018 11:54:24 +0100 Subject: Cycles: Cleanup, split array from vector Those are similar but different types, no reason to keep their definitions in a single file. --- intern/cycles/blender/blender_util.h | 1 + intern/cycles/bvh/bvh.h | 2 +- intern/cycles/bvh/bvh_build.h | 1 + intern/cycles/device/device_memory.h | 1 + intern/cycles/graph/node.h | 2 +- intern/cycles/graph/node_type.h | 2 +- intern/cycles/render/camera.h | 1 + intern/cycles/render/curves.h | 2 +- intern/cycles/render/mesh.h | 1 + intern/cycles/render/nodes.h | 1 + intern/cycles/render/object.h | 2 + intern/cycles/render/osl.h | 1 + intern/cycles/render/particles.h | 2 +- intern/cycles/render/svm.h | 1 + intern/cycles/subd/subd_patch_table.h | 2 +- intern/cycles/test/render_graph_finalize_test.cpp | 1 + intern/cycles/util/CMakeLists.txt | 1 + intern/cycles/util/util_array.h | 289 ++++++++++++++++++++++ intern/cycles/util/util_vector.h | 265 +------------------- 19 files changed, 308 insertions(+), 270 deletions(-) create mode 100644 intern/cycles/util/util_array.h (limited to 'intern') diff --git a/intern/cycles/blender/blender_util.h b/intern/cycles/blender/blender_util.h index 8f5fb5971ff..eb7019f45bc 100644 --- a/intern/cycles/blender/blender_util.h +++ b/intern/cycles/blender/blender_util.h @@ -20,6 +20,7 @@ #include "render/mesh.h" #include "util/util_algorithm.h" +#include "util/util_array.h" #include "util/util_map.h" #include "util/util_path.h" #include "util/util_set.h" diff --git a/intern/cycles/bvh/bvh.h b/intern/cycles/bvh/bvh.h index 9c290139e74..c8ad29004d7 100644 --- a/intern/cycles/bvh/bvh.h +++ b/intern/cycles/bvh/bvh.h @@ -19,7 +19,7 @@ #define __BVH_H__ #include "bvh/bvh_params.h" - +#include "util/util_array.h" #include "util/util_types.h" #include "util/util_vector.h" diff --git a/intern/cycles/bvh/bvh_build.h b/intern/cycles/bvh/bvh_build.h index 6dae5a9e6bf..dd95a5cc0e8 100644 --- a/intern/cycles/bvh/bvh_build.h +++ b/intern/cycles/bvh/bvh_build.h @@ -23,6 +23,7 @@ #include "bvh/bvh_params.h" #include "bvh/bvh_unaligned.h" +#include "util/util_array.h" #include "util/util_task.h" #include "util/util_vector.h" diff --git a/intern/cycles/device/device_memory.h b/intern/cycles/device/device_memory.h index 1801cda0de0..e43834bdc8d 100644 --- a/intern/cycles/device/device_memory.h +++ b/intern/cycles/device/device_memory.h @@ -21,6 +21,7 @@ * * Data types for allocating, copying and freeing device memory. */ +#include "util/util_array.h" #include "util/util_half.h" #include "util/util_texture.h" #include "util/util_types.h" diff --git a/intern/cycles/graph/node.h b/intern/cycles/graph/node.h index 11695a8631d..d50a3786139 100644 --- a/intern/cycles/graph/node.h +++ b/intern/cycles/graph/node.h @@ -18,9 +18,9 @@ #include "graph/node_type.h" +#include "util/util_array.h" #include "util/util_map.h" #include "util/util_param.h" -#include "util/util_vector.h" CCL_NAMESPACE_BEGIN diff --git a/intern/cycles/graph/node_type.h b/intern/cycles/graph/node_type.h index 1d565794b27..7d6abae2314 100644 --- a/intern/cycles/graph/node_type.h +++ b/intern/cycles/graph/node_type.h @@ -17,7 +17,7 @@ #pragma once #include "graph/node_enum.h" - +#include "util/util_array.h" #include "util/util_map.h" #include "util/util_param.h" #include "util/util_string.h" diff --git a/intern/cycles/render/camera.h b/intern/cycles/render/camera.h index 9f0830d7a54..37f5dea624f 100644 --- a/intern/cycles/render/camera.h +++ b/intern/cycles/render/camera.h @@ -21,6 +21,7 @@ #include "graph/node.h" +#include "util/util_array.h" #include "util/util_boundbox.h" #include "util/util_projection.h" #include "util/util_transform.h" diff --git a/intern/cycles/render/curves.h b/intern/cycles/render/curves.h index 3d8611ca714..cf75751c58f 100644 --- a/intern/cycles/render/curves.h +++ b/intern/cycles/render/curves.h @@ -17,8 +17,8 @@ #ifndef __CURVES_H__ #define __CURVES_H__ +#include "util/util_array.h" #include "util/util_types.h" -#include "util/util_vector.h" CCL_NAMESPACE_BEGIN diff --git a/intern/cycles/render/mesh.h b/intern/cycles/render/mesh.h index 8a6629e38f8..7d36b2cd7ca 100644 --- a/intern/cycles/render/mesh.h +++ b/intern/cycles/render/mesh.h @@ -22,6 +22,7 @@ #include "render/attribute.h" #include "render/shader.h" +#include "util/util_array.h" #include "util/util_boundbox.h" #include "util/util_list.h" #include "util/util_map.h" diff --git a/intern/cycles/render/nodes.h b/intern/cycles/render/nodes.h index 154ae31551c..048f0fcaa24 100644 --- a/intern/cycles/render/nodes.h +++ b/intern/cycles/render/nodes.h @@ -20,6 +20,7 @@ #include "render/graph.h" #include "graph/node.h" +#include "util/util_array.h" #include "util/util_string.h" CCL_NAMESPACE_BEGIN diff --git a/intern/cycles/render/object.h b/intern/cycles/render/object.h index 285362f7ec4..87e6e6652ad 100644 --- a/intern/cycles/render/object.h +++ b/intern/cycles/render/object.h @@ -20,11 +20,13 @@ #include "graph/node.h" #include "render/scene.h" +#include "util/util_array.h" #include "util/util_boundbox.h" #include "util/util_param.h" #include "util/util_transform.h" #include "util/util_thread.h" #include "util/util_types.h" +#include "util/util_vector.h" CCL_NAMESPACE_BEGIN diff --git a/intern/cycles/render/osl.h b/intern/cycles/render/osl.h index d0542d1ae92..e196e0be787 100644 --- a/intern/cycles/render/osl.h +++ b/intern/cycles/render/osl.h @@ -17,6 +17,7 @@ #ifndef __OSL_H__ #define __OSL_H__ +#include "util/util_array.h" #include "util/util_set.h" #include "util/util_string.h" #include "util/util_thread.h" diff --git a/intern/cycles/render/particles.h b/intern/cycles/render/particles.h index 3a744c9c73f..27821907af0 100644 --- a/intern/cycles/render/particles.h +++ b/intern/cycles/render/particles.h @@ -17,8 +17,8 @@ #ifndef __PARTICLES_H__ #define __PARTICLES_H__ +#include "util/util_array.h" #include "util/util_types.h" -#include "util/util_vector.h" CCL_NAMESPACE_BEGIN diff --git a/intern/cycles/render/svm.h b/intern/cycles/render/svm.h index 6a1f24462fb..af97a490a87 100644 --- a/intern/cycles/render/svm.h +++ b/intern/cycles/render/svm.h @@ -21,6 +21,7 @@ #include "render/graph.h" #include "render/shader.h" +#include "util/util_array.h" #include "util/util_set.h" #include "util/util_string.h" #include "util/util_thread.h" diff --git a/intern/cycles/subd/subd_patch_table.h b/intern/cycles/subd/subd_patch_table.h index d96a43adfb9..1765578c42e 100644 --- a/intern/cycles/subd/subd_patch_table.h +++ b/intern/cycles/subd/subd_patch_table.h @@ -17,8 +17,8 @@ #ifndef __SUBD_PATCH_TABLE_H__ #define __SUBD_PATCH_TABLE_H__ +#include "util/util_array.h" #include "util/util_types.h" -#include "util/util_vector.h" #ifdef WITH_OPENSUBDIV #ifdef _MSC_VER diff --git a/intern/cycles/test/render_graph_finalize_test.cpp b/intern/cycles/test/render_graph_finalize_test.cpp index b66a91adbda..cfdab7a6433 100644 --- a/intern/cycles/test/render_graph_finalize_test.cpp +++ b/intern/cycles/test/render_graph_finalize_test.cpp @@ -20,6 +20,7 @@ #include "render/graph.h" #include "render/scene.h" #include "render/nodes.h" +#include "util/util_array.h" #include "util/util_logging.h" #include "util/util_string.h" #include "util/util_vector.h" diff --git a/intern/cycles/util/CMakeLists.txt b/intern/cycles/util/CMakeLists.txt index 4f623c5dfb7..77d47984ee7 100644 --- a/intern/cycles/util/CMakeLists.txt +++ b/intern/cycles/util/CMakeLists.txt @@ -37,6 +37,7 @@ set(SRC_HEADERS util_algorithm.h util_aligned_malloc.h util_args.h + util_array.h util_atomic.h util_boundbox.h util_debug.h diff --git a/intern/cycles/util/util_array.h b/intern/cycles/util/util_array.h new file mode 100644 index 00000000000..5f18d434c31 --- /dev/null +++ b/intern/cycles/util/util_array.h @@ -0,0 +1,289 @@ +/* + * Copyright 2011-2018 Blender Foundation + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 + * limitations under the License. + */ + +#ifndef __UTIL_ARRAY_H__ +#define __UTIL_ARRAY_H__ + +#include +#include + +#include "util/util_aligned_malloc.h" +#include "util/util_guarded_allocator.h" +#include "util/util_types.h" +#include "util/util_vector.h" + +CCL_NAMESPACE_BEGIN + +/* Simplified version of vector, serving multiple purposes: + * - somewhat faster in that it does not clear memory on resize/alloc, + * 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 CPU native data types */ + +template +class array +{ +public: + array() + : data_(NULL), + datasize_(0), + capacity_(0) + {} + + explicit array(size_t newsize) + { + if(newsize == 0) { + data_ = NULL; + datasize_ = 0; + capacity_ = 0; + } + else { + data_ = mem_allocate(newsize); + datasize_ = newsize; + capacity_ = datasize_; + } + } + + array(const array& from) + { + if(from.datasize_ == 0) { + data_ = NULL; + datasize_ = 0; + capacity_ = 0; + } + else { + data_ = mem_allocate(from.datasize_); + memcpy(data_, from.data_, from.datasize_*sizeof(T)); + datasize_ = from.datasize_; + capacity_ = datasize_; + } + } + + array& operator=(const array& from) + { + if(this != &from) { + resize(from.size()); + memcpy((void*)data_, from.data_, datasize_*sizeof(T)); + } + + return *this; + } + + array& operator=(const vector& from) + { + resize(from.size()); + + if(from.size() > 0) { + memcpy(data_, &from[0], datasize_*sizeof(T)); + } + + return *this; + } + + ~array() + { + mem_free(data_, capacity_); + } + + bool operator==(const array& other) const + { + if(datasize_ != other.datasize_) { + return false; + } + + return memcmp(data_, other.data_, datasize_*sizeof(T)) == 0; + } + + bool operator!=(const array& other) const + { + return !(*this == other); + } + + void steal_data(array& from) + { + if(this != &from) { + clear(); + + data_ = from.data_; + datasize_ = from.datasize_; + capacity_ = from.capacity_; + + from.data_ = NULL; + from.datasize_ = 0; + from.capacity_ = 0; + } + } + + T *steal_pointer() + { + T *ptr = data_; + data_ = NULL; + clear(); + return ptr; + } + + T* resize(size_t newsize) + { + if(newsize == 0) { + clear(); + } + else if(newsize != datasize_) { + if(newsize > capacity_) { + T *newdata = mem_allocate(newsize); + if(newdata == NULL) { + /* Allocation failed, likely out of memory. */ + clear(); + return NULL; + } + else if(data_ != NULL) { + memcpy((void *)newdata, + data_, + ((datasize_ < newsize)? datasize_: newsize)*sizeof(T)); + mem_free(data_, capacity_); + } + data_ = newdata; + capacity_ = newsize; + } + datasize_ = newsize; + } + return data_; + } + + T* resize(size_t newsize, const T& value) + { + size_t oldsize = size(); + resize(newsize); + + for(size_t i = oldsize; i < size(); i++) { + data_[i] = value; + } + + return data_; + } + + void clear() + { + if(data_ != NULL) { + mem_free(data_, capacity_); + data_ = NULL; + } + datasize_ = 0; + capacity_ = 0; + } + + size_t empty() const + { + return datasize_ == 0; + } + + size_t size() const + { + return datasize_; + } + + T* data() + { + return data_; + } + + const T* data() const + { + return data_; + } + + T& operator[](size_t i) const + { + assert(i < datasize_); + return data_[i]; + } + + void reserve(size_t newcapacity) + { + if(newcapacity > capacity_) { + T *newdata = mem_allocate(newcapacity); + if(data_ != NULL) { + memcpy(newdata, data_, ((datasize_ < newcapacity)? datasize_: newcapacity)*sizeof(T)); + mem_free(data_, capacity_); + } + data_ = newdata; + capacity_ = newcapacity; + } + } + + size_t capacity() const + { + return capacity_; + } + + // do not use this method unless you are sure the code is not performance critical + void push_back_slow(const T& t) + { + if(capacity_ == datasize_) + { + reserve(datasize_ == 0 ? 1 : (size_t)((datasize_ + 1) * 1.2)); + } + + data_[datasize_++] = t; + } + + void push_back_reserved(const T& t) + { + assert(datasize_ < capacity_); + push_back_slow(t); + } + + void append(const array& from) + { + if(from.size()) { + size_t old_size = size(); + resize(old_size + from.size()); + memcpy(data_ + old_size, from.data(), sizeof(T) * from.size()); + } + } + +protected: + inline T* mem_allocate(size_t N) + { + if(N == 0) { + return NULL; + } + T *mem = (T*)util_aligned_malloc(sizeof(T)*N, alignment); + if(mem != NULL) { + util_guarded_mem_alloc(sizeof(T)*N); + } + else { + throw std::bad_alloc(); + } + return mem; + } + + inline void mem_free(T *mem, size_t N) + { + if(mem != NULL) { + util_guarded_mem_free(sizeof(T)*N); + util_aligned_free(mem); + } + } + + T *data_; + size_t datasize_; + size_t capacity_; +}; + +CCL_NAMESPACE_END + +#endif /* __UTIL_ARRAY_H__ */ diff --git a/intern/cycles/util/util_vector.h b/intern/cycles/util/util_vector.h index 082c531b69a..fb9c5784adf 100644 --- a/intern/cycles/util/util_vector.h +++ b/intern/cycles/util/util_vector.h @@ -17,8 +17,6 @@ #ifndef __UTIL_VECTOR_H__ #define __UTIL_VECTOR_H__ -/* Vector */ - #include #include #include @@ -29,9 +27,7 @@ CCL_NAMESPACE_BEGIN -/* Vector - * - * Own subclass-ed vestion of std::vector. Subclass is needed because: +/* Own subclass-ed vestion of std::vector. Subclass is needed because: * * - Use own allocator which keeps track of used/peak memory. * - Have method to ensure capacity is re-set to 0. @@ -60,265 +56,6 @@ public: } }; -/* Array - * - * Simplified version of vector, serving multiple purposes: - * - somewhat faster in that it does not clear memory on resize/alloc, - * 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 CPU native data types */ - -template -class array -{ -public: - array() - : data_(NULL), - datasize_(0), - capacity_(0) - {} - - explicit array(size_t newsize) - { - if(newsize == 0) { - data_ = NULL; - datasize_ = 0; - capacity_ = 0; - } - else { - data_ = mem_allocate(newsize); - datasize_ = newsize; - capacity_ = datasize_; - } - } - - array(const array& from) - { - if(from.datasize_ == 0) { - data_ = NULL; - datasize_ = 0; - capacity_ = 0; - } - else { - data_ = mem_allocate(from.datasize_); - memcpy(data_, from.data_, from.datasize_*sizeof(T)); - datasize_ = from.datasize_; - capacity_ = datasize_; - } - } - - array& operator=(const array& from) - { - if(this != &from) { - resize(from.size()); - memcpy((void*)data_, from.data_, datasize_*sizeof(T)); - } - - return *this; - } - - array& operator=(const vector& from) - { - resize(from.size()); - - if(from.size() > 0) { - memcpy(data_, &from[0], datasize_*sizeof(T)); - } - - return *this; - } - - ~array() - { - mem_free(data_, capacity_); - } - - bool operator==(const array& other) const - { - if(datasize_ != other.datasize_) { - return false; - } - - return memcmp(data_, other.data_, datasize_*sizeof(T)) == 0; - } - - bool operator!=(const array& other) const - { - return !(*this == other); - } - - void steal_data(array& from) - { - if(this != &from) { - clear(); - - data_ = from.data_; - datasize_ = from.datasize_; - capacity_ = from.capacity_; - - from.data_ = NULL; - from.datasize_ = 0; - from.capacity_ = 0; - } - } - - T *steal_pointer() - { - T *ptr = data_; - data_ = NULL; - clear(); - return ptr; - } - - T* resize(size_t newsize) - { - if(newsize == 0) { - clear(); - } - else if(newsize != datasize_) { - if(newsize > capacity_) { - T *newdata = mem_allocate(newsize); - if(newdata == NULL) { - /* Allocation failed, likely out of memory. */ - clear(); - return NULL; - } - else if(data_ != NULL) { - memcpy((void *)newdata, - data_, - ((datasize_ < newsize)? datasize_: newsize)*sizeof(T)); - mem_free(data_, capacity_); - } - data_ = newdata; - capacity_ = newsize; - } - datasize_ = newsize; - } - return data_; - } - - T* resize(size_t newsize, const T& value) - { - size_t oldsize = size(); - resize(newsize); - - for(size_t i = oldsize; i < size(); i++) { - data_[i] = value; - } - - return data_; - } - - void clear() - { - if(data_ != NULL) { - mem_free(data_, capacity_); - data_ = NULL; - } - datasize_ = 0; - capacity_ = 0; - } - - size_t empty() const - { - return datasize_ == 0; - } - - size_t size() const - { - return datasize_; - } - - T* data() - { - return data_; - } - - const T* data() const - { - return data_; - } - - T& operator[](size_t i) const - { - assert(i < datasize_); - return data_[i]; - } - - void reserve(size_t newcapacity) - { - if(newcapacity > capacity_) { - T *newdata = mem_allocate(newcapacity); - if(data_ != NULL) { - memcpy(newdata, data_, ((datasize_ < newcapacity)? datasize_: newcapacity)*sizeof(T)); - mem_free(data_, capacity_); - } - data_ = newdata; - capacity_ = newcapacity; - } - } - - size_t capacity() const - { - return capacity_; - } - - // do not use this method unless you are sure the code is not performance critical - void push_back_slow(const T& t) - { - if(capacity_ == datasize_) - { - reserve(datasize_ == 0 ? 1 : (size_t)((datasize_ + 1) * 1.2)); - } - - data_[datasize_++] = t; - } - - void push_back_reserved(const T& t) - { - assert(datasize_ < capacity_); - push_back_slow(t); - } - - void append(const array& from) - { - if(from.size()) { - size_t old_size = size(); - resize(old_size + from.size()); - memcpy(data_ + old_size, from.data(), sizeof(T) * from.size()); - } - } - -protected: - inline T* mem_allocate(size_t N) - { - if(N == 0) { - return NULL; - } - T *mem = (T*)util_aligned_malloc(sizeof(T)*N, alignment); - if(mem != NULL) { - util_guarded_mem_alloc(sizeof(T)*N); - } - else { - throw std::bad_alloc(); - } - return mem; - } - - inline void mem_free(T *mem, size_t N) - { - if(mem != NULL) { - util_guarded_mem_free(sizeof(T)*N); - util_aligned_free(mem); - } - } - - T *data_; - size_t datasize_; - size_t capacity_; -}; - CCL_NAMESPACE_END #endif /* __UTIL_VECTOR_H__ */ -- cgit v1.2.3