diff mbox series

[v4,1/3] tests/migration: mem leak fix

Message ID 95ceba2addaa3136db8ee233ea2e0ff9d13db7c0.1570208781.git.maozhongyi@cmss.chinamobile.com (mailing list archive)
State New, archived
Headers show
Series some fix in tests/migration | expand

Commit Message

Mao Zhongyi Oct. 4, 2019, 5:32 p.m. UTC
‘data’ has the possibility of memory leaks, so use the
glib macros g_autofree recommended by CODING_STYLE.rst
to automatically release the memory that returned from
g_malloc().

Signed-off-by: Mao Zhongyi <maozhongyi@cmss.chinamobile.com>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
---
 tests/migration/stress.c | 21 ++-------------------
 1 file changed, 2 insertions(+), 19 deletions(-)

Comments

Laurent Vivier Oct. 23, 2019, 9:45 a.m. UTC | #1
Le 04/10/2019 à 19:32, Mao Zhongyi a écrit :
> ‘data’ has the possibility of memory leaks, so use the
> glib macros g_autofree recommended by CODING_STYLE.rst
> to automatically release the memory that returned from
> g_malloc().
> 
> Signed-off-by: Mao Zhongyi <maozhongyi@cmss.chinamobile.com>
> Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
> ---
>  tests/migration/stress.c | 21 ++-------------------
>  1 file changed, 2 insertions(+), 19 deletions(-)
> 
> diff --git a/tests/migration/stress.c b/tests/migration/stress.c
> index d9aa4afe92..d8a6f64af0 100644
> --- a/tests/migration/stress.c
> +++ b/tests/migration/stress.c
> @@ -170,26 +170,14 @@ static unsigned long long now(void)
>  static int stressone(unsigned long long ramsizeMB)
>  {
>      size_t pagesPerMB = 1024 * 1024 / PAGE_SIZE;
> -    char *ram = malloc(ramsizeMB * 1024 * 1024);
> +    g_autofree char *ram = g_malloc(ramsizeMB * 1024 * 1024);
>      char *ramptr;
>      size_t i, j, k;
> -    char *data = malloc(PAGE_SIZE);
> +    g_autofree char *data = g_malloc(PAGE_SIZE);
>      char *dataptr;
>      size_t nMB = 0;
>      unsigned long long before, after;
>  
> -    if (!ram) {
> -        fprintf(stderr, "%s (%05d): ERROR: cannot allocate %llu MB of RAM: %s\n",
> -                argv0, gettid(), ramsizeMB, strerror(errno));
> -        return -1;
> -    }
> -    if (!data) {
> -        fprintf(stderr, "%s (%d): ERROR: cannot allocate %d bytes of RAM: %s\n",
> -                argv0, gettid(), PAGE_SIZE, strerror(errno));
> -        free(ram);
> -        return -1;
> -    }
> -
>      /* We don't care about initial state, but we do want
>       * to fault it all into RAM, otherwise the first iter
>       * of the loop below will be quite slow. We cna't use
> @@ -198,8 +186,6 @@ static int stressone(unsigned long long ramsizeMB)
>      memset(ram, 0xfe, ramsizeMB * 1024 * 1024);
>  
>      if (random_bytes(data, PAGE_SIZE) < 0) {
> -        free(ram);
> -        free(data);
>          return -1;
>      }
>  
> @@ -227,9 +213,6 @@ static int stressone(unsigned long long ramsizeMB)
>              }
>          }
>      }
> -
> -    free(data);
> -    free(ram);
>  }
>  
>  
> 

Reviewed-by: Laurent Vivier <laurent@vivier.eu>
diff mbox series

Patch

diff --git a/tests/migration/stress.c b/tests/migration/stress.c
index d9aa4afe92..d8a6f64af0 100644
--- a/tests/migration/stress.c
+++ b/tests/migration/stress.c
@@ -170,26 +170,14 @@  static unsigned long long now(void)
 static int stressone(unsigned long long ramsizeMB)
 {
     size_t pagesPerMB = 1024 * 1024 / PAGE_SIZE;
-    char *ram = malloc(ramsizeMB * 1024 * 1024);
+    g_autofree char *ram = g_malloc(ramsizeMB * 1024 * 1024);
     char *ramptr;
     size_t i, j, k;
-    char *data = malloc(PAGE_SIZE);
+    g_autofree char *data = g_malloc(PAGE_SIZE);
     char *dataptr;
     size_t nMB = 0;
     unsigned long long before, after;
 
-    if (!ram) {
-        fprintf(stderr, "%s (%05d): ERROR: cannot allocate %llu MB of RAM: %s\n",
-                argv0, gettid(), ramsizeMB, strerror(errno));
-        return -1;
-    }
-    if (!data) {
-        fprintf(stderr, "%s (%d): ERROR: cannot allocate %d bytes of RAM: %s\n",
-                argv0, gettid(), PAGE_SIZE, strerror(errno));
-        free(ram);
-        return -1;
-    }
-
     /* We don't care about initial state, but we do want
      * to fault it all into RAM, otherwise the first iter
      * of the loop below will be quite slow. We cna't use
@@ -198,8 +186,6 @@  static int stressone(unsigned long long ramsizeMB)
     memset(ram, 0xfe, ramsizeMB * 1024 * 1024);
 
     if (random_bytes(data, PAGE_SIZE) < 0) {
-        free(ram);
-        free(data);
         return -1;
     }
 
@@ -227,9 +213,6 @@  static int stressone(unsigned long long ramsizeMB)
             }
         }
     }
-
-    free(data);
-    free(ram);
 }