[gtk/wip/otte/vector: 7/10] vector: Add null-termination



commit 43f9743495deb5f6d608119819fa54b01e16ebb2
Author: Benjamin Otte <otte redhat com>
Date:   Fri Jul 3 03:05:40 2020 +0200

    vector: Add null-termination

 gtk/gtkvectorimpl.c        | 36 +++++++++++++++++++++++++++++-------
 testsuite/gtk/vector.c     | 40 ++++++++++++++++++++++++++++++++++++++--
 testsuite/gtk/vectorimpl.c | 10 +++++++++-
 3 files changed, 76 insertions(+), 10 deletions(-)
---
diff --git a/gtk/gtkvectorimpl.c b/gtk/gtkvectorimpl.c
index 2a0714f38d..774aac41c5 100644
--- a/gtk/gtkvectorimpl.c
+++ b/gtk/gtkvectorimpl.c
@@ -39,6 +39,12 @@ G_BEGIN_DECLS
 #endif
 #endif
 
+#ifdef GTK_VECTOR_NULL_TERMINATED
+#define GTK_VECTOR_REAL_SIZE(_size) ((_size) + 1)
+#else
+#define GTK_VECTOR_REAL_SIZE(_size) (_size)
+#endif
+
 /* make this readable */
 #define _T_ GTK_VECTOR_ELEMENT_TYPE
 #define GtkVector GTK_VECTOR_TYPE_NAME
@@ -54,7 +60,7 @@ struct GtkVector
   _T_ *end;
   _T_ *end_allocation;
 #ifdef GTK_VECTOR_PREALLOC
-  _T_ preallocated[GTK_VECTOR_PREALLOC];
+  _T_ preallocated[GTK_VECTOR_REAL_SIZE(GTK_VECTOR_PREALLOC)];
 #endif
 };
 
@@ -66,6 +72,9 @@ gtk_vector(init) (GtkVector *self)
   self->start = self->preallocated;
   self->end = self->start;
   self->end_allocation = self->start + GTK_VECTOR_PREALLOC;
+#ifdef GTK_VECTOR_NULL_TERMINATED
+  *self->start = *(_T_[1]) {};
+#endif
 #else
   self->start = NULL;
   self->end = NULL;
