Gentoo Archives: gentoo-commits

From: Fabian Groffen <grobian@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/portage-utils:master commit in: libq/
Date: Wed, 27 Feb 2019 20:53:43
Message-Id: 1551300587.ace156ca85c3d5c804ec06e7cb4172f5b6d279e1.grobian@gentoo
1 commit: ace156ca85c3d5c804ec06e7cb4172f5b6d279e1
2 Author: Fabian Groffen <grobian <AT> gentoo <DOT> org>
3 AuthorDate: Tue Feb 26 08:32:50 2019 +0000
4 Commit: Fabian Groffen <grobian <AT> gentoo <DOT> org>
5 CommitDate: Wed Feb 27 20:49:47 2019 +0000
6 URL: https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=ace156ca
7
8 xarray: improve malloc efficiency, allow removing elements
9
10 Pre-allocate members such that we don't have to re-alloc for every
11 insertion.
12 Allow removing elements from the array, such that they can be used as
13 containers of running lists.
14
15 Signed-off-by: Fabian Groffen <grobian <AT> gentoo.org>
16
17 libq/xarray.c | 22 ++++++++++++++++++++--
18 1 file changed, 20 insertions(+), 2 deletions(-)
19
20 diff --git a/libq/xarray.c b/libq/xarray.c
21 index 64334bb..3ed9872 100644
22 --- a/libq/xarray.c
23 +++ b/libq/xarray.c
24 @@ -1,14 +1,16 @@
25 /*
26 - * Copyright 2003-2018 Gentoo Foundation
27 + * Copyright 2003-2019 Gentoo Foundation
28 * Distributed under the terms of the GNU General Public License v2
29 *
30 * Copyright 2003-2007 Ned Ludd - <solar@g.o>
31 * Copyright 2004-2014 Mike Frysinger - <vapier@g.o>
32 + * Copyright 2018- Fabian Groffen - <grobian@g.o>
33 */
34
35 typedef struct {
36 void **eles;
37 size_t num;
38 + size_t len;
39 } array_t;
40
41 #define xrealloc_array(ptr, size, ele_size) xrealloc(ptr, (size) * (ele_size))
42 @@ -23,6 +25,7 @@ typedef struct {
43 #define array_init_decl { .eles = NULL, .num = 0, }
44 #define array_cnt(arr) (arr)->num
45 #define DECLARE_ARRAY(arr) array_t _##arr = array_init_decl, *arr = &_##arr
46 +#define ARRAY_INC_SIZE 32
47
48 /* Push a pointer to memory we already hold and don't want to release. Do not
49 * mix xarraypush_ptr usage with the other push funcs which duplicate memory.
50 @@ -31,7 +34,10 @@ typedef struct {
51 static void *xarraypush_ptr(array_t *arr, void *ele)
52 {
53 size_t n = arr->num++;
54 - arr->eles = xrealloc_array(arr->eles, arr->num, sizeof(ele));
55 + if (arr->num > arr->len) {
56 + arr->len += ARRAY_INC_SIZE;
57 + arr->eles = xrealloc_array(arr->eles, arr->len, sizeof(ele));
58 + }
59 arr->eles[n] = ele;
60 return ele;
61 }
62 @@ -42,6 +48,18 @@ static void *xarraypush(array_t *arr, const void *ele, size_t ele_len)
63 #define xarraypush_str(arr, ele) xarraypush(arr, ele, strlen(ele) + 1 /*NUL*/)
64 #define xarraypush_struct(arr, ele) xarraypush(arr, ele, sizeof(*(ele)))
65
66 +static void xarraydelete_ptr(array_t *arr, size_t elem)
67 +{
68 + arr->num--;
69 + memmove(&arr->eles[elem], &arr->eles[elem + 1], arr->num - elem);
70 +}
71 +
72 +static void xarraydelete(array_t *arr, size_t elem)
73 +{
74 + free(arr->eles[elem]);
75 + xarraydelete_ptr(arr, elem);
76 +}
77 +
78 /* Useful for people who call xarraypush_ptr as it does not free any of the
79 * pointers in the eles list.
80 */