[Bf-blender-cvs] [369d5e8ad2b] master: BLI: new C++ ArrayRef, Vector, Stack, ... data structures

Jacques Lucke noreply at git.blender.org
Thu Sep 12 14:27:32 CEST 2019


Commit: 369d5e8ad2bb7c249c6b941779066b6aa99f9ea0
Author: Jacques Lucke
Date:   Thu Sep 12 14:23:21 2019 +0200
Branches: master
https://developer.blender.org/rB369d5e8ad2bb7c249c6b941779066b6aa99f9ea0

BLI: new C++ ArrayRef, Vector, Stack, ... data structures

Many generic C++ data structures have been developed in the
functions branch. This commit merges a first chunk of them into
master. The following new data structures are included:

Array: Owns a memory buffer with a fixed size. It is different
  from std::array in that the size is not part of the type.

ArrayRef: References an array owned by someone else. All elements
  in the referenced array are considered to be const. This should
  be the preferred parameter type for functions that take arrays
  as input.

MutableArrayRef: References an array owned by someone else. The
  elements in the referenced array can be changed.

IndexRange: Specifies a continuous range of integers with a start
  and end index.

IntrusiveListBaseWrapper: A utility class that allows iterating
  over ListBase instances where the prev and next pointer are
  stored in the objects directly.

Stack: A stack implemented on top of a vector.

Vector: An array that can grow dynamically.

Allocators: Three allocator types are included that can be used
  by the container types to support different use cases.

The Stack and Vector support small object optimization. So when
the amount of elements in them is below a certain threshold, no
memory allocation is performed.

Additionally, most methods have unit tests.

I'm merging this without normal code review, after I checked the
code roughly with Sergey, and after we talked about it with Brecht.

===================================================================

A	source/blender/blenlib/BLI_allocator.h
A	source/blender/blenlib/BLI_array_cxx.h
A	source/blender/blenlib/BLI_array_ref.h
A	source/blender/blenlib/BLI_index_range.h
A	source/blender/blenlib/BLI_listbase_wrapper.h
A	source/blender/blenlib/BLI_memory_utils_cxx.h
A	source/blender/blenlib/BLI_stack_cxx.h
A	source/blender/blenlib/BLI_temporary_allocator.h
A	source/blender/blenlib/BLI_temporary_allocator_cxx.h
A	source/blender/blenlib/BLI_vector.h
M	source/blender/blenlib/CMakeLists.txt
A	source/blender/blenlib/intern/BLI_index_range.cc
A	source/blender/blenlib/intern/BLI_temporary_allocator.cc
A	tests/gtests/blenlib/BLI_array_ref_test.cc
A	tests/gtests/blenlib/BLI_array_test.cc
A	tests/gtests/blenlib/BLI_index_range_test.cc
A	tests/gtests/blenlib/BLI_stack_cxx_test.cc
A	tests/gtests/blenlib/BLI_vector_test.cc
M	tests/gtests/blenlib/CMakeLists.txt

===================================================================

