Lines Matching full:array
19 * This memory array uses an xfile (which itself is a memfd "file") to store
22 * because we don't have to pin so much memory. However, array access is less
23 * direct than would be in a regular memory array. Access to the array is
25 * provided for convenience. Array elements can be unset, which sets them to
34 * buffer array items when we need space to store values temporarily.
36 static inline void *xfarray_scratch(struct xfarray *array) in xfarray_scratch() argument
38 return (array + 1); in xfarray_scratch()
41 /* Compute array index given an xfile offset. */
44 struct xfarray *array, in xfarray_idx() argument
47 if (array->obj_size_log >= 0) in xfarray_idx()
48 return (xfarray_idx_t)pos >> array->obj_size_log; in xfarray_idx()
50 return div_u64((xfarray_idx_t)pos, array->obj_size); in xfarray_idx()
53 /* Compute xfile offset of array element. */
54 static inline loff_t xfarray_pos(struct xfarray *array, xfarray_idx_t idx) in xfarray_pos() argument
56 if (array->obj_size_log >= 0) in xfarray_pos()
57 return idx << array->obj_size_log; in xfarray_pos()
59 return idx * array->obj_size; in xfarray_pos()
63 * Initialize a big memory array. Array records cannot be larger than a
64 * page, and the array cannot span more bytes than the page cache supports.
65 * If @required_capacity is nonzero, the maximum array size will be set to this
66 * quantity and the array creation will fail if the underlying storage cannot
76 struct xfarray *array; in xfarray_create() local
87 array = kzalloc(sizeof(struct xfarray) + obj_size, XCHK_GFP_FLAGS); in xfarray_create()
88 if (!array) in xfarray_create()
91 array->xfile = xfile; in xfarray_create()
92 array->obj_size = obj_size; in xfarray_create()
95 array->obj_size_log = ilog2(obj_size); in xfarray_create()
97 array->obj_size_log = -1; in xfarray_create()
99 array->max_nr = xfarray_idx(array, MAX_LFS_FILESIZE); in xfarray_create()
100 trace_xfarray_create(array, required_capacity); in xfarray_create()
103 if (array->max_nr < required_capacity) { in xfarray_create()
107 array->max_nr = required_capacity; in xfarray_create()
110 *arrayp = array; in xfarray_create()
114 kfree(array); in xfarray_create()
120 /* Destroy the array. */
123 struct xfarray *array) in xfarray_destroy() argument
125 xfile_destroy(array->xfile); in xfarray_destroy()
126 kfree(array); in xfarray_destroy()
129 /* Load an element from the array. */
132 struct xfarray *array, in xfarray_load() argument
136 if (idx >= array->nr) in xfarray_load()
139 return xfile_obj_load(array->xfile, ptr, array->obj_size, in xfarray_load()
140 xfarray_pos(array, idx)); in xfarray_load()
143 /* Is this array element potentially unset? */
146 struct xfarray *array, in xfarray_is_unset() argument
149 void *temp = xfarray_scratch(array); in xfarray_is_unset()
152 if (array->unset_slots == 0) in xfarray_is_unset()
155 error = xfile_obj_load(array->xfile, temp, array->obj_size, pos); in xfarray_is_unset()
156 if (!error && xfarray_element_is_null(array, temp)) in xfarray_is_unset()
163 * Unset an array element. If @idx is the last element in the array, the
164 * array will be truncated. Otherwise, the entry will be zeroed.
168 struct xfarray *array, in xfarray_unset() argument
171 void *temp = xfarray_scratch(array); in xfarray_unset()
172 loff_t pos = xfarray_pos(array, idx); in xfarray_unset()
175 if (idx >= array->nr) in xfarray_unset()
178 if (idx == array->nr - 1) { in xfarray_unset()
179 array->nr--; in xfarray_unset()
183 if (xfarray_is_unset(array, pos)) in xfarray_unset()
186 memset(temp, 0, array->obj_size); in xfarray_unset()
187 error = xfile_obj_store(array->xfile, temp, array->obj_size, pos); in xfarray_unset()
191 array->unset_slots++; in xfarray_unset()
196 * Store an element in the array. The element must not be completely zeroed,
201 struct xfarray *array, in xfarray_store() argument
207 if (idx >= array->max_nr) in xfarray_store()
210 ASSERT(!xfarray_element_is_null(array, ptr)); in xfarray_store()
212 ret = xfile_obj_store(array->xfile, ptr, array->obj_size, in xfarray_store()
213 xfarray_pos(array, idx)); in xfarray_store()
217 array->nr = max(array->nr, idx + 1); in xfarray_store()
221 /* Is this array element NULL? */
224 struct xfarray *array, in xfarray_element_is_null() argument
227 return !memchr_inv(ptr, 0, array->obj_size); in xfarray_element_is_null()
231 * Store an element anywhere in the array that is unset. If there are no
232 * unset slots, append the element to the array.
236 struct xfarray *array, in xfarray_store_anywhere() argument
239 void *temp = xfarray_scratch(array); in xfarray_store_anywhere()
240 loff_t endpos = xfarray_pos(array, array->nr); in xfarray_store_anywhere()
246 pos < endpos && array->unset_slots > 0; in xfarray_store_anywhere()
247 pos += array->obj_size) { in xfarray_store_anywhere()
248 error = xfile_obj_load(array->xfile, temp, array->obj_size, in xfarray_store_anywhere()
250 if (error || !xfarray_element_is_null(array, temp)) in xfarray_store_anywhere()
253 error = xfile_obj_store(array->xfile, ptr, array->obj_size, in xfarray_store_anywhere()
258 array->unset_slots--; in xfarray_store_anywhere()
263 array->unset_slots = 0; in xfarray_store_anywhere()
264 return xfarray_append(array, ptr); in xfarray_store_anywhere()
267 /* Return length of array. */
270 struct xfarray *array) in xfarray_length() argument
272 return array->nr; in xfarray_length()
276 * Decide which array item we're going to read as part of an _iter_get.
277 * @cur is the array index, and @pos is the file offset of that array index in
281 * iterating a (possibly sparse) array we need to figure out if the cursor is
287 struct xfarray *array, in xfarray_find_data() argument
292 loff_t end_pos = *pos + array->obj_size - 1; in xfarray_find_data()
296 * If the current array record is not adjacent to a page boundary, we in xfarray_find_data()
299 if (pgoff != 0 && pgoff + array->obj_size - 1 < PAGE_SIZE) in xfarray_find_data()
313 new_pos = xfile_seek_data(array->xfile, end_pos); in xfarray_find_data()
323 * find more data. Move the array index to the first record past the in xfarray_find_data()
326 new_pos = roundup_64(new_pos, array->obj_size); in xfarray_find_data()
327 *cur = xfarray_idx(array, new_pos); in xfarray_find_data()
328 *pos = xfarray_pos(array, *cur); in xfarray_find_data()
333 * Starting at *idx, fetch the next non-null array entry and advance the index
335 * the array. Callers must set @*idx to XFARRAY_CURSOR_INIT before the first
340 struct xfarray *array, in xfarray_load_next() argument
345 loff_t pos = xfarray_pos(array, cur); in xfarray_load_next()
349 if (cur >= array->nr) in xfarray_load_next()
356 error = xfarray_find_data(array, &cur, &pos); in xfarray_load_next()
359 error = xfarray_load(array, cur, rec); in xfarray_load_next()
364 pos += array->obj_size; in xfarray_load_next()
365 } while (xfarray_element_is_null(array, rec)); in xfarray_load_next()
385 /* Load an array element for sorting. */
393 return xfarray_load(si->array, idx, ptr); in xfarray_sort_load()
396 /* Store an array element for sorting. */
404 return xfarray_store(si->array, idx, ptr); in xfarray_sort_store()
407 /* Compare an array element for sorting. */
430 /* Size of each element in the quicksort pivot array. */
433 struct xfarray *array) in xfarray_pivot_rec_sz() argument
435 return round_up(array->obj_size, 8) + sizeof(xfarray_idx_t); in xfarray_pivot_rec_sz()
441 struct xfarray *array, in xfarray_sortinfo_alloc() argument
448 size_t pivot_rec_sz = xfarray_pivot_rec_sz(array); in xfarray_sortinfo_alloc()
465 max_stack_depth = ilog2(array->nr) + 1 - (XFARRAY_ISORT_SHIFT - 1); in xfarray_sortinfo_alloc()
475 XFARRAY_ISORT_NR * array->obj_size); in xfarray_sortinfo_alloc()
481 si->array = array; in xfarray_sortinfo_alloc()
488 xfarray_sortinfo_hi(si)[0] = array->nr - 1; in xfarray_sortinfo_alloc()
525 * For array subsets that fit in the scratchpad, it's much faster to in xfarray_want_isort()
538 * Sort a small number of array records using scratchpad memory. The records
548 loff_t lo_pos = xfarray_pos(si->array, lo); in xfarray_isort()
549 loff_t len = xfarray_pos(si->array, hi - lo + 1); in xfarray_isort()
555 error = xfile_obj_load(si->array->xfile, scratch, len, lo_pos); in xfarray_isort()
560 sort(scratch, hi - lo + 1, si->array->obj_size, si->cmp_fn, NULL); in xfarray_isort()
563 return xfile_obj_store(si->array->xfile, scratch, len, lo_pos); in xfarray_isort()
575 error = xfile_get_page(si->array->xfile, pos, len, &si->xfpage); in xfarray_sort_get_page()
598 return xfile_put_page(si->array->xfile, &si->xfpage); in xfarray_sort_put_page()
613 lo_page = xfarray_pos(si->array, lo) >> PAGE_SHIFT; in xfarray_want_pagesort()
614 end_pos = xfarray_pos(si->array, hi) + si->array->obj_size - 1; in xfarray_want_pagesort()
628 loff_t lo_pos = xfarray_pos(si->array, lo); in xfarray_pagesort()
629 uint64_t len = xfarray_pos(si->array, hi - lo); in xfarray_pagesort()
641 sort(startp, hi - lo + 1, si->array->obj_size, si->cmp_fn, NULL); in xfarray_pagesort()
653 /* Return a pointer to the start of the pivot array. */
658 return xfarray_sortinfo_pivot(si) + si->array->obj_size; in xfarray_sortinfo_pivot_array()
661 /* The xfarray record is stored at the start of each pivot array element. */
671 /* The xfarray index is stored at the end of each pivot array element. */
689 * quicksort behavior, since our array values are nearly always evenly sorted.
702 size_t pivot_rec_sz = xfarray_pivot_rec_sz(si->array); in xfarray_qsort_pivot()
710 * pivot array. in xfarray_qsort_pivot()
722 /* Load the selected xfarray records into the pivot array. */ in xfarray_qsort_pivot()
729 /* No unset records; load directly into the array. */ in xfarray_qsort_pivot()
730 if (likely(si->array->unset_slots == 0)) { in xfarray_qsort_pivot()
739 * the xfarray_idx_t in the pivot array. in xfarray_qsort_pivot()
743 error = xfarray_load_next(si->array, &idx, recp); in xfarray_qsort_pivot()
752 * We sorted the pivot array records (which includes the xfarray in xfarray_qsort_pivot()
754 * array contains the xfarray record that we will use as the pivot. in xfarray_qsort_pivot()
759 memcpy(pivot, recp, si->array->obj_size); in xfarray_qsort_pivot()
768 * Find the cached copy of a[lo] in the pivot array so that we can swap in xfarray_qsort_pivot()
833 * Load an element from the array into the first scratchpad and cache the page,
842 loff_t idx_pos = xfarray_pos(si->array, idx); in xfarray_sort_load_cached()
852 endpage = (idx_pos + si->array->obj_size - 1) >> PAGE_SHIFT; in xfarray_sort_load_cached()
861 return xfile_obj_load(si->array->xfile, ptr, in xfarray_sort_load_cached()
862 si->array->obj_size, idx_pos); in xfarray_sort_load_cached()
888 si->array->obj_size); in xfarray_sort_load_cached()
893 * Sort the array elements via quicksort. This implementation incorporates
896 * 1. Use an explicit stack of array indices to store the next array partition
931 struct xfarray *array, in xfarray_sort() argument
938 void *scratch = xfarray_scratch(array); in xfarray_sort()
942 if (array->nr < 2) in xfarray_sort()
944 if (array->nr >= QSORT_MAX_RECS) in xfarray_sort()
947 error = xfarray_sortinfo_alloc(array, cmp_fn, flags, &si); in xfarray_sort()