1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Copyright (C) 2021-2023 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <djwong@kernel.org>
6 #ifndef __XFS_SCRUB_XFARRAY_H__
7 #define __XFS_SCRUB_XFARRAY_H__
9 /* xfile array index type, along with cursor initialization */
10 typedef uint64_t xfarray_idx_t;
11 #define XFARRAY_CURSOR_INIT ((__force xfarray_idx_t)0)
13 /* Iterate each index of an xfile array. */
14 #define foreach_xfarray_idx(array, idx) \
15 for ((idx) = XFARRAY_CURSOR_INIT; \
16 (idx) < xfarray_length(array); \
20 /* Underlying file that backs the array. */
23 /* Number of array elements. */
26 /* Maximum possible array size. */
29 /* Number of unset slots in the array below @nr. */
32 /* Size of an array element. */
35 /* log2 of array element size, if possible. */
39 int xfarray_create(const char *descr, unsigned long long required_capacity,
40 size_t obj_size, struct xfarray **arrayp);
41 void xfarray_destroy(struct xfarray *array);
42 int xfarray_load(struct xfarray *array, xfarray_idx_t idx, void *ptr);
43 int xfarray_unset(struct xfarray *array, xfarray_idx_t idx);
44 int xfarray_store(struct xfarray *array, xfarray_idx_t idx, const void *ptr);
45 int xfarray_store_anywhere(struct xfarray *array, const void *ptr);
46 bool xfarray_element_is_null(struct xfarray *array, const void *ptr);
48 /* Append an element to the array. */
49 static inline int xfarray_append(struct xfarray *array, const void *ptr)
51 return xfarray_store(array, array->nr, ptr);
54 uint64_t xfarray_length(struct xfarray *array);
55 int xfarray_load_next(struct xfarray *array, xfarray_idx_t *idx, void *rec);
57 /* Declarations for xfile array sort functionality. */
59 typedef cmp_func_t xfarray_cmp_fn;
61 struct xfarray_sortinfo {
62 struct xfarray *array;
64 /* Comparison function for the sort. */
65 xfarray_cmp_fn cmp_fn;
67 /* Maximum height of the partition stack. */
68 uint8_t max_stack_depth;
70 /* Current height of the partition stack. */
73 /* Maximum stack depth ever used. */
74 uint8_t max_stack_used;
76 /* XFARRAY_SORT_* flags; see below. */
80 /* Performance statistics. */
87 * Extra bytes are allocated beyond the end of the structure to store
88 * quicksort information. C does not permit multiple VLAs per struct,
89 * so we document all of this in a comment.
91 * Pretend that we have a typedef for array records:
93 * typedef char[array->obj_size] xfarray_rec_t;
95 * First comes the quicksort partition stack:
97 * xfarray_idx_t lo[max_stack_depth];
98 * xfarray_idx_t hi[max_stack_depth];
102 * If for a given subset we decide to use an insertion sort, we use the
103 * scratchpad record after the xfarray and a second scratchpad record
104 * here to compare items:
106 * xfarray_rec_t scratch;
108 * Otherwise, we want to partition the records to partition the array.
109 * We store the chosen pivot record here and use the xfarray scratchpad
110 * to rearrange the array around the pivot:
112 * xfarray_rec_t pivot;
118 /* Sort can be interrupted by a fatal signal. */
119 #define XFARRAY_SORT_KILLABLE (1U << 0)
121 int xfarray_sort(struct xfarray *array, xfarray_cmp_fn cmp_fn,
124 #endif /* __XFS_SCRUB_XFARRAY_H__ */