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-12-13 04:54:33 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                 :      16112 : spdk_pipe_create(void *buf, uint32_t sz)
      20                 :            : {
      21                 :            :         struct spdk_pipe *pipe;
      22                 :            : 
      23                 :      16112 :         pipe = calloc(1, sizeof(*pipe));
      24         [ +  + ]:      16112 :         if (pipe == NULL) {
      25                 :          0 :                 return NULL;
      26                 :            :         }
      27                 :            : 
      28   [ +  -  +  - ]:      16112 :         pipe->buf = buf;
      29   [ +  -  +  - ]:      16112 :         pipe->sz = sz;
      30                 :            : 
      31                 :      16112 :         return pipe;
      32                 :       4104 : }
      33                 :            : 
      34                 :            : void
      35                 :      16625 : spdk_pipe_destroy(struct spdk_pipe *pipe)
      36                 :            : {
      37                 :      16625 :         free(pipe);
      38                 :      16625 : }
      39                 :            : 
      40                 :            : int
      41                 :  222178531 : 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   [ +  -  +  - ]:  222178531 :         read = pipe->read;
      48   [ +  -  +  - ]:  222178531 :         write = pipe->write;
      49                 :            : 
      50   [ +  +  +  +  :  222178531 :         if (pipe->full || requested_sz == 0) {
          +  +  +  +  +  
                      + ]
      51   [ #  #  #  #  :         20 :                 iovs[0].iov_base = NULL;
                   #  # ]
      52   [ #  #  #  #  :          6 :                 iovs[0].iov_len = 0;
                   #  # ]
      53                 :          6 :                 return 0;
      54                 :            :         }
      55                 :            : 
      56         [ +  + ]:  222178525 :         if (read <= write) {
      57   [ +  -  +  -  :  222178516 :                 sz = spdk_min(requested_sz, pipe->sz - write);
          +  +  +  -  +  
                      - ]
      58                 :            : 
      59   [ +  -  +  -  :  222178516 :                 iovs[0].iov_base = pipe->buf + write;
          +  -  +  -  +  
                -  +  - ]
      60   [ +  -  +  -  :  222178516 :                 iovs[0].iov_len = sz;
                   +  - ]
      61                 :            : 
      62                 :  222178516 :                 requested_sz -= sz;
      63                 :            : 
      64         [ +  + ]:  222178516 :                 if (requested_sz > 0) {
      65         [ -  + ]:  219553362 :                         sz = spdk_min(requested_sz, read);
      66                 :            : 
      67   [ +  +  -  +  :  219553362 :                         iovs[1].iov_base = (sz == 0) ? NULL : pipe->buf;
          -  +  +  -  +  
                -  +  - ]
      68   [ +  -  +  -  :  219553362 :                         iovs[1].iov_len = sz;
                   +  - ]
      69                 :     268225 :                 } else {
      70   [ +  -  +  -  :    2625154 :                         iovs[1].iov_base = NULL;
                   +  - ]
      71   [ +  -  +  -  :    2625154 :                         iovs[1].iov_len = 0;
                   +  - ]
      72                 :            :                 }
      73                 :     307687 :         } 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   [ +  -  +  -  :  222178525 :         return iovs[0].iov_len + iovs[1].iov_len;
          +  -  +  -  +  
                -  +  - ]
      83                 :     307687 : }
      84                 :            : 
      85                 :            : int
      86                 :   27737997 : 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   [ +  -  +  - ]:   27737997 :         read = pipe->read;
      93   [ +  -  +  - ]:   27737997 :         write = pipe->write;
      94                 :            : 
      95   [ +  +  +  +  :   27737997 :         if (requested_sz > pipe->sz || pipe->full) {
          +  +  +  -  +  
             -  +  -  -  
                      + ]
      96                 :          3 :                 return -EINVAL;
      97                 :            :         }
      98                 :            : 
      99         [ +  + ]:   27737994 :         if (read <= write) {
     100   [ +  +  +  -  :   27737982 :                 if (requested_sz > (pipe->sz - write) + read) {
                   -  + ]
     101                 :          3 :                         return -EINVAL;
     102                 :            :                 }
     103                 :            : 
     104   [ +  -  +  -  :   27737979 :                 sz = spdk_min(requested_sz, pipe->sz - write);
          +  -  #  #  #  
                      # ]
     105                 :            : 
     106                 :   27737979 :                 write += sz;
     107   [ +  +  +  -  :   27737979 :                 if (write == pipe->sz) {
                   +  - ]
     108                 :    4587093 :                         write = 0;
     109                 :          0 :                 }
     110                 :   27737979 :                 requested_sz -= sz;
     111                 :            : 
     112         [ +  + ]:   27737979 :                 if (requested_sz > 0) {
     113                 :    4569823 :                         write = requested_sz;
     114                 :          0 :                 }
     115                 :      43881 :         } else {
     116         [ +  + ]:         12 :                 if (requested_sz > (read - write)) {
     117                 :          6 :                         return -EINVAL;
     118                 :            :                 }
     119                 :            : 
     120                 :          6 :                 write += requested_sz;
     121                 :            :         }
     122                 :            : 
     123         [ +  + ]:   27737985 :         if (read == write) {
     124   [ #  #  #  # ]:    3058412 :                 pipe->full = true;
     125                 :          0 :         }
     126   [ +  -  +  - ]:   27737985 :         pipe->write = write;
     127                 :            : 
     128                 :   27737985 :         return 0;
     129                 :      43881 : }
     130                 :            : 
     131                 :            : uint32_t
     132                 :  359318617 : spdk_pipe_reader_bytes_available(struct spdk_pipe *pipe)
     133                 :            : {
     134                 :            :         uint32_t read;
     135                 :            :         uint32_t write;
     136                 :            : 
     137   [ +  -  +  - ]:  359318617 :         read = pipe->read;
     138   [ +  -  +  - ]:  359318617 :         write = pipe->write;
     139                 :            : 
     140   [ +  +  +  +  :  359318617 :         if (read == write && !pipe->full) {
          +  -  +  -  -  
                      + ]
     141                 :  220782111 :                 return 0;
     142         [ +  + ]:  138536506 :         } else if (read < write) {
     143                 :   89623621 :                 return write - read;
     144                 :            :         } else {
     145   [ #  #  #  # ]:   48912885 :                 return (pipe->sz - read) + write;
     146                 :            :         }
     147                 :     113049 : }
     148                 :            : 
     149                 :            : int
     150                 :  153228159 : 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   [ +  -  +  - ]:  153228159 :         read = pipe->read;
     157   [ +  -  +  - ]:  153228159 :         write = pipe->write;
     158                 :            : 
     159   [ +  +  +  +  :  153228159 :         if ((read == write && !pipe->full) || requested_sz == 0) {
          +  +  +  +  +  
                      + ]
     160   [ +  -  +  -  :   15255908 :                 iovs[0].iov_base = NULL;
                   +  - ]
     161   [ +  -  +  -  :   15255908 :                 iovs[0].iov_len = 0;
                   +  - ]
     162   [ +  -  +  -  :   15255908 :                 iovs[1].iov_base = NULL;
                   +  - ]
     163   [ +  -  +  -  :   15255908 :                 iovs[1].iov_len = 0;
                   +  - ]
     164         [ +  + ]:  137994166 :         } else if (read < write) {
     165         [ -  + ]:   95727478 :                 sz = spdk_min(requested_sz, write - read);
     166                 :            : 
     167   [ +  -  +  -  :   95727478 :                 iovs[0].iov_base = pipe->buf + read;
          +  -  +  -  +  
                -  +  - ]
     168   [ +  -  +  -  :   95727478 :                 iovs[0].iov_len = sz;
                   +  - ]
     169   [ +  -  +  -  :   95727478 :                 iovs[1].iov_base = NULL;
                   +  - ]
     170   [ +  -  +  -  :   95727478 :                 iovs[1].iov_len = 0;
                   +  - ]
     171                 :     113058 :         } else {
     172   [ #  #  #  #  :   42244773 :                 sz = spdk_min(requested_sz, pipe->sz - read);
          #  #  #  #  #  
                      # ]
     173                 :            : 
     174   [ #  #  #  #  :   42244773 :                 iovs[0].iov_base = pipe->buf + read;
          #  #  #  #  #  
                #  #  # ]
     175   [ #  #  #  #  :   42244773 :                 iovs[0].iov_len = sz;
                   #  # ]
     176                 :            : 
     177                 :   42244773 :                 requested_sz -= sz;
     178                 :            : 
     179         [ +  + ]:   42244773 :                 if (requested_sz > 0) {
     180         [ #  # ]:   42234254 :                         sz = spdk_min(requested_sz, write);
     181   [ +  +  #  #  :   42234254 :                         iovs[1].iov_base = (sz == 0) ? NULL : pipe->buf;
          #  #  #  #  #  
                #  #  # ]
     182   [ #  #  #  #  :   42234254 :                         iovs[1].iov_len = sz;
                   #  # ]
     183                 :          0 :                 } else {
     184   [ #  #  #  #  :      10519 :                         iovs[1].iov_base = NULL;
                   #  # ]
     185   [ #  #  #  #  :      10519 :                         iovs[1].iov_len = 0;
                   #  # ]
     186                 :            :                 }
     187                 :            :         }
     188                 :            : 
     189   [ +  -  +  -  :  153228159 :         return iovs[0].iov_len + iovs[1].iov_len;
          +  -  +  -  +  
                -  +  - ]
     190                 :            : }
     191                 :            : 
     192                 :            : int
     193                 :  137972240 : 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   [ +  -  +  - ]:  137972240 :         read = pipe->read;
     200   [ +  -  +  - ]:  137972240 :         write = pipe->write;
     201                 :            : 
     202         [ +  + ]:  137972240 :         if (requested_sz == 0) {
     203                 :          0 :                 return 0;
     204                 :            :         }
     205                 :            : 
     206         [ +  + ]:  137972240 :         if (read < write) {
     207         [ +  + ]:   95727476 :                 if (requested_sz > (write - read)) {
     208                 :          6 :                         return -EINVAL;
     209                 :            :                 }
     210                 :            : 
     211                 :   95727470 :                 read += requested_sz;
     212                 :     113050 :         } else {
     213   [ #  #  #  #  :   42244764 :                 sz = spdk_min(requested_sz, pipe->sz - read);
          #  #  #  #  #  
                      # ]
     214                 :            : 
     215                 :   42244764 :                 read += sz;
     216   [ +  +  #  #  :   42244764 :                 if (read == pipe->sz) {
                   #  # ]
     217                 :    4587086 :                         read = 0;
     218                 :          0 :                 }
     219                 :   42244764 :                 requested_sz -= sz;
     220                 :            : 
     221         [ +  + ]:   42244764 :                 if (requested_sz > 0) {
     222         [ -  + ]:    4505285 :                         if (requested_sz > write) {
     223                 :          0 :                                 return -EINVAL;
     224                 :            :                         }
     225                 :            : 
     226                 :    4505285 :                         read = requested_sz;
     227                 :          0 :                 }
     228                 :            :         }
     229                 :            : 
     230                 :            :         /* We know we advanced at least one byte, so the pipe isn't full. */
     231   [ +  -  +  - ]:  137972234 :         pipe->full = false;
     232   [ +  -  +  - ]:  137972234 :         pipe->read = read;
     233                 :            : 
     234                 :  137972234 :         return 0;
     235                 :     113050 : }

Generated by: LCOV version 1.15