]>
git.proxmox.com Git - mirror_frr.git/blob - lib/vector.c
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
27 DEFINE_MTYPE_STATIC(LIB
, VECTOR
, "Vector")
28 DEFINE_MTYPE( LIB
, VECTOR_INDEX
, "Vector index")
30 /* Initialize vector : allocate memory and return vector. */
32 vector_init (unsigned int size
)
34 vector v
= XCALLOC (MTYPE_VECTOR
, sizeof (struct _vector
));
36 /* allocate at least one slot */
42 v
->index
= XCALLOC (MTYPE_VECTOR_INDEX
, sizeof (void *) * size
);
47 vector_only_wrapper_free (vector v
)
49 XFREE (MTYPE_VECTOR
, v
);
53 vector_only_index_free (void *index
)
55 XFREE (MTYPE_VECTOR_INDEX
, index
);
59 vector_free (vector v
)
61 XFREE (MTYPE_VECTOR_INDEX
, v
->index
);
62 XFREE (MTYPE_VECTOR
, v
);
66 vector_copy (vector v
)
69 vector
new = XCALLOC (MTYPE_VECTOR
, sizeof (struct _vector
));
71 new->active
= v
->active
;
72 new->alloced
= v
->alloced
;
74 size
= sizeof (void *) * (v
->alloced
);
75 new->index
= XCALLOC (MTYPE_VECTOR_INDEX
, size
);
76 memcpy (new->index
, v
->index
, size
);
81 /* Check assigned index, and if it runs short double index pointer */
83 vector_ensure (vector v
, unsigned int num
)
88 v
->index
= XREALLOC (MTYPE_VECTOR_INDEX
,
89 v
->index
, sizeof (void *) * (v
->alloced
* 2));
90 memset (&v
->index
[v
->alloced
], 0, sizeof (void *) * v
->alloced
);
93 if (v
->alloced
<= num
)
94 vector_ensure (v
, num
);
97 /* This function only returns next empty slot index. It dose not mean
98 the slot's index memory is assigned, please call vector_ensure()
99 after calling this function. */
101 vector_empty_slot (vector v
)
108 for (i
= 0; i
< v
->active
; i
++)
109 if (v
->index
[i
] == 0)
115 /* Set value to the smallest empty slot. */
117 vector_set (vector v
, void *val
)
121 i
= vector_empty_slot (v
);
122 vector_ensure (v
, i
);
132 /* Set value to specified index slot. */
134 vector_set_index (vector v
, unsigned int i
, void *val
)
136 vector_ensure (v
, i
);
146 /* Look up vector. */
148 vector_lookup (vector v
, unsigned int i
)
155 /* Lookup vector, ensure it. */
157 vector_lookup_ensure (vector v
, unsigned int i
)
159 vector_ensure (v
, i
);
163 /* Unset value at specified index slot. */
165 vector_unset (vector v
, unsigned int i
)
172 if (i
+ 1 == v
->active
)
175 while (i
&& v
->index
[--i
] == NULL
&& v
->active
--)
176 ; /* Is this ugly ? */
180 /* Count the number of not emplty slot. */
182 vector_count (vector v
)
187 for (i
= 0; i
< v
->active
; i
++)
188 if (v
->index
[i
] != NULL
)