xref: /aosp_15_r20/external/mesa3d/src/util/u_worklist.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright (c) 2022 Collabora Ltd.
3  * Copyright © 2014 Intel Corporation
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 
25 
26 #ifndef _U_WORKLIST_
27 #define _U_WORKLIST_
28 
29 #include "util/bitset.h"
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 /** Represents a double-ended queue of unique entries. Each entry must have a
36  * unique index in the range [0, num_entries). Internally, entries are tracked
37  * as pointers to that index (so you can go from the index pointer back to a
38  * containing structure). This requires index pointers to remain valid while
39  * they are in the worklist (i.e. no realloc).
40  *
41  * The worklist data structure guarantees that each entry is in the queue at
42  * most once. Pushing an entry onto either end of the queue is a no-op if the
43  * entry is already in the queue. Internally, the data structure maintains a
44  * bitset of present entries.
45  */
46 typedef struct {
47    /* The total size of the worklist */
48    unsigned size;
49 
50    /* The number of entries currently in the worklist */
51    unsigned count;
52 
53    /* The offset in the array of entries at which the list starts */
54    unsigned start;
55 
56    /* A bitset of all of the entries currently present in the worklist */
57    BITSET_WORD *present;
58 
59    /* The actual worklist */
60    unsigned **entries;
61 } u_worklist;
62 
63 void u_worklist_init(u_worklist *w, unsigned num_entries, void *mem_ctx);
64 
65 void u_worklist_fini(u_worklist *w);
66 
67 static inline bool
u_worklist_is_empty(const u_worklist * w)68 u_worklist_is_empty(const u_worklist *w)
69 {
70    return w->count == 0;
71 }
72 
73 void u_worklist_push_head_index(u_worklist *w, unsigned *block);
74 
75 unsigned *u_worklist_peek_head_index(const u_worklist *w);
76 
77 unsigned *u_worklist_pop_head_index(u_worklist *w);
78 
79 unsigned *u_worklist_peek_tail_index(const u_worklist *w);
80 
81 void u_worklist_push_tail_index(u_worklist *w, unsigned *block);
82 
83 unsigned *u_worklist_pop_tail_index(u_worklist *w);
84 
85 #define u_worklist_push_tail(w, block, index) \
86    u_worklist_push_tail_index(w, &((block)->index))
87 
88 #define u_worklist_push_head(w, block, index) \
89    u_worklist_push_head_index(w, &((block)->index))
90 
91 #define u_worklist_pop_head(w, entry_t, index) \
92    container_of(u_worklist_pop_head_index(w), entry_t, index)
93 
94 #define u_worklist_pop_tail(w, entry_t, index) \
95    container_of(u_worklist_pop_tail_index(w), entry_t, index)
96 
97 #define u_worklist_peek_head(w, entry_t, index) \
98    container_of(u_worklist_peek_head_index(w), entry_t, index)
99 
100 #define u_worklist_peek_tail(w, entry_t, index) \
101    container_of(u_worklist_peek_tail_index(w), entry_t, index)
102 
103 #ifdef __cplusplus
104 } /* extern "C" */
105 #endif
106 
107 #endif /* _U_WORKLIST_ */
108