/src/rtpproxy/src/rtpp_refproxy.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2024 Sippy Software, Inc., http://www.sippysoft.com |
3 | | * All rights reserved. |
4 | | * |
5 | | * Redistribution and use in source and binary forms, with or without |
6 | | * modification, are permitted provided that the following conditions |
7 | | * are met: |
8 | | * 1. Redistributions of source code must retain the above copyright |
9 | | * notice, this list of conditions and the following disclaimer. |
10 | | * 2. Redistributions in binary form must reproduce the above copyright |
11 | | * notice, this list of conditions and the following disclaimer in the |
12 | | * documentation and/or other materials provided with the distribution. |
13 | | * |
14 | | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
15 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
17 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
18 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
19 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
20 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
21 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
22 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
23 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
24 | | * SUCH DAMAGE. |
25 | | * |
26 | | */ |
27 | | |
28 | | #include <assert.h> |
29 | | #include <stddef.h> |
30 | | #include <stdlib.h> |
31 | | |
32 | | #include "rtpp_types.h" |
33 | | #include "rtpp_mallocs.h" |
34 | | #include "rtpp_codeptr.h" |
35 | | #include "rtpp_refcnt.h" |
36 | | #include "rtpp_refproxy.h" |
37 | | #include "rtpp_refproxy_fin.h" |
38 | | |
39 | | struct rtpp_refproxy_priv { |
40 | | struct rtpp_refproxy pub; |
41 | | int alen; |
42 | | int ulen; |
43 | | struct rtpp_refcnt *rcnts[0]; |
44 | | }; |
45 | | |
46 | | static void rtpp_refproxy_dtor(struct rtpp_refproxy_priv *); |
47 | | static void rtpp_refproxy_add(struct rtpp_refproxy *, struct rtpp_refcnt *); |
48 | | |
49 | | DEFINE_SMETHODS(rtpp_refproxy, |
50 | | .add = &rtpp_refproxy_add, |
51 | | ); |
52 | | |
53 | | struct rtpp_refproxy * |
54 | | rtpp_refproxy_ctor(int nrefs) |
55 | 0 | { |
56 | 0 | struct rtpp_refproxy_priv *pvt; |
57 | 0 | size_t asize = sizeof(struct rtpp_refproxy_priv); |
58 | |
|
59 | 0 | asize += nrefs * sizeof(struct rtpp_refcnt *); |
60 | 0 | pvt = rtpp_rzmalloc(asize, PVT_RCOFFS(pvt)); |
61 | 0 | if (pvt == NULL) { |
62 | 0 | goto e0; |
63 | 0 | } |
64 | 0 | pvt->alen = nrefs; |
65 | 0 | PUBINST_FININIT(&pvt->pub, pvt, rtpp_refproxy_dtor); |
66 | 0 | return ((&pvt->pub)); |
67 | | |
68 | 0 | e0: |
69 | 0 | return (NULL); |
70 | 0 | } |
71 | | |
72 | | static void |
73 | | rtpp_refproxy_dtor(struct rtpp_refproxy_priv *pvt) |
74 | 0 | { |
75 | |
|
76 | 0 | rtpp_refproxy_fin(&(pvt->pub)); |
77 | 0 | for (int i = 0; i < pvt->ulen; i++) { |
78 | 0 | RC_DECREF(pvt->rcnts[i]); |
79 | 0 | } |
80 | 0 | } |
81 | | |
82 | | static void |
83 | | rtpp_refproxy_add(struct rtpp_refproxy *self, struct rtpp_refcnt *rcnt) |
84 | 0 | { |
85 | 0 | struct rtpp_refproxy_priv *pvt; |
86 | |
|
87 | 0 | PUB2PVT(self, pvt); |
88 | 0 | assert(pvt->alen > pvt->ulen); |
89 | 0 | RC_INCREF(rcnt); |
90 | 0 | pvt->rcnts[pvt->ulen] = rcnt; |
91 | 0 | pvt->ulen += 1; |
92 | 0 | } |