1 /*
2  * Copyright © 2015 Intel Corporation
3  *
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:
10  *
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
13  * Software.
14  *
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
21  * IN THE SOFTWARE.
22  */
23 
24 /*
25  * u_vector is a vector based queue for storing arbitrary
26  * sized arrays of objects without using a linked list.
27  */
28 
29 #ifndef U_VECTOR_H
30 #define U_VECTOR_H
31 
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include "util/macros.h"
35 #include "util/u_math.h"
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 /* TODO - move to u_math.h - name it better etc */
42 static inline uint32_t
u_align_u32(uint32_t v,uint32_t a)43 u_align_u32(uint32_t v, uint32_t a)
44 {
45    assert(a != 0 && a == (a & -((int32_t) a)));
46    return (v + a - 1) & ~(a - 1);
47 }
48 
49 struct u_vector {
50    uint32_t head;
51    uint32_t tail;
52    uint32_t element_size;
53    uint32_t size;
54    void *data;
55 };
56 
57 int u_vector_init_pow2(struct u_vector *queue,
58                        uint32_t initial_element_count,
59                        uint32_t element_size);
60 
61 void *u_vector_add(struct u_vector *queue);
62 void *u_vector_remove(struct u_vector *queue);
63 
64 static inline int
u_vector_init(struct u_vector * queue,uint32_t initial_element_count,uint32_t element_size)65 u_vector_init(struct u_vector *queue,
66               uint32_t initial_element_count,
67               uint32_t element_size)
68 {
69    initial_element_count = util_next_power_of_two(initial_element_count);
70    element_size = util_next_power_of_two(element_size);
71    return u_vector_init_pow2(queue, initial_element_count, element_size);
72 }
73 
74 static inline int
u_vector_length(struct u_vector * queue)75 u_vector_length(struct u_vector *queue)
76 {
77    return (queue->head - queue->tail) / queue->element_size;
78 }
79 
80 static inline void *
u_vector_head(struct u_vector * vector)81 u_vector_head(struct u_vector *vector)
82 {
83    assert(vector->tail < vector->head);
84    return (void *)((char *)vector->data +
85                    ((vector->head - vector->element_size) &
86                     (vector->size - 1)));
87 }
88 
89 static inline void *
u_vector_tail(struct u_vector * vector)90 u_vector_tail(struct u_vector *vector)
91 {
92    return (void *)((char *)vector->data + (vector->tail & (vector->size - 1)));
93 }
94 
95 static inline void
u_vector_finish(struct u_vector * queue)96 u_vector_finish(struct u_vector *queue)
97 {
98    free(queue->data);
99 }
100 
101 #define u_vector_foreach(elem, queue)                                  \
102    STATIC_ASSERT(__builtin_types_compatible_p(__typeof__(queue), struct u_vector *)); \
103    for (uint32_t __u_vector_offset = (queue)->tail;                                \
104         elem = (void *)((char *)(queue)->data + (__u_vector_offset & ((queue)->size - 1))), __u_vector_offset != (queue)->head; \
105         __u_vector_offset += (queue)->element_size)
106 
107 #ifdef __cplusplus
108 }
109 #endif
110 
111 #endif
112 
113