commit:     62fe544cf61aa9ae3e3560323d84f221c6c1a375
Author:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
AuthorDate: Wed Feb 27 20:41:01 2019 +0000
Commit:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
CommitDate: Wed Feb 27 20:50:16 2019 +0000
URL:        https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=62fe544c

xarray: add xarrayget function to retrieve a given item

Signed-off-by: Fabian Groffen <grobian <AT> gentoo.org>

 libq/xarray.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/libq/xarray.c b/libq/xarray.c
index 3ed9872..0ab1c5a 100644
--- a/libq/xarray.c
+++ b/libq/xarray.c
@@ -18,15 +18,24 @@ typedef struct {
  * already do not permit pushing of NULL pointers), but we can't put it in the
  * increment phase as that will cause a load beyond the bounds of valid memory.
  */
+/* TODO: remove ele = NULL after checking all consumers don't rely on this */
 #define array_for_each(arr, n, ele) \
-       for (n = 0, ele = array_cnt(arr) ? arr->eles[n] : NULL; \
-            n < array_cnt(arr) && (ele = arr->eles[n]); \
-            ++n)
+       for (n = 0, ele = NULL; n < array_cnt(arr) && (ele = arr->eles[n]); n++)
+#define array_for_each_rev(arr, n, ele) \
+       for (n = array_cnt(arr); n-- > 0 && (ele = arr->eles[n]); /*nothing*/)
+#define array_get_elem(arr, n) (arr->eles[n])
 #define array_init_decl { .eles = NULL, .num = 0, }
 #define array_cnt(arr) (arr)->num
 #define DECLARE_ARRAY(arr) array_t _##arr = array_init_decl, *arr = &_##arr
 #define ARRAY_INC_SIZE 32
 
+static void *xarrayget(array_t *arr, size_t idx)
+{
+       if (idx >= arr->num)
+               return NULL;
+       return arr->eles[idx];
+}
+
 /* Push a pointer to memory we already hold and don't want to release.  Do not
  * mix xarraypush_ptr usage with the other push funcs which duplicate memory.
  * The free stage won't know which pointers to release directly.
@@ -51,7 +60,10 @@ static void *xarraypush(array_t *arr, const void *ele, 
size_t ele_len)
 static void xarraydelete_ptr(array_t *arr, size_t elem)
 {
        arr->num--;
-       memmove(&arr->eles[elem], &arr->eles[elem + 1], arr->num - elem);
+       if (elem < arr->num)
+               memmove(&arr->eles[elem], &arr->eles[elem + 1],
+                               sizeof(arr->eles[0]) * (arr->num - elem));
+       arr->eles[arr->num] = NULL;
 }
 
 static void xarraydelete(array_t *arr, size_t elem)

Reply via email to