]>
git.proxmox.com Git - mirror_frr.git/blob - lib/vector.c
bbea67c12f22e850e1f99fffffb06dd6f2973efb
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Generic vector interface routine
3 * Copyright (C) 1997 Kunihiro Ishiguro
11 DEFINE_MTYPE_STATIC(LIB
, VECTOR
, "Vector");
12 DEFINE_MTYPE_STATIC(LIB
, VECTOR_INDEX
, "Vector index");
14 /* Initialize vector : allocate memory and return vector. */
15 vector
vector_init(unsigned int size
)
17 vector v
= XCALLOC(MTYPE_VECTOR
, sizeof(struct _vector
));
19 /* allocate at least one slot */
26 v
->index
= XCALLOC(MTYPE_VECTOR_INDEX
, sizeof(void *) * size
);
30 void vector_free(vector v
)
32 XFREE(MTYPE_VECTOR_INDEX
, v
->index
);
33 XFREE(MTYPE_VECTOR
, v
);
36 vector
vector_copy(vector v
)
39 vector
new = XCALLOC(MTYPE_VECTOR
, sizeof(struct _vector
));
41 new->active
= v
->active
;
42 new->alloced
= v
->alloced
;
43 new->count
= v
->count
;
45 size
= sizeof(void *) * (v
->alloced
);
46 new->index
= XCALLOC(MTYPE_VECTOR_INDEX
, size
);
47 memcpy(new->index
, v
->index
, size
);
52 /* Check assigned index, and if it runs short double index pointer */
53 void vector_ensure(vector v
, unsigned int num
)
58 v
->index
= XREALLOC(MTYPE_VECTOR_INDEX
, v
->index
,
59 sizeof(void *) * (v
->alloced
* 2));
60 memset(&v
->index
[v
->alloced
], 0, sizeof(void *) * v
->alloced
);
63 if (v
->alloced
<= num
)
64 vector_ensure(v
, num
);
67 /* This function only returns next empty slot index. It dose not mean
68 the slot's index memory is assigned, please call vector_ensure()
69 after calling this function. */
70 int vector_empty_slot(vector v
)
74 if (v
->active
== v
->count
)
80 for (i
= 0; i
< v
->active
; i
++)
87 /* Set value to the smallest empty slot. */
88 int vector_set(vector v
, void *val
)
92 i
= vector_empty_slot(v
);
107 /* Set value to specified index slot. */
108 int vector_set_index(vector v
, unsigned int i
, void *val
)
124 /* Look up vector. */
125 void *vector_lookup(vector v
, unsigned int i
)
132 /* Lookup vector, ensure it. */
133 void *vector_lookup_ensure(vector v
, unsigned int i
)
139 /* Unset value at specified index slot. */
140 void vector_unset(vector v
, unsigned int i
)
150 if (i
+ 1 == v
->active
) {
152 while (i
&& v
->index
[--i
] == NULL
&& v
->active
--)
153 ; /* Is this ugly ? */
157 void vector_remove(vector v
, unsigned int ix
)
165 int n
= (--v
->active
) - ix
;
167 memmove(&v
->index
[ix
], &v
->index
[ix
+ 1], n
* sizeof(void *));
168 v
->index
[v
->active
] = NULL
;
171 void vector_compact(vector v
)
173 for (unsigned int i
= 0; i
< vector_active(v
); ++i
) {
174 if (vector_slot(v
, i
) == NULL
) {
181 void vector_unset_value(vector v
, void *val
)
185 for (i
= 0; i
< v
->active
; i
++)
186 if (v
->index
[i
] == val
) {
192 if (i
+ 1 == v
->active
)
195 while (i
&& v
->index
[--i
] == NULL
);
198 void vector_to_array(vector v
, void ***dest
, int *argc
)
200 *dest
= XCALLOC(MTYPE_TMP
, sizeof(void *) * v
->active
);
201 memcpy(*dest
, v
->index
, sizeof(void *) * v
->active
);
205 vector
array_to_vector(void **src
, int argc
)
207 vector v
= vector_init(VECTOR_MIN_SIZE
);
209 for (int i
= 0; i
< argc
; i
++)
210 vector_set_index(v
, i
, src
[i
]);