1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2019 Intel Corporation 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21bf215546Sopenharmony_ci * IN THE SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci#ifndef _UTIL_SPARSE_ARRAY_H 25bf215546Sopenharmony_ci#define _UTIL_SPARSE_ARRAY_H 26bf215546Sopenharmony_ci 27bf215546Sopenharmony_ci#include <stdint.h> 28bf215546Sopenharmony_ci 29bf215546Sopenharmony_ci#include "c11/threads.h" 30bf215546Sopenharmony_ci#include "macros.h" 31bf215546Sopenharmony_ci#include "u_atomic.h" 32bf215546Sopenharmony_ci#include "u_math.h" 33bf215546Sopenharmony_ci 34bf215546Sopenharmony_ci#ifdef __cplusplus 35bf215546Sopenharmony_ciextern "C" { 36bf215546Sopenharmony_ci#endif 37bf215546Sopenharmony_ci 38bf215546Sopenharmony_cistruct util_sparse_array_node; 39bf215546Sopenharmony_ci 40bf215546Sopenharmony_ci/** A thread-safe automatically growing sparse array data structure 41bf215546Sopenharmony_ci * 42bf215546Sopenharmony_ci * This data structure has the following very nice properties: 43bf215546Sopenharmony_ci * 44bf215546Sopenharmony_ci * 1. Accessing an element is basically constant time. Technically, it's 45bf215546Sopenharmony_ci * O(log_b n) where the base b is the node size and n is the maximum 46bf215546Sopenharmony_ci * index. However, node sizes are expected to be fairly large and the 47bf215546Sopenharmony_ci * index is a uint64_t so, if your node size is 256, it's O(8). 48bf215546Sopenharmony_ci * 49bf215546Sopenharmony_ci * 2. The data stored in the array is never moved in memory. Instead, the 50bf215546Sopenharmony_ci * data structure only ever grows and new nodes are added as-needed. This 51bf215546Sopenharmony_ci * means it's safe to store a pointer to something stored in the sparse 52bf215546Sopenharmony_ci * array without worrying about a realloc invalidating it. 53bf215546Sopenharmony_ci * 54bf215546Sopenharmony_ci * 3. The data structure is thread-safe. No guarantees are made about the 55bf215546Sopenharmony_ci * data stored in the sparse array but it is safe to call 56bf215546Sopenharmony_ci * util_sparse_array_get(arr, idx) from as many threads as you'd like and 57bf215546Sopenharmony_ci * we guarantee that two calls to util_sparse_array_get(arr, idx) with the 58bf215546Sopenharmony_ci * same array and index will always return the same pointer regardless 59bf215546Sopenharmony_ci * contention between threads. 60bf215546Sopenharmony_ci * 61bf215546Sopenharmony_ci * 4. The data structure is lock-free. All manipulations of the tree are 62bf215546Sopenharmony_ci * done by a careful use of atomics to maintain thread safety and no locks 63bf215546Sopenharmony_ci * are ever taken other than those taken implicitly by calloc(). If no 64bf215546Sopenharmony_ci * allocation is required, util_sparse_array_get(arr, idx) does a simple 65bf215546Sopenharmony_ci * walk over the tree should be efficient even in the case where many 66bf215546Sopenharmony_ci * threads are accessing the sparse array at once. 67bf215546Sopenharmony_ci */ 68bf215546Sopenharmony_cistruct util_sparse_array { 69bf215546Sopenharmony_ci size_t elem_size; 70bf215546Sopenharmony_ci unsigned node_size_log2; 71bf215546Sopenharmony_ci 72bf215546Sopenharmony_ci uintptr_t root; 73bf215546Sopenharmony_ci}; 74bf215546Sopenharmony_ci 75bf215546Sopenharmony_civoid util_sparse_array_init(struct util_sparse_array *arr, 76bf215546Sopenharmony_ci size_t elem_size, size_t node_size); 77bf215546Sopenharmony_ci 78bf215546Sopenharmony_civoid util_sparse_array_finish(struct util_sparse_array *arr); 79bf215546Sopenharmony_ci 80bf215546Sopenharmony_civoid *util_sparse_array_get(struct util_sparse_array *arr, uint64_t idx); 81bf215546Sopenharmony_ci 82bf215546Sopenharmony_civoid util_sparse_array_validate(struct util_sparse_array *arr); 83bf215546Sopenharmony_ci 84bf215546Sopenharmony_ci/** A thread-safe free list for use with struct util_sparse_array 85bf215546Sopenharmony_ci * 86bf215546Sopenharmony_ci * This data structure provides an easy way to manage a singly linked list of 87bf215546Sopenharmony_ci * "free" elements backed by a util_sparse_array. The list supports only two 88bf215546Sopenharmony_ci * operations: push and pop both of which are thread-safe and lock-free. T 89bf215546Sopenharmony_ci */ 90bf215546Sopenharmony_cistruct util_sparse_array_free_list 91bf215546Sopenharmony_ci{ 92bf215546Sopenharmony_ci /** Head of the list 93bf215546Sopenharmony_ci * 94bf215546Sopenharmony_ci * The bottom 64 bits of this value are the index to the next free element 95bf215546Sopenharmony_ci * or the sentinel value if the list is empty. 96bf215546Sopenharmony_ci * 97bf215546Sopenharmony_ci * We want this element to be 8-byte aligned. Otherwise, the performance 98bf215546Sopenharmony_ci * of atomic operations on it will be aweful on 32-bit platforms. 99bf215546Sopenharmony_ci */ 100bf215546Sopenharmony_ci alignas(8) uint64_t head; 101bf215546Sopenharmony_ci 102bf215546Sopenharmony_ci /** The array backing this free list */ 103bf215546Sopenharmony_ci struct util_sparse_array *arr; 104bf215546Sopenharmony_ci 105bf215546Sopenharmony_ci /** Sentinel value to indicate the end of the list 106bf215546Sopenharmony_ci * 107bf215546Sopenharmony_ci * This value must never be passed into util_sparse_array_free_list_push. 108bf215546Sopenharmony_ci */ 109bf215546Sopenharmony_ci uint32_t sentinel; 110bf215546Sopenharmony_ci 111bf215546Sopenharmony_ci /** Offset into the array element at which to find the "next" value 112bf215546Sopenharmony_ci * 113bf215546Sopenharmony_ci * The assumption is that there is some uint32_t "next" value embedded in 114bf215546Sopenharmony_ci * the array element for use in the free list. This is its offset. 115bf215546Sopenharmony_ci */ 116bf215546Sopenharmony_ci uint32_t next_offset; 117bf215546Sopenharmony_ci}; 118bf215546Sopenharmony_ci 119bf215546Sopenharmony_civoid util_sparse_array_free_list_init(struct util_sparse_array_free_list *fl, 120bf215546Sopenharmony_ci struct util_sparse_array *arr, 121bf215546Sopenharmony_ci uint32_t sentinel, 122bf215546Sopenharmony_ci uint32_t next_offset); 123bf215546Sopenharmony_ci 124bf215546Sopenharmony_civoid util_sparse_array_free_list_push(struct util_sparse_array_free_list *fl, 125bf215546Sopenharmony_ci uint32_t *items, unsigned num_items); 126bf215546Sopenharmony_ci 127bf215546Sopenharmony_ciuint32_t util_sparse_array_free_list_pop_idx(struct util_sparse_array_free_list *fl); 128bf215546Sopenharmony_civoid *util_sparse_array_free_list_pop_elem(struct util_sparse_array_free_list *fl); 129bf215546Sopenharmony_ci 130bf215546Sopenharmony_ci#ifdef __cplusplus 131bf215546Sopenharmony_ci} /* extern C */ 132bf215546Sopenharmony_ci#endif 133bf215546Sopenharmony_ci 134bf215546Sopenharmony_ci#endif /* _UTIL_SPARSE_ARRAY_H */ 135