summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrancesco Romani <fromani@gmail.com>2014-07-05 18:04:33 +0200
committerSebastian Dröge <sebastian@centricular.com>2023-04-12 19:28:38 +0300
commitfed9b6f27f1c27babcddfef57184deef68fa6132 (patch)
tree008e5e0139e8be10e89b0b7f420e9093e4258f85
parent615d6129f465cc2286d842e51aadf3703766505c (diff)
downloadorc-fed9b6f27f1c27babcddfef57184deef68fa6132.tar.gz
utils: add utility dynamic array
Add utility code to handle a dynamic array. Supported operations: * append an item * grow the array * get an item * get the length of the array * check if the array has any data on it. There are clear use cases for this utility in the OrcParser, but may be useful elsewhere as well. Part-of: <https://gitlab.freedesktop.org/gstreamer/orc/-/merge_requests/30>
-rw-r--r--orc/orcutils.c31
-rw-r--r--orc/orcutils.h20
2 files changed, 51 insertions, 0 deletions
diff --git a/orc/orcutils.c b/orc/orcutils.c
index f0b77f6..adbb5ad 100644
--- a/orc/orcutils.c
+++ b/orc/orcutils.c
@@ -206,3 +206,34 @@ _strtoll (const char *nptr, char **endptr, int base)
return (neg) ? - val : val;
}
+int
+orc_vector_length (OrcVector *vector)
+{
+ return (vector == NULL) ?0 :vector->n_items;
+}
+
+int
+orc_vector_has_data (OrcVector *vector)
+{
+ return (orc_vector_length (vector) > 0 &&
+ vector->items != NULL && /* extra sanity check */
+ vector->items[0] != NULL);
+}
+
+void
+orc_vector_extend (OrcVector *vector)
+{
+ vector->n_items_alloc += ORC_VECTOR_ITEM_CHUNK;
+ vector->items = realloc (vector->items, sizeof(void *) * vector->n_items_alloc);
+}
+
+void
+orc_vector_append (OrcVector *vector, void *item)
+{
+ if (vector->n_items == vector->n_items_alloc) {
+ orc_vector_extend (vector);
+ }
+ vector->items[vector->n_items] = item;
+ vector->n_items++;
+}
+
diff --git a/orc/orcutils.h b/orc/orcutils.h
index c20e27f..9b1f993 100644
--- a/orc/orcutils.h
+++ b/orc/orcutils.h
@@ -231,6 +231,26 @@ ORC_BEGIN_DECLS
char * get_proc_cpuinfo (void);
#endif
+#define ORC_VECTOR_ITEM_CHUNK 32
+
+typedef struct _OrcVector OrcVector;
+struct _OrcVector {
+ void **items;
+ int n_items;
+ int n_items_alloc;
+};
+
+#define ORC_VECTOR_AS_TYPE(VECTOR, TYPE) \
+ ((TYPE **)(((VECTOR)->items)))
+
+#define ORC_VECTOR_GET_ITEM(VECTOR, INDEX, TYPEPTR) \
+ ((TYPEPTR) &((VECTOR)->items[(INDEX)]))
+
+void orc_vector_extend (OrcVector *vector);
+void orc_vector_append (OrcVector *vector, void *item);
+int orc_vector_length (OrcVector *vector);
+int orc_vector_has_data (OrcVector *vector);
+
char * _strndup (const char *s, int n);
char ** strsplit (const char *s, char delimiter);
char * get_tag_value (char *s, const char *tag);