1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2017 6WIND S.A.
3 * Copyright 2017 Mellanox Technologies, Ltd
8 * Utility functions used by the mlx4 driver.
16 #include <rte_errno.h>
17 #include <rte_malloc.h>
18 #include <rte_memory.h>
20 #include "mlx4_utils.h"
23 * Make a file descriptor non-blocking.
26 * File descriptor to alter.
29 * 0 on success, negative errno value otherwise and rte_errno is set.
32 mlx4_fd_set_non_blocking(int fd)
34 int ret = fcntl(fd, F_GETFL);
36 if (ret != -1 && !fcntl(fd, F_SETFL, ret | O_NONBLOCK))
44 * Internal helper to allocate memory once for several disparate objects.
46 * The most restrictive alignment constraint for standard objects is assumed
47 * to be sizeof(double) and is used as a default value.
49 * C11 code would include stdalign.h and use alignof(max_align_t) however
50 * we'll stick with C99 for the time being.
53 mlx4_mallocv_inline(const char *type, const struct mlx4_malloc_vec *vec,
54 unsigned int cnt, int zero, int socket)
60 int fill = !vec[0].addr;
65 for (i = 0; i < cnt; ++i) {
66 size_t align = (uintptr_t)vec[i].align;
69 align = sizeof(double);
70 } else if (!rte_is_power_of_2(align)) {
76 align = RTE_ALIGN_CEIL(size, align);
77 size = align + vec[i].size;
78 if (fill && vec[i].addr)
79 *vec[i].addr = data + align;
84 data = rte_malloc_socket(type, size, least, socket);
86 data = rte_zmalloc_socket(type, size, least, socket);
93 for (i = 0; i != cnt; ++i)
100 * Allocate memory once for several disparate objects.
102 * This function adds iovec-like semantics (e.g. readv()) to rte_malloc().
103 * Memory is allocated once for several contiguous objects of nonuniform
104 * sizes and alignment constraints.
106 * Each entry of @p vec describes the size, alignment constraint and
107 * provides a buffer address where the resulting object pointer must be
110 * The buffer of the first entry is guaranteed to point to the beginning of
111 * the allocated region and is safe to use with rte_free().
113 * NULL buffers are silently ignored.
115 * Providing a NULL buffer in the first entry prevents this function from
116 * allocating any memory but has otherwise no effect on its behavior. In
117 * this case, the contents of remaining non-NULL buffers are updated with
118 * addresses relative to zero (i.e. offsets that would have been used during
122 * A string identifying the type of allocated objects (useful for debug
123 * purposes, such as identifying the cause of a memory leak). Can be NULL.
124 * @param[in, out] vec
125 * Description of objects to allocate memory for.
127 * Number of entries in @p vec.
130 * Size in bytes of the allocated region including any padding. In case of
131 * error, rte_errno is set, 0 is returned and NULL is stored in the
132 * non-NULL buffers pointed by @p vec.
134 * @see struct mlx4_malloc_vec
138 mlx4_mallocv(const char *type, const struct mlx4_malloc_vec *vec,
141 return mlx4_mallocv_inline(type, vec, cnt, 0, SOCKET_ID_ANY);
145 * Combines the semantics of mlx4_mallocv() with those of rte_zmalloc().
147 * @see mlx4_mallocv()
151 mlx4_zmallocv(const char *type, const struct mlx4_malloc_vec *vec,
154 return mlx4_mallocv_inline(type, vec, cnt, 1, SOCKET_ID_ANY);
158 * Socket-aware version of mlx4_mallocv().
160 * This function takes one additional parameter.
163 * NUMA socket to allocate memory on. If SOCKET_ID_ANY is used, this
164 * function will behave the same as mlx4_mallocv().
166 * @see mlx4_mallocv()
167 * @see rte_malloc_socket()
170 mlx4_mallocv_socket(const char *type, const struct mlx4_malloc_vec *vec,
171 unsigned int cnt, int socket)
173 return mlx4_mallocv_inline(type, vec, cnt, 0, socket);
177 * Combines the semantics of mlx4_mallocv_socket() with those of
178 * mlx4_zmalloc_socket().
180 * @see mlx4_mallocv_socket()
181 * @see rte_zmalloc_socket()
184 mlx4_zmallocv_socket(const char *type, const struct mlx4_malloc_vec *vec,
185 unsigned int cnt, int socket)
187 return mlx4_mallocv_inline(type, vec, cnt, 1, socket);