su_vector.c 7.75 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**@defgroup su_vector Vectors
 *
 * Pointer vectors.
 *
 * Unidimensional arrays using #su_home_t.
 *
 */

/**@ingroup su_vector
 * @CFILE su_vector.c
 * @brief Simple pointer vectors
 *
 * The vectors are resizeable unidimensional arrays.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
40
 *
Pekka Pessi's avatar
Pekka Pessi committed
41 42 43 44 45 46 47 48 49 50 51 52 53
 * @date Created: Fri Sep 27 14:43:29 2002 ppessi
 */

#include "config.h"

#include <stdlib.h>
#include <stddef.h>
#include <memory.h>
#include <limits.h>
#include <string.h>

#include <assert.h>

54 55
#include "sofia-sip/su_config.h"
#include "sofia-sip/su_vector.h"
Pekka Pessi's avatar
Pekka Pessi committed
56 57 58 59 60 61 62

enum { N = 8 };

struct su_vector_s
{
  su_home_t         v_home[1];
  su_home_t        *v_parent;
63 64
  size_t            v_size;
  size_t            v_len;
Pekka Pessi's avatar
Pekka Pessi committed
65 66 67 68
  su_free_func_t    v_free_func;
  void              **v_list;
};

69
/** Create a vector.
Pekka Pessi's avatar
Pekka Pessi committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
 *
 * The function su_vector_create() creates a pointer vector object. The
 * vector is initially empty. The function clones a memory home for the
 * vector object from @a home. If a @a free_func is provided then that
 * will be used to free the individual nodes (NULL if not used).
 */
su_vector_t *su_vector_create(su_home_t *home, su_free_func_t free_func)
{
  su_vector_t *vector;

  vector = su_home_clone(home, sizeof(*vector) + N * sizeof(*vector->v_list));
  if (vector) {
    vector->v_parent = home;
    vector->v_size = N;
    vector->v_free_func = free_func;
    vector->v_list = (void **)(vector + 1);
  }
  return vector;
}

/** Destroy a vector.
 *
 * The function su_vector_destroy() destroys a vector and frees all
 * its nodes if a freeing function is available
 */
void su_vector_destroy(su_vector_t *vector)
{
97
  size_t i;
Pekka Pessi's avatar
Pekka Pessi committed
98 99 100 101 102 103 104 105

  if (vector) {
    if (vector->v_free_func != NULL) {
      for (i = 0; i < vector->v_len; i++) {
        (vector->v_free_func)(vector->v_list[i]);
      }
    }

106
    su_home_zap(vector->v_home);
Pekka Pessi's avatar
Pekka Pessi committed
107 108 109 110
  }
}

/** Increase the list size for next item, if necessary. */
111
static int su_vector_make_place(su_vector_t *vector, usize_t index)
Pekka Pessi's avatar
Pekka Pessi committed
112 113
{
  if (vector->v_size <= vector->v_len + 1) {
114
    size_t newsize = 2 * vector->v_size * sizeof(vector->v_list[0]);
Pekka Pessi's avatar
Pekka Pessi committed
115 116
    void **list;

117
    if (newsize < vector->v_size * sizeof(vector->v_list[0])) /* overflow */
Pekka Pessi's avatar
Pekka Pessi committed
118 119 120
      return -1;

    if (vector->v_list != (void **)(vector + 1) && index == vector->v_len) {
121
      if (!(list = su_realloc(vector->v_home, vector->v_list, newsize)))
Pekka Pessi's avatar
Pekka Pessi committed
122 123 124
	return 0;
    }
    else {
125
      if (!(list = su_alloc(vector->v_home, newsize)))
Pekka Pessi's avatar
Pekka Pessi committed
126 127 128
	return 0;

      memcpy(list, vector->v_list, index * sizeof(vector->v_list[0]));
129
      memcpy(list + index + 1, vector->v_list + index,
Pekka Pessi's avatar
Pekka Pessi committed
130 131 132 133 134 135
	     (vector->v_len - index) * sizeof(vector->v_list[0]));

      if (vector->v_list != (void **)(vector + 1)) {
	su_free(vector->v_home, vector->v_list);
      }
    }
136

Pekka Pessi's avatar
Pekka Pessi committed
137
    vector->v_list = list;
138
    vector->v_size *= 2;
Pekka Pessi's avatar
Pekka Pessi committed
139 140
  }
  else {
141
    memmove(vector->v_list + index + 1, vector->v_list + index,
Pekka Pessi's avatar
Pekka Pessi committed
142 143 144 145 146 147 148 149
	    (vector->v_len - index) * sizeof(vector->v_list[0]));
  }

  vector->v_len++;

  return 1;
}

150
/**Insert an item to vector.
Pekka Pessi's avatar
Pekka Pessi committed
151 152 153 154 155 156 157
 *
 * The function su_vector_insert() inserts an @a item to the @a vector.
 * The items after the @a index will be moved further within the vector.
 *
 * @param vector pointer to a vector object
 * @param item   item to be appended
 * @param index  index for the new item
158 159 160
 *
 * @retval 0 when successful
 * @retval -1 upon an error
Pekka Pessi's avatar
Pekka Pessi committed
161
 */
162
int su_vector_insert(su_vector_t *vector, usize_t index, void *item)
Pekka Pessi's avatar
Pekka Pessi committed
163
{
164
  if (vector &&
Pekka Pessi's avatar
Pekka Pessi committed
165
      index <= vector->v_len &&
166
      su_vector_make_place(vector, index) > 0) {
Pekka Pessi's avatar
Pekka Pessi committed
167 168 169 170 171 172
    vector->v_list[index] = item;
    return 0;
  }
  return -1;
}