@@ -130,7 +139,7 @@ gtk_vector(is_empty) (GtkVector *self)
 
 G_GNUC_UNUSED static void
 gtk_vector(reserve) (GtkVector *self,
-                        gsize      n)
+                     gsize      n)
 {
   gsize new_size, size;
 
@@ -144,7 +153,15 @@ gtk_vector(reserve) (GtkVector *self,
   if (self->start == self->preallocated)
     {
       self->start = g_new (_T_, new_size);
-      memcpy (self->start, self->preallocated, sizeof (_T_) * size);
+      memcpy (self->start, self->preallocated, sizeof (_T_) * GTK_VECTOR_REAL_SIZE (size));
+    }
+  else
+#endif
+#ifdef GTK_VECTOR_NULL_TERMINATED
+  if (self->start == NULL)
+    {
+      self->start = g_new (_T_, new_size);
+      *self->start = *(_T_[1]) {};
     }
   else
 #endif
@@ -161,19 +178,22 @@ gtk_vector(splice) (GtkVector *self,
                     _T_       *additions,
                     gsize      added)
 {
-  gssize size = gtk_vector(get_size) (self);
+  gsize size;
+  gsize remaining;
 
+  size = gtk_vector(get_size) (self);
   g_assert (pos + removed <= size);
+  remaining = size - pos - removed;
 
   gtk_vector(free_elements) (gtk_vector(index) (self, pos),
                              gtk_vector(index) (self, pos + removed));
 
   gtk_vector(reserve) (self, size - removed + added);
 
-  if (pos + removed < size && removed != added)
+  if (GTK_VECTOR_REAL_SIZE (remaining) && removed != added)
     memmove (gtk_vector(index) (self, pos + added),
              gtk_vector(index) (self, pos + removed),
-             (size - pos - removed) * sizeof (_T_));
+             GTK_VECTOR_REAL_SIZE (remaining) * sizeof (_T_));
 
   if (added)
     memcpy (gtk_vector(index) (self, pos),
@@ -210,10 +230,12 @@ gtk_vector(get) (GtkVector *self,
 #undef gtk_vector_paste_more
 #undef gtk_vector_paste
 #undef gtk_vector
+#undef GTK_VECTOR_REAL_SIZE
 
-#undef GTK_VECTOR_PREALLOC
 #undef GTK_VECTOR_ELEMENT_TYPE
 #undef GTK_VECTOR_NAME
 #undef GTK_VECTOR_TYPE_NAME
+#undef GTK_VECTOR_PREALLOC
+#undef GTK_VECTOR_NULL_TERMINATED
 
 #endif
diff --git a/testsuite/gtk/vector.c b/testsuite/gtk/vector.c
index 5f47c9572e..6d8e55a6b5 100644
--- a/testsuite/gtk/vector.c
+++ b/testsuite/gtk/vector.c
@@ -50,6 +50,34 @@ int_free_func (int data)
 #define GTK_VECTOR_FREE_FUNC int_free_func
 #include "vectorimpl.c"
 
+#define GTK_VECTOR_ELEMENT_TYPE int
+#define GTK_VECTOR_NAME null_int_vector
+#define GTK_VECTOR_TYPE_NAME NullIntVector
+#define GTK_VECTOR_NULL_TERMINATED 1
+#include "vectorimpl.c"
+
+#define GTK_VECTOR_ELEMENT_TYPE int
+#define GTK_VECTOR_NAME null_pre_int_vector
+#define GTK_VECTOR_TYPE_NAME NullPreIntVector
+#define GTK_VECTOR_PREALLOC 100
+#define GTK_VECTOR_NULL_TERMINATED 1
+#include "vectorimpl.c"
+
+#define GTK_VECTOR_ELEMENT_TYPE int
+#define GTK_VECTOR_NAME null_free_int_vector
+#define GTK_VECTOR_TYPE_NAME NullFreeIntVector
+#define GTK_VECTOR_FREE_FUNC int_free_func
+#define GTK_VECTOR_NULL_TERMINATED 1
+#include "vectorimpl.c"
+
+#define GTK_VECTOR_ELEMENT_TYPE int
+#define GTK_VECTOR_NAME null_pre_free_int_vector
+#define GTK_VECTOR_TYPE_NAME NullPreFreeIntVector
+#define GTK_VECTOR_PREALLOC 100
+#define GTK_VECTOR_FREE_FUNC int_free_func
+#define GTK_VECTOR_NULL_TERMINATED 1
+#include "vectorimpl.c"
+
 int
 main (int argc, char *argv[])
 {
@@ -59,11 +87,19 @@ main (int argc, char *argv[])
   g_test_add_func ("/intvector/simple", int_vector_test_simple);
   g_test_add_func ("/intvector/prealloc/simple", pre_int_vector_test_simple);
   g_test_add_func ("/intvector/freefunc/simple", free_int_vector_test_simple);
-  g_test_add_func ("/intvector/prealloc_freefunc_simple", pre_free_int_vector_test_simple);
+  g_test_add_func ("/intvector/prealloc/freefunc/simple", pre_free_int_vector_test_simple);
+  g_test_add_func ("/intvector/null/simple", null_int_vector_test_simple);
+  g_test_add_func ("/intvector/null/prealloc/simple", null_pre_int_vector_test_simple);
+  g_test_add_func ("/intvector/null/freefunc/simple", null_free_int_vector_test_simple);
+  g_test_add_func ("/intvector/null/prealloc/freefunc/simple", null_pre_free_int_vector_test_simple);
   g_test_add_func ("/intvector/splice", int_vector_test_splice);
   g_test_add_func ("/intvector/prealloc/splice", pre_int_vector_test_splice);
   g_test_add_func ("/intvector/freefunc/splice", free_int_vector_test_splice);
-  g_test_add_func ("/intvector/prealloc_freefunc_splice", pre_free_int_vector_test_splice);
+  g_test_add_func ("/intvector/prealloc/freefunc/splice", pre_free_int_vector_test_splice);
+  g_test_add_func ("/intvector/null/splice", null_int_vector_test_splice);
+  g_test_add_func ("/intvector/null/prealloc/splice", null_pre_int_vector_test_splice);
+  g_test_add_func ("/intvector/null/freefunc/splice", null_free_int_vector_test_splice);
+  g_test_add_func ("/intvector/null/prealloc/freefunc/splice", null_pre_free_int_vector_test_splice);
 
   return g_test_run ();
 }
diff --git a/testsuite/gtk/vectorimpl.c b/testsuite/gtk/vectorimpl.c
index 3b0c0f905a..c9d191e2aa 100644
--- a/testsuite/gtk/vectorimpl.c
+++ b/testsuite/gtk/vectorimpl.c
@@ -36,6 +36,9 @@ gtk_vector(test_simple) (void)
       g_assert_cmpint (gtk_vector(get_size) (&v), ==, i);
       g_assert_cmpint (gtk_vector(get_size) (&v), <=, gtk_vector(get_capacity) (&v));
       gtk_vector(append) (&v, i);
+#ifdef GTK_VECTOR_NULL_TERMINATED
+      g_assert_cmpint (*gtk_vector(index) (&v, gtk_vector(get_size) (&v)), ==, 0);
+#endif
     }
   g_assert_cmpint (gtk_vector(get_size) (&v), ==, i);
   g_assert_cmpint (gtk_vector(get_size) (&v), <=, gtk_vector(get_capacity) (&v));
@@ -84,6 +87,9 @@ gtk_vector(test_splice) (void)
 
       g_assert_cmpint (gtk_vector(get_size) (&v), ==, old_size + add - remove);
       g_assert_cmpint (gtk_vector(get_size) (&v), <=, gtk_vector(get_capacity) (&v));
+#ifdef GTK_VECTOR_NULL_TERMINATED
+      g_assert_cmpint (*gtk_vector(index) (&v, gtk_vector(get_size) (&v)), ==, 0);
+#endif
       for (j = 0; j < add; j++)
         g_assert_cmpint (gtk_vector(get) (&v, pos + j), ==, additions[j]);
     }
@@ -100,9 +106,11 @@ gtk_vector(test_splice) (void)
 #undef gtk_vector_paste_more
 #undef gtk_vector_paste
 #undef gtk_vector
+#undef GTK_VECTOR_REAL_SIZE
 
-#undef GTK_VECTOR_PREALLOC
 #undef GTK_VECTOR_ELEMENT_TYPE
 #undef GTK_VECTOR_NAME
 #undef GTK_VECTOR_TYPE_NAME
+#undef GTK_VECTOR_PREALLOC
+#undef GTK_VECTOR_NULL_TERMINATED
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]