item: improve kdbus_item_set()
authorDavid Herrmann <dh.herrmann@gmail.com>
Sun, 11 Jan 2015 01:09:02 +0000 (02:09 +0100)
committerDavid Herrmann <dh.herrmann@gmail.com>
Sun, 11 Jan 2015 01:09:02 +0000 (02:09 +0100)
Two improvements:
 - Return the next item as return value
 - Clear padding even if @data is given

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
item.c
item.h

diff --git a/item.c b/item.c
index 8c32e03a36ac30d8cb0b916c4afa8ec710b7250a..6cc67b1566caaf6a6edc94601480b589a6ee4365 100644 (file)
--- a/item.c
+++ b/item.c
@@ -282,9 +282,17 @@ const char *kdbus_items_get_str(const struct kdbus_item *items,
  * @type:      The item type to set (KDBUS_ITEM_*)
  * @data:      Data to copy to item->data, may be %NULL
  * @len:       Number of bytes in @data
+ *
+ * This sets type, size and data fields of an item. If @data is NULL, the data
+ * memory is cleared.
+ *
+ * Note that you must align your @data memory to 8 bytes. Trailing padding (in
+ * case @len is not 8byte aligned) is cleared by this call.
+ *
+ * Returns: Pointer to the following item.
  */
-void kdbus_item_set(struct kdbus_item *item, u64 type,
-                   const void *data, size_t len)
+struct kdbus_item *kdbus_item_set(struct kdbus_item *item, u64 type,
+                                 const void *data, size_t len)
 {
        item->type = type;
        item->size = KDBUS_ITEM_HEADER_SIZE + len;
@@ -293,6 +301,8 @@ void kdbus_item_set(struct kdbus_item *item, u64 type,
                memcpy(item->data, data, len);
                memset(item->data + len, 0, KDBUS_ALIGN8(len) - len);
        } else {
-               memset(item->data, 0, len);
+               memset(item->data, 0, KDBUS_ALIGN8(len));
        }
+
+       return KDBUS_ITEM_NEXT(item);
 }
diff --git a/item.h b/item.h
index 592711d91a20668bf3205e170bff812ec4c7e547..6364b5c3be57693abfb9dee2e438148d4bee9dee 100644 (file)
--- a/item.h
+++ b/item.h
@@ -50,6 +50,7 @@ struct kdbus_item *kdbus_items_get(const struct kdbus_item *items,
 const char *kdbus_items_get_str(const struct kdbus_item *items,
                                size_t items_size,
                                unsigned int item_type);
-void kdbus_item_set(struct kdbus_item *item, u64 type,
-                   const void *data, size_t len);
+struct kdbus_item *kdbus_item_set(struct kdbus_item *item, u64 type,
+                                 const void *data, size_t len);
+
 #endif