[libdazzle] ring: avoid modulus in DzlRing



commit a604661076ec88cd06d73eb548b9974e1fd3d02f
Author: Christian Hergert <chergert redhat com>
Date:   Fri Jun 9 19:21:54 2017 -0700

    ring: avoid modulus in DzlRing
    
    We don't really need to support any range of index, we just need to support
    one range outside of the ring size.
    
    If we support multiple element insertion, we night need to change this to
    two range checks.

 src/util/dzl-ring.h |   20 +++++++++++++++++++-
 1 files changed, 19 insertions(+), 1 deletions(-)
---
diff --git a/src/util/dzl-ring.h b/src/util/dzl-ring.h
index a34e0c9..75fedf3 100644
--- a/src/util/dzl-ring.h
+++ b/src/util/dzl-ring.h
@@ -36,6 +36,22 @@ G_BEGIN_DECLS
 #define dzl_ring_append_val(ring, val) dzl_ring_append_vals(ring, &(val), 1)
 
 /**
+ * _dzl_ring_index: (skip)
+ *
+ * Used to convert an index to valid indx within the ring. We only support
+ * offsets within +/- one range of the length of the ring.
+ */
+#define _dzl_ring_index(ring, i)         \
+  ({                                     \
+    gint __idx = (gint)(ring->pos) + i;  \
+    if (__idx < 0)                       \
+      __idx += (gint)ring->len;          \
+    else if (__idx >= (gint)(ring)->len) \
+      __idx -= (gint)ring->len;          \
+    __idx;                               \
+  })
+
+/**
  * dzl_ring_get_index:
  * @ring: A #DzlRing.
  * @type: The type to extract.
@@ -53,13 +69,15 @@ G_BEGIN_DECLS
  * Returns: The value at the given index.
  */
 #define dzl_ring_get_index(ring, type, i) \
-  ((((type*)(ring)->data))[((i) + (ring)->pos) % (ring)->len])
+  ((((type *)(gpointer)(ring)->data))[_dzl_ring_index(ring, i)])
 
 typedef struct
 {
        guint8 *data;
        guint   len;
        guint   pos;
+
+  /*< private >*/
 } DzlRing;
 
 GType    dzl_ring_get_type    (void);


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