[Mesa-dev] [PATCH 01/11] util: add vector util code.
Dave Airlie
airlied at gmail.com
Mon Oct 17 02:07:12 UTC 2016
From: Dave Airlie <airlied at redhat.com>
This is ported from anv, both anv and radv can share this.
Signed-off-by: Dave Airlie <airlied at redhat.com>
---
src/util/Makefile.sources | 4 +-
src/util/u_vector.c | 98 +++++++++++++++++++++++++++++++++++++++++++++++
src/util/u_vector.h | 91 +++++++++++++++++++++++++++++++++++++++++++
3 files changed, 192 insertions(+), 1 deletion(-)
create mode 100644 src/util/u_vector.c
create mode 100644 src/util/u_vector.h
diff --git a/src/util/Makefile.sources b/src/util/Makefile.sources
index 8b17bcf..b7b1e91 100644
--- a/src/util/Makefile.sources
+++ b/src/util/Makefile.sources
@@ -35,7 +35,9 @@ MESA_UTIL_FILES := \
strtod.h \
texcompress_rgtc_tmp.h \
u_atomic.h \
- u_endian.h
+ u_endian.h \
+ u_vector.c \
+ u_vector.h
MESA_UTIL_GENERATED_FILES = \
format_srgb.c
diff --git a/src/util/u_vector.c b/src/util/u_vector.c
new file mode 100644
index 0000000..37c4245
--- /dev/null
+++ b/src/util/u_vector.c
@@ -0,0 +1,98 @@
+/*
+ * Copyright © 2015 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+#include "util/u_vector.h"
+
+int
+u_vector_init(struct u_vector *vector, uint32_t element_size, uint32_t size)
+{
+ assert(util_is_power_of_two(size));
+ assert(element_size < size && util_is_power_of_two(element_size));
+
+ vector->head = 0;
+ vector->tail = 0;
+ vector->element_size = element_size;
+ vector->size = size;
+ vector->data = malloc(size);
+
+ return vector->data != NULL;
+}
+
+void *
+u_vector_add(struct u_vector *vector)
+{
+ uint32_t offset, size, split, src_tail, dst_tail;
+ void *data;
+
+ if (vector->head - vector->tail == vector->size) {
+ size = vector->size * 2;
+ data = malloc(size);
+ if (data == NULL)
+ return NULL;
+ src_tail = vector->tail & (vector->size - 1);
+ dst_tail = vector->tail & (size - 1);
+ if (src_tail == 0) {
+ /* Since we know that the vector is full, this means that it's
+ * linear from start to end so we can do one copy.
+ */
+ memcpy((char *)data + dst_tail, vector->data, vector->size);
+ } else {
+ /* In this case, the vector is split into two pieces and we have
+ * to do two copies. We have to be careful to make sure each
+ * piece goes to the right locations. Thanks to the change in
+ * size, it may or may not still wrap around.
+ */
+ split = u_align_u32(vector->tail, vector->size);
+ assert(vector->tail <= split && split < vector->head);
+ memcpy((char *)data + dst_tail, (char *)vector->data + src_tail,
+ split - vector->tail);
+ memcpy((char *)data + (split & (size - 1)), vector->data,
+ vector->head - split);
+ }
+ free(vector->data);
+ vector->data = data;
+ vector->size = size;
+ }
+
+ assert(vector->head - vector->tail < vector->size);
+
+ offset = vector->head & (vector->size - 1);
+ vector->head += vector->element_size;
+
+ return (char *)vector->data + offset;
+}
+
+void *
+u_vector_remove(struct u_vector *vector)
+{
+ uint32_t offset;
+
+ if (vector->head == vector->tail)
+ return NULL;
+
+ assert(vector->head - vector->tail <= vector->size);
+
+ offset = vector->tail & (vector->size - 1);
+ vector->tail += vector->element_size;
+
+ return (char *)vector->data + offset;
+}
diff --git a/src/util/u_vector.h b/src/util/u_vector.h
new file mode 100644
index 0000000..6e57a84
--- /dev/null
+++ b/src/util/u_vector.h
@@ -0,0 +1,91 @@
+/*
+ * Copyright © 2015 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+/*
+ * u_vector is a vector based queue for storing arbitrary
+ * sized arrays of objects without using a linked list.
+ */
+
+#ifndef U_VECTOR_H
+#define U_VECTOR_H
+
+#include <stdint.h>
+#include <stdlib.h>
+#include "util/u_math.h"
+#include "util/macros.h"
+
+static inline uint32_t
+u_align_u32(uint32_t v, uint32_t a)
+{
+ assert(a != 0 && a == (a & -a));
+ return (v + a - 1) & ~(a - 1);
+}
+
+struct u_vector {
+ uint32_t head;
+ uint32_t tail;
+ uint32_t element_size;
+ uint32_t size;
+ void *data;
+};
+
+int u_vector_init(struct u_vector *queue, uint32_t element_size, uint32_t size);
+void *u_vector_add(struct u_vector *queue);
+void *u_vector_remove(struct u_vector *queue);
+
+static inline int
+u_vector_length(struct u_vector *queue)
+{
+ return (queue->head - queue->tail) / queue->element_size;
+}
+
+static inline void *
+u_vector_head(struct u_vector *vector)
+{
+ assert(vector->tail < vector->head);
+ return (void *)((char *)vector->data +
+ ((vector->head - vector->element_size) &
+ (vector->size - 1)));
+}
+
+static inline void *
+u_vector_tail(struct u_vector *vector)
+{
+ return (void *)((char *)vector->data + (vector->tail & (vector->size - 1)));
+}
+
+static inline void
+u_vector_finish(struct u_vector *queue)
+{
+ free(queue->data);
+}
+
+#define u_vector_foreach(elem, queue) \
+ static_assert(__builtin_types_compatible_p(__typeof__(queue), struct u_vector *), ""); \
+ for (uint32_t __u_vector_offset = (queue)->tail; \
+ elem = (queue)->data + (__u_vector_offset & ((queue)->size - 1)), __u_vector_offset < (queue)->head; \
+ __u_vector_offset += (queue)->element_size)
+
+
+#endif
+
--
2.5.5
More information about the mesa-dev
mailing list