From patchwork Thu Jan 10 10:07:03 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Colin Ian King X-Patchwork-Id: 210966 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from chlorine.canonical.com (chlorine.canonical.com [91.189.94.204]) by ozlabs.org (Postfix) with ESMTP id 7FB322C0330 for ; Thu, 10 Jan 2013 21:07:07 +1100 (EST) Received: from localhost ([127.0.0.1] helo=chlorine.canonical.com) by chlorine.canonical.com with esmtp (Exim 4.71) (envelope-from ) id 1TtF2P-0000Cf-GQ; Thu, 10 Jan 2013 10:07:05 +0000 Received: from youngberry.canonical.com ([91.189.89.112]) by chlorine.canonical.com with esmtp (Exim 4.71) (envelope-from ) id 1TtF2N-0000AX-RS for fwts-devel@lists.ubuntu.com; Thu, 10 Jan 2013 10:07:03 +0000 Received: from cpc3-craw6-2-0-cust180.croy.cable.virginmedia.com ([77.100.248.181] helo=localhost) by youngberry.canonical.com with esmtpsa (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1TtF2N-0003H0-Ne for fwts-devel@lists.ubuntu.com; Thu, 10 Jan 2013 10:07:03 +0000 From: Colin King To: fwts-devel@lists.ubuntu.com Subject: [PATCH] lib: fwts_list: optimize list functions a little, make small helper funcs inline Date: Thu, 10 Jan 2013 10:07:03 +0000 Message-Id: <1357812423-16476-1-git-send-email-colin.king@canonical.com> X-Mailer: git-send-email 1.8.0 X-BeenThere: fwts-devel@lists.ubuntu.com X-Mailman-Version: 2.1.13 Precedence: list List-Id: Firmware Test Suite Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: fwts-devel-bounces@lists.ubuntu.com Errors-To: fwts-devel-bounces@lists.ubuntu.com From: Colin Ian King Make fwts_list_init(), fwts_list_new(), fwts_list_len() inline'd as they are just one-liner functions. Make some minor code optimisations. Signed-off-by: Colin Ian King Acked-by: Keng-Yu Lin Acked-by: Ivan Hu --- src/lib/include/fwts_list.h | 31 +++++++++++++++++++++++--- src/lib/src/fwts_list.c | 54 ++++++++++----------------------------------- 2 files changed, 40 insertions(+), 45 deletions(-) diff --git a/src/lib/include/fwts_list.h b/src/lib/include/fwts_list.h index ec0c826..454eddc 100644 --- a/src/lib/include/fwts_list.h +++ b/src/lib/include/fwts_list.h @@ -62,13 +62,38 @@ typedef int (fwts_list_compare)(void *data1, void *data2); #define fwts_list_null(list) \ ((list) == NULL) -void fwts_list_init(fwts_list *list); -fwts_list *fwts_list_new(void); -int fwts_list_len(fwts_list *list); void fwts_list_free_items(fwts_list *list, fwts_list_link_free data_free); void fwts_list_free(fwts_list *list, fwts_list_link_free data_free); void fwts_list_iterate(fwts_list *list, fwts_list_foreach_callback callback, void *private); fwts_list_link *fwts_list_append(fwts_list *list, void *data); fwts_list_link *fwts_list_add_ordered(fwts_list *list, void *new_data, fwts_list_compare compare); +/* + * fwts_list_init() + * initialize a list header + */ +static inline void fwts_list_init(fwts_list *list) +{ + memset(list, 0, sizeof(fwts_list)); +} + +/* + * fwts_list_new() + * allocate and initialise a list header, return NULL if failed + */ +static inline fwts_list *fwts_list_new(void) +{ + /* calloc already zero's the list */ + return calloc(1, sizeof(fwts_list)); +} + +/* + * fwts_list_len() + * return list length, return 0 if list is NULL + */ +static inline int fwts_list_len(fwts_list *list) +{ + return list ? list->len : 0; +} + #endif diff --git a/src/lib/src/fwts_list.c b/src/lib/src/fwts_list.c index c6958cf..2b4c81a 100644 --- a/src/lib/src/fwts_list.c +++ b/src/lib/src/fwts_list.c @@ -24,34 +24,6 @@ #include "fwts.h" /* - * fwts_list_init() - * initialize a list header - */ -void fwts_list_init(fwts_list *list) -{ - memset(list, 0, sizeof(fwts_list)); -} - -/* - * fwts_list_new() - * allocate and initialise a list header, return NULL if failed - */ -fwts_list *fwts_list_new(void) -{ - /* calloc already zero's the list */ - return calloc(1, sizeof(fwts_list)); -} - -/* - * fwts_list_len() - * return list length, return 0 if list is NULL - */ -int fwts_list_len(fwts_list *list) -{ - return list ? list->len : 0; -} - -/* * fwts_list_iterate() * iterate over items in list, call callback function to operate on each * item, and pass private data over to callback. private may be NULL if not used @@ -60,7 +32,7 @@ void fwts_list_iterate(fwts_list *list, fwts_list_foreach_callback callback, voi { fwts_list_link *item; - if (list == NULL) + if (!list) return; fwts_list_foreach(item, list) @@ -78,12 +50,12 @@ void fwts_list_free_items(fwts_list *list, fwts_list_link_free data_free) fwts_list_link *item; fwts_list_link *next; - if (list == NULL) + if (!list) return; - for (item = list->head; item != NULL; item = next) { + for (item = list->head; item; item = next) { next = item->next; - if ((item->data != NULL) && (data_free != NULL)) + if (item->data && data_free) data_free(item->data); free(item); } @@ -110,7 +82,7 @@ fwts_list_link *fwts_list_append(fwts_list *list, void *data) { fwts_list_link *link; - if (list == NULL) + if (!list) return NULL; if ((link = calloc(1,sizeof(fwts_list_link))) == NULL) @@ -118,13 +90,12 @@ fwts_list_link *fwts_list_append(fwts_list *list, void *data) link->data = data; - if (list->head == NULL) { - list->head = link; - list->tail = link; - } else { + if (list->head) list->tail->next = link; - list->tail = link; - } + else + list->head = link; + + list->tail = link; list->len++; return link; @@ -144,18 +115,17 @@ fwts_list_link *fwts_list_add_ordered(fwts_list *list, void *new_data, fwts_list new_list_item->data = new_data; - for (list_item = &list->head; *list_item != NULL; list_item = &(*list_item)->next) { + for (list_item = &list->head; *list_item; list_item = &(*list_item)->next) { void *data = (void *)(*list_item)->data; if (compare(data, new_data) >= 0) { new_list_item->next = (*list_item); break; } } - if (new_list_item->next == NULL) + if (!new_list_item->next) list->tail = new_list_item; *list_item = new_list_item; - list->len++; return new_list_item;