LCOV - code coverage report
Current view: top level - libbpfilter - counter.c (source / functions) Coverage Total Hit
Test: coverage.lcov Lines: 100.0 % 25 25
Test Date: 2026-02-28 14:57:49 Functions: 100.0 % 4 4
Branches: 68.8 % 16 11

             Branch data     Line data    Source code
       1                 :             : /* SPDX-License-Identifier: GPL-2.0-only */
       2                 :             : /*
       3                 :             :  * Copyright (c) 2023 Meta Platforms, Inc. and affiliates.
       4                 :             :  */
       5                 :             : #include "bpfilter/counter.h"
       6                 :             : 
       7                 :             : #include <errno.h>
       8                 :             : #include <stdint.h>
       9                 :             : #include <stdlib.h>
      10                 :             : #include <string.h>
      11                 :             : 
      12                 :             : #include "bpfilter/helper.h"
      13                 :             : 
      14                 :         238 : int bf_counter_new(struct bf_counter **counter, uint64_t count, uint64_t size)
      15                 :             : {
      16                 :             :     _cleanup_free_ struct bf_counter *_counter = NULL;
      17                 :             : 
      18                 :             :     assert(counter);
      19                 :             : 
      20                 :         238 :     _counter = malloc(sizeof(*_counter));
      21         [ +  - ]:         238 :     if (!_counter)
      22                 :             :         return -ENOMEM;
      23                 :             : 
      24                 :         238 :     _counter->count = count;
      25                 :         238 :     _counter->size = size;
      26                 :             : 
      27                 :         238 :     *counter = TAKE_PTR(_counter);
      28                 :             : 
      29                 :         238 :     return 0;
      30                 :             : }
      31                 :             : 
      32                 :         119 : int bf_counter_new_from_pack(struct bf_counter **counter, bf_rpack_node_t node)
      33                 :             : {
      34                 :         119 :     _free_bf_counter_ struct bf_counter *_counter = NULL;
      35                 :             :     int r;
      36                 :             : 
      37                 :             :     assert(counter);
      38                 :             : 
      39                 :         119 :     r = bf_counter_new(&_counter, 0, 0);
      40         [ +  - ]:         119 :     if (r)
      41                 :             :         return r;
      42                 :             : 
      43                 :         119 :     r = bf_rpack_kv_u64(node, "count", &_counter->count);
      44         [ +  + ]:         119 :     if (r)
      45         [ +  - ]:           1 :         return bf_rpack_key_err(r, "bf_counter.count");
      46                 :             : 
      47                 :         118 :     r = bf_rpack_kv_u64(node, "size", &_counter->size);
      48         [ +  + ]:         118 :     if (r)
      49         [ +  - ]:           1 :         return bf_rpack_key_err(r, "bf_counter.size");
      50                 :             : 
      51                 :         117 :     *counter = TAKE_PTR(_counter);
      52                 :             : 
      53                 :         117 :     return 0;
      54                 :             : }
      55                 :             : 
      56                 :         589 : void bf_counter_free(struct bf_counter **counter)
      57                 :             : {
      58                 :             :     assert(counter);
      59                 :             : 
      60         [ +  + ]:         589 :     if (!*counter)
      61                 :             :         return;
      62                 :             : 
      63                 :             :     freep((void *)counter);
      64                 :             : }
      65                 :             : 
      66                 :         117 : int bf_counter_pack(const struct bf_counter *counter, bf_wpack_t *pack)
      67                 :             : {
      68                 :             :     assert(counter);
      69                 :             :     assert(pack);
      70                 :             : 
      71                 :         117 :     bf_wpack_kv_u64(pack, "count", counter->count);
      72                 :         117 :     bf_wpack_kv_u64(pack, "size", counter->size);
      73                 :             : 
      74         [ -  + ]:         117 :     return bf_wpack_is_valid(pack) ? 0 : -EINVAL;
      75                 :             : }
        

Generated by: LCOV version 2.0-1