LCOV - code coverage report
Current view: top level - spdk/lib/util - pipe.c (source / functions) Hit Total Coverage
Test: Combined Lines: 139 142 97.9 %
Date: 2024-12-14 17:45:12 Functions: 7 7 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 255 424 60.1 %

           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                 :      16191 : spdk_pipe_create(void *buf, uint32_t sz)
      20                 :            : {
      21                 :          7 :         struct spdk_pipe *pipe;
      22                 :            : 
      23                 :      16191 :         pipe = calloc(1, sizeof(*pipe));
      24         [ +  + ]:      16191 :         if (pipe == NULL) {
      25                 :          0 :                 return NULL;
      26                 :            :         }
      27                 :            : 
      28   [ +  -  +  - ]:      16191 :         pipe->buf = buf;
      29   [ +  -  +  - ]:      16191 :         pipe->sz = sz;
      30                 :            : 
      31                 :      16191 :         return pipe;
      32                 :       4159 : }
      33                 :            : 
      34                 :            : void
      35                 :      16762 : spdk_pipe_destroy(struct spdk_pipe *pipe)
      36                 :            : {
      37                 :      16762 :         free(pipe);
      38                 :      16762 : }
      39                 :            : 
      40                 :            : int
      41                 :  212891305 : spdk_pipe_writer_get_buffer(struct spdk_pipe *pipe, uint32_t requested_sz, struct iovec *iovs)
      42                 :            : {
      43                 :         13 :         uint32_t sz;
      44                 :         13 :         uint32_t read;
      45                 :         13 :         uint32_t write;
      46                 :            : 
      47   [ +  -  +  - ]:  212891305 :         read = pipe->read;
      48   [ +  -  +  - ]:  212891305 :         write = pipe->write;
      49                 :            : 
      50   [ +  +  +  +  :  212891305 :         if (pipe->full || requested_sz == 0) {
          +  +  +  +  +  
                      + ]
      51   [ +  -  +  -  :         32 :                 iovs[0].iov_base = NULL;
                   +  - ]
      52   [ +  -  +  -  :         12 :                 iovs[0].iov_len = 0;
                   +  - ]
      53                 :         12 :                 return 0;
      54                 :            :         }
      55                 :            : 
      56         [ +  + ]:  212891273 :         if (read <= write) {
      57   [ +  -  +  -  :  212891255 :                 sz = spdk_min(requested_sz, pipe->sz - write);
          +  +  +  -  +  
                      - ]
      58                 :            : 
      59   [ +  -  +  -  :  212891255 :                 iovs[0].iov_base = pipe->buf + write;
          +  -  +  -  +  
                -  +  - ]
      60   [ +  -  +  -  :  212891251 :                 iovs[0].iov_len = sz;
                   +  - ]
      61                 :            : 
      62                 :  212891251 :                 requested_sz -= sz;
      63                 :            : 
      64         [ +  + ]:  212891251 :                 if (requested_sz > 0) {
      65         [ -  + ]:  210413998 :                         sz = spdk_min(requested_sz, read);
      66                 :            : 
      67   [ +  +  -  +  :  210413998 :                         iovs[1].iov_base = (sz == 0) ? NULL : pipe->buf;
          -  +  +  -  +  
                -  +  - ]
      68   [ +  -  +  -  :  210413998 :                         iovs[1].iov_len = sz;
                   +  - ]
      69                 :     260911 :                 } else {
      70   [ +  -  +  -  :    2477253 :                         iovs[1].iov_base = NULL;
                   +  - ]
      71   [ +  -  +  -  :    2477253 :                         iovs[1].iov_len = 0;
                   +  - ]
      72                 :            :                 }
      73                 :     298816 :         } else {
      74         [ +  + ]:         18 :                 sz = spdk_min(requested_sz, read - write);
      75                 :            : 
      76   [ +  -  +  -  :         18 :                 iovs[0].iov_base = pipe->buf + write;
          +  -  +  -  +  
                -  +  - ]
      77   [ +  -  +  -  :         18 :                 iovs[0].iov_len = sz;
                   +  - ]
      78   [ +  -  +  -  :         18 :                 iovs[1].iov_base = NULL;
                   +  - ]
      79   [ +  -  +  -  :         18 :                 iovs[1].iov_len = 0;
                   +  - ]
      80                 :            :         }
      81                 :            : 
      82   [ +  -  +  -  :  212891269 :         return iovs[0].iov_len + iovs[1].iov_len;
          +  -  +  -  +  
                -  +  - ]
      83                 :     298821 : }
      84                 :            : 
      85                 :            : int
      86                 :   24369516 : spdk_pipe_writer_advance(struct spdk_pipe *pipe, uint32_t requested_sz)
      87                 :            : {
      88                 :         11 :         uint32_t sz;
      89                 :         11 :         uint32_t read;
      90                 :         11 :         uint32_t write;
      91                 :            : 
      92   [ +  -  +  - ]:   24369516 :         read = pipe->read;
      93   [ +  -  +  - ]:   24369516 :         write = pipe->write;
      94                 :            : 
      95   [ +  +  +  +  :   24369516 :         if (requested_sz > pipe->sz || pipe->full) {
          +  +  +  -  +  
             -  +  -  -  
                      + ]
      96                 :          6 :                 return -EINVAL;
      97                 :            :         }
      98                 :            : 
      99         [ +  + ]:   24369510 :         if (read <= write) {
     100   [ +  +  +  -  :   24369486 :                 if (requested_sz > (pipe->sz - write) + read) {
                   +  + ]
     101                 :          6 :                         return -EINVAL;
     102                 :            :                 }
     103                 :            : 
     104   [ +  -  +  -  :   24369480 :                 sz = spdk_min(requested_sz, pipe->sz - write);
          +  +  -  +  -  
                      + ]
     105                 :            : 
     106                 :   24369480 :                 write += sz;
     107   [ +  +  +  -  :   24369480 :                 if (write == pipe->sz) {
                   +  + ]
     108                 :    4483887 :                         write = 0;
     109                 :          3 :                 }
     110                 :   24369480 :                 requested_sz -= sz;
     111                 :            : 
     112         [ +  + ]:   24369480 :                 if (requested_sz > 0) {
     113                 :    4468579 :                         write = requested_sz;
     114                 :          1 :                 }
     115                 :      44407 :         } else {
     116         [ +  + ]:         24 :                 if (requested_sz > (read - write)) {
     117                 :         12 :                         return -EINVAL;
     118                 :            :                 }
     119                 :            : 
     120                 :         12 :                 write += requested_sz;
     121                 :            :         }
     122                 :            : 
     123         [ +  + ]:   24369492 :         if (read == write) {
     124   [ +  -  +  - ]:    3100132 :                 pipe->full = true;
     125                 :          3 :         }
     126   [ +  -  +  - ]:   24369492 :         pipe->write = write;
     127                 :            : 
     128                 :   24369492 :         return 0;
     129                 :      44413 : }
     130                 :            : 
     131                 :            : uint32_t
     132                 :  353698497 : spdk_pipe_reader_bytes_available(struct spdk_pipe *pipe)
     133                 :            : {
     134                 :          3 :         uint32_t read;
     135                 :          3 :         uint32_t write;
     136                 :            : 
     137   [ +  -  +  - ]:  353698497 :         read = pipe->read;
     138   [ +  -  +  - ]:  353698497 :         write = pipe->write;
     139                 :            : 
     140   [ +  +  +  +  :  353698497 :         if (read == write && !pipe->full) {
          +  -  +  -  -  
                      + ]
     141                 :  213364140 :                 return 0;
     142         [ +  + ]:  140334357 :         } else if (read < write) {
     143                 :   90520670 :                 return write - read;
     144                 :            :         } else {
     145   [ -  +  -  + ]:   49813687 :                 return (pipe->sz - read) + write;
     146                 :            :         }
     147                 :     113885 : }
     148                 :            : 
     149                 :            : int
     150                 :  147667748 : spdk_pipe_reader_get_buffer(struct spdk_pipe *pipe, uint32_t requested_sz, struct iovec *iovs)
     151                 :            : {
     152                 :         12 :         uint32_t sz;
     153                 :         12 :         uint32_t read;
     154                 :         12 :         uint32_t write;
     155                 :            : 
     156   [ +  -  +  - ]:  147667748 :         read = pipe->read;
     157   [ +  -  +  - ]:  147667748 :         write = pipe->write;
     158                 :            : 
     159   [ +  +  +  +  :  147667748 :         if ((read == write && !pipe->full) || requested_sz == 0) {
          +  +  +  +  +  
                      + ]
     160   [ +  -  +  -  :   11609917 :                 iovs[0].iov_base = NULL;
                   +  - ]
     161   [ +  -  +  -  :   11609917 :                 iovs[0].iov_len = 0;
                   +  - ]
     162   [ +  -  +  -  :   11609917 :                 iovs[1].iov_base = NULL;
                   +  - ]
     163   [ +  -  +  -  :   11609917 :                 iovs[1].iov_len = 0;
                   +  - ]
     164         [ +  + ]:  136080010 :         } else if (read < write) {
     165         [ +  + ]:   93344062 :                 sz = spdk_min(requested_sz, write - read);
     166                 :            : 
     167   [ +  -  +  -  :   93344062 :                 iovs[0].iov_base = pipe->buf + read;
          +  -  +  -  +  
                -  +  - ]
     168   [ +  -  +  -  :   93344062 :                 iovs[0].iov_len = sz;
                   +  - ]
     169   [ +  -  +  -  :   93344062 :                 iovs[1].iov_base = NULL;
                   +  - ]
     170   [ +  -  +  -  :   93344062 :                 iovs[1].iov_len = 0;
                   +  - ]
     171                 :     113890 :         } else {
     172   [ +  -  +  -  :   42713769 :                 sz = spdk_min(requested_sz, pipe->sz - read);
          +  +  +  -  +  
                      - ]
     173                 :            : 
     174   [ +  -  +  -  :   42713769 :                 iovs[0].iov_base = pipe->buf + read;
          +  -  +  -  +  
                -  +  - ]
     175   [ +  -  +  -  :   42713769 :                 iovs[0].iov_len = sz;
                   +  - ]
     176                 :            : 
     177                 :   42713769 :                 requested_sz -= sz;
     178                 :            : 
     179         [ +  + ]:   42713769 :                 if (requested_sz > 0) {
     180         [ -  + ]:   42703969 :                         sz = spdk_min(requested_sz, write);
     181   [ +  +  -  +  :   42703969 :                         iovs[1].iov_base = (sz == 0) ? NULL : pipe->buf;
          -  +  +  -  +  
                -  +  - ]
     182   [ +  -  +  -  :   42703969 :                         iovs[1].iov_len = sz;
                   +  - ]
     183                 :          4 :                 } else {
     184   [ +  -  +  -  :       9800 :                         iovs[1].iov_base = NULL;
                   +  - ]
     185   [ +  -  +  -  :       9800 :                         iovs[1].iov_len = 0;
                   +  - ]
     186                 :            :                 }
     187                 :            :         }
     188                 :            : 
     189   [ +  -  +  -  :  147667748 :         return iovs[0].iov_len + iovs[1].iov_len;
          +  -  +  -  +  
                -  +  - ]
     190                 :         12 : }
     191                 :            : 
     192                 :            : int
     193                 :  136057818 : spdk_pipe_reader_advance(struct spdk_pipe *pipe, uint32_t requested_sz)
     194                 :            : {
     195                 :          9 :         uint32_t sz;
     196                 :          9 :         uint32_t read;
     197                 :          9 :         uint32_t write;
     198                 :            : 
     199   [ +  -  +  - ]:  136057818 :         read = pipe->read;
     200   [ +  -  +  - ]:  136057818 :         write = pipe->write;
     201                 :            : 
     202         [ +  + ]:  136057818 :         if (requested_sz == 0) {
     203                 :          0 :                 return 0;
     204                 :            :         }
     205                 :            : 
     206         [ +  + ]:  136057818 :         if (read < write) {
     207         [ +  + ]:   93344067 :                 if (requested_sz > (write - read)) {
     208                 :         12 :                         return -EINVAL;
     209                 :            :                 }
     210                 :            : 
     211                 :   93344055 :                 read += requested_sz;
     212                 :     113883 :         } else {
     213   [ +  -  +  -  :   42713751 :                 sz = spdk_min(requested_sz, pipe->sz - read);
          +  +  +  -  +  
                      - ]
     214                 :            : 
     215                 :   42713751 :                 read += sz;
     216   [ +  +  +  -  :   42713751 :                 if (read == pipe->sz) {
                   +  + ]
     217                 :    4483874 :                         read = 0;
     218                 :          2 :                 }
     219                 :   42713751 :                 requested_sz -= sz;
     220                 :            : 
     221         [ +  + ]:   42713751 :                 if (requested_sz > 0) {
     222         [ -  + ]:    4402217 :                         if (requested_sz > write) {
     223                 :          0 :                                 return -EINVAL;
     224                 :            :                         }
     225                 :            : 
     226                 :    4402217 :                         read = requested_sz;
     227                 :          2 :                 }
     228                 :            :         }
     229                 :            : 
     230                 :            :         /* We know we advanced at least one byte, so the pipe isn't full. */
     231   [ +  -  +  - ]:  136057806 :         pipe->full = false;
     232   [ +  -  +  - ]:  136057806 :         pipe->read = read;
     233                 :            : 
     234                 :  136057806 :         return 0;
     235                 :     113888 : }

Generated by: LCOV version 1.15