1 /* Generic vector interface routine
2 * Copyright (C) 1997 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 #include <osmocom/vty/vector.h>
26 #include <osmocom/vty/vty.h>
27 #include <osmocom/core/talloc.h>
30 void *tall_vty_vec_ctx;
32 /* Initialize vector : allocate memory and return vector. */
33 vector vector_init(unsigned int size)
35 vector v = talloc_zero(tall_vty_vec_ctx, struct _vector);
39 /* allocate at least one slot */
45 v->index = _talloc_zero(tall_vty_vec_ctx, sizeof(void *) * size,
54 void vector_only_wrapper_free(vector v)
59 void vector_only_index_free(void *index)
64 void vector_free(vector v)
66 talloc_free(v->index);
70 vector vector_copy(vector v)
73 vector new = talloc_zero(tall_vty_vec_ctx, struct _vector);
77 new->active = v->active;
78 new->alloced = v->alloced;
80 size = sizeof(void *) * (v->alloced);
81 new->index = _talloc_zero(tall_vty_vec_ctx, size, "vector_copy:index");
86 memcpy(new->index, v->index, size);
91 /* Check assigned index, and if it runs short double index pointer */
92 void vector_ensure(vector v, unsigned int num)
97 v->index = talloc_realloc_size(tall_vty_vec_ctx, v->index,
98 sizeof(void *) * (v->alloced * 2));
99 memset(&v->index[v->alloced], 0, sizeof(void *) * v->alloced);
102 if (v->alloced <= num)
103 vector_ensure(v, num);
106 /* This function only returns next empty slot index. It dose not mean
107 the slot's index memory is assigned, please call vector_ensure()
108 after calling this function. */
109 int vector_empty_slot(vector v)
116 for (i = 0; i < v->active; i++)
117 if (v->index[i] == 0)
123 /* Set value to the smallest empty slot. */
124 int vector_set(vector v, void *val)
128 i = vector_empty_slot(v);
139 /* Set value to specified index slot. */
140 int vector_set_index(vector v, unsigned int i, void *val)
152 /* Look up vector. */
153 void *vector_lookup(vector v, unsigned int i)
160 /* Lookup vector, ensure it. */
161 void *vector_lookup_ensure(vector v, unsigned int i)
167 /* Unset value at specified index slot. */
168 void vector_unset(vector v, unsigned int i)
175 if (i + 1 == v->active) {
177 while (i && v->index[--i] == NULL && v->active--) ; /* Is this ugly ? */
181 /* Count the number of not emplty slot. */
182 unsigned int vector_count(vector v)
187 for (i = 0; i < v->active; i++)
188 if (v->index[i] != NULL)