LCOV - code coverage report
Current view: top level - src/server/protocol2/champ_chooser - sparse.c (source / functions) Hit Total Coverage
Test: burp-coverage-clean.info Lines: 30 31 96.8 %
Date: 2016-11-07 Functions: 5 5 100.0 %

          Line data    Source code
       1             : #include "../../../burp.h"
       2             : #include "../../../alloc.h"
       3             : #include "candidate.h"
       4             : #include "sparse.h"
       5             : 
       6             : static struct sparse *sparse_table=NULL;
       7             : 
       8          37 : static struct sparse *sparse_add(uint64_t fingerprint)
       9             : {
      10             :         struct sparse *sparse;
      11          37 :         if(!(sparse=(struct sparse *)
      12             :                 calloc_w(1, sizeof(struct sparse), __func__)))
      13             :                         return NULL;
      14          36 :         sparse->fingerprint=fingerprint;
      15          42 :         HASH_ADD_INT(sparse_table, fingerprint, sparse);
      16          36 :         return sparse;
      17             : }
      18             : 
      19        4141 : struct sparse *sparse_find(uint64_t *fingerprint)
      20             : {
      21        4141 :         struct sparse *sparse=NULL;
      22        4141 :         HASH_FIND_INT(sparse_table, fingerprint, sparse);
      23        4141 :         return sparse;
      24             : }
      25             : 
      26           3 : void sparse_delete_all(void)
      27             : {
      28             :         struct sparse *tmp;
      29             :         struct sparse *sparse;
      30             : 
      31          39 :         HASH_ITER(hh, sparse_table, sparse, tmp)
      32             :         {
      33          36 :                 HASH_DEL(sparse_table, sparse);
      34          36 :                 free_v((void **)&sparse->candidates);
      35          36 :                 free_v((void **)&sparse);
      36             :         }
      37           3 :         sparse_table=NULL;
      38           3 : }
      39             : 
      40          41 : int sparse_add_candidate(uint64_t *fingerprint, struct candidate *candidate)
      41             : {
      42             :         static size_t s;
      43             :         static struct sparse *sparse;
      44             : 
      45          41 :         if((sparse=sparse_find(fingerprint)))
      46             :         {
      47             :                 // Do not add it to the list if it has already been added.
      48           8 :                 for(s=0; s<sparse->size; s++)
      49           5 :                         if(sparse->candidates[s]==candidate)
      50             :                                 return 0;
      51             :         }
      52             : 
      53          40 :         if(!sparse && !(sparse=sparse_add(*fingerprint)))
      54             :                 return -1;
      55          39 :         if(!(sparse->candidates=(struct candidate **)
      56          39 :                 realloc_w(sparse->candidates,
      57          39 :                         (sparse->size+1)*sizeof(struct candidate *), __func__)))
      58             :                                 return -1;
      59          39 :         sparse->candidates[sparse->size++]=candidate;
      60             :         
      61          39 :         return 0;
      62             : }
      63             : 
      64           1 : void sparse_delete_fresh_candidate(struct candidate *candidate)
      65             : {
      66             :         struct sparse *tmp;
      67             :         struct sparse *sparse;
      68             : 
      69          34 :         HASH_ITER(hh, sparse_table, sparse, tmp)
      70             :         {
      71             :                 // Only works if the candidate being deleted is the most recent
      72             :                 // one added. Which is fine for candidate_add_fresh().
      73          33 :                 if(sparse->candidates[sparse->size-1]==candidate)
      74           0 :                         sparse->size--;
      75             :         }
      76           1 : }

Generated by: LCOV version 1.10