2 * Copyright © 2015 Intel Corporation
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:
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
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
25 #include "util/u_math.h"
26 #include "util/u_vector.h"
30 * A dynamically growable, circular buffer. Elements are added at head and
31 * removed from tail. head and tail are free-running uint32_t indices and we
32 * only compute the modulo with size when accessing the array. This way,
33 * number of bytes in the queue is always head - tail, even in case of
38 u_vector_init(struct u_vector
*vector
, uint32_t element_size
, uint32_t size
)
40 assert(util_is_power_of_two_nonzero(size
));
41 assert(element_size
< size
&& util_is_power_of_two_nonzero(element_size
));
45 vector
->element_size
= element_size
;
47 vector
->data
= malloc(size
);
49 return vector
->data
!= NULL
;
53 u_vector_add(struct u_vector
*vector
)
55 uint32_t offset
, size
, split
, src_tail
, dst_tail
;
58 if (vector
->head
- vector
->tail
== vector
->size
) {
59 size
= vector
->size
* 2;
63 src_tail
= vector
->tail
& (vector
->size
- 1);
64 dst_tail
= vector
->tail
& (size
- 1);
66 /* Since we know that the vector is full, this means that it's
67 * linear from start to end so we can do one copy.
69 memcpy((char *)data
+ dst_tail
, vector
->data
, vector
->size
);
71 /* In this case, the vector is split into two pieces and we have
72 * to do two copies. We have to be careful to make sure each
73 * piece goes to the right locations. Thanks to the change in
74 * size, it may or may not still wrap around.
76 split
= u_align_u32(vector
->tail
, vector
->size
);
77 assert(vector
->tail
<= split
&& split
< vector
->head
);
78 memcpy((char *)data
+ dst_tail
, (char *)vector
->data
+ src_tail
,
79 split
- vector
->tail
);
80 memcpy((char *)data
+ (split
& (size
- 1)), vector
->data
,
81 vector
->head
- split
);
88 assert(vector
->head
- vector
->tail
< vector
->size
);
90 offset
= vector
->head
& (vector
->size
- 1);
91 vector
->head
+= vector
->element_size
;
93 return (char *)vector
->data
+ offset
;
97 u_vector_remove(struct u_vector
*vector
)
101 if (vector
->head
== vector
->tail
)
104 assert(vector
->head
- vector
->tail
<= vector
->size
);
106 offset
= vector
->tail
& (vector
->size
- 1);
107 vector
->tail
+= vector
->element_size
;
109 return (char *)vector
->data
+ offset
;