|
| 1 | +#include "virtgpu-utils.h" |
| 2 | +#include <malloc.h> |
| 3 | +#include <cstring> |
| 4 | +#include <stdlib.h> |
| 5 | + |
| 6 | +#define NODE_ALLOC_ALIGN 64 |
| 7 | +#define NODE_PTR_MASK (~((uintptr_t)NODE_ALLOC_ALIGN - 1)) |
| 8 | +#define NODE_LEVEL_MASK ((uintptr_t)NODE_ALLOC_ALIGN - 1) |
| 9 | +#define NULL_NODE 0 |
| 10 | + |
| 11 | +#define os_malloc_aligned(_size, _align) _aligned_malloc(_size, _align) |
| 12 | +#define os_free_aligned(_ptr) free(_ptr) |
| 13 | +#define p_atomic_cmpxchg(v, old, _new) \ |
| 14 | + __sync_val_compare_and_swap((v), (old), (_new)) |
| 15 | + |
| 16 | +static inline uint64_t |
| 17 | +util_logbase2_64(uint64_t n) |
| 18 | +{ |
| 19 | +#if defined(HAVE___BUILTIN_CLZLL) |
| 20 | + return ((sizeof(uint64_t) * 8 - 1) - __builtin_clzll(n | 1)); |
| 21 | +#else |
| 22 | + uint64_t pos = 0ull; |
| 23 | + if (n >= 1ull<<32) { n >>= 32; pos += 32; } |
| 24 | + if (n >= 1ull<<16) { n >>= 16; pos += 16; } |
| 25 | + if (n >= 1ull<< 8) { n >>= 8; pos += 8; } |
| 26 | + if (n >= 1ull<< 4) { n >>= 4; pos += 4; } |
| 27 | + if (n >= 1ull<< 2) { n >>= 2; pos += 2; } |
| 28 | + if (n >= 1ull<< 1) { pos += 1; } |
| 29 | + return pos; |
| 30 | +#endif |
| 31 | +} |
| 32 | + |
| 33 | +void |
| 34 | +util_sparse_array_init(struct util_sparse_array *arr, |
| 35 | + size_t elem_size, size_t node_size) |
| 36 | +{ |
| 37 | + memset(arr, 0, sizeof(*arr)); |
| 38 | + arr->elem_size = elem_size; |
| 39 | + arr->node_size_log2 = util_logbase2_64(node_size); |
| 40 | + assert(node_size >= 2 && node_size == (1ull << arr->node_size_log2)); |
| 41 | +} |
| 42 | + |
| 43 | +static inline void * |
| 44 | +os_malloc_aligned(size_t size, size_t alignment) |
| 45 | +{ |
| 46 | + void *ptr; |
| 47 | + alignment = (alignment + sizeof(void*) - 1) & ~(sizeof(void*) - 1); |
| 48 | + if(posix_memalign(&ptr, alignment, size) != 0) |
| 49 | + return NULL; |
| 50 | + return ptr; |
| 51 | +} |
| 52 | + |
| 53 | +static inline void * |
| 54 | +_util_sparse_array_node_data(uintptr_t handle) |
| 55 | +{ |
| 56 | + return (void *)(handle & NODE_PTR_MASK); |
| 57 | +} |
| 58 | + |
| 59 | +static inline unsigned |
| 60 | +_util_sparse_array_node_level(uintptr_t handle) |
| 61 | +{ |
| 62 | + return handle & NODE_LEVEL_MASK; |
| 63 | +} |
| 64 | + |
| 65 | +static inline void |
| 66 | +_util_sparse_array_node_finish(struct util_sparse_array *arr, |
| 67 | + uintptr_t node) |
| 68 | +{ |
| 69 | + if (_util_sparse_array_node_level(node) > 0) { |
| 70 | + uintptr_t *children = (uintptr_t *) _util_sparse_array_node_data(node); |
| 71 | + size_t node_size = 1ull << arr->node_size_log2; |
| 72 | + for (size_t i = 0; i < node_size; i++) { |
| 73 | + if (children[i]) |
| 74 | + _util_sparse_array_node_finish(arr, children[i]); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + os_free_aligned(_util_sparse_array_node_data(node)); |
| 79 | +} |
| 80 | + |
| 81 | +static inline uintptr_t |
| 82 | +_util_sparse_array_node(void *data, unsigned level) |
| 83 | +{ |
| 84 | + assert(data != NULL); |
| 85 | + assert(((uintptr_t)data & NODE_LEVEL_MASK) == 0); |
| 86 | + assert((level & NODE_PTR_MASK) == 0); |
| 87 | + return (uintptr_t)data | level; |
| 88 | +} |
| 89 | + |
| 90 | +inline uintptr_t |
| 91 | +_util_sparse_array_node_alloc(struct util_sparse_array *arr, |
| 92 | + unsigned level) |
| 93 | +{ |
| 94 | + size_t size; |
| 95 | + if (level == 0) { |
| 96 | + size = arr->elem_size << arr->node_size_log2; |
| 97 | + } else { |
| 98 | + size = sizeof(uintptr_t) << arr->node_size_log2; |
| 99 | + } |
| 100 | + |
| 101 | + void *data = os_malloc_aligned(size, NODE_ALLOC_ALIGN); |
| 102 | + memset(data, 0, size); |
| 103 | + |
| 104 | + return _util_sparse_array_node(data, level); |
| 105 | +} |
| 106 | + |
| 107 | +static inline uintptr_t |
| 108 | +_util_sparse_array_set_or_free_node(uintptr_t *node_ptr, |
| 109 | + uintptr_t cmp_node, |
| 110 | + uintptr_t node) |
| 111 | +{ |
| 112 | + uintptr_t prev_node = p_atomic_cmpxchg(node_ptr, cmp_node, node); |
| 113 | + |
| 114 | + if (prev_node != cmp_node) { |
| 115 | + /* We lost the race. Free this one and return the one that was already |
| 116 | + * allocated. |
| 117 | + */ |
| 118 | + os_free_aligned(_util_sparse_array_node_data(node)); |
| 119 | + return prev_node; |
| 120 | + } else { |
| 121 | + return node; |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +void * |
| 126 | +util_sparse_array_get(struct util_sparse_array *arr, uint64_t idx) |
| 127 | +{ |
| 128 | + const unsigned node_size_log2 = arr->node_size_log2; |
| 129 | + uintptr_t root = p_atomic_read(&arr->root); |
| 130 | + if (unlikely(!root)) { |
| 131 | + unsigned root_level = 0; |
| 132 | + uint64_t idx_iter = idx >> node_size_log2; |
| 133 | + while (idx_iter) { |
| 134 | + idx_iter >>= node_size_log2; |
| 135 | + root_level++; |
| 136 | + } |
| 137 | + uintptr_t new_root = _util_sparse_array_node_alloc(arr, root_level); |
| 138 | + root = _util_sparse_array_set_or_free_node(&arr->root, |
| 139 | + NULL_NODE, new_root); |
| 140 | + } |
| 141 | + |
| 142 | + while (1) { |
| 143 | + unsigned root_level = _util_sparse_array_node_level(root); |
| 144 | + uint64_t root_idx = idx >> (root_level * node_size_log2); |
| 145 | + if (likely(root_idx < (1ull << node_size_log2))) |
| 146 | + break; |
| 147 | + |
| 148 | + /* In this case, we have a root but its level is low enough that the |
| 149 | + * requested index is out-of-bounds. |
| 150 | + */ |
| 151 | + uintptr_t new_root = _util_sparse_array_node_alloc(arr, root_level + 1); |
| 152 | + |
| 153 | + uintptr_t *new_root_children = (uintptr_t *) _util_sparse_array_node_data(new_root); |
| 154 | + new_root_children[0] = root; |
| 155 | + |
| 156 | + /* We only add one at a time instead of the whole tree because it's |
| 157 | + * easier to ensure correctness of both the tree building and the |
| 158 | + * clean-up path. Because we're only adding one node we never have to |
| 159 | + * worry about trying to free multiple things without freeing the old |
| 160 | + * things. |
| 161 | + */ |
| 162 | + root = _util_sparse_array_set_or_free_node(&arr->root, root, new_root); |
| 163 | + } |
| 164 | + |
| 165 | + void *node_data = _util_sparse_array_node_data(root); |
| 166 | + unsigned node_level = _util_sparse_array_node_level(root); |
| 167 | + while (node_level > 0) { |
| 168 | + uint64_t child_idx = (idx >> (node_level * node_size_log2)) & |
| 169 | + ((1ull << node_size_log2) - 1); |
| 170 | + |
| 171 | + uintptr_t *children = (uintptr_t *) node_data; |
| 172 | + uintptr_t child = p_atomic_read(&children[child_idx]); |
| 173 | + |
| 174 | + if (unlikely(!child)) { |
| 175 | + child = _util_sparse_array_node_alloc(arr, node_level - 1); |
| 176 | + child = _util_sparse_array_set_or_free_node(&children[child_idx], |
| 177 | + NULL_NODE, child); |
| 178 | + } |
| 179 | + |
| 180 | + node_data = _util_sparse_array_node_data(child); |
| 181 | + node_level = _util_sparse_array_node_level(child); |
| 182 | + } |
| 183 | + |
| 184 | + uint64_t elem_idx = idx & ((1ull << node_size_log2) - 1); |
| 185 | + return (void *)((char *)node_data + (elem_idx * arr->elem_size)); |
| 186 | +} |
0 commit comments