[Bf-blender-cvs] [e0ac932156b] temp-copy-on-write: initial data structure

Jacques Lucke noreply at git.blender.org
Tue Jan 4 13:38:16 CET 2022


Commit: e0ac932156b9b44190d5638f2c38d17af7770594
Author: Jacques Lucke
Date:   Mon Jan 3 15:29:23 2022 +0100
Branches: temp-copy-on-write
https://developer.blender.org/rBe0ac932156b9b44190d5638f2c38d17af7770594

initial data structure

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

A	source/blender/blenlib/BLI_copy_on_write.h
M	source/blender/blenlib/CMakeLists.txt
A	source/blender/blenlib/intern/copy_on_write.cc
A	source/blender/makesdna/DNA_copy_on_write.h
M	source/blender/makesdna/intern/makesdna.c

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

diff --git a/source/blender/blenlib/BLI_copy_on_write.h b/source/blender/blenlib/BLI_copy_on_write.h
new file mode 100644
index 00000000000..838d4c38b0e
--- /dev/null
+++ b/source/blender/blenlib/BLI_copy_on_write.h
@@ -0,0 +1,45 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+/** \file
+ * \ingroup bli
+ */
+
+#include "BLI_compiler_attrs.h"
+
+#include "DNA_copy_on_write.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+bCopyOnWrite *BLI_cow_new(int user_count);
+void BLI_cow_free(const bCopyOnWrite *cow);
+
+void BLI_cow_init(const bCopyOnWrite *cow, int user_count);
+
+bool BLI_cow_is_shared(const bCopyOnWrite *cow);
+bool BLI_cow_is_mutable(const bCopyOnWrite *cow);
+bool BLI_cow_is_zero(const bCopyOnWrite *cow);
+
+void BLI_cow_user_add(const bCopyOnWrite *cow);
+bool BLI_cow_user_remove(const bCopyOnWrite *cow) ATTR_WARN_UNUSED_RESULT;
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt
index 516d9d2fe84..aeb85ab3eac 100644
--- a/source/blender/blenlib/CMakeLists.txt
+++ b/source/blender/blenlib/CMakeLists.txt
@@ -68,6 +68,7 @@ set(SRC
   intern/boxpack_2d.c
   intern/buffer.c
   intern/convexhull_2d.c
+  intern/copy_on_write.cc
   intern/delaunay_2d.cc
   intern/dot_export.cc
   intern/dynlib.c
@@ -187,6 +188,7 @@ set(SRC
   BLI_compiler_typecheck.h
   BLI_console.h
   BLI_convexhull_2d.h
+  BLI_copy_on_write.h
   BLI_delaunay_2d.h
   BLI_dial_2d.h
   BLI_disjoint_set.hh
diff --git a/source/blender/blenlib/intern/copy_on_write.cc b/source/blender/blenlib/intern/copy_on_write.cc
new file mode 100644
index 00000000000..c1fc7a7569b
--- /dev/null
+++ b/source/blender/blenlib/intern/copy_on_write.cc
@@ -0,0 +1,78 @@
+/*
+ * 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
+ */
+
+#include "atomic_ops.h"
+
+#include "BLI_assert.h"
+#include "BLI_copy_on_write.h"
+
+#include "MEM_guardedalloc.h"
+
+static int &get_counter(const bCopyOnWrite *cow)
+{
+  BLI_assert(cow != nullptr);
+  return *const_cast<int *>(&cow->user_count);
+}
+
+bCopyOnWrite *BLI_cow_new(int user_count)
+{
+  bCopyOnWrite *cow = MEM_cnew<bCopyOnWrite>(__func__);
+  cow->user_count = user_count;
+  return cow;
+}
+
+void BLI_cow_free(const bCopyOnWrite *cow)
+{
+  BLI_assert(cow->user_count == 0);
+  MEM_freeN(const_cast<bCopyOnWrite *>(cow));
+}
+
+void BLI_cow_init(const bCopyOnWrite *cow, int user_count)
+{
+  get_counter(cow) = user_count;
+}
+
+bool BLI_cow_is_mutable(const bCopyOnWrite *cow)
+{
+  return !BLI_cow_is_shared(cow);
+}
+
+bool BLI_cow_is_shared(const bCopyOnWrite *cow)
+{
+  return cow->user_count >= 2;
+}
+
+bool BLI_cow_is_zero(const bCopyOnWrite *cow)
+{
+  return cow->user_count == 0;
+}
+
+void BLI_cow_user_add(const bCopyOnWrite *cow)
+{
+  atomic_fetch_and_add_int32(&get_counter(cow), 1);
+}
+
+bool BLI_cow_user_remove(const bCopyOnWrite *cow)
+{
+  const int new_user_count = atomic_sub_and_fetch_int32(&get_counter(cow), 1);
+  BLI_assert(new_user_count >= 0);
+  const bool has_no_user_anymore = new_user_count == 0;
+  return has_no_user_anymore;
+}
diff --git a/source/blender/makesdna/DNA_copy_on_write.h b/source/blender/makesdna/DNA_copy_on_write.h
new file mode 100644
index 00000000000..4cf3b753be5
--- /dev/null
+++ b/source/blender/makesdna/DNA_copy_on_write.h
@@ -0,0 +1,25 @@
+/*
+ * 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 DNA
+ */
+
+#pragma once
+
+typedef struct bCopyOnWrite {
+  int user_count;
+} bCopyOnWrite;
diff --git a/source/blender/makesdna/intern/makesdna.c b/source/blender/makesdna/intern/makesdna.c
index 114c0b40407..78268dc3cff 100644
--- a/source/blender/makesdna/intern/makesdna.c
+++ b/source/blender/makesdna/intern/makesdna.c
@@ -141,6 +141,7 @@ static const char *includefiles[] = {
     "DNA_pointcache_types.h",
     "DNA_uuid_types.h",
     "DNA_asset_types.h",
+    "DNA_copy_on_write.h",
 
     /* see comment above before editing! */
 
@@ -1624,6 +1625,7 @@ int main(int argc, char **argv)
 #include "DNA_collection_types.h"
 #include "DNA_color_types.h"
 #include "DNA_constraint_types.h"
+#include "DNA_copy_on_write.h"
 #include "DNA_curve_types.h"
 #include "DNA_curveprofile_types.h"
 #include "DNA_customdata_types.h"



More information about the Bf-blender-cvs mailing list