Before this commit system call memalign was used for aligned
allocations, however memalign is deprecated.
Based on (1) - POSIX requires that memory aligned allocations can be
freed using free. Some systems provide no way to reclaim memory
allocated with memalign (because one can only pass to free a pointer
gotten from malloc, while, memalign would call malloc and then align the
obtained value).
Another issue is that 64/32 bits architectures use a minimal alignment
size. So any requested alignment below the minimal system size can be
simplified by calling malloc.
The glibc implementation allows memory obtained from posix_memalign to
be reclaimed with free. This commit replaces system call memalign with
system call posix_memalign. It also calls malloc in case the requested
alignment is below the minimal system size.
(1) https://linux.die.net/man/3/memalign
Fixes:
d38e3d526657 ("common/mlx5: add memory management functions")
Cc: stable@dpdk.org
Signed-off-by: Ophir Munk <ophirmu@nvidia.com>
Acked-by: Matan Azrad <matan@mellanox.com>
mlx5_alloc_align(size_t size, unsigned int align, unsigned int zero)
{
void *buf;
- buf = memalign(align, size);
- if (!buf) {
- DRV_LOG(ERR, "Couldn't allocate buf.\n");
+ 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);
+
return NULL;
}
if (zero)
return addr;
}
/* The memory will be allocated from system. */
- if (align)
+ if (align > MLX5_MALLOC_ALIGNMENT)
addr = mlx5_alloc_align(size, align, !!(flags & MLX5_MEM_ZERO));
else if (flags & MLX5_MEM_ZERO)
addr = calloc(1, size);
extern "C" {
#endif
+#ifndef MLX5_MALLOC_ALIGNMENT
+#ifndef RTE_ARCH_64
+#define MLX5_MALLOC_ALIGNMENT 8
+#else
+#define MLX5_MALLOC_ALIGNMENT 16
+#endif
+#endif
+
enum mlx5_mem_flags {
MLX5_MEM_ANY = 0,
/* Memory will be allocated dpends on sys_mem_en. */