tools/virtio/ringtest: Replace obsolete memalign() with posix_memalign()

Message ID 20230412072536.2029-1-wangdeming@inspur.com
State New
Headers
Series tools/virtio/ringtest: Replace obsolete memalign() with posix_memalign() |

Commit Message

Deming Wang April 12, 2023, 7:25 a.m. UTC
  memalign() is obsolete according to its manpage.

Replace memalign() with posix_memalign() and remove malloc.h include
that was there for memalign().

As a pointer is passed into posix_memalign(), initialize *p to NULL
to silence a warning about the function's return value being used as
uninitialized (which is not valid anyway because the error is properly
checked before p is returned).

Signed-off-by: Deming Wang <wangdeming@inspur.com>
---
 tools/virtio/ringtest/ptr_ring.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
  

Comments

Xuan Zhuo April 13, 2023, 7:50 a.m. UTC | #1
On Wed, 12 Apr 2023 03:25:36 -0400, Deming Wang <wangdeming@inspur.com> wrote:
> memalign() is obsolete according to its manpage.
>
> Replace memalign() with posix_memalign() and remove malloc.h include
> that was there for memalign().
>
> As a pointer is passed into posix_memalign(), initialize *p to NULL
> to silence a warning about the function's return value being used as
> uninitialized (which is not valid anyway because the error is properly
> checked before p is returned).
>
> Signed-off-by: Deming Wang <wangdeming@inspur.com>

LGTM

Reviewed-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>

> ---
>  tools/virtio/ringtest/ptr_ring.c | 9 ++++++---
>  1 file changed, 6 insertions(+), 3 deletions(-)
>
> diff --git a/tools/virtio/ringtest/ptr_ring.c b/tools/virtio/ringtest/ptr_ring.c
> index c9b26335f891..a0bf4978eace 100644
> --- a/tools/virtio/ringtest/ptr_ring.c
> +++ b/tools/virtio/ringtest/ptr_ring.c
> @@ -26,9 +26,12 @@ typedef int gfp_t;
>
>  static void *kmalloc(unsigned size, gfp_t gfp)
>  {
> -	void *p = memalign(64, size);
> -	if (!p)
> -		return p;
> +	void *p;
> +	int ret;
> +
> +	ret = posix_memalign(&p, 64, size);
> +	if (ret < 0)
> +		return NULL;
>
>  	if (gfp & __GFP_ZERO)
>  		memset(p, 0, size);
> --
> 2.27.0
>
  

Patch

diff --git a/tools/virtio/ringtest/ptr_ring.c b/tools/virtio/ringtest/ptr_ring.c
index c9b26335f891..a0bf4978eace 100644
--- a/tools/virtio/ringtest/ptr_ring.c
+++ b/tools/virtio/ringtest/ptr_ring.c
@@ -26,9 +26,12 @@  typedef int gfp_t;
 
 static void *kmalloc(unsigned size, gfp_t gfp)
 {
-	void *p = memalign(64, size);
-	if (!p)
-		return p;
+	void *p;
+	int ret;
+
+	ret = posix_memalign(&p, 64, size);
+	if (ret < 0)
+		return NULL;
 
 	if (gfp & __GFP_ZERO)
 		memset(p, 0, size);