173
/**Remove an item from vector.
Pekka Pessi's avatar
Pekka Pessi committed
174 175 176 177 178 179
 *
 * The function su_vector_remove() removes an item from the @a vector.
 * The items after the @a index will be moved backwards within the vector.
 *
 * @param vector pointer to a vector object
 * @param index  index for the removed item
180 181 182
 *
 * @retval 0 when successful
 * @retval -1 upon an error
Pekka Pessi's avatar
Pekka Pessi committed
183
 */
184
int su_vector_remove(su_vector_t *vector, usize_t index)
Pekka Pessi's avatar
Pekka Pessi committed
185 186 187 188
{
  if (vector && index < vector->v_len) {
    if (vector->v_free_func)
        (vector->v_free_func)(vector->v_list[index]);
189 190

    memmove(vector->v_list + index,
Pekka Pessi's avatar
Pekka Pessi committed
191 192 193 194 195 196 197 198 199
            vector->v_list + index + 1,
            (vector->v_len - index - 1) * sizeof(vector->v_list[0]));
    vector->v_len--;
    return 0;
  }

  return -1;
}

200
/**Remove all items from vector.
Pekka Pessi's avatar
Pekka Pessi committed
201 202 203 204
 *
 * The function su_vector_empty() removes all items from the @a vector.
 *
 * @param vector pointer to a vector object
205 206 207
 *
 * @retval 0 if successful
 * @retval -1 upon an error
Pekka Pessi's avatar
Pekka Pessi committed
208 209 210
 */
int su_vector_empty(su_vector_t *vector)
{
211
  size_t i;
Pekka Pessi's avatar
Pekka Pessi committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226

  if (vector) {
    if (vector->v_free_func != NULL) {
      for (i = 0; i < vector->v_len; i++) {
        (vector->v_free_func)(vector->v_list[i]);
      }
    }

    vector->v_len = 0;
    return 0;
  }

  return -1;
}

227
/**Append an item to vector.
Pekka Pessi's avatar
Pekka Pessi committed
228 229 230 231 232 233 234 235 236 237 238
 *
 * The function su_vector_append() appends an @a item to the @a vector.
 *
 * @param vector  pointer to a vector object
 * @param item    item to be appended
 *
 * @retval 0 if successful
 * @retval -1 upon an error
 */
int su_vector_append(su_vector_t *vector, void *item)
{
239
  size_t index;
Pekka Pessi's avatar
Pekka Pessi committed
240

241 242 243 244 245 246 247 248 249 250
  if (vector == 0)
    return -1;

  index = vector->v_len;

  if (su_vector_make_place(vector, index) <= 0)
    return -1;

  vector->v_list[index] = item;
  return 0;
Pekka Pessi's avatar
Pekka Pessi committed
251 252
}

253
/**Get a numbered item from list.
Pekka Pessi's avatar
Pekka Pessi committed
254 255 256
 *
 * The function su_vector_item() returns a numbered item from vector. The
 * numbering starts from 0.
257
 *
Pekka Pessi's avatar
Pekka Pessi committed
258 259 260 261 262 263
 * @param vector  pointer to a vector object
 * @param i     index
 *
 * @return
 * Pointer, if item exists, or NULL upon an error.
 */
264
void *su_vector_item(su_vector_t const *vector, usize_t i)
Pekka Pessi's avatar
Pekka Pessi committed
265 266 267 268 269 270 271
{
  if (vector && i < vector->v_len)
    return vector->v_list[i];
  else
    return NULL;
}

272
/** Get number of items in list.
Pekka Pessi's avatar
Pekka Pessi committed
273
 *
274
 * The function su_vector_len() returns the number of items in the
Pekka Pessi's avatar
Pekka Pessi committed
275 276
 * vector.
 */
277
usize_t su_vector_len(su_vector_t const *l)
Pekka Pessi's avatar
Pekka Pessi committed
278 279 280 281 282 283 284 285 286
{
  return l ? l->v_len : 0;
}

int su_vector_is_empty(su_vector_t const *vector)
{
  return su_vector_len(vector) == 0;
}

287
/**Get a pointer array from list.
Pekka Pessi's avatar
Pekka Pessi committed
288 289 290 291
 *
 * The function su_vector_get_array() returns an array of pointer. The
 * length of the array is always one longer than the length of the vector,
 * and the last item in the returned array is always NULL.
292
 *
Pekka Pessi's avatar
Pekka Pessi committed
293 294 295 296 297 298 299 300
 * @param vector  pointer to a vector object
 *
 * @return
 * Pointer to array, or NULL if error occurred.
 */
void **su_vector_get_array(su_vector_t *vector)
{
  if (vector) {
301 302 303 304
    void **retval;
    size_t newsize = sizeof(retval[0]) * (vector->v_len + 1);

    retval = su_alloc(vector->v_home, newsize);
305

Pekka Pessi's avatar
Pekka Pessi committed
306 307 308 309 310 311 312 313 314
    if (retval) {
      retval[vector->v_len] = NULL;
      return memcpy(retval, vector->v_list, sizeof(retval[0]) * vector->v_len);
    }
  }

  return NULL;
}

315
/**Free a string array.
Pekka Pessi's avatar
Pekka Pessi committed
316 317 318
 *
 * The function su_vector_free_array() discards a string array allocated
 * with su_vector_get_array().
319
 *
Pekka Pessi's avatar
Pekka Pessi committed
320 321 322 323 324 325 326 327
 * @param vector  pointer to a vector object
 * @param array  string array to be freed
 *
 */
void su_vector_free_array(su_vector_t *vector, void **array)
{
  su_free(vector->v_home, array);
}