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