diff --git a/source/blender/blenlib/BLI_allocator.h b/source/blender/blenlib/BLI_allocator.h
new file mode 100644
index 00000000000..77506aa3dc5
--- /dev/null
+++ b/source/blender/blenlib/BLI_allocator.h
@@ -0,0 +1,127 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+/** \file
+ * \ingroup bli
+ *
+ * This file offers a couple of memory allocators that can be used with containers such as Vector
+ * and Map. Note that these allocators are not designed to work with standard containers like
+ * std::vector.
+ *
+ * Also see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html for why the standard
+ * allocators are not a good fit applications like Blender. The current implementations in this
+ * file are fairly simple still, more complexity can be added when necessary. For now they do their
+ * job good enough.
+ */
+
+#pragma once
+
+#include <stdlib.h>
+
+#include "MEM_guardedalloc.h"
+
+#include "BLI_utildefines.h"
+#include "BLI_math_base.h"
+#include "BLI_temporary_allocator.h"
+
+namespace BLI {
+
+/**
+ * Use Blenders guarded allocator (aka MEM_malloc). This should always be used except there is a
+ * good reason not to use it.
+ */
+class GuardedAllocator {
+ public:
+  void *allocate(uint size, const char *name)
+  {
+    return MEM_mallocN(size, name);
+  }
+
+  void *allocate_aligned(uint size, uint alignment, const char *name)
+  {
+    alignment = std::max<uint>(alignment, 8);
+    return MEM_mallocN_aligned(size, alignment, name);
+  }
+
+  void deallocate(void *ptr)
+  {
+    MEM_freeN(ptr);
+  }
+};
+
+/**
+ * This is a simple wrapper around malloc/free. Only use this when the GuardedAllocator cannot be
+ * used. This can be the case when the allocated element might live longer than Blenders Allocator.
+ */
+class RawAllocator {
+ private:
+  struct MemHead {
+    int offset;
+  };
+
+ public:
+  void *allocate(uint size, const char *UNUSED(name))
+  {
+    void *ptr = malloc(size + sizeof(MemHead));
+    ((MemHead *)ptr)->offset = sizeof(MemHead);
+    return POINTER_OFFSET(ptr, sizeof(MemHead));
+  }
+
+  void *allocate_aligned(uint size, uint alignment, const char *UNUSED(name))
+  {
+    BLI_assert(is_power_of_2_i(alignment));
+    void *ptr = malloc(size + alignment + sizeof(MemHead));
+    void *used_ptr = (void *)((uintptr_t)POINTER_OFFSET(ptr, alignment + sizeof(MemHead)) &
+                              ~((uintptr_t)alignment - 1));
+    uint offset = (uintptr_t)used_ptr - (uintptr_t)ptr;
+    BLI_assert(offset >= sizeof(MemHead));
+    ((MemHead *)used_ptr - 1)->offset = offset;
+    return used_ptr;
+  }
+
+  void deallocate(void *ptr)
+  {
+    MemHead *head = (MemHead *)ptr - 1;
+    int offset = -head->offset;
+    void *actual_pointer = POINTER_OFFSET(ptr, offset);
+    free(actual_pointer);
+  }
+};
+
+/**
+ * Use this only under specific circumstances as described in BLI_temporary_allocator.h.
+ */
+class TemporaryAllocator {
+ public:
+  void *allocate(uint size, const char *UNUSED(name))
+  {
+    return BLI_temporary_allocate(size);
+  }
+
+  void *allocate_aligned(uint size, uint alignment, const char *UNUSED(name))
+  {
+    BLI_assert(alignment <= 64);
+    UNUSED_VARS_NDEBUG(alignment);
+    return BLI_temporary_allocate(size);
+  }
+
+  void deallocate(void *ptr)
+  {
+    BLI_temporary_deallocate(ptr);
+  }
+};
+
+}  // namespace BLI
diff --git a/source/blender/blenlib/BLI_array_cxx.h b/source/blender/blenlib/BLI_array_cxx.h
new file mode 100644
index 00000000000..f48ba05842e
--- /dev/null
+++ b/source/blender/blenlib/BLI_array_cxx.h
@@ -0,0 +1,195 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+/** \file
+ * \ingroup bli
+ *
+ * This is a container that contains a fixed size array. Note however, the size of the array is not
+ * a template argument. Instead it can be specified at the construction time.
+ */
+
+#pragma once
+
+#include "BLI_utildefines.h"
+#include "BLI_allocator.h"
+#include "BLI_array_ref.h"
+#include "BLI_memory_utils_cxx.h"
+
+namespace BLI {
+
+template<typename T, typename Allocator = GuardedAllocator> class Array {
+ private:
+  T *m_data;
+  uint m_size;
+  Allocator m_allocator;
+
+ public:
+  Array()
+  {
+    m_data = nullptr;
+    m_size = 0;
+  }
+
+  Array(ArrayRef<T> values)
+  {
+    m_size = values.size();
+    m_data = this->allocate(m_size);
+    uninitialized_copy_n(values.begin(), m_size, m_data);
+  }
+
+  Array(const std::initializer_list<T> &values) : Array(ArrayRef<T>(values))
+  {
+  }
+
+  explicit Array(uint size)
+  {
+    m_data = this->allocate(size);
+    m_size = size;
+
+    for (uint i = 0; i < m_size; i++) {
+      new (m_data + i) T();
+    }
+  }
+
+  Array(uint size, const T &value)
+  {
+    m_data = this->allocate(size);
+    m_size = size;
+    uninitialized_fill_n(m_data, m_size, value);
+  }
+
+  Array(const Array &other)
+  {
+    m_size = other.size();
+    m_allocator = other.m_allocator;
+
+    if (m_size == 0) {
+      m_data = nullptr;
+      return;
+    }
+    else {
+      m_data = this->allocate(m_size);
+      copy_n(other.begin(), m_size, m_data);
+    }
+  }
+
+  Array(Array &&other) noexcept
+  {
+    m_data = other.m_data;
+    m_size = other.m_size;
+    m_allocator = other.m_allocator;
+
+    other.m_data = nullptr;
+    other.m_size = 0;
+  }
+
+  ~Array()
+  {
+    destruct_n(m_data, m_size);
+    if (m_data != nullptr) {
+      m_allocator.deallocate((void *)m_data);
+    }
+  }
+
+  Array &operator=(const Array &other)
+  {
+    if (this == &other) {
+      return *this;
+    }
+
+    this->~Array();
+    new (this) Array(other);
+    return *this;
+  }
+
+  Array &operator=(Array &&other)
+  {
+    if (this == &other) {
+      return *this;
+    }
+
+    this->~Array();
+    new (this) Array(std::move(other));
+    return *this;
+  }
+
+  operator ArrayRef<T>() const
+  {
+    return ArrayRef<T>(m_data, m_size);
+  }
+
+  operator MutableArrayRef<T>()
+  {
+    return MutableArrayRef<T>(m_data, m_size);
+  }
+
+  ArrayRef<T> as_ref() const
+  {
+    return *this;
+  }
+
+  T &operator[](uint index)
+  {
+    BLI_assert(index < m_size);
+    return m_data[index];
+  }
+
+  uint size() const
+  {
+    return m_size;
+  }
+
+  void fill(const T &value)
+  {
+    MutableArrayRef<T>(*this).fill(value);
+  }
+
+  void fill_indices(ArrayRef<uint> indices, const T &value)
+  {
+    MutableArrayRef<T>(*this).fill_indices(indices, value);
+  }
+
+  const T *begin() const
+  {
+    return m_data;
+  }
+
+  const T *end() const
+  {
+    return m_data + m_size;
+  }
+
+  T *begin()
+  {
+    return m_data;
+  }
+
+  T *end()
+  {
+    return m_data + m_size;
+  }
+
+ private:
+  T *allocate(uint size)
+  {
+    return (T *)m_allocator.allocate_aligned(
+        size * sizeof(T), std::alignment_of<T>::value, __func__);
+  }
+};
+
+template<typename T> using TemporaryArray = Array<T, TemporaryAllocator>;
+
+}  // namespace BLI
diff --git a/source/blender/blenlib/BLI_array_ref.h b/source/blender/blenlib/BLI_array_ref.h
new file mode 100644
index 00000000000..a771d1c1329
--- /dev/null
+++ b/source/blender/blenlib/BLI_array_ref.h
@@ -0,0 +1,423 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+/** \file
+ * \ingroup bli
+ *
+ * These classes offer a convenient way to work with continuous chunks of memory of a certain type.
+ * We differentiate ArrayRef and MutableArrayRef. The elements in the former are const while the
+ * elements in the other are not.
+ *
+ * Passing array references as parameters has multiple benefits:
+ *   - Less templates are used because the function does not have to work with different
+ *     container types.
+ *   - It encourages an Struct-of-Arrays data layout which is often benefitial when
+ *     writing high performance code. Also it makes it easier to reuse code.
+ *   - Array references offer convenient ways of slicing and other operations.
+ *
+ * The instances of ArrayRef and MutableArrayRef are very small and should be passed by value.
+ * Since array references do not own any memory, it is generally not save to store them.
+ */
+
+#pragma once
+
+#include <vector>
+#include <array>
+#include <algorithm>
+#include <iostream>
+#include <string>

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list