[gtk/wip/chergert/glproto: 460/493] add definable inline array




commit 348c9149607c9e0fb00f0c8f5229f8bccb12f7d8
Author: Christian Hergert <chergert redhat com>
Date:   Mon Feb 15 17:15:57 2021 -0800

    add definable inline array
    
    this is useful because it can 1) remove an extra dereference, 2) by type safe,
    3) avoid extraneous function calls across libraries, 4, allow batched appends
    without copying data.

 gsk/next/inlinearray.h | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 77 insertions(+)
---
diff --git a/gsk/next/inlinearray.h b/gsk/next/inlinearray.h
new file mode 100644
index 0000000000..3495846222
--- /dev/null
+++ b/gsk/next/inlinearray.h
@@ -0,0 +1,77 @@
+#ifndef __INLINE_ARRAY_H__
+#define __INLINE_ARRAY_H__
+
+#define DEFINE_INLINE_ARRAY(Type, prefix)                           \
+  typedef struct _##Type##Array {                                   \
+    gsize len;                                                      \
+    gsize allocated;                                                \
+    Type *elements;                                                 \
+  } Type##Array;                                                    \
+                                                                    \
+  static inline void                                                \
+  prefix##_init (Type##Array *ar,                                   \
+                 gsize        initial_size)                         \
+  {                                                                 \
+    ar->len = 0;                                                    \
+    ar->allocated = initial_size ? initial_size : 16;               \
+    ar->elements = g_new0 (Type, ar->allocated);                    \
+  }                                                                 \
+                                                                    \
+  static inline void                                                \
+  prefix##_clear (Type##Array *ar)                                  \
+  {                                                                 \
+    ar->len = 0;                                                    \
+    ar->allocated = 0;                                              \
+    g_clear_pointer (&ar->elements, g_free);                        \
+  }                                                                 \
+                                                                    \
+  static inline Type *                                              \
+  prefix##_head (Type##Array *ar)                                   \
+  {                                                                 \
+    return &ar->elements[0];                                        \
+  }                                                                 \
+                                                                    \
+  static inline Type *                                              \
+  prefix##_tail (Type##Array *ar)                                   \
+  {                                                                 \
+    return &ar->elements[ar->len-1];                                \
+  }                                                                 \
+                                                                    \
+  static inline Type *                                              \
+  prefix##_append (Type##Array *ar)                                 \
+  {                                                                 \
+    if G_UNLIKELY (ar->len == ar->allocated)                        \
+      {                                                             \
+        ar->allocated *= 2;                                         \
+        ar->elements = g_renew (Type, ar->elements, ar->allocated); \
+      }                                                             \
+                                                                    \
+    ar->len++;                                                      \
+                                                                    \
+    return prefix##_tail (ar);                                      \
+  }                                                                 \
+                                                                    \
+  static inline Type *                                              \
+  prefix##_append_n (Type##Array *ar,                               \
+                     gsize        n)                                \
+  {                                                                 \
+    if G_UNLIKELY ((ar->len + n) > ar->allocated)                   \
+      {                                                             \
+        while ((ar->len + n) > ar->allocated)                       \
+          ar->allocated *= 2;                                       \
+        ar->elements = g_renew (Type, ar->elements, ar->allocated); \
+      }                                                             \
+                                                                    \
+    ar->len += n;                                                   \
+                                                                    \
+    return &ar->elements[ar->len-n];                                \
+  }                                                                 \
+                                                                    \
+  static inline gsize                                               \
+  prefix##_index_of (Type##Array *ar,                               \
+                     const Type  *element)                          \
+  {                                                                 \
+    return element - &ar->elements[0];                              \
+  }
+
+#endif /* __INLINE_ARRAY_H__ */


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