LCOV - code coverage report
Current view: top level - py - ringbuf.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.24.0-7-g548babf8a.info Lines: 35 36 97.2 %
Date: 2024-10-30 09:06:48 Functions: 5 5 100.0 %
Branches: 23 26 88.5 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * This file is part of the MicroPython project, http://micropython.org/
       3                 :            :  *
       4                 :            :  * The MIT License (MIT)
       5                 :            :  *
       6                 :            :  * Copyright (c) 2019 Jim Mussared
       7                 :            :  *
       8                 :            :  * Permission is hereby granted, free of charge, to any person obtaining a copy
       9                 :            :  * of this software and associated documentation files (the "Software"), to deal
      10                 :            :  * in the Software without restriction, including without limitation the rights
      11                 :            :  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      12                 :            :  * copies of the Software, and to permit persons to whom the Software is
      13                 :            :  * furnished to do so, subject to the following conditions:
      14                 :            :  *
      15                 :            :  * The above copyright notice and this permission notice shall be included in
      16                 :            :  * all copies or substantial portions of the Software.
      17                 :            :  *
      18                 :            :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      19                 :            :  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      20                 :            :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      21                 :            :  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      22                 :            :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      23                 :            :  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
      24                 :            :  * THE SOFTWARE.
      25                 :            :  */
      26                 :            : 
      27                 :            : #include "ringbuf.h"
      28                 :            : 
      29                 :         12 : int ringbuf_get16(ringbuf_t *r) {
      30                 :         12 :     int v = ringbuf_peek16(r);
      31         [ +  + ]:         12 :     if (v == -1) {
      32                 :            :         return v;
      33                 :            :     }
      34                 :          8 :     r->iget += 2;
      35         [ +  + ]:          8 :     if (r->iget >= r->size) {
      36                 :          4 :         r->iget -= r->size;
      37                 :            :     }
      38                 :            :     return v;
      39                 :            : }
      40                 :            : 
      41                 :         12 : int ringbuf_peek16(ringbuf_t *r) {
      42         [ +  + ]:         12 :     if (r->iget == r->iput) {
      43                 :            :         return -1;
      44                 :            :     }
      45                 :         10 :     uint32_t iget_a = r->iget + 1;
      46         [ +  + ]:         10 :     if (iget_a == r->size) {
      47                 :          2 :         iget_a = 0;
      48                 :            :     }
      49         [ +  + ]:         10 :     if (iget_a == r->iput) {
      50                 :            :         return -1;
      51                 :            :     }
      52                 :          8 :     return (r->buf[r->iget] << 8) | (r->buf[iget_a]);
      53                 :            : }
      54                 :            : 
      55                 :         12 : int ringbuf_put16(ringbuf_t *r, uint16_t v) {
      56                 :         12 :     uint32_t iput_a = r->iput + 1;
      57         [ +  + ]:         12 :     if (iput_a == r->size) {
      58                 :          2 :         iput_a = 0;
      59                 :            :     }
      60         [ +  + ]:         12 :     if (iput_a == r->iget) {
      61                 :            :         return -1;
      62                 :            :     }
      63                 :         10 :     uint32_t iput_b = iput_a + 1;
      64         [ +  + ]:         10 :     if (iput_b == r->size) {
      65                 :          2 :         iput_b = 0;
      66                 :            :     }
      67         [ +  + ]:         10 :     if (iput_b == r->iget) {
      68                 :            :         return -1;
      69                 :            :     }
      70                 :          8 :     r->buf[r->iput] = (v >> 8) & 0xff;
      71                 :          8 :     r->buf[iput_a] = v & 0xff;
      72                 :          8 :     r->iput = iput_b;
      73                 :          8 :     return 0;
      74                 :            : }
      75                 :            : 
      76                 :            : // Returns:
      77                 :            : //    0: Success
      78                 :            : //   -1: Not enough data available to complete read (try again later)
      79                 :            : //   -2: Requested read is larger than buffer - will never succeed
      80                 :          2 : int ringbuf_get_bytes(ringbuf_t *r, uint8_t *data, size_t data_len) {
      81         [ -  + ]:          2 :     if (ringbuf_avail(r) < data_len) {
      82         [ #  # ]:          0 :         return (r->size <= data_len) ? -2 : -1;
      83                 :            :     }
      84                 :          2 :     ringbuf_memcpy_get_internal(r, data, data_len);
      85                 :          2 :     return 0;
      86                 :            : }
      87                 :            : 
      88                 :            : // Returns:
      89                 :            : //    0: Success
      90                 :            : //   -1: Not enough free space available to complete write (try again later)
      91                 :            : //   -2: Requested write is larger than buffer - will never succeed
      92                 :          6 : int ringbuf_put_bytes(ringbuf_t *r, const uint8_t *data, size_t data_len) {
      93         [ +  + ]:          6 :     if (ringbuf_free(r) < data_len) {
      94         [ +  + ]:          4 :         return (r->size <= data_len) ? -2 : -1;
      95                 :            :     }
      96                 :          2 :     ringbuf_memcpy_put_internal(r, data, data_len);
      97                 :          2 :     return 0;
      98                 :            : }

Generated by: LCOV version 1.15-5-g462f71d