Patchwork lib: fwts_list: optimize list functions a little, make small helper funcs inline

login
register
mail settings
Submitter Colin King
Date Jan. 10, 2013, 10:07 a.m.
Message ID <1357812423-16476-1-git-send-email-colin.king@canonical.com>
Download mbox | patch
Permalink /patch/210966/
State Accepted
Headers show

Comments

Colin King - Jan. 10, 2013, 10:07 a.m.
From: Colin Ian King <colin.king@canonical.com>

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 <colin.king@canonical.com>
---
 src/lib/include/fwts_list.h | 31 +++++++++++++++++++++++---
 src/lib/src/fwts_list.c     | 54 ++++++++++-----------------------------------
 2 files changed, 40 insertions(+), 45 deletions(-)
Keng-Yu Lin - Jan. 29, 2013, 5:28 a.m.
On Thu, Jan 10, 2013 at 6:07 PM, Colin King <colin.king@canonical.com> wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> 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 <colin.king@canonical.com>
> ---
>  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;
> --
> 1.8.0
>
Acked-by: Keng-Yu Lin <kengyu@canonical.com>
Ivan Hu - Jan. 31, 2013, 8:48 a.m.
On 01/10/2013 06:07 PM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> 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 <colin.king@canonical.com>
> ---
>   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;
>
Acked-by: Ivan Hu <ivan.hu@canonical.com>

Patch

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;