1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017-2018 Intel Corporation
11 * File-backed shared indexed array for DPDK.
13 * Basic workflow is expected to be the following:
14 * 1) Allocate array either using ``rte_fbarray_init()`` or
15 * ``rte_fbarray_attach()`` (depending on whether it's shared between
16 * multiple DPDK processes)
17 * 2) find free spots using ``rte_fbarray_find_next_free()``
18 * 3) get pointer to data in the free spot using ``rte_fbarray_get()``, and
19 * copy data into the pointer (element size is fixed)
20 * 4) mark entry as used using ``rte_fbarray_set_used()``
22 * Calls to ``rte_fbarray_init()`` and ``rte_fbarray_destroy()`` will have
23 * consequences for all processes, while calls to ``rte_fbarray_attach()`` and
24 * ``rte_fbarray_detach()`` will only have consequences within a single process.
25 * Therefore, it is safe to call ``rte_fbarray_attach()`` or
26 * ``rte_fbarray_detach()`` while another process is using ``rte_fbarray``,
27 * provided no other thread within the same process will try to use
28 * ``rte_fbarray`` before attaching or after detaching. It is not safe to call
29 * ``rte_fbarray_init()`` or ``rte_fbarray_destroy()`` while another thread or
30 * another process is using ``rte_fbarray``.
40 #include <rte_compat.h>
41 #include <rte_rwlock.h>
43 #define RTE_FBARRAY_NAME_LEN 64
46 char name[RTE_FBARRAY_NAME_LEN]; /**< name associated with an array */
47 unsigned int count; /**< number of entries stored */
48 unsigned int len; /**< current length of the array */
49 unsigned int elt_sz; /**< size of each element */
50 void *data; /**< data pointer */
51 rte_rwlock_t rwlock; /**< multiprocess lock */
55 * Set up ``rte_fbarray`` structure and allocate underlying resources.
57 * Call this function to correctly set up ``rte_fbarray`` and allocate
58 * underlying files that will be backing the data in the current process. Note
59 * that in order to use and share ``rte_fbarray`` between multiple processes,
60 * data pointed to by ``arr`` pointer must itself be allocated in shared memory.
63 * Valid pointer to allocated ``rte_fbarray`` structure.
66 * Unique name to be assigned to this array.
69 * Number of elements initially available in the array.
72 * Size of each element.
76 * - -1 on failure, with ``rte_errno`` indicating reason for failure.
78 int __rte_experimental
79 rte_fbarray_init(struct rte_fbarray *arr, const char *name, unsigned int len,
84 * Attach to a file backing an already allocated and correctly set up
85 * ``rte_fbarray`` structure.
87 * Call this function to attach to file that will be backing the data in the
88 * current process. The structure must have been previously correctly set up
89 * with a call to ``rte_fbarray_init()``. Calls to ``rte_fbarray_attach()`` are
90 * usually meant to be performed in a multiprocessing scenario, with data
91 * pointed to by ``arr`` pointer allocated in shared memory.
94 * Valid pointer to allocated and correctly set up rte_fbarray structure.
98 * - -1 on failure, with ``rte_errno`` indicating reason for failure.
100 int __rte_experimental
101 rte_fbarray_attach(struct rte_fbarray *arr);
105 * Deallocate resources for an already allocated and correctly set up
106 * ``rte_fbarray`` structure, and remove the underlying file.
108 * Call this function to deallocate all resources associated with an
109 * ``rte_fbarray`` structure within the current process. This will also
110 * zero-fill data pointed to by ``arr`` pointer and remove the underlying file
111 * backing the data, so it is expected that by the time this function is called,
112 * all other processes have detached from this ``rte_fbarray``.
115 * Valid pointer to allocated and correctly set up ``rte_fbarray`` structure.
119 * - -1 on failure, with ``rte_errno`` indicating reason for failure.
121 int __rte_experimental
122 rte_fbarray_destroy(struct rte_fbarray *arr);
126 * Deallocate resources for an already allocated and correctly set up
127 * ``rte_fbarray`` structure.
129 * Call this function to deallocate all resources associated with an
130 * ``rte_fbarray`` structure within current process.
133 * Valid pointer to allocated and correctly set up ``rte_fbarray`` structure.
137 * - -1 on failure, with ``rte_errno`` indicating reason for failure.
139 int __rte_experimental
140 rte_fbarray_detach(struct rte_fbarray *arr);
144 * Get pointer to element residing at specified index.
147 * Valid pointer to allocated and correctly set up ``rte_fbarray`` structure.
150 * Index of an element to get a pointer to.
153 * - non-NULL pointer on success.
154 * - NULL on failure, with ``rte_errno`` indicating reason for failure.
156 void * __rte_experimental
157 rte_fbarray_get(const struct rte_fbarray *arr, unsigned int idx);
161 * Find index of a specified element within the array.
164 * Valid pointer to allocated and correctly set up ``rte_fbarray`` structure.
167 * Pointer to element to find index to.
170 * - non-negative integer on success.
171 * - -1 on failure, with ``rte_errno`` indicating reason for failure.
173 int __rte_experimental
174 rte_fbarray_find_idx(const struct rte_fbarray *arr, const void *elt);
178 * Mark specified element as used.
181 * Valid pointer to allocated and correctly set up ``rte_fbarray`` structure.
184 * Element index to mark as used.
188 * - -1 on failure, with ``rte_errno`` indicating reason for failure.
190 int __rte_experimental
191 rte_fbarray_set_used(struct rte_fbarray *arr, unsigned int idx);
195 * Mark specified element as free.
198 * Valid pointer to allocated and correctly set up ``rte_fbarray`` structure.
201 * Element index to mark as free.
205 * - -1 on failure, with ``rte_errno`` indicating reason for failure.
207 int __rte_experimental
208 rte_fbarray_set_free(struct rte_fbarray *arr, unsigned int idx);
212 * Check whether element at specified index is marked as used.
215 * Valid pointer to allocated and correctly set up ``rte_fbarray`` structure.
218 * Element index to check as used.
221 * - 1 if element is used.
222 * - 0 if element is unused.
223 * - -1 on failure, with ``rte_errno`` indicating reason for failure.
225 int __rte_experimental
226 rte_fbarray_is_used(struct rte_fbarray *arr, unsigned int idx);
230 * Find index of next free element, starting at specified index.
233 * Valid pointer to allocated and correctly set up ``rte_fbarray`` structure.
236 * Element index to start search from.
239 * - non-negative integer on success.
240 * - -1 on failure, with ``rte_errno`` indicating reason for failure.
242 int __rte_experimental
243 rte_fbarray_find_next_free(struct rte_fbarray *arr, unsigned int start);
247 * Find index of next used element, starting at specified index.
250 * Valid pointer to allocated and correctly set up ``rte_fbarray`` structure.
253 * Element index to start search from.
256 * - non-negative integer on success.
257 * - -1 on failure, with ``rte_errno`` indicating reason for failure.
259 int __rte_experimental
260 rte_fbarray_find_next_used(struct rte_fbarray *arr, unsigned int start);
264 * Find index of next chunk of ``n`` free elements, starting at specified index.
267 * Valid pointer to allocated and correctly set up ``rte_fbarray`` structure.
270 * Element index to start search from.
273 * Number of free elements to look for.
276 * - non-negative integer on success.
277 * - -1 on failure, with ``rte_errno`` indicating reason for failure.
279 int __rte_experimental
280 rte_fbarray_find_next_n_free(struct rte_fbarray *arr, unsigned int start,
285 * Find index of next chunk of ``n`` used elements, starting at specified index.
288 * Valid pointer to allocated and correctly set up ``rte_fbarray`` structure.
291 * Element index to start search from.
294 * Number of used elements to look for.
297 * - non-negative integer on success.
298 * - -1 on failure, with ``rte_errno`` indicating reason for failure.
300 int __rte_experimental
301 rte_fbarray_find_next_n_used(struct rte_fbarray *arr, unsigned int start,
306 * Find how many more free entries there are, starting at specified index.
309 * Valid pointer to allocated and correctly set up ``rte_fbarray`` structure.
312 * Element index to start search from.
315 * - non-negative integer on success.
316 * - -1 on failure, with ``rte_errno`` indicating reason for failure.
318 int __rte_experimental
319 rte_fbarray_find_contig_free(struct rte_fbarray *arr,
324 * Find how many more used entries there are, starting at specified index.
327 * Valid pointer to allocated and correctly set up ``rte_fbarray`` structure.
330 * Element index to start search from.
333 * - non-negative integer on success.
334 * - -1 on failure, with ``rte_errno`` indicating reason for failure.
336 int __rte_experimental
337 rte_fbarray_find_contig_used(struct rte_fbarray *arr, unsigned int start);
341 * Dump ``rte_fbarray`` metadata.
344 * Valid pointer to allocated and correctly set up ``rte_fbarray`` structure.
347 * File object to dump information into.
349 void __rte_experimental
350 rte_fbarray_dump_metadata(struct rte_fbarray *arr, FILE *f);
356 #endif /* RTE_FBARRAY_H */