From 9b5e106d113aafe9005ff24246ebb244e3fd0b80 Mon Sep 17 00:00:00 2001 From: David Herrmann Date: Sun, 11 Jan 2015 02:09:02 +0100 Subject: [PATCH] item: improve kdbus_item_set() Two improvements: - Return the next item as return value - Clear padding even if @data is given Signed-off-by: David Herrmann --- item.c | 16 +++++++++++++--- item.h | 5 +++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/item.c b/item.c index 8c32e03..6cc67b1 100644 --- 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 592711d..6364b5c 100644 --- 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 -- 2.34.1