LCOV - code coverage report
Current view: top level - spdk/lib/util - pipe.c (source / functions) Hit Total Coverage
Test: Combined Lines: 107 110 97.3 %
Date: 2024-07-10 11:15:48 Functions: 7 7 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 58 66 87.9 %

           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                 :      10895 : spdk_pipe_create(void *buf, uint32_t sz)
      20                 :            : {
      21                 :            :         struct spdk_pipe *pipe;
      22                 :            : 
      23                 :      10895 :         pipe = calloc(1, sizeof(*pipe));
      24         [ -  + ]:      10895 :         if (pipe == NULL) {
      25                 :          0 :                 return NULL;
      26                 :            :         }
      27                 :            : 
      28                 :      10895 :         pipe->buf = buf;
      29                 :      10895 :         pipe->sz = sz;
      30                 :            : 
      31                 :      10895 :         return pipe;
      32                 :            : }
      33                 :            : 
      34                 :            : void
      35                 :      11442 : spdk_pipe_destroy(struct spdk_pipe *pipe)
      36                 :            : {
      37                 :      11442 :         free(pipe);
      38                 :      11442 : }
      39                 :            : 
      40                 :            : int
      41                 :  186866323 : 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                 :  186866323 :         read = pipe->read;
      48                 :  186866323 :         write = pipe->write;
      49                 :            : 
      50   [ +  +  +  +  :  186866323 :         if (pipe->full || requested_sz == 0) {
                   +  + ]
      51                 :         12 :                 iovs[0].iov_base = NULL;
      52                 :         12 :                 iovs[0].iov_len = 0;
      53                 :         12 :                 return 0;
      54                 :            :         }
      55                 :            : 
      56         [ +  + ]:  186866311 :         if (read <= write) {
      57                 :  186866293 :                 sz = spdk_min(requested_sz, pipe->sz - write);
      58                 :            : 
      59                 :  186866293 :                 iovs[0].iov_base = pipe->buf + write;
      60                 :  186866293 :                 iovs[0].iov_len = sz;
      61                 :            : 
      62                 :  186866293 :                 requested_sz -= sz;
      63                 :            : 
      64         [ +  + ]:  186866293 :                 if (requested_sz > 0) {
      65                 :  184540392 :                         sz = spdk_min(requested_sz, read);
      66                 :            : 
      67         [ +  + ]:  184540392 :                         iovs[1].iov_base = (sz == 0) ? NULL : pipe->buf;
      68                 :  184540392 :                         iovs[1].iov_len = sz;
      69                 :            :                 } else {
      70                 :    2325901 :                         iovs[1].iov_base = NULL;
      71                 :    2325901 :                         iovs[1].iov_len = 0;
      72                 :            :                 }
      73                 :            :         } 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                 :  186866311 :         return iovs[0].iov_len + iovs[1].iov_len;
      83                 :            : }
      84                 :            : 
      85                 :            : int
      86                 :   20753408 : 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                 :   20753408 :         read = pipe->read;
      93                 :   20753408 :         write = pipe->write;
      94                 :            : 
      95   [ +  +  -  +  :   20753408 :         if (requested_sz > pipe->sz || pipe->full) {
                   -  + ]
      96                 :          6 :                 return -EINVAL;
      97                 :            :         }
      98                 :            : 
      99         [ +  + ]:   20753402 :         if (read <= write) {
     100         [ +  + ]:   20753378 :                 if (requested_sz > (pipe->sz - write) + read) {
     101                 :          6 :                         return -EINVAL;
     102                 :            :                 }
     103                 :            : 
     104                 :   20753372 :                 sz = spdk_min(requested_sz, pipe->sz - write);
     105                 :            : 
     106                 :   20753372 :                 write += sz;
     107         [ +  + ]:   20753372 :                 if (write == pipe->sz) {
     108                 :    3895905 :                         write = 0;
     109                 :            :                 }
     110                 :   20753372 :                 requested_sz -= sz;
     111                 :            : 
     112         [ +  + ]:   20753372 :                 if (requested_sz > 0) {
     113                 :    3878941 :                         write = requested_sz;
     114                 :            :                 }
     115                 :            :         } else {
     116         [ +  + ]:         24 :                 if (requested_sz > (read - write)) {
     117                 :         12 :                         return -EINVAL;
     118                 :            :                 }
     119                 :            : 
     120                 :         12 :                 write += requested_sz;
     121                 :            :         }
     122                 :            : 
     123         [ +  + ]:   20753384 :         if (read == write) {
     124                 :    2650705 :                 pipe->full = true;
     125                 :            :         }
     126                 :   20753384 :         pipe->write = write;
     127                 :            : 
     128                 :   20753384 :         return 0;
     129                 :            : }
     130                 :            : 
     131                 :            : uint32_t
     132                 :  304487135 : spdk_pipe_reader_bytes_available(struct spdk_pipe *pipe)
     133                 :            : {
     134                 :            :         uint32_t read;
     135                 :            :         uint32_t write;
     136                 :            : 
     137                 :  304487135 :         read = pipe->read;
     138                 :  304487135 :         write = pipe->write;
     139                 :            : 
     140   [ +  +  -  +  :  304487135 :         if (read == write && !pipe->full) {
                   +  - ]
     141                 :  187584922 :                 return 0;
     142         [ +  + ]:  116902213 :         } else if (read < write) {
     143                 :   74951318 :                 return write - read;
     144                 :            :         } else {
     145                 :   41950895 :                 return (pipe->sz - read) + write;
     146                 :            :         }
     147                 :            : }
     148                 :            : 
     149                 :            : int
     150                 :  121665502 : 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                 :  121665502 :         read = pipe->read;
     157                 :  121665502 :         write = pipe->write;
     158                 :            : 
     159   [ +  +  -  +  :  121665502 :         if ((read == write && !pipe->full) || requested_sz == 0) {
             +  +  +  + ]
     160                 :    9485890 :                 iovs[0].iov_base = NULL;
     161                 :    9485890 :                 iovs[0].iov_len = 0;
     162                 :    9485890 :                 iovs[1].iov_base = NULL;
     163                 :    9485890 :                 iovs[1].iov_len = 0;
     164         [ +  + ]:  112179612 :         } else if (read < write) {
     165                 :   76540641 :                 sz = spdk_min(requested_sz, write - read);
     166                 :            : 
     167                 :   76540641 :                 iovs[0].iov_base = pipe->buf + read;
     168                 :   76540641 :                 iovs[0].iov_len = sz;
     169                 :   76540641 :                 iovs[1].iov_base = NULL;
     170                 :   76540641 :                 iovs[1].iov_len = 0;
     171                 :            :         } else {
     172                 :   35638971 :                 sz = spdk_min(requested_sz, pipe->sz - read);
     173                 :            : 
     174                 :   35638971 :                 iovs[0].iov_base = pipe->buf + read;
     175                 :   35638971 :                 iovs[0].iov_len = sz;
     176                 :            : 
     177                 :   35638971 :                 requested_sz -= sz;
     178                 :            : 
     179         [ +  + ]:   35638971 :                 if (requested_sz > 0) {
     180                 :   35625548 :                         sz = spdk_min(requested_sz, write);
     181         [ +  + ]:   35625548 :                         iovs[1].iov_base = (sz == 0) ? NULL : pipe->buf;
     182                 :   35625548 :                         iovs[1].iov_len = sz;
     183                 :            :                 } else {
     184                 :      13423 :                         iovs[1].iov_base = NULL;
     185                 :      13423 :                         iovs[1].iov_len = 0;
     186                 :            :                 }
     187                 :            :         }
     188                 :            : 
     189                 :  121665502 :         return iovs[0].iov_len + iovs[1].iov_len;
     190                 :            : }
     191                 :            : 
     192                 :            : int
     193                 :  112179606 : 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                 :  112179606 :         read = pipe->read;
     200                 :  112179606 :         write = pipe->write;
     201                 :            : 
     202         [ -  + ]:  112179606 :         if (requested_sz == 0) {
     203                 :          0 :                 return 0;
     204                 :            :         }
     205                 :            : 
     206         [ +  + ]:  112179606 :         if (read < write) {
     207         [ +  + ]:   76540653 :                 if (requested_sz > (write - read)) {
     208                 :         12 :                         return -EINVAL;
     209                 :            :                 }
     210                 :            : 
     211                 :   76540641 :                 read += requested_sz;
     212                 :            :         } else {
     213                 :   35638953 :                 sz = spdk_min(requested_sz, pipe->sz - read);
     214                 :            : 
     215                 :   35638953 :                 read += sz;
     216         [ +  + ]:   35638953 :                 if (read == pipe->sz) {
     217                 :    3895893 :                         read = 0;
     218                 :            :                 }
     219                 :   35638953 :                 requested_sz -= sz;
     220                 :            : 
     221         [ +  + ]:   35638953 :                 if (requested_sz > 0) {
     222         [ -  + ]:    3813848 :                         if (requested_sz > write) {
     223                 :          0 :                                 return -EINVAL;
     224                 :            :                         }
     225                 :            : 
     226                 :    3813848 :                         read = requested_sz;
     227                 :            :                 }
     228                 :            :         }
     229                 :            : 
     230                 :            :         /* We know we advanced at least one byte, so the pipe isn't full. */
     231                 :  112179594 :         pipe->full = false;
     232                 :  112179594 :         pipe->read = read;
     233                 :            : 
     234                 :  112179594 :         return 0;
     235                 :            : }

Generated by: LCOV version 1.14