2 * Copyright © 2019 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 #include "util/sparse_array.h"
30 #include "c11/threads.h"
32 #define NUM_THREADS 16
34 #define NUM_SETS_PER_THREAD (1 << 10)
35 #define MAX_ARR_SIZE (1 << 20)
38 test_thread(void *_state
)
40 struct util_sparse_array
*arr
= _state
;
41 for (unsigned i
= 0; i
< NUM_SETS_PER_THREAD
; i
++) {
42 uint32_t idx
= rand() % MAX_ARR_SIZE
;
43 uint32_t *elem
= util_sparse_array_get(arr
, idx
);
51 run_test(unsigned run_idx
)
53 size_t node_size
= 4 << (run_idx
/ 2);
55 struct util_sparse_array arr
;
56 util_sparse_array_init(&arr
, sizeof(uint32_t), node_size
);
58 thrd_t threads
[NUM_THREADS
];
59 for (unsigned i
= 0; i
< NUM_THREADS
; i
++) {
60 int ret
= thrd_create(&threads
[i
], test_thread
, &arr
);
61 assert(ret
== thrd_success
);
64 for (unsigned i
= 0; i
< NUM_THREADS
; i
++) {
65 int ret
= thrd_join(threads
[i
], NULL
);
66 assert(ret
== thrd_success
);
69 util_sparse_array_validate(&arr
);
71 for (unsigned i
= 0; i
< MAX_ARR_SIZE
; i
++) {
72 uint32_t *elem
= util_sparse_array_get(&arr
, i
);
73 assert(*elem
== 0 || *elem
== i
);
76 util_sparse_array_finish(&arr
);
80 main(int argc
, char **argv
)
82 for (unsigned i
= 0; i
< NUM_RUNS
; i
++)