LCOV - code coverage report
Current view: top level - util - safe_ring_buffer.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 0.0 % 27 0
Test Date: 2026-02-15 13:08:52 Functions: 0.0 % 6 0
Branches: 0.0 % 28 0

             Branch data     Line data    Source code
       1                 :             : #include <stdint.h>
       2                 :             : #include <string.h>
       3                 :             : 
       4                 :             : #include "safe_ring_buffer.h"
       5                 :             : 
       6                 :             : static size_t get_offset_bytes(const srb_ctx_t *ctx, size_t index) {
       7   [ #  #  #  #  :           0 :         if (index >= ctx->max_elements) {
                   #  # ]
       8                 :             :                 return 0;
       9                 :             :         }
      10                 :           0 :         return index * (ctx->element_size);
      11                 :             : }
      12                 :             : 
      13                 :           0 : void srb_init(srb_ctx_t *ctx, void *pool, size_t pool_size, size_t element_size) {
      14                 :           0 :         ctx->memory_pool = pool;
      15                 :           0 :         ctx->element_size = element_size;
      16                 :           0 :         ctx->max_elements = (pool_size / (element_size));
      17                 :           0 :         ctx->rd_idx = 0;
      18                 :           0 :         ctx->wr_idx = 0;
      19                 :           0 : }
      20                 :             : 
      21                 :           0 : bool srb_push(srb_ctx_t *ctx, const void *element) {
      22                 :             :         if (srb_is_full(ctx)) {
      23                 :             :                 return false;
      24                 :             :         }
      25                 :             :         size_t offset = get_offset_bytes(ctx, ctx->wr_idx);
      26                 :           0 :         memcpy(((uint8_t *)ctx->memory_pool) + offset, element, ctx->element_size);
      27         [ #  # ]:           0 :         if (++(ctx->wr_idx) >= ctx->max_elements) {
      28                 :           0 :                 ctx->wr_idx = 0;
      29                 :             :         }
      30                 :             :         return true;
      31                 :             : }
      32                 :             : 
      33                 :           0 : bool srb_is_full(const srb_ctx_t *ctx) {
      34   [ #  #  #  # ]:           0 :         if ((ctx->wr_idx + 1 == ctx->rd_idx) ||
      35   [ #  #  #  #  :           0 :                 (ctx->wr_idx + 1 == ctx->max_elements && ctx->rd_idx == 0)) {
             #  #  #  # ]
      36                 :             :                 return true;
      37                 :             :         } else {
      38                 :           0 :                 return false;
      39                 :             :         }
      40                 :             : }
      41                 :             : 
      42                 :           0 : bool srb_is_empty(const srb_ctx_t *ctx) {
      43         [ #  # ]:           0 :         if (ctx->wr_idx == ctx->rd_idx) {
      44                 :             :                 return true;
      45                 :             :         } else {
      46                 :           0 :                 return false;
      47                 :             :         }
      48                 :             : }
      49                 :             : 
      50         [ #  # ]:           0 : bool srb_pop(srb_ctx_t *ctx, void *element) {
      51                 :             :         if (srb_is_empty(ctx)) {
      52                 :             :                 return false;
      53                 :             :         }
      54                 :             :         size_t offset = get_offset_bytes(ctx, ctx->rd_idx);
      55                 :           0 :         memcpy(element, ((uint8_t *)ctx->memory_pool) + offset, ctx->element_size);
      56         [ #  # ]:           0 :         if (++(ctx->rd_idx) >= ctx->max_elements) {
      57                 :           0 :                 ctx->rd_idx = 0;
      58                 :             :         }
      59                 :             :         return true;
      60                 :             : }
      61                 :             : 
      62         [ #  # ]:           0 : bool srb_peek(const srb_ctx_t *ctx, void *element) {
      63                 :             :         if (srb_is_empty(ctx)) {
      64                 :             :                 return false;
      65                 :             :         }
      66                 :             :         size_t offset = get_offset_bytes(ctx, ctx->rd_idx);
      67                 :           0 :         memcpy(element, ((uint8_t *)ctx->memory_pool) + offset, ctx->element_size);
      68                 :           0 :         return true;
      69                 :             : }
        

Generated by: LCOV version 2.0-1