Coverage Report

Created: 2025-10-10 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/rtpproxy/src/rtpp_ringbuf.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2004-2006 Maxim Sobolev <sobomax@FreeBSD.org>
3
 * Copyright (c) 2006-2015 Sippy Software, Inc., http://www.sippysoft.com
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 * 1. Redistributions of source code must retain the above copyright
10
 *    notice, this list of conditions and the following disclaimer.
11
 * 2. Redistributions in binary form must reproduce the above copyright
12
 *    notice, this list of conditions and the following disclaimer in the
13
 *    documentation and/or other materials provided with the distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
 * SUCH DAMAGE.
26
 *
27
 */
28
29
#include <stdlib.h>
30
#include <stddef.h>
31
#include <string.h>
32
33
#include "rtpp_types.h"
34
#include "rtpp_codeptr.h"
35
#include "rtpp_refcnt.h"
36
#include "rtpp_ringbuf.h"
37
#include "rtpp_ringbuf_fin.h"
38
#include "rtpp_mallocs.h"
39
40
struct rtpp_ringbuf_priv
41
{
42
    struct rtpp_ringbuf pub;
43
    void *elements;
44
    int nelements;
45
    size_t el_size;
46
    int c_elem;
47
    int b_full;
48
};
49
50
static void rtpp_ringbuf_dtor(struct rtpp_ringbuf_priv *);
51
static void rtpp_ringbuf_push(struct rtpp_ringbuf *, void *);
52
static void rtpp_ringbuf_flush(struct rtpp_ringbuf *);
53
static int rtpp_ringbuf_locate(struct rtpp_ringbuf *, void *);
54
55
DEFINE_SMETHODS(rtpp_ringbuf,
56
    .push = &rtpp_ringbuf_push,
57
    .flush = &rtpp_ringbuf_flush,
58
    .locate = &rtpp_ringbuf_locate,
59
);
60
61
struct rtpp_ringbuf *
62
rtpp_ringbuf_ctor(size_t el_size, int nelements)
63
0
{
64
0
    struct rtpp_ringbuf_priv *pvt;
65
66
0
    pvt = rtpp_rzmalloc(sizeof(struct rtpp_ringbuf_priv), PVT_RCOFFS(pvt));
67
0
    if (pvt == NULL) {
68
0
        goto e0;
69
0
    }
70
0
    pvt->elements = rtpp_zmalloc(el_size * nelements);
71
0
    if (pvt->elements == NULL) {
72
0
        goto e1;
73
0
    }
74
0
    pvt->el_size = el_size;
75
0
    pvt->nelements = nelements;
76
0
    PUBINST_FININIT(&pvt->pub, pvt, rtpp_ringbuf_dtor);
77
0
    return (&pvt->pub);
78
0
e1:
79
0
    RTPP_OBJ_DECREF(&(pvt->pub));
80
0
e0:
81
0
    return (NULL);
82
0
}
83
84
static void
85
rtpp_ringbuf_dtor(struct rtpp_ringbuf_priv *pvt)
86
0
{
87
88
0
    rtpp_ringbuf_fin(&(pvt->pub));
89
0
    free(pvt->elements);
90
0
}
91
92
static void
93
rtpp_ringbuf_push(struct rtpp_ringbuf *self, void *data)
94
0
{
95
0
    struct rtpp_ringbuf_priv *pvt;
96
0
    void *dp;
97
98
0
    PUB2PVT(self, pvt);
99
0
    dp = (char *)pvt->elements + (pvt->el_size * pvt->c_elem);
100
0
    memcpy(dp, data, pvt->el_size);
101
0
    pvt->c_elem++;
102
0
    if (pvt->c_elem == pvt->nelements) {
103
0
        if (pvt->b_full == 0) {
104
0
            pvt->b_full = 1;
105
0
        }
106
0
        pvt->c_elem = 0;
107
0
    }
108
0
}
109
110
static void
111
rtpp_ringbuf_flush(struct rtpp_ringbuf *self)
112
0
{
113
0
    struct rtpp_ringbuf_priv *pvt;
114
115
0
    PUB2PVT(self, pvt);
116
0
    pvt->b_full = 0;
117
0
    pvt->c_elem = 0;
118
0
}
119
120
static int
121
rtpp_ringbuf_locate(struct rtpp_ringbuf *self, void *data)
122
0
{
123
0
    struct rtpp_ringbuf_priv *pvt;
124
0
    int i, last_el;
125
0
    void *dp;
126
127
0
    PUB2PVT(self, pvt);
128
0
    last_el = (pvt->b_full != 0) ? pvt->nelements : pvt->c_elem;
129
0
    for (i = 0; i < last_el; i++) {
130
0
        dp = (char *)pvt->elements + (pvt->el_size * i);
131
0
        if (memcmp(dp, data, pvt->el_size) == 0) {
132
0
            return (i);
133
0
        }
134
0
    }
135
0
    return (-1);
136
0
}