diff mbox

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

Message ID 1355181252-16105-1-git-send-email-colin.king@canonical.com
State Accepted
Headers show

Commit Message

Colin Ian King Dec. 10, 2012, 11:14 p.m. UTC
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(-)

Comments

Keng-Yu Lin Dec. 12, 2012, 7:53 a.m. UTC | #1
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. UTC | #2
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>
diff mbox

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));
 }
 
 /*