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 34 73.5 %
Date: 2015-10-31 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           3 : static struct sparse *sparse_add(uint64_t fingerprint)
       9             : {
      10             :         struct sparse *sparse;
      11           3 :         if(!(sparse=(struct sparse *)
      12             :                 calloc_w(1, sizeof(struct sparse), __func__)))
      13           0 :                         return NULL;
      14           3 :         sparse->fingerprint=fingerprint;
      15           3 :         HASH_ADD_INT(sparse_table, fingerprint, sparse);
      16           3 :         return sparse;
      17             : }
      18             : 
      19          10 : struct sparse *sparse_find(uint64_t *fingerprint)
      20             : {
      21          10 :         struct sparse *sparse=NULL;
      22          10 :         HASH_FIND_INT(sparse_table, fingerprint, sparse);
      23          10 :         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             : 
      41           6 : int sparse_add_candidate(uint64_t *fingerprint, struct candidate *candidate)
      42             : {
      43             :         static size_t s;
      44             :         static struct sparse *sparse;
      45             : 
      46           6 :         if((sparse=sparse_find(fingerprint)))
      47             :         {
      48             :                 // Do not add it to the list if it has already been added.
      49           7 :                 for(s=0; s<sparse->size; s++)
      50           4 :                         if((sparse->candidates[s]==candidate))
      51           0 :                                 return 0;
      52             :         }
      53             : 
      54           6 :         if(!sparse && !(sparse=sparse_add(*fingerprint)))
      55           0 :                 return -1;
      56          12 :         if(!(sparse->candidates=(struct candidate **)
      57             :                 realloc_w(sparse->candidates,
      58          12 :                         (sparse->size+1)*sizeof(struct candidate *), __func__)))
      59           0 :                                 return -1;
      60           6 :         sparse->candidates[sparse->size++]=candidate;
      61             :         
      62           6 :         return 0;
      63             : }
      64             : 
      65           0 : void sparse_delete_fresh_candidate(struct candidate *candidate)
      66             : {
      67             :         struct sparse *tmp;
      68             :         struct sparse *sparse;
      69             : 
      70           0 :         HASH_ITER(hh, sparse_table, sparse, tmp)
      71             :         {
      72             :                 // Only works if the candidate being deleted is the most recent
      73             :                 // one added. Which is fine for candidate_add_fresh().
      74           0 :                 if(sparse->candidates[sparse->size-1]==candidate)
      75           0 :                         sparse->size--;
      76             :         }
      77           0 : }

Generated by: LCOV version 1.10