common/mlx5: wrap memory allocation on Linux

mlx5_malloc() API has an alignment parameter for system memory
allocations. malloc() is called for non-aligned allocations and
posix_memalign() is called for aligned allocations. When calling
mlx5_free() there is no distinction whether the memory was originally
allocated with or without alignment. Freeing a memory may be handled
differently by operating systems. Therefore this commit wraps these APIs
with OS specific calls: mlx5_os_malloc(), mlx5_os_free().

Signed-off-by: Ophir Munk <ophirmu@nvidia.com>
Acked-by: Matan Azrad <matan@nvidia.com>
This commit is contained in:
Ophir Munk 2020-12-28 11:54:17 +02:00 committed by Ferruh Yigit
parent 471da3682c
commit 7e7af4e99a
2 changed files with 44 additions and 8 deletions

View File

@ -6,6 +6,7 @@
#define RTE_PMD_MLX5_COMMON_OS_H_
#include <stdio.h>
#include <malloc.h>
#include <rte_pci.h>
#include <rte_debug.h>
@ -16,6 +17,7 @@
#include "mlx5_autoconf.h"
#include "mlx5_glue.h"
#include "mlx5_malloc.h"
/**
* Get device name. Given an ibv_device pointer - return a
@ -224,4 +226,40 @@ mlx5_os_umem_dereg(void *pumem)
{
return mlx5_glue->devx_umem_dereg(pumem);
}
/**
* Memory allocation optionally with alignment.
*
* @param[in] align
* Alignment size (may be zero)
* @param[in] size
* Size in bytes to allocate
*
* @return
* Valid pointer to allocated memory, NULL in case of failure
*/
static inline void *
mlx5_os_malloc(size_t align, size_t size)
{
void *buf;
if (posix_memalign(&buf, align, size))
return NULL;
return buf;
}
/**
* This API de-allocates a memory that originally could have been
* allocated aligned or non-aligned. In Linux it is a wrapper
* around free().
*
* @param[in] addr
* Pointer to address to free
*
*/
static inline void
mlx5_os_free(void *addr)
{
free(addr);
}
#endif /* RTE_PMD_MLX5_COMMON_OS_H_ */

View File

@ -9,6 +9,7 @@
#include <string.h>
#include "mlx5_common_utils.h"
#include "mlx5_common_os.h"
#include "mlx5_malloc.h"
struct mlx5_sys_mem {
@ -148,14 +149,11 @@ static void *
mlx5_alloc_align(size_t size, unsigned int align, unsigned int zero)
{
void *buf;
int ret;
ret = posix_memalign(&buf, align, size);
if (ret) {
DRV_LOG(ERR,
"Couldn't allocate buf size=%zu align=%u. Err=%d\n",
size, align, ret);
buf = mlx5_os_malloc(align, size);
if (!buf) {
DRV_LOG(ERR, "Couldn't allocate buf size=%zu align=%u.",
size, align);
return NULL;
}
if (zero)
@ -264,7 +262,7 @@ mlx5_free(void *addr)
__atomic_add_fetch(&mlx5_sys_mem.free_sys, 1,
__ATOMIC_RELAXED);
#endif
free(addr);
mlx5_os_free(addr);
} else {
#ifdef RTE_LIBRTE_MLX5_DEBUG
__atomic_add_fetch(&mlx5_sys_mem.free_rte, 1,