back to topotato report
topotato coverage report
Current view: top level - lib - vector.c (source / functions) Hit Total Coverage
Test: test_pim6_prune_propagate.py::PIM6PrunePropagate Lines: 68 110 61.8 %
Date: 2023-02-24 18:39:23 Functions: 14 19 73.7 %

          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 along
      17             :  * with this program; see the file COPYING; if not, write to the Free Software
      18             :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
      19             :  */
      20             : 
      21             : #include <zebra.h>
      22             : 
      23             : #include "vector.h"
      24             : #include "memory.h"
      25             : 
      26          44 : DEFINE_MTYPE_STATIC(LIB, VECTOR, "Vector");
      27          44 : DEFINE_MTYPE_STATIC(LIB, VECTOR_INDEX, "Vector index");
      28             : 
      29             : /* Initialize vector : allocate memory and return vector. */
      30      398524 : vector vector_init(unsigned int size)
      31             : {
      32      398524 :         vector v = XCALLOC(MTYPE_VECTOR, sizeof(struct _vector));
      33             : 
      34             :         /* allocate at least one slot */
      35      398524 :         if (size == 0)
      36             :                 size = 1;
      37             : 
      38      398524 :         v->alloced = size;
      39      398524 :         v->active = 0;
      40      398524 :         v->count = 0;
      41      398524 :         v->index = XCALLOC(MTYPE_VECTOR_INDEX, sizeof(void *) * size);
      42      398524 :         return v;
      43             : }
      44             : 
      45      240340 : void vector_free(vector v)
      46             : {
      47      240340 :         XFREE(MTYPE_VECTOR_INDEX, v->index);
      48      240340 :         XFREE(MTYPE_VECTOR, v);
      49      240340 : }
      50             : 
      51           0 : vector vector_copy(vector v)
      52             : {
      53           0 :         unsigned int size;
      54           0 :         vector new = XCALLOC(MTYPE_VECTOR, sizeof(struct _vector));
      55             : 
      56           0 :         new->active = v->active;
      57           0 :         new->alloced = v->alloced;
      58           0 :         new->count = v->count;
      59             : 
      60           0 :         size = sizeof(void *) * (v->alloced);
      61           0 :         new->index = XCALLOC(MTYPE_VECTOR_INDEX, size);
      62           0 :         memcpy(new->index, v->index, size);
      63             : 
      64           0 :         return new;
      65             : }
      66             : 
      67             : /* Check assigned index, and if it runs short double index pointer */
      68      782869 : void vector_ensure(vector v, unsigned int num)
      69             : {
      70      782901 :         if (v->alloced > num)
      71             :                 return;
      72             : 
      73      127573 :         v->index = XREALLOC(MTYPE_VECTOR_INDEX, v->index,
      74             :                             sizeof(void *) * (v->alloced * 2));
      75      127573 :         memset(&v->index[v->alloced], 0, sizeof(void *) * v->alloced);
      76      127573 :         v->alloced *= 2;
      77             : 
      78      127573 :         if (v->alloced <= num)
      79             :                 vector_ensure(v, num);
      80             : }
      81             : 
      82             : /* This function only returns next empty slot index.  It dose not mean
      83             :    the slot's index memory is assigned, please call vector_ensure()
      84             :    after calling this function. */
      85      780680 : int vector_empty_slot(vector v)
      86             : {
      87      780680 :         unsigned int i;
      88             : 
      89      780680 :         if (v->active == v->count)
      90      764400 :                 return v->active;
      91             : 
      92       16280 :         if (v->active == 0)
      93             :                 return 0;
      94             : 
      95         102 :         for (i = 0; i < v->active; i++)
      96          51 :                 if (v->index[i] == 0)
      97           0 :                         return i;
      98             : 
      99          51 :         return i;
     100             : }
     101             : 
     102             : /* Set value to the smallest empty slot. */
     103      780680 : int vector_set(vector v, void *val)
     104             : {
     105      780680 :         unsigned int i;
     106             : 
     107      780680 :         i = vector_empty_slot(v);
     108      780680 :         vector_ensure(v, i);
     109             : 
     110      780680 :         if (v->index[i])
     111           0 :                 v->count--;
     112      780680 :         if (val)
     113      780680 :                 v->count++;
     114      780680 :         v->index[i] = val;
     115             : 
     116      780680 :         if (v->active <= i)
     117      780680 :                 v->active = i + 1;
     118             : 
     119      780680 :         return i;
     120             : }
     121             : 
     122             : /* Set value to specified index slot. */
     123        2189 : int vector_set_index(vector v, unsigned int i, void *val)
     124             : {
     125        2189 :         vector_ensure(v, i);
     126             : 
     127        2189 :         if (v->index[i])
     128           0 :                 v->count--;
     129        2189 :         if (val)
     130        2189 :                 v->count++;
     131        2189 :         v->index[i] = val;
     132             : 
     133        2189 :         if (v->active <= i)
     134        1793 :                 v->active = i + 1;
     135             : 
     136        2189 :         return i;
     137             : }
     138             : 
     139             : /* Look up vector.  */
     140       26476 : void *vector_lookup(vector v, unsigned int i)
     141             : {
     142       26476 :         if (i >= v->active)
     143             :                 return NULL;
     144       26476 :         return v->index[i];
     145             : }
     146             : 
     147             : /* Lookup vector, ensure it. */
     148           0 : void *vector_lookup_ensure(vector v, unsigned int i)
     149             : {
     150           0 :         vector_ensure(v, i);
     151           0 :         return v->index[i];
     152             : }
     153             : 
     154             : /* Unset value at specified index slot. */
     155      120190 : void vector_unset(vector v, unsigned int i)
     156             : {
     157      120190 :         if (i >= v->alloced)
     158             :                 return;
     159             : 
     160      120190 :         if (v->index[i])
     161      119988 :                 v->count--;
     162             : 
     163      120190 :         v->index[i] = NULL;
     164             : 
     165      120190 :         if (i + 1 == v->active) {
     166       21446 :                 v->active--;
     167      120190 :                 while (i && v->index[--i] == NULL && v->active--)
     168             :                         ; /* Is this ugly ? */
     169             :         }
     170             : }
     171             : 
     172           0 : void vector_remove(vector v, unsigned int ix)
     173             : {
     174           0 :         if (ix >= v->active)
     175             :                 return;
     176             : 
     177           0 :         if (v->index[ix])
     178           0 :                 v->count--;
     179             : 
     180           0 :         int n = (--v->active) - ix;
     181             : 
     182           0 :         memmove(&v->index[ix], &v->index[ix + 1], n * sizeof(void *));
     183           0 :         v->index[v->active] = NULL;
     184             : }
     185             : 
     186         446 : void vector_compact(vector v)
     187             : {
     188        1519 :         for (unsigned int i = 0; i < vector_active(v); ++i) {
     189        1073 :                 if (vector_slot(v, i) == NULL) {
     190           0 :                         vector_remove(v, i);
     191           0 :                         --i;
     192             :                 }
     193             :         }
     194         446 : }
     195             : 
     196           0 : void vector_unset_value(vector v, void *val)
     197             : {
     198           0 :         size_t i;
     199             : 
     200           0 :         for (i = 0; i < v->active; i++)
     201           0 :                 if (v->index[i] == val) {
     202           0 :                         v->index[i] = NULL;
     203           0 :                         v->count--;
     204           0 :                         break;
     205             :                 }
     206             : 
     207           0 :         if (i + 1 == v->active)
     208           0 :                 do
     209           0 :                         v->active--;
     210           0 :                 while (i && v->index[--i] == NULL);
     211           0 : }
     212             : 
     213           0 : void vector_to_array(vector v, void ***dest, int *argc)
     214             : {
     215           0 :         *dest = XCALLOC(MTYPE_TMP, sizeof(void *) * v->active);
     216           0 :         memcpy(*dest, v->index, sizeof(void *) * v->active);
     217           0 :         *argc = v->active;
     218           0 : }
     219             : 
     220         446 : vector array_to_vector(void **src, int argc)
     221             : {
     222         446 :         vector v = vector_init(VECTOR_MIN_SIZE);
     223             : 
     224        1721 :         for (int i = 0; i < argc; i++)
     225        1275 :                 vector_set_index(v, i, src[i]);
     226         446 :         return v;
     227             : }

Generated by: LCOV version v1.16-topotato