Patchwork lib: fwts_list: make list creation and initialisation more optimal.

login
register
mail settings
Submitter Colin King
Date Dec. 10, 2012, 11:14 p.m.
Message ID <1355181252-16105-1-git-send-email-colin.king@canonical.com>
Download mbox | patch
Permalink /patch/205055/
State Accepted
Headers show

Comments

Colin King - Dec. 10, 2012, 11:14 p.m.
From: Colin Ian King <colin.king@canonical.com>

We can just memset() a list structure to initialise it rather than
zero'ing individual members.  Also, when creating a new list struct
we may as well just return the pointer from calloc() rather than
checking for NULL and then returning NULL if calloc() failed.

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 src/lib/src/fwts_list.c | 14 +++-----------
 1 file changed, 3 insertions(+), 11 deletions(-)
Keng-Yu Lin - Dec. 12, 2012, 7:53 a.m.
On Tue, Dec 11, 2012 at 7:14 AM, Colin King <colin.king@canonical.com> wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> We can just memset() a list structure to initialise it rather than
> zero'ing individual members.  Also, when creating a new list struct
> we may as well just return the pointer from calloc() rather than
> checking for NULL and then returning NULL if calloc() failed.
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  src/lib/src/fwts_list.c | 14 +++-----------
>  1 file changed, 3 insertions(+), 11 deletions(-)
>
> diff --git a/src/lib/src/fwts_list.c b/src/lib/src/fwts_list.c
> index 935910b..0f1e8a9 100644
> --- a/src/lib/src/fwts_list.c
> +++ b/src/lib/src/fwts_list.c
> @@ -29,9 +29,7 @@
>   */
>  void fwts_list_init(fwts_list *list)
>  {
> -       list->head = NULL;
> -       list->tail = NULL;
> -       list->len = 0;
> +       memset(list, 0, sizeof(fwts_list));
>  }
>
>  /*
> @@ -40,14 +38,8 @@ void fwts_list_init(fwts_list *list)
>   */
>  fwts_list *fwts_list_new(void)
>  {
> -       fwts_list *list;
> -
> -       if ((list = calloc(1, sizeof(fwts_list))) == NULL)
> -               return NULL;
> -
> -       fwts_list_init(list);
> -
> -       return list;
> +       /* calloc already zero's the list */
> +       return calloc(1, sizeof(fwts_list));
>  }
>
>  /*
> --
> 1.8.0
>
Acked-by: Keng-Yu Lin <kengyu@canonical.com>
Ivan Hu - Dec. 19, 2012, 5:51 a.m.
On 12/11/2012 07:14 AM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> We can just memset() a list structure to initialise it rather than
> zero'ing individual members.  Also, when creating a new list struct
> we may as well just return the pointer from calloc() rather than
> checking for NULL and then returning NULL if calloc() failed.
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>   src/lib/src/fwts_list.c | 14 +++-----------
>   1 file changed, 3 insertions(+), 11 deletions(-)
>
> diff --git a/src/lib/src/fwts_list.c b/src/lib/src/fwts_list.c
> index 935910b..0f1e8a9 100644
> --- a/src/lib/src/fwts_list.c
> +++ b/src/lib/src/fwts_list.c
> @@ -29,9 +29,7 @@
>    */
>   void fwts_list_init(fwts_list *list)
>   {
> -	list->head = NULL;
> -	list->tail = NULL;
> -	list->len = 0;
> +	memset(list, 0, sizeof(fwts_list));
>   }
>
>   /*
> @@ -40,14 +38,8 @@ void fwts_list_init(fwts_list *list)
>    */
>   fwts_list *fwts_list_new(void)
>   {
> -	fwts_list *list;
> -
> -	if ((list = calloc(1, sizeof(fwts_list))) == NULL)
> -		return NULL;
> -
> -	fwts_list_init(list);
> -
> -	return list;
> +	/* calloc already zero's the list */
> +	return calloc(1, sizeof(fwts_list));
>   }
>
>   /*
>
Acked-by: Ivan Hu <ivan.hu@canonical.com>

Patch

diff --git a/src/lib/src/fwts_list.c b/src/lib/src/fwts_list.c
index 935910b..0f1e8a9 100644
--- a/src/lib/src/fwts_list.c
+++ b/src/lib/src/fwts_list.c
@@ -29,9 +29,7 @@ 
  */
 void fwts_list_init(fwts_list *list)
 {
-	list->head = NULL;
-	list->tail = NULL;
-	list->len = 0;
+	memset(list, 0, sizeof(fwts_list));
 }
 
 /*
@@ -40,14 +38,8 @@  void fwts_list_init(fwts_list *list)
  */
 fwts_list *fwts_list_new(void)
 {
-	fwts_list *list;
-
-	if ((list = calloc(1, sizeof(fwts_list))) == NULL)
-		return NULL;
-
-	fwts_list_init(list);
-
-	return list;
+	/* calloc already zero's the list */
+	return calloc(1, sizeof(fwts_list));
 }
 
 /*