1 |
commit: 62fe544cf61aa9ae3e3560323d84f221c6c1a375 |
2 |
Author: Fabian Groffen <grobian <AT> gentoo <DOT> org> |
3 |
AuthorDate: Wed Feb 27 20:41:01 2019 +0000 |
4 |
Commit: Fabian Groffen <grobian <AT> gentoo <DOT> org> |
5 |
CommitDate: Wed Feb 27 20:50:16 2019 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=62fe544c |
7 |
|
8 |
xarray: add xarrayget function to retrieve a given item |
9 |
|
10 |
Signed-off-by: Fabian Groffen <grobian <AT> gentoo.org> |
11 |
|
12 |
libq/xarray.c | 20 ++++++++++++++++---- |
13 |
1 file changed, 16 insertions(+), 4 deletions(-) |
14 |
|
15 |
diff --git a/libq/xarray.c b/libq/xarray.c |
16 |
index 3ed9872..0ab1c5a 100644 |
17 |
--- a/libq/xarray.c |
18 |
+++ b/libq/xarray.c |
19 |
@@ -18,15 +18,24 @@ typedef struct { |
20 |
* already do not permit pushing of NULL pointers), but we can't put it in the |
21 |
* increment phase as that will cause a load beyond the bounds of valid memory. |
22 |
*/ |
23 |
+/* TODO: remove ele = NULL after checking all consumers don't rely on this */ |
24 |
#define array_for_each(arr, n, ele) \ |
25 |
- for (n = 0, ele = array_cnt(arr) ? arr->eles[n] : NULL; \ |
26 |
- n < array_cnt(arr) && (ele = arr->eles[n]); \ |
27 |
- ++n) |
28 |
+ for (n = 0, ele = NULL; n < array_cnt(arr) && (ele = arr->eles[n]); n++) |
29 |
+#define array_for_each_rev(arr, n, ele) \ |
30 |
+ for (n = array_cnt(arr); n-- > 0 && (ele = arr->eles[n]); /*nothing*/) |
31 |
+#define array_get_elem(arr, n) (arr->eles[n]) |
32 |
#define array_init_decl { .eles = NULL, .num = 0, } |
33 |
#define array_cnt(arr) (arr)->num |
34 |
#define DECLARE_ARRAY(arr) array_t _##arr = array_init_decl, *arr = &_##arr |
35 |
#define ARRAY_INC_SIZE 32 |
36 |
|
37 |
+static void *xarrayget(array_t *arr, size_t idx) |
38 |
+{ |
39 |
+ if (idx >= arr->num) |
40 |
+ return NULL; |
41 |
+ return arr->eles[idx]; |
42 |
+} |
43 |
+ |
44 |
/* Push a pointer to memory we already hold and don't want to release. Do not |
45 |
* mix xarraypush_ptr usage with the other push funcs which duplicate memory. |
46 |
* The free stage won't know which pointers to release directly. |
47 |
@@ -51,7 +60,10 @@ static void *xarraypush(array_t *arr, const void *ele, size_t ele_len) |
48 |
static void xarraydelete_ptr(array_t *arr, size_t elem) |
49 |
{ |
50 |
arr->num--; |
51 |
- memmove(&arr->eles[elem], &arr->eles[elem + 1], arr->num - elem); |
52 |
+ if (elem < arr->num) |
53 |
+ memmove(&arr->eles[elem], &arr->eles[elem + 1], |
54 |
+ sizeof(arr->eles[0]) * (arr->num - elem)); |
55 |
+ arr->eles[arr->num] = NULL; |
56 |
} |
57 |
|
58 |
static void xarraydelete(array_t *arr, size_t elem) |