LCOV - code coverage report
Current view: top level - spdk/lib/util - pipe.c (source / functions) Hit Total Coverage
Test: Combined Lines: 117 126 92.9 %
Date: 2024-11-16 21:22:34 Functions: 7 7 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 183 424 43.2 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2019 Intel Corporation.
       3                 :            :  *   All rights reserved.
       4                 :            :  */
       5                 :            : 
       6                 :            : #include "spdk/pipe.h"
       7                 :            : #include "spdk/util.h"
       8                 :            : 
       9                 :            : struct spdk_pipe {
      10                 :            :         uint8_t *buf;
      11                 :            :         uint32_t sz;
      12                 :            : 
      13                 :            :         uint32_t write;
      14                 :            :         uint32_t read;
      15                 :            :         bool full;
      16                 :            : };
      17                 :            : 
      18                 :            : struct spdk_pipe *
      19                 :      13768 : spdk_pipe_create(void *buf, uint32_t sz)
      20                 :            : {
      21                 :            :         struct spdk_pipe *pipe;
      22                 :            : 
      23                 :      13768 :         pipe = calloc(1, sizeof(*pipe));
      24         [ +  + ]:      13768 :         if (pipe == NULL) {
      25                 :          0 :                 return NULL;
      26                 :            :         }
      27                 :            : 
      28   [ +  -  +  - ]:      13768 :         pipe->buf = buf;
      29   [ +  -  +  - ]:      13768 :         pipe->sz = sz;
      30                 :            : 
      31                 :      13768 :         return pipe;
      32                 :       4016 : }
      33                 :            : 
      34                 :            : void
      35                 :      14281 : spdk_pipe_destroy(struct spdk_pipe *pipe)
      36                 :            : {
      37                 :      14281 :         free(pipe);
      38                 :      14281 : }
      39                 :            : 
      40                 :            : int
      41                 :  216811797 : spdk_pipe_writer_get_buffer(struct spdk_pipe *pipe, uint32_t requested_sz, struct iovec *iovs)
      42                 :            : {
      43                 :            :         uint32_t sz;
      44                 :            :         uint32_t read;
      45                 :            :         uint32_t write;
      46                 :            : 
      47   [ +  -  +  - ]:  216811797 :         read = pipe->read;
      48   [ +  -  +  - ]:  216811797 :         write = pipe->write;
      49                 :            : 
      50   [ +  +  +  +  :  216811797 :         if (pipe->full || requested_sz == 0) {
          +  +  +  +  +  
                      + ]
      51   [ #  #  #  #  :         64 :                 iovs[0].iov_base = NULL;
                   #  # ]
      52   [ #  #  #  #  :          6 :                 iovs[0].iov_len = 0;
                   #  # ]
      53                 :          6 :                 return 0;
      54                 :            :         }
      55                 :            : 
      56         [ +  + ]:  216811791 :         if (read <= write) {
      57   [ +  -  +  -  :  216811782 :                 sz = spdk_min(requested_sz, pipe->sz - write);
          +  +  +  -  +  
                      - ]
      58                 :            : 
      59   [ +  -  +  -  :  216811782 :                 iovs[0].iov_base = pipe->buf + write;
          +  -  +  -  +  
                -  +  - ]
      60   [ +  -  +  -  :  216811730 :                 iovs[0].iov_len = sz;
                   +  - ]
      61                 :            : 
      62                 :  216811730 :                 requested_sz -= sz;
      63                 :            : 
      64         [ +  + ]:  216811730 :                 if (requested_sz > 0) {
      65         [ -  + ]:  214303217 :                         sz = spdk_min(requested_sz, read);
      66                 :            : 
      67   [ +  +  -  +  :  214303217 :                         iovs[1].iov_base = (sz == 0) ? NULL : pipe->buf;
          -  +  +  -  +  
                -  +  - ]
      68   [ +  -  +  -  :  214303217 :                         iovs[1].iov_len = sz;
                   +  - ]
      69                 :     257193 :                 } else {
      70   [ +  -  +  -  :    2508513 :                         iovs[1].iov_base = NULL;
                   +  - ]
      71   [ +  -  +  -  :    2508513 :                         iovs[1].iov_len = 0;
                   +  - ]
      72                 :            :                 }
      73                 :     295259 :         } else {
      74         [ #  # ]:          9 :                 sz = spdk_min(requested_sz, read - write);
      75                 :            : 
      76   [ #  #  #  #  :          9 :                 iovs[0].iov_base = pipe->buf + write;
          #  #  #  #  #  
                #  #  # ]
      77   [ #  #  #  #  :          9 :                 iovs[0].iov_len = sz;
                   #  # ]
      78   [ #  #  #  #  :          9 :                 iovs[1].iov_base = NULL;
                   #  # ]
      79   [ #  #  #  #  :          9 :                 iovs[1].iov_len = 0;
                   #  # ]
      80                 :            :         }
      81                 :            : 
      82   [ +  -  +  -  :  216811739 :         return iovs[0].iov_len + iovs[1].iov_len;
          +  -  +  -  +  
                -  +  - ]
      83                 :     295259 : }
      84                 :            : 
      85                 :            : int
      86                 :   23629005 : spdk_pipe_writer_advance(struct spdk_pipe *pipe, uint32_t requested_sz)
      87                 :            : {
      88                 :            :         uint32_t sz;
      89                 :            :         uint32_t read;
      90                 :            :         uint32_t write;
      91                 :            : 
      92   [ +  -  +  - ]:   23629005 :         read = pipe->read;
      93   [ +  -  +  - ]:   23629005 :         write = pipe->write;
      94                 :            : 
      95   [ +  +  +  +  :   23629005 :         if (requested_sz > pipe->sz || pipe->full) {
          +  +  +  -  +  
             -  +  -  -  
                      + ]
      96                 :          3 :                 return -EINVAL;
      97                 :            :         }
      98                 :            : 
      99         [ +  + ]:   23629002 :         if (read <= write) {
     100   [ +  +  +  -  :   23628990 :                 if (requested_sz > (pipe->sz - write) + read) {
                   -  + ]
     101                 :          3 :                         return -EINVAL;
     102                 :            :                 }
     103                 :            : 
     104   [ +  -  +  -  :   23628987 :                 sz = spdk_min(requested_sz, pipe->sz - write);
          +  -  #  #  #  
                      # ]
     105                 :            : 
     106                 :   23628987 :                 write += sz;
     107   [ +  +  +  -  :   23628987 :                 if (write == pipe->sz) {
                   +  - ]
     108                 :    4183444 :                         write = 0;
     109                 :          0 :                 }
     110                 :   23628987 :                 requested_sz -= sz;
     111                 :            : 
     112         [ +  + ]:   23628987 :                 if (requested_sz > 0) {
     113                 :    4173135 :                         write = requested_sz;
     114                 :          0 :                 }
     115                 :      42910 :         } else {
     116         [ +  + ]:         12 :                 if (requested_sz > (read - write)) {
     117                 :          6 :                         return -EINVAL;
     118                 :            :                 }
     119                 :            : 
     120                 :          6 :                 write += requested_sz;
     121                 :            :         }
     122                 :            : 
     123         [ +  + ]:   23628993 :         if (read == write) {
     124   [ #  #  #  # ]:    2857480 :                 pipe->full = true;
     125                 :          0 :         }
     126   [ +  -  +  - ]:   23628993 :         pipe->write = write;
     127                 :            : 
     128                 :   23628993 :         return 0;
     129                 :      42910 : }
     130                 :            : 
     131                 :            : uint32_t
     132                 :  343655101 : spdk_pipe_reader_bytes_available(struct spdk_pipe *pipe)
     133                 :            : {
     134                 :            :         uint32_t read;
     135                 :            :         uint32_t write;
     136                 :            : 
     137   [ +  -  +  - ]:  343655101 :         read = pipe->read;
     138   [ +  -  +  - ]:  343655101 :         write = pipe->write;
     139                 :            : 
     140   [ +  +  +  +  :  343655101 :         if (read == write && !pipe->full) {
          +  -  +  -  -  
                      + ]
     141                 :  216637150 :                 return 0;
     142         [ +  + ]:  127017951 :         } else if (read < write) {
     143                 :   81286611 :                 return write - read;
     144                 :            :         } else {
     145   [ #  #  #  # ]:   45731340 :                 return (pipe->sz - read) + write;
     146                 :            :         }
     147                 :     110840 : }
     148                 :            : 
     149                 :            : int
     150                 :  133287559 : spdk_pipe_reader_get_buffer(struct spdk_pipe *pipe, uint32_t requested_sz, struct iovec *iovs)
     151                 :            : {
     152                 :            :         uint32_t sz;
     153                 :            :         uint32_t read;
     154                 :            :         uint32_t write;
     155                 :            : 
     156   [ +  -  +  - ]:  133287559 :         read = pipe->read;
     157   [ +  -  +  - ]:  133287559 :         write = pipe->write;
     158                 :            : 
     159   [ +  +  +  +  :  133287559 :         if ((read == write && !pipe->full) || requested_sz == 0) {
          +  +  +  +  +  
                      + ]
     160   [ +  -  +  -  :   11059870 :                 iovs[0].iov_base = NULL;
                   +  - ]
     161   [ +  -  +  -  :   11059870 :                 iovs[0].iov_len = 0;
                   +  - ]
     162   [ +  -  +  -  :   11059870 :                 iovs[1].iov_base = NULL;
                   +  - ]
     163   [ +  -  +  -  :   11059870 :                 iovs[1].iov_len = 0;
                   +  - ]
     164         [ +  + ]:  122249123 :         } else if (read < write) {
     165         [ -  + ]:   83628758 :                 sz = spdk_min(requested_sz, write - read);
     166                 :            : 
     167   [ +  -  +  -  :   83628758 :                 iovs[0].iov_base = pipe->buf + read;
          +  -  +  -  +  
                -  +  - ]
     168   [ +  -  +  -  :   83628758 :                 iovs[0].iov_len = sz;
                   +  - ]
     169   [ +  -  +  -  :   83628758 :                 iovs[1].iov_base = NULL;
                   +  - ]
     170   [ +  -  +  -  :   83628758 :                 iovs[1].iov_len = 0;
                   +  - ]
     171                 :     110854 :         } else {
     172   [ #  #  #  #  :   38598931 :                 sz = spdk_min(requested_sz, pipe->sz - read);
          #  #  #  #  #  
                      # ]
     173                 :            : 
     174   [ #  #  #  #  :   38598931 :                 iovs[0].iov_base = pipe->buf + read;
          #  #  #  #  #  
                #  #  # ]
     175   [ #  #  #  #  :   38598931 :                 iovs[0].iov_len = sz;
                   #  # ]
     176                 :            : 
     177                 :   38598931 :                 requested_sz -= sz;
     178                 :            : 
     179         [ +  + ]:   38598931 :                 if (requested_sz > 0) {
     180         [ #  # ]:   38592350 :                         sz = spdk_min(requested_sz, write);
     181   [ +  +  #  #  :   38592350 :                         iovs[1].iov_base = (sz == 0) ? NULL : pipe->buf;
          #  #  #  #  #  
                #  #  # ]
     182   [ #  #  #  #  :   38592350 :                         iovs[1].iov_len = sz;
                   #  # ]
     183                 :          0 :                 } else {
     184   [ #  #  #  #  :       6581 :                         iovs[1].iov_base = NULL;
                   #  # ]
     185   [ #  #  #  #  :       6581 :                         iovs[1].iov_len = 0;
                   #  # ]
     186                 :            :                 }
     187                 :            :         }
     188                 :            : 
     189   [ +  -  +  -  :  133287559 :         return iovs[0].iov_len + iovs[1].iov_len;
          +  -  +  -  +  
                -  +  - ]
     190                 :            : }
     191                 :            : 
     192                 :            : int
     193                 :  122227674 : spdk_pipe_reader_advance(struct spdk_pipe *pipe, uint32_t requested_sz)
     194                 :            : {
     195                 :            :         uint32_t sz;
     196                 :            :         uint32_t read;
     197                 :            :         uint32_t write;
     198                 :            : 
     199   [ +  -  +  - ]:  122227674 :         read = pipe->read;
     200   [ +  -  +  - ]:  122227674 :         write = pipe->write;
     201                 :            : 
     202         [ +  + ]:  122227674 :         if (requested_sz == 0) {
     203                 :          0 :                 return 0;
     204                 :            :         }
     205                 :            : 
     206         [ +  + ]:  122227674 :         if (read < write) {
     207         [ +  + ]:   83628752 :                 if (requested_sz > (write - read)) {
     208                 :          6 :                         return -EINVAL;
     209                 :            :                 }
     210                 :            : 
     211                 :   83628746 :                 read += requested_sz;
     212                 :     110842 :         } else {
     213   [ #  #  #  #  :   38598922 :                 sz = spdk_min(requested_sz, pipe->sz - read);
          #  #  #  #  #  
                      # ]
     214                 :            : 
     215                 :   38598922 :                 read += sz;
     216   [ +  +  #  #  :   38598922 :                 if (read == pipe->sz) {
                   #  # ]
     217                 :    4183433 :                         read = 0;
     218                 :          0 :                 }
     219                 :   38598922 :                 requested_sz -= sz;
     220                 :            : 
     221         [ +  + ]:   38598922 :                 if (requested_sz > 0) {
     222         [ -  + ]:    4118953 :                         if (requested_sz > write) {
     223                 :          0 :                                 return -EINVAL;
     224                 :            :                         }
     225                 :            : 
     226                 :    4118953 :                         read = requested_sz;
     227                 :          0 :                 }
     228                 :            :         }
     229                 :            : 
     230                 :            :         /* We know we advanced at least one byte, so the pipe isn't full. */
     231   [ +  -  +  - ]:  122227668 :         pipe->full = false;
     232   [ +  -  +  - ]:  122227668 :         pipe->read = read;
     233                 :            : 
     234                 :  122227668 :         return 0;
     235                 :     110842 : }

Generated by: LCOV version 1.15