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: 25 30 83.3 %
Date: 2016-01-03 Functions: 4 5 80.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           4 : static struct sparse *sparse_add(uint64_t fingerprint)
       9             : {
      10             :         struct sparse *sparse;
      11           4 :         if(!(sparse=(struct sparse *)
      12             :                 calloc_w(1, sizeof(struct sparse), __func__)))
      13             :                         return NULL;
      14           3 :         sparse->fingerprint=fingerprint;
      15           7 :         HASH_ADD_INT(sparse_table, fingerprint, sparse);
      16           3 :         return sparse;
      17             : }
      18             : 
      19        4108 : struct sparse *sparse_find(uint64_t *fingerprint)
      20             : {
      21        4108 :         struct sparse *sparse=NULL;
      22        4108 :         HASH_FIND_INT(sparse_table, fingerprint, sparse);
      23        4108 :         return sparse;
      24             : }
      25             : 
      26           2 : void sparse_delete_all(void)
      27             : {
      28             :         struct sparse *tmp;
      29             :         struct sparse *sparse;
      30             : 
      31           5 :         HASH_ITER(hh, sparse_table, sparse, tmp)
      32             :         {
      33           3 :                 HASH_DEL(sparse_table, sparse);
      34           3 :                 free_v((void **)&sparse->candidates);
      35           3 :                 free_v((void **)&sparse);
      36             :         }
      37           2 :         sparse_table=NULL;
      38           2 : }
      39             : 
      40           8 : int sparse_add_candidate(uint64_t *fingerprint, struct candidate *candidate)
      41             : {
      42             :         static size_t s;
      43             :         static struct sparse *sparse;
      44             : 
      45           8 :         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           7 :         if(!sparse && !(sparse=sparse_add(*fingerprint)))
      54             :                 return -1;
      55           6 :         if(!(sparse->candidates=(struct candidate **)
      56             :                 realloc_w(sparse->candidates,
      57           6 :                         (sparse->size+1)*sizeof(struct candidate *), __func__)))
      58             :                                 return -1;
      59           6 :         sparse->candidates[sparse->size++]=candidate;
      60             :         
      61           6 :         return 0;
      62             : }
      63             : 
      64           0 : void sparse_delete_fresh_candidate(struct candidate *candidate)
      65             : {
      66             :         struct sparse *tmp;
      67             :         struct sparse *sparse;
      68             : 
      69           0 :         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           0 :                 if(sparse->candidates[sparse->size-1]==candidate)
      74           0 :                         sparse->size--;
      75             :         }
      76           0 : }

Generated by: LCOV version 1.10