[waffle] [PATCH 13/17] core: Add func wcore_attrib_list_copy() (v2)

Chad Versace chad.versace at intel.com
Sun Jan 4 14:03:06 PST 2015


This is useful for making a writable copy of a read-only attribute list.

Signed-off-by: Chad Versace <chad.versace at intel.com>
---
 src/waffle/core/wcore_attrib_list.c | 37 +++++++++++++++++++++++++++++++++++++
 src/waffle/core/wcore_attrib_list.h |  3 +++
 2 files changed, 40 insertions(+)

v2:
    - Correctly handle malloc failure. [emil]

diff --git a/src/waffle/core/wcore_attrib_list.c b/src/waffle/core/wcore_attrib_list.c
index 0c447c0..6446ca0 100644
--- a/src/waffle/core/wcore_attrib_list.c
+++ b/src/waffle/core/wcore_attrib_list.c
@@ -28,6 +28,7 @@
 #include <stdbool.h>
 #include <stdint.h>
 #include <stddef.h>
+#include <string.h>
 
 #include "wcore_error.h"
 #include "wcore_util.h"
@@ -167,3 +168,39 @@ wcore_attrib_list_from_int32(const int32_t attrib_list32[])
 
     return attrib_list;
 }
+
+intptr_t*
+wcore_attrib_list_copy(const intptr_t attrib_list[])
+{
+    intptr_t *copy = NULL;
+
+    if (attrib_list) {
+        size_t len;
+        size_t size = 0;
+
+        len = wcore_attrib_list_length(attrib_list);
+
+        if (!wcore_attrib_list_get_size(&size, len)) {
+            // Arithmetic overflow occurred, therefore we can't allocate the
+            // memory.
+            wcore_error(WAFFLE_ERROR_BAD_ALLOC);
+            return NULL;
+        }
+
+        copy = wcore_malloc(size);
+        if (!copy) {
+            return NULL;
+        }
+
+        memcpy(copy, attrib_list, size);
+    } else {
+        copy = wcore_malloc(sizeof(intptr_t));
+        if (!copy) {
+            return NULL;
+        }
+
+        copy[0] = 0;
+    }
+
+    return copy;
+}
diff --git a/src/waffle/core/wcore_attrib_list.h b/src/waffle/core/wcore_attrib_list.h
index 7a6aa2b..d4a771b 100644
--- a/src/waffle/core/wcore_attrib_list.h
+++ b/src/waffle/core/wcore_attrib_list.h
@@ -37,6 +37,9 @@ wcore_attrib_list_from_int32(const int32_t attrib_list[]);
 size_t
 wcore_attrib_list_length(const intptr_t attrib_list[]);
 
+intptr_t*
+wcore_attrib_list_copy(const intptr_t attrib_list[]);
+
 bool
 wcore_attrib_list_get(
         const intptr_t *attrib_list,
-- 
2.2.0



More information about the waffle mailing list