Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 1 | /* Generic vector interface routine |
| 2 | * Copyright (C) 1997 Kunihiro Ishiguro |
| 3 | * |
| 4 | * This file is part of GNU Zebra. |
| 5 | * |
| 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 |
| 9 | * later version. |
| 10 | * |
| 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. |
| 15 | * |
| 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 |
| 19 | * 02111-1307, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <stdlib.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | #include <vty/vector.h> |
Harald Welte | 1bf1972 | 2009-08-07 13:25:41 +0200 | [diff] [blame^] | 26 | #include <vty/vty.h> |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 27 | #include <openbsc/talloc.h> |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 28 | #include <memory.h> |
| 29 | |
| 30 | /* Initialize vector : allocate memory and return vector. */ |
| 31 | vector vector_init(unsigned int size) |
| 32 | { |
Harald Welte | 1bf1972 | 2009-08-07 13:25:41 +0200 | [diff] [blame^] | 33 | vector v = talloc_zero(tall_vty_ctx, struct _vector); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 34 | if (!v) |
| 35 | return NULL; |
| 36 | |
| 37 | /* allocate at least one slot */ |
| 38 | if (size == 0) |
| 39 | size = 1; |
| 40 | |
| 41 | v->alloced = size; |
| 42 | v->active = 0; |
Harald Welte | 1bf1972 | 2009-08-07 13:25:41 +0200 | [diff] [blame^] | 43 | v->index = _talloc_zero(tall_vty_ctx, sizeof(void *) * size, |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 44 | "vector_init:index"); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 45 | if (!v->index) { |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 46 | talloc_free(v); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 47 | return NULL; |
| 48 | } |
| 49 | return v; |
| 50 | } |
| 51 | |
| 52 | void vector_only_wrapper_free(vector v) |
| 53 | { |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 54 | talloc_free(v); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void vector_only_index_free(void *index) |
| 58 | { |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 59 | talloc_free(index); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void vector_free(vector v) |
| 63 | { |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 64 | talloc_free(v->index); |
| 65 | talloc_free(v); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | vector vector_copy(vector v) |
| 69 | { |
| 70 | unsigned int size; |
Harald Welte | 1bf1972 | 2009-08-07 13:25:41 +0200 | [diff] [blame^] | 71 | vector new = talloc_zero(tall_vty_ctx, struct _vector); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 72 | if (!new) |
| 73 | return NULL; |
| 74 | |
| 75 | new->active = v->active; |
| 76 | new->alloced = v->alloced; |
| 77 | |
| 78 | size = sizeof(void *) * (v->alloced); |
Harald Welte | 1bf1972 | 2009-08-07 13:25:41 +0200 | [diff] [blame^] | 79 | new->index = _talloc_zero(tall_vty_ctx, size, "vector_copy:index"); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 80 | if (!new->index) { |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 81 | talloc_free(new); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 82 | return NULL; |
| 83 | } |
| 84 | memcpy(new->index, v->index, size); |
| 85 | |
| 86 | return new; |
| 87 | } |
| 88 | |
| 89 | /* Check assigned index, and if it runs short double index pointer */ |
| 90 | void vector_ensure(vector v, unsigned int num) |
| 91 | { |
| 92 | if (v->alloced > num) |
| 93 | return; |
| 94 | |
Harald Welte | 1bf1972 | 2009-08-07 13:25:41 +0200 | [diff] [blame^] | 95 | v->index = talloc_realloc_size(tall_vty_ctx, v->index, |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 96 | sizeof(void *) * (v->alloced * 2)); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 97 | memset(&v->index[v->alloced], 0, sizeof(void *) * v->alloced); |
| 98 | v->alloced *= 2; |
| 99 | |
| 100 | if (v->alloced <= num) |
| 101 | vector_ensure(v, num); |
| 102 | } |
| 103 | |
| 104 | /* This function only returns next empty slot index. It dose not mean |
| 105 | the slot's index memory is assigned, please call vector_ensure() |
| 106 | after calling this function. */ |
| 107 | int vector_empty_slot(vector v) |
| 108 | { |
| 109 | unsigned int i; |
| 110 | |
| 111 | if (v->active == 0) |
| 112 | return 0; |
| 113 | |
| 114 | for (i = 0; i < v->active; i++) |
| 115 | if (v->index[i] == 0) |
| 116 | return i; |
| 117 | |
| 118 | return i; |
| 119 | } |
| 120 | |
| 121 | /* Set value to the smallest empty slot. */ |
| 122 | int vector_set(vector v, void *val) |
| 123 | { |
| 124 | unsigned int i; |
| 125 | |
| 126 | i = vector_empty_slot(v); |
| 127 | vector_ensure(v, i); |
| 128 | |
| 129 | v->index[i] = val; |
| 130 | |
| 131 | if (v->active <= i) |
| 132 | v->active = i + 1; |
| 133 | |
| 134 | return i; |
| 135 | } |
| 136 | |
| 137 | /* Set value to specified index slot. */ |
| 138 | int vector_set_index(vector v, unsigned int i, void *val) |
| 139 | { |
| 140 | vector_ensure(v, i); |
| 141 | |
| 142 | v->index[i] = val; |
| 143 | |
| 144 | if (v->active <= i) |
| 145 | v->active = i + 1; |
| 146 | |
| 147 | return i; |
| 148 | } |
| 149 | |
| 150 | /* Look up vector. */ |
| 151 | void *vector_lookup(vector v, unsigned int i) |
| 152 | { |
| 153 | if (i >= v->active) |
| 154 | return NULL; |
| 155 | return v->index[i]; |
| 156 | } |
| 157 | |
| 158 | /* Lookup vector, ensure it. */ |
| 159 | void *vector_lookup_ensure(vector v, unsigned int i) |
| 160 | { |
| 161 | vector_ensure(v, i); |
| 162 | return v->index[i]; |
| 163 | } |
| 164 | |
| 165 | /* Unset value at specified index slot. */ |
| 166 | void vector_unset(vector v, unsigned int i) |
| 167 | { |
| 168 | if (i >= v->alloced) |
| 169 | return; |
| 170 | |
| 171 | v->index[i] = NULL; |
| 172 | |
| 173 | if (i + 1 == v->active) { |
| 174 | v->active--; |
| 175 | while (i && v->index[--i] == NULL && v->active--) ; /* Is this ugly ? */ |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /* Count the number of not emplty slot. */ |
| 180 | unsigned int vector_count(vector v) |
| 181 | { |
| 182 | unsigned int i; |
| 183 | unsigned count = 0; |
| 184 | |
| 185 | for (i = 0; i < v->active; i++) |
| 186 | if (v->index[i] != NULL) |
| 187 | count++; |
| 188 | |
| 189 | return count; |
| 190 | } |