[waffle] [PATCH 05/17] core: Define wcore_attrib_list funcs with type-generic template

Chad Versace chad.versace at intel.com
Sun Jan 4 14:02:58 PST 2015


The current wcore_attrib_list functions work on int32_t attribute lists.
A future commit will add variants that work on intptr_t attribute lists.
To prevent code duplication, this patch moves the definition of all
wcore_attrib_list32 functions into a type-generic macro,
WCORE_ATTRIB_LIST_COMMON_FUNCS, which will also be used to define the
upcoming intptr_t functions.

Signed-off-by: Chad Versace <chad.versace at intel.com>
---
 src/waffle/core/wcore_attrib_list.c | 152 +++++++++++++++++++-----------------
 1 file changed, 82 insertions(+), 70 deletions(-)

diff --git a/src/waffle/core/wcore_attrib_list.c b/src/waffle/core/wcore_attrib_list.c
index b80f5cf..f8c2e6d 100644
--- a/src/waffle/core/wcore_attrib_list.c
+++ b/src/waffle/core/wcore_attrib_list.c
@@ -29,75 +29,87 @@
 #include <stdint.h>
 #include <stddef.h>
 
-size_t
-wcore_attrib_list32_length(const int32_t attrib_list[])
-{
-    const int32_t *i = attrib_list;
-
-    if (attrib_list == NULL)
-        return 0;
-
-    while (*i != 0)
-        i += 2;
-
-    return (i - attrib_list) / 2;
-}
-
-bool
-wcore_attrib_list32_get(
-        const int32_t *attrib_list,
-        int32_t key,
-        int32_t *value)
-{
-    if (attrib_list == NULL)
-        return false;
-
-    for (int i = 0; attrib_list[i] != 0; i += 2) {
-        if (attrib_list[i] != key)
-            continue;
-
-        *value = attrib_list[i + 1];
-        return true;
+#define WCORE_ATTRIB_LIST_COMMON_FUNCS(T,                               \
+                                       length_func,                     \
+                                       get_func,                        \
+                                       get_with_default_func,           \
+                                       update_func)                     \
+                                                                        \
+    size_t                                                              \
+    length_func(const T attrib_list[])                                  \
+    {                                                                   \
+        const T *i = attrib_list;                                       \
+                                                                        \
+        if (!attrib_list) {                                             \
+            return 0;                                                   \
+        }                                                               \
+                                                                        \
+        while (*i) {                                                    \
+            i += 2;                                                     \
+        }                                                               \
+                                                                        \
+        return (i - attrib_list) / 2;                                   \
+    }                                                                   \
+                                                                        \
+    bool                                                                \
+    get_func(const T *attrib_list,                                      \
+             T key,                                                     \
+             T *value)                                                  \
+    {                                                                   \
+        if (!attrib_list) {                                             \
+            return false;                                               \
+        }                                                               \
+                                                                        \
+        for (size_t i = 0; attrib_list[i] != 0; i += 2) {               \
+            if (attrib_list[i] == key) {                                \
+                *value = attrib_list[i + 1];                            \
+                return true;                                            \
+            }                                                           \
+        }                                                               \
+                                                                        \
+        return false;                                                   \
+    }                                                                   \
+                                                                        \
+    bool                                                                \
+    get_with_default_func(                                              \
+            const T attrib_list[],                                      \
+            T key,                                                      \
+            T *value,                                                   \
+            T default_value)                                            \
+    {                                                                   \
+        if (get_func(attrib_list, key, value)) {                        \
+            return true;                                                \
+        } else {                                                        \
+            *value = default_value;                                     \
+            return false;                                               \
+        }                                                               \
+    }                                                                   \
+                                                                        \
+    bool                                                                \
+    update_func(T *attrib_list,                                         \
+                T key,                                                  \
+                T value)                                                \
+    {                                                                   \
+        T *i = attrib_list;                                             \
+                                                                        \
+        if (attrib_list == NULL) {                                      \
+            return false;                                               \
+        }                                                               \
+                                                                        \
+        while (*i != 0 && *i != key) {                                  \
+            i += 2;                                                     \
+        }                                                               \
+                                                                        \
+        if (*i == key) {                                                \
+            i[1] = value;                                               \
+            return true;                                                \
+        } else {                                                        \
+            return false;                                               \
+        }                                                               \
     }
 
-    return false;
-}
-
-bool
-wcore_attrib_list32_get_with_default(
-        const int32_t attrib_list[],
-        int32_t key,
-        int32_t *value,
-        int32_t default_value)
-{
-    if (wcore_attrib_list32_get(attrib_list, key, value)) {
-        return true;
-    }
-    else {
-        *value = default_value;
-        return false;
-    }
-}
-
-bool
-wcore_attrib_list32_update(
-        int32_t *attrib_list,
-        int32_t key,
-        int32_t value)
-{
-    int32_t *i = attrib_list;
-
-    if (attrib_list == NULL)
-        return false;
-
-    while (*i != 0 && *i != key)
-        i += 2;
-
-    if (*i == key) {
-        i[1] = value;
-        return true;
-    }
-    else {
-        return false;
-    }
-}
+WCORE_ATTRIB_LIST_COMMON_FUNCS(int32_t,
+                               wcore_attrib_list32_length,
+                               wcore_attrib_list32_get,
+                               wcore_attrib_list32_get_with_default,
+                               wcore_attrib_list32_update)
-- 
2.2.0



More information about the waffle mailing list