LCOV - code coverage report
Current view: top level - lib - vector.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 55 74 74.3 %
Date: 2015-11-19 Functions: 9 13 69.2 %

          Line data    Source code
       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 <zebra.h>
      23             : 
      24             : #include "vector.h"
      25             : #include "memory.h"
      26             : 
      27             : /* Initialize vector : allocate memory and return vector. */
      28             : vector
      29       53289 : vector_init (unsigned int size)
      30             : {
      31       53289 :   vector v = XCALLOC (MTYPE_VECTOR, sizeof (struct _vector));
      32             : 
      33             :   /* allocate at least one slot */
      34       53289 :   if (size == 0)
      35           0 :     size = 1;
      36             : 
      37       53289 :   v->alloced = size;
      38       53289 :   v->active = 0;
      39       53289 :   v->index = XCALLOC (MTYPE_VECTOR_INDEX, sizeof (void *) * size);
      40       53289 :   return v;
      41             : }
      42             : 
      43             : void
      44           0 : vector_only_wrapper_free (vector v)
      45             : {
      46           0 :   XFREE (MTYPE_VECTOR, v);
      47           0 : }
      48             : 
      49             : void
      50           0 : vector_only_index_free (void *index)
      51             : {
      52           0 :   XFREE (MTYPE_VECTOR_INDEX, index);
      53           0 : }
      54             : 
      55             : void
      56       22428 : vector_free (vector v)
      57             : {
      58       22428 :   XFREE (MTYPE_VECTOR_INDEX, v->index);
      59       22428 :   XFREE (MTYPE_VECTOR, v);
      60       22428 : }
      61             : 
      62             : vector
      63         594 : vector_copy (vector v)
      64             : {
      65             :   unsigned int size;
      66         594 :   vector new = XCALLOC (MTYPE_VECTOR, sizeof (struct _vector));
      67             : 
      68         594 :   new->active = v->active;
      69         594 :   new->alloced = v->alloced;
      70             : 
      71         594 :   size = sizeof (void *) * (v->alloced);
      72         594 :   new->index = XCALLOC (MTYPE_VECTOR_INDEX, size);
      73         594 :   memcpy (new->index, v->index, size);
      74             : 
      75         594 :   return new;
      76             : }
      77             : 
      78             : /* Check assigned index, and if it runs short double index pointer */
      79             : void
      80      296987 : vector_ensure (vector v, unsigned int num)
      81             : {
      82      296987 :   if (v->alloced > num)
      83      223682 :     return;
      84             : 
      85       73305 :   v->index = XREALLOC (MTYPE_VECTOR_INDEX, 
      86             :                        v->index, sizeof (void *) * (v->alloced * 2));
      87       73305 :   memset (&v->index[v->alloced], 0, sizeof (void *) * v->alloced);
      88       73305 :   v->alloced *= 2;
      89             :   
      90       73305 :   if (v->alloced <= num)
      91        5237 :     vector_ensure (v, num);
      92             : }
      93             : 
      94             : /* This function only returns next empty slot index.  It dose not mean
      95             :    the slot's index memory is assigned, please call vector_ensure()
      96             :    after calling this function. */
      97             : int
      98      165918 : vector_empty_slot (vector v)
      99             : {
     100             :   unsigned int i;
     101             : 
     102      165918 :   if (v->active == 0)
     103       50903 :     return 0;
     104             : 
     105     2179811 :   for (i = 0; i < v->active; i++)
     106     2064796 :     if (v->index[i] == 0)
     107           0 :       return i;
     108             : 
     109      115015 :   return i;
     110             : }
     111             : 
     112             : /* Set value to the smallest empty slot. */
     113             : int
     114      165918 : vector_set (vector v, void *val)
     115             : {
     116             :   unsigned int i;
     117             : 
     118      165918 :   i = vector_empty_slot (v);
     119      165918 :   vector_ensure (v, i);
     120             : 
     121      165918 :   v->index[i] = val;
     122             : 
     123      165918 :   if (v->active <= i)
     124      165918 :     v->active = i + 1;
     125             : 
     126      165918 :   return i;
     127             : }
     128             : 
     129             : /* Set value to specified index slot. */
     130             : int
     131      125832 : vector_set_index (vector v, unsigned int i, void *val)
     132             : {
     133      125832 :   vector_ensure (v, i);
     134             : 
     135      125832 :   v->index[i] = val;
     136             : 
     137      125832 :   if (v->active <= i)
     138      125202 :     v->active = i + 1;
     139             : 
     140      125832 :   return i;
     141             : }
     142             : 
     143             : /* Look up vector.  */
     144             : void *
     145        2276 : vector_lookup (vector v, unsigned int i)
     146             : {
     147        2276 :   if (i >= v->active)
     148           0 :     return NULL;
     149        2276 :   return v->index[i];
     150             : }
     151             : 
     152             : /* Lookup vector, ensure it. */
     153             : void *
     154           0 : vector_lookup_ensure (vector v, unsigned int i)
     155             : {
     156           0 :   vector_ensure (v, i);
     157           0 :   return v->index[i];
     158             : }
     159             : 
     160             : /* Unset value at specified index slot. */
     161             : void
     162          47 : vector_unset (vector v, unsigned int i)
     163             : {
     164          47 :   if (i >= v->alloced)
     165           0 :     return;
     166             : 
     167          47 :   v->index[i] = NULL;
     168             : 
     169          47 :   if (i + 1 == v->active) 
     170             :     {
     171           2 :       v->active--;
     172           2 :       while (i && v->index[--i] == NULL && v->active--) 
     173             :         ;                               /* Is this ugly ? */
     174             :     }
     175             : }
     176             : 
     177             : /* Count the number of not emplty slot. */
     178             : unsigned int
     179           0 : vector_count (vector v)
     180             : {
     181             :   unsigned int i;
     182           0 :   unsigned count = 0;
     183             : 
     184           0 :   for (i = 0; i < v->active; i++) 
     185           0 :     if (v->index[i] != NULL)
     186           0 :       count++;
     187             : 
     188           0 :   return count;
     189             : }

Generated by: LCOV version 1